From db2e3a613208a6cdf2daf89eacecce3aedcd0138 Mon Sep 17 00:00:00 2001 From: ant385525 <121154142+ant385525@users.noreply.github.com> Date: Fri, 7 Jun 2024 21:17:37 -0700 Subject: [PATCH 1/8] Delete FileFlowsRepository.sln --- FileFlowsRepository.sln | 25 ------------------------- 1 file changed, 25 deletions(-) delete mode 100644 FileFlowsRepository.sln diff --git a/FileFlowsRepository.sln b/FileFlowsRepository.sln deleted file mode 100644 index 8aba8a4..0000000 --- a/FileFlowsRepository.sln +++ /dev/null @@ -1,25 +0,0 @@ - -Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Version 17 -VisualStudioVersion = 17.5.002.0 -MinimumVisualStudioVersion = 10.0.40219.1 -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FileFlowsScriptRepo", "FileFlowsScriptRepo.csproj", "{F13042EA-E10F-4E1D-AB8C-BBA05CBDBBE6}" -EndProject -Global - GlobalSection(SolutionConfigurationPlatforms) = preSolution - Debug|Any CPU = Debug|Any CPU - Release|Any CPU = Release|Any CPU - EndGlobalSection - GlobalSection(ProjectConfigurationPlatforms) = postSolution - {F13042EA-E10F-4E1D-AB8C-BBA05CBDBBE6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {F13042EA-E10F-4E1D-AB8C-BBA05CBDBBE6}.Debug|Any CPU.Build.0 = Debug|Any CPU - {F13042EA-E10F-4E1D-AB8C-BBA05CBDBBE6}.Release|Any CPU.ActiveCfg = Release|Any CPU - {F13042EA-E10F-4E1D-AB8C-BBA05CBDBBE6}.Release|Any CPU.Build.0 = Release|Any CPU - EndGlobalSection - GlobalSection(SolutionProperties) = preSolution - HideSolutionNode = FALSE - EndGlobalSection - GlobalSection(ExtensibilityGlobals) = postSolution - SolutionGuid = {B22C6563-C33B-4D38-B55A-9F8DF345600C} - EndGlobalSection -EndGlobal From 136e21f9d00c724b28e85e9b5725389e5d12528d Mon Sep 17 00:00:00 2001 From: ant385525 <121154142+ant385525@users.noreply.github.com> Date: Fri, 7 Jun 2024 21:19:24 -0700 Subject: [PATCH 2/8] misspelling in comment --- Scripts/Flow/Applications/Sonarr/Sonarr - Rename.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Scripts/Flow/Applications/Sonarr/Sonarr - Rename.js b/Scripts/Flow/Applications/Sonarr/Sonarr - Rename.js index f3fb0ce..8a1c9c5 100644 --- a/Scripts/Flow/Applications/Sonarr/Sonarr - Rename.js +++ b/Scripts/Flow/Applications/Sonarr/Sonarr - Rename.js @@ -1,7 +1,7 @@ import { Sonarr } from 'Shared/Sonarr'; /** * This script will send a rename command to Sonarr - * @autor Anthony Clerici + * @author Anthony Clerici * @version 1.0.0 * @revision 1 * @param {string} URI Sonarr root URI and port (e.g. http://sonarr:1234) @@ -111,4 +111,4 @@ function fetchRenamedFiles(seriesId, sonarr) { let queryParams = `seriesId=${seriesId}`; let response = sonarr.fetchJson(endpoint, queryParams); return response; -} \ No newline at end of file +} From 45b9c9e38f4643beb50a6f58a8a4863fafadf388 Mon Sep 17 00:00:00 2001 From: ant385525 Date: Sat, 8 Jun 2024 20:19:20 -0700 Subject: [PATCH 3/8] Add former author and update revision number --- Scripts/Flow/Applications/Sonarr/Sonarr - Rename.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Scripts/Flow/Applications/Sonarr/Sonarr - Rename.js b/Scripts/Flow/Applications/Sonarr/Sonarr - Rename.js index 8a1c9c5..29d7056 100644 --- a/Scripts/Flow/Applications/Sonarr/Sonarr - Rename.js +++ b/Scripts/Flow/Applications/Sonarr/Sonarr - Rename.js @@ -2,8 +2,9 @@ import { Sonarr } from 'Shared/Sonarr'; /** * This script will send a rename command to Sonarr * @author Anthony Clerici + * @author Shaun Agius * @version 1.0.0 - * @revision 1 + * @revision 5 * @param {string} URI Sonarr root URI and port (e.g. http://sonarr:1234) * @param {string} ApiKey API Key * @output Item renamed From 38a30e15affea9969406838875788324fff8b32d Mon Sep 17 00:00:00 2001 From: John Andrews Date: Sun, 9 Jun 2024 15:27:21 +1200 Subject: [PATCH 4/8] Update Sonarr - Rename.js --- Scripts/Flow/Applications/Sonarr/Sonarr - Rename.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Scripts/Flow/Applications/Sonarr/Sonarr - Rename.js b/Scripts/Flow/Applications/Sonarr/Sonarr - Rename.js index 29d7056..7975959 100644 --- a/Scripts/Flow/Applications/Sonarr/Sonarr - Rename.js +++ b/Scripts/Flow/Applications/Sonarr/Sonarr - Rename.js @@ -1,10 +1,10 @@ import { Sonarr } from 'Shared/Sonarr'; /** - * This script will send a rename command to Sonarr * @author Anthony Clerici * @author Shaun Agius - * @version 1.0.0 - * @revision 5 + * @uid 5ac44abd-cfe9-4a84-904b-9424908509de + * @description This script will send a rename command to Sonarr + * @revision 6 * @param {string} URI Sonarr root URI and port (e.g. http://sonarr:1234) * @param {string} ApiKey API Key * @output Item renamed From 5bc5ac18540e951fbc0844d0a97ab694611eb699 Mon Sep 17 00:00:00 2001 From: John Andrews Date: Sun, 9 Jun 2024 15:27:43 +1200 Subject: [PATCH 5/8] Update Sonarr.js --- Scripts/Shared/Sonarr.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/Scripts/Shared/Sonarr.js b/Scripts/Shared/Sonarr.js index 0648626..1a729e4 100644 --- a/Scripts/Shared/Sonarr.js +++ b/Scripts/Shared/Sonarr.js @@ -1,9 +1,8 @@ -// path: Scripts/Shared/Sonarr.js - /** - * Class that interacts with Sonarr * @name Sonarr - * @revision 5 + * @uid 0f5836c0-d20b-4740-9824-f81b5200ec3d + * @description Class that interacts with Sonarr + * @revision 6 * @minimumVersion 1.0.0.0 */ export class Sonarr From 933fe5385664375bc2534c4933665e33970b9d86 Mon Sep 17 00:00:00 2001 From: ant385525 Date: Sat, 8 Jun 2024 20:54:33 -0700 Subject: [PATCH 6/8] Something went horribly wrong there, I'm terribly sorry I don't use Github often. --- .../Applications/Sonarr/Sonarr - Rename.js | 84 ++++++++++++++++--- Scripts/Shared/Sonarr.js | 56 +++++++++++++ 2 files changed, 129 insertions(+), 11 deletions(-) diff --git a/Scripts/Flow/Applications/Sonarr/Sonarr - Rename.js b/Scripts/Flow/Applications/Sonarr/Sonarr - Rename.js index d6ae007..9688a27 100644 --- a/Scripts/Flow/Applications/Sonarr/Sonarr - Rename.js +++ b/Scripts/Flow/Applications/Sonarr/Sonarr - Rename.js @@ -11,18 +11,80 @@ import { Sonarr } from 'Shared/Sonarr'; * @output Item renamed * @output Item not found */ + function Script(URI, ApiKey) { let sonarr = new Sonarr(URI, ApiKey); - let folder = Variables.folder.FullName - var season - if (folder.includes("Season")) - season = folder.indexOf('Season')-1 - else - { - if (folder.includes("/")) - season = folder.lastIndexOf("/") - else if (folder.includes("\\")) - season = folder.lastIndexOf("\\") + const folderPath = Variables.folder.FullName; + const currentFileName = Variables.file.Name; + let newFileName = null; + let episodeFileId = null; + + // Find series name from sonarr + let series = findSeries(folderPath, sonarr); + + if (!series) { + Logger.WLog('Series not found for path: ' + folderPath); + return 2; + } + + try { + // Ensure series is refreshed before renaming + let refreshBody = { + seriesId: series.id + } + let refreshData = sonarr.sendCommand('RescanSeries', refreshBody) + Logger.ILog(`Series refreshed`); + + // Wait for the completion of the refresh scan + let refreshCompleted = sonarr.waitForCompletion(refreshData.id, sonarr); + if (!refreshCompleted) { + Logger.ILog('Refresh not completed'); + return -1; + } + + let renamedEpisodes = fetchRenamedFiles(series.id, sonarr); + if (!renamedEpisodes) { + Logger.ILog('No episodes need to be renamed'); + return 2; + } + + Logger.ILog(`Searching for an episode previously named ${currentFileName}`); + renamedEpisodes.every((episode) => { + if (episode.existingPath.endsWith(currentFileName)) { + episodeFileId = episode.episodeFileId; + newFileName = System.IO.Path.GetFileName(episode.newPath); + Logger.ILog(`Found it, renaming file ${episodeFileId} to ${newFileName}`); + return false; + } + return true; + }); + + if (newFileName === null) { + Logger.WLog('No matching episode found to rename.'); + return 2; + } + + let renameBody = { + seriesId: series.id, + files: [episodeFileId] + } + let renameResponse = sonarr.sendCommand('RenameFiles', renameBody, URI, ApiKey); + let renameCompleted = sonarr.waitForCompletion(renameResponse.id); + + if (!renameCompleted) { + Logger.ILog('Rename not completed'); + return -1; + } + Logger.ILog(`Episode ${episodeFileId} 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; + + } catch (error) { + Logger.WLog('Error: ' + error.message); + return -1; } } @@ -52,4 +114,4 @@ function fetchRenamedFiles(seriesId, sonarr) { let queryParams = `seriesId=${seriesId}`; let response = sonarr.fetchJson(endpoint, queryParams); return response; -} +} \ No newline at end of file diff --git a/Scripts/Shared/Sonarr.js b/Scripts/Shared/Sonarr.js index 1ef1f8f..1e8d0c3 100644 --- a/Scripts/Shared/Sonarr.js +++ b/Scripts/Shared/Sonarr.js @@ -231,4 +231,60 @@ export class Sonarr } return language[1]; } +} + +/** + * Specifies a command for Sonarr to run. see sonarr rename script for usage + * @param {string} commandName the name of the command to be run + * @param {object} commandBody the body of the command to be sent + * @returns {object} JSON of the response or null if unsuccessful + */ +sendCommand(commandName, commandBody) +{ + let endpoint = `${this.URL}/api/v3/command`; + commandBody['name'] = commandName; + + let jsonData = JSON.stringify(commandBody); + http.DefaultRequestHeaders.Add("X-API-Key", this.ApiKey); + let response = http.PostAsync(endpoint, JsonContent(jsonData)).Result; + + http.DefaultRequestHeaders.Remove("X-API-Key"); + + if (response.IsSuccessStatusCode) { + let responseData = JSON.parse(response.Content.ReadAsStringAsync().Result); + Logger.ILog(`${commandName} command sent successfully`); + return responseData; + } else { + let error = response.Content.ReadAsStringAsync().Result; + Logger.WLog("API error: " + error); + return null; + } +} + +/** + * Sleeps, waiting for a command to complete + * @param {int} commandId ID of command being run + * @returns bool whether the coommand ran successfully + */ +waitForCompletion(commandId) +{ + const startTime = new Date().getTime(); + const timeout = 30000; + const endpoint = `command/${commandId}`; + + while (new Date().getTime() - startTime <= timeout) { + let response = this.fetchJson(endpoint, ''); + if (response.status === 'completed') { + Logger.ILog('Scan completed!'); + return true; + } else if (response.status === 'failed') { + Logger.WLog(`Command ${commandId} failed`) + return false; + } + Logger.ILog(`Checking status: ${response.status}`); + Sleep(100); + } + Logger.WLog('Timeout: Scan did not complete within 30 seconds.'); + return false; +} } \ No newline at end of file From be5acda22139e9dd23465f9bc213d1a3fd460fa1 Mon Sep 17 00:00:00 2001 From: ant385525 Date: Sat, 8 Jun 2024 22:30:30 -0700 Subject: [PATCH 7/8] Fix rogue curly brace --- Scripts/Shared/Sonarr.js | 93 ++++++++++++++++++++-------------------- 1 file changed, 46 insertions(+), 47 deletions(-) diff --git a/Scripts/Shared/Sonarr.js b/Scripts/Shared/Sonarr.js index 1e8d0c3..101a6eb 100644 --- a/Scripts/Shared/Sonarr.js +++ b/Scripts/Shared/Sonarr.js @@ -231,60 +231,59 @@ export class Sonarr } return language[1]; } -} -/** - * Specifies a command for Sonarr to run. see sonarr rename script for usage - * @param {string} commandName the name of the command to be run - * @param {object} commandBody the body of the command to be sent - * @returns {object} JSON of the response or null if unsuccessful - */ -sendCommand(commandName, commandBody) -{ - let endpoint = `${this.URL}/api/v3/command`; - commandBody['name'] = commandName; + /** + * Specifies a command for Sonarr to run. see sonarr rename script for usage + * @param {string} commandName the name of the command to be run + * @param {object} commandBody the body of the command to be sent + * @returns {object} JSON of the response or null if unsuccessful + */ + sendCommand(commandName, commandBody) + { + let endpoint = `${this.URL}/api/v3/command`; + commandBody['name'] = commandName; - let jsonData = JSON.stringify(commandBody); - http.DefaultRequestHeaders.Add("X-API-Key", this.ApiKey); - let response = http.PostAsync(endpoint, JsonContent(jsonData)).Result; + let jsonData = JSON.stringify(commandBody); + http.DefaultRequestHeaders.Add("X-API-Key", this.ApiKey); + let response = http.PostAsync(endpoint, JsonContent(jsonData)).Result; - http.DefaultRequestHeaders.Remove("X-API-Key"); + http.DefaultRequestHeaders.Remove("X-API-Key"); - if (response.IsSuccessStatusCode) { - let responseData = JSON.parse(response.Content.ReadAsStringAsync().Result); - Logger.ILog(`${commandName} command sent successfully`); - return responseData; - } else { - let error = response.Content.ReadAsStringAsync().Result; - Logger.WLog("API error: " + error); - return null; + if (response.IsSuccessStatusCode) { + let responseData = JSON.parse(response.Content.ReadAsStringAsync().Result); + Logger.ILog(`${commandName} command sent successfully`); + return responseData; + } else { + let error = response.Content.ReadAsStringAsync().Result; + Logger.WLog("API error: " + error); + return null; + } } -} -/** - * Sleeps, waiting for a command to complete - * @param {int} commandId ID of command being run - * @returns bool whether the coommand ran successfully - */ -waitForCompletion(commandId) -{ - const startTime = new Date().getTime(); - const timeout = 30000; - const endpoint = `command/${commandId}`; + /** + * Sleeps, waiting for a command to complete + * @param {int} commandId ID of command being run + * @returns bool whether the coommand ran successfully + */ + waitForCompletion(commandId) + { + const startTime = new Date().getTime(); + const timeout = 30000; + const endpoint = `command/${commandId}`; - while (new Date().getTime() - startTime <= timeout) { - let response = this.fetchJson(endpoint, ''); - if (response.status === 'completed') { - Logger.ILog('Scan completed!'); - return true; - } else if (response.status === 'failed') { - Logger.WLog(`Command ${commandId} failed`) - return false; + while (new Date().getTime() - startTime <= timeout) { + let response = this.fetchJson(endpoint, ''); + if (response.status === 'completed') { + Logger.ILog('Scan completed!'); + return true; + } else if (response.status === 'failed') { + Logger.WLog(`Command ${commandId} failed`) + return false; + } + Logger.ILog(`Checking status: ${response.status}`); + Sleep(100); } - Logger.ILog(`Checking status: ${response.status}`); - Sleep(100); + Logger.WLog('Timeout: Scan did not complete within 30 seconds.'); + return false; } - Logger.WLog('Timeout: Scan did not complete within 30 seconds.'); - return false; -} } \ No newline at end of file From 1cdd6793616b2bf9d38829b2984c0b170c12b5aa Mon Sep 17 00:00:00 2001 From: John Andrews Date: Sun, 9 Jun 2024 17:45:10 +1200 Subject: [PATCH 8/8] Update Sonarr.js --- Scripts/Shared/Sonarr.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Scripts/Shared/Sonarr.js b/Scripts/Shared/Sonarr.js index 101a6eb..ca4a14d 100644 --- a/Scripts/Shared/Sonarr.js +++ b/Scripts/Shared/Sonarr.js @@ -2,7 +2,7 @@ * @name Sonarr * @uid 0f5836c0-d20b-4740-9824-f81b5200ec3d * @description Class that interacts with Sonarr - * @revision 6 + * @revision 7 * @minimumVersion 1.0.0.0 */ export class Sonarr @@ -286,4 +286,4 @@ export class Sonarr Logger.WLog('Timeout: Scan did not complete within 30 seconds.'); return false; } -} \ No newline at end of file +}