Skip to content

Commit

Permalink
Merge pull request #75 from ant385525/renamer-update
Browse files Browse the repository at this point in the history
Fix bug in creating new file path relative to fileflows
  • Loading branch information
revenz authored Dec 23, 2024
2 parents 9fdc282 + 461945f commit 0cfbd12
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 20 deletions.
20 changes: 13 additions & 7 deletions Scripts/Flow/Applications/Radarr/Radarr - Rename.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,23 @@ 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
* @output Rename not required for item
* @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);
Expand Down Expand Up @@ -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.');
Expand All @@ -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;

Expand Down Expand Up @@ -129,17 +135,17 @@ 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
Logger.ILog(`Movie not found at ${currentPath}. Trying ${System.IO.Path.GetDirectoryName(currentPath)}`);
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];
}
31 changes: 18 additions & 13 deletions Scripts/Flow/Applications/Sonarr/Sonarr - Rename.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,26 @@ 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
* @output Rename not required for item
* @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 {
Expand All @@ -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
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;

Expand All @@ -152,18 +157,18 @@ 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
Logger.ILog(`Show not found at ${currentPath}. Trying ${System.IO.Path.GetDirectoryName(currentPath)}`)
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) {
Expand Down

0 comments on commit 0cfbd12

Please sign in to comment.