diff --git a/Scripts/Flow/Applications/Radarr/Radarr - Rename.js b/Scripts/Flow/Applications/Radarr/Radarr - Rename.js index 7c64baa..0eb6e78 100644 --- a/Scripts/Flow/Applications/Radarr/Radarr - Rename.js +++ b/Scripts/Flow/Applications/Radarr/Radarr - Rename.js @@ -3,7 +3,7 @@ import { Radarr } from 'Shared/Radarr'; /** * @description This script will send a rename command to Radarr * @author Shaun Agius, Anthony Clerici - * @revision 10 + * @revision 11 * @param {string} URI Radarr root URI and port (e.g. http://radarr:7878) * @param {string} ApiKey API Key * @output Item renamed successfully @@ -11,13 +11,15 @@ import { Radarr } from 'Shared/Radarr'; * @output Item not found */ function Script(URI, ApiKey) { + // Remove trailing / from URI + URI = URI.replace(/\/$/, ''); let radarr = new Radarr(URI, ApiKey); let folderPath = Variables.folder.FullName; let currentFileName = Variables.file.Name; let newFileName = null; // Find movie name from radarr - let movie = findMovie(folderPath, radarr); + let [movie, basePath] = findMovie(folderPath, radarr); if (!movie) { Logger.WLog('Movie not found for path: ' + folderPath); @@ -75,7 +77,12 @@ function Script(URI, ApiKey) { } newFileName = renamedMovie.newPath; - Logger.ILog(`Found it, renaming file to ${newFileName}`); + // Get differences in the path + Logger.ILog(`Base path: ${basePath}`); + // Construct new filepath + Logger.ILog(`New path: ${newFileName}`); + newFilePath = System.IO.Path.Combine(basePath, newFileName); + Logger.ILog(`Found it, renaming file ${newEpisodeFileId} to ${newFilePath}`); if (newFileName === null) { Logger.WLog('No matching movie found to rename.'); @@ -98,7 +105,6 @@ function Script(URI, ApiKey) { Logger.ILog(`Movie ${movie.id} successfully renamed. Setting as working file.`) // Radarr has successfully renamed the file, set new filename as working directory - let newFilePath = System.IO.Path.Combine(Variables.folder.FullName, newFileName); Flow.SetWorkingFile(newFilePath); return 1; @@ -129,7 +135,7 @@ function findMovie(filePath, radarr) { if (movieFolders[currentFolder]) { movie = movieFolders[currentFolder]; Logger.ILog('Movie found: ' + movie.id); - return movie; + return [movie, currentPath]; } // Log the path where the movie was not found and move up one directory @@ -137,9 +143,9 @@ function findMovie(filePath, radarr) { currentPath = System.IO.Path.GetDirectoryName(currentPath); if (!currentPath) { Logger.WLog('Unable to find movie file at path ' + filePath); - return null; + return [null, null]; } } - return null; + return [null, null]; } \ No newline at end of file diff --git a/Scripts/Flow/Applications/Sonarr/Sonarr - Rename.js b/Scripts/Flow/Applications/Sonarr/Sonarr - Rename.js index 6eefd98..8affa79 100644 --- a/Scripts/Flow/Applications/Sonarr/Sonarr - Rename.js +++ b/Scripts/Flow/Applications/Sonarr/Sonarr - Rename.js @@ -3,7 +3,7 @@ import { Sonarr } from 'Shared/Sonarr'; /** * @description This script will rename the file through Sonarr * @author Shaun Agius, Anthony Clerici - * @revision 11 + * @revision 12 * @param {string} URI Sonarr root URI and port (e.g. http://sonarr:8989) * @param {string} ApiKey API Key * @output Item renamed successfully @@ -11,16 +11,18 @@ import { Sonarr } from 'Shared/Sonarr'; * @output Item not found */ function Script(URI, ApiKey) { + // Remove trailing / from URI + URI = URI.replace(/\/$/, ''); let sonarr = new Sonarr(URI, ApiKey); const folderPath = Variables.folder.Orig.FullName; const ogFileName = Variables.file.Orig.FileName; let currentFileName = Variables.file.FullName; - let newFileName = null; + let newFilePath = null; // Find series name from sonarr - let series = findSeries(folderPath, sonarr); + let [series, basePath] = findSeries(folderPath, sonarr); - if (!series) { + if (series?.id === undefined) { Logger.WLog('Series not found for path: ' + folderPath); return 3; } else { @@ -30,11 +32,11 @@ function Script(URI, ApiKey) { // Find episode let [ogEpisodeFile, episode] = fetchEpisode(currentFileName, series, sonarr); - if (episode) { + if (episode?.id !== undefined) { Logger.ILog(`Original episode found: Season ${episode.seasonNumber} Episode: ${episode.episodeNumber}`) } else { Logger.WLog(`Episode could not be extracted from series`); - return -1; + return 3; } // Ensure series is refreshed before renaming @@ -98,15 +100,19 @@ function Script(URI, ApiKey) { Logger.ILog(`Searching for episode file with id ${newEpisodeFileId}`); renamedEpisodes.every((renamedFile) => { if (renamedFile.episodeFileId === newEpisodeFileId) { + let newFileName = renamedFile.newPath; // Get differences in the path - newFileName = renamedFile.newPath; - Logger.ILog(`Found it, renaming file ${newEpisodeFileId} to ${newFileName}`); + Logger.ILog(`Base path: ${basePath}`); + // Construct new filepath + Logger.ILog(`New path: ${newFileName}`); + newFilePath = System.IO.Path.Combine(basePath, newFileName); + Logger.ILog(`Found it, renaming file ${newEpisodeFileId} to ${newFilePath}`); return false; } return true; }); - if (newFileName === null) { + if (newFilePath === null) { Logger.WLog("Episode doesn't need renaming"); return 2; } @@ -125,7 +131,6 @@ function Script(URI, ApiKey) { Logger.ILog(`Episode file ${newEpisodeFileId} successfully renamed. Setting as working file.`) // Sonarr has successfully renamed the file, set new filename as working directory - let newFilePath = System.IO.Path.Combine(Variables.folder.FullName, newFileName); Flow.SetWorkingFile(newFilePath); return 1; @@ -152,7 +157,7 @@ function findSeries(filePath, sonarr) { if (seriesFolders[currentFolder]) { show = seriesFolders[currentFolder]; Logger.ILog('Show found: ' + show.id); - return show; + return [show, currentPath]; } // If no show is found, go up 1 dir @@ -160,10 +165,10 @@ function findSeries(filePath, sonarr) { currentPath = System.IO.Path.GetDirectoryName(currentPath); if (!currentPath) { Logger.WLog('Unable to find show file at path ' + filePath); - return null; + return [null, null]; } } - return null; + return [null, null]; } function fetchRenamedFiles(seriesId, sonarr) {