From cb57ff4fed209fe447a1011af6d97f6e5c071573 Mon Sep 17 00:00:00 2001 From: "jeanchristophe.mqt@gmail.com" Date: Tue, 23 Jan 2024 11:45:21 +0100 Subject: [PATCH] Apply radarr/sonarr naming policy : handled file not found use case and enforced some strong typing --- .../1.0.0/index.js | 11 +++- .../1.0.0/index.ts | 56 ++++++++++--------- 2 files changed, 38 insertions(+), 29 deletions(-) diff --git a/FlowPlugins/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.js b/FlowPlugins/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.js index 9d4cab8..78c335e 100644 --- a/FlowPlugins/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.js +++ b/FlowPlugins/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.js @@ -134,6 +134,7 @@ var plugin = function (args) { return __awaiter(void 0, void 0, void 0, function case 1: parseRequestResult = _a.sent(); id = delegates.getId(parseRequestResult); + if (!(id !== '-1')) return [3 /*break*/, 6]; previewRenameRequestConfig = { method: 'get', url: delegates.getPreviewRenameResquestUrl(id, parseRequestResult), @@ -158,14 +159,18 @@ var plugin = function (args) { return __awaiter(void 0, void 0, void 0, function case 4: args.jobLog('✔ No rename necessary.'); _a.label = 5; - case 5: return [2 /*return*/, destinationPath]; + case 5: return [3 /*break*/, 7]; + case 6: + args.jobLog("\u2714 No ".concat(arr === 'radarr' ? 'movie' : 'serie', " with a file named '").concat(fileName, "'.")); + _a.label = 7; + case 7: return [2 /*return*/, destinationPath]; } }); }); }; destinationPath = ''; if (!(arr === 'radarr')) return [3 /*break*/, 2]; return [4 /*yield*/, rename({ - getId: function (parseRequestResult) { return parseRequestResult.data.movie.movieFile.movieId; }, + getId: function (parseRequestResult) { var _a, _b, _c, _d; return String((_d = (_c = (_b = (_a = parseRequestResult.data) === null || _a === void 0 ? void 0 : _a.movie) === null || _b === void 0 ? void 0 : _b.movieFile) === null || _c === void 0 ? void 0 : _c.movieId) !== null && _d !== void 0 ? _d : -1); }, getPreviewRenameResquestUrl: function (id, parseRequestResult) { return "".concat(arrHost, "/api/v3/rename?movieId=").concat(id); }, getFileToRename: function (previewRenameRequestResult) { var _a, _b; @@ -181,7 +186,7 @@ var plugin = function (args) { return __awaiter(void 0, void 0, void 0, function if (!(arr === 'sonarr')) return [3 /*break*/, 4]; episodeNumber_1 = 0; return [4 /*yield*/, rename({ - getId: function (parseRequestResult) { return parseRequestResult.data.series.id; }, + getId: function (parseRequestResult) { var _a, _b, _c; return String((_c = (_b = (_a = parseRequestResult.data) === null || _a === void 0 ? void 0 : _a.series) === null || _b === void 0 ? void 0 : _b.id) !== null && _c !== void 0 ? _c : -1); }, getPreviewRenameResquestUrl: function (id, parseRequestResult) { episodeNumber_1 = parseRequestResult.data.parsedEpisodeInfo.episodeNumbers[0]; return "".concat(arrHost, "/api/v3/rename?seriesId=").concat(id, "&seasonNumber=").concat(parseRequestResult.data.parsedEpisodeInfo.seasonNumber); diff --git a/FlowPluginsTs/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.ts b/FlowPluginsTs/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.ts index e778c13..46523d7 100644 --- a/FlowPluginsTs/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.ts +++ b/FlowPluginsTs/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.ts @@ -77,8 +77,8 @@ const plugin = async (args: IpluginInputArgs): Promise => { const fileName = getFileName(args.inputFileObj._id); interface IRenameDelegates { - getId: (parseRequestResult: any) => any, - getPreviewRenameResquestUrl: (id: any, parseRequestResult: any) => any, + getId: (parseRequestResult: any) => string, + getPreviewRenameResquestUrl: (id: string, parseRequestResult: any) => string, getFileToRename: (previewRenameRequestResult: any) => any } @@ -104,28 +104,33 @@ const plugin = async (args: IpluginInputArgs): Promise => { const parseRequestResult = await args.deps.axios(parseRequestConfig); const id = delegates.getId(parseRequestResult); - // Using rename endpoint to get ids of all the files that need renaming. - const previewRenameRequestConfig = { - method: 'get', - url: delegates.getPreviewRenameResquestUrl(id, parseRequestResult), - headers, - }; - const previewRenameRequestResult = await args.deps.axios(previewRenameRequestConfig); - const fileToRename = delegates.getFileToRename(previewRenameRequestResult); + // Checking that the file has been found. A file not found might be caused because Radarr/Sonarr hasn't been notified of a file rename (notify plugin missing ?) + // or because Radarr/Sonarr has upgraded the movie/serie to another release before the end of the plugin stack execution. + if (id !== '-1') { + // Using rename endpoint to get ids of all the files that need renaming. + const previewRenameRequestConfig = { + method: 'get', + url: delegates.getPreviewRenameResquestUrl(id, parseRequestResult), + headers, + }; + const previewRenameRequestResult = await args.deps.axios(previewRenameRequestConfig); + const fileToRename = delegates.getFileToRename(previewRenameRequestResult); - // Only if there is a rename to execute - if (fileToRename !== undefined) { - destinationPath = `${getFileAbosluteDir(args.inputFileObj._id)}/${getFileName(fileToRename.newPath)}.${getContainer(fileToRename.newPath)}`; + // Only if there is a rename to execute + if (fileToRename !== undefined) { + destinationPath = `${getFileAbosluteDir(args.inputFileObj._id)}/${getFileName(fileToRename.newPath)}.${getContainer(fileToRename.newPath)}`; - await fileMoveOrCopy({ - operation: 'move', - sourcePath: args.inputFileObj._id, - destinationPath: destinationPath, - args, - }); - args.jobLog(`✔ Renamed ${arr === 'radarr' ? 'movie' : 'serie'} ${id} : '${args.inputFileObj._id}' => '${destinationPath}'.`); + await fileMoveOrCopy({ + operation: 'move', + sourcePath: args.inputFileObj._id, + destinationPath: destinationPath, + args, + }); + args.jobLog(`✔ Renamed ${arr === 'radarr' ? 'movie' : 'serie'} ${id} : '${args.inputFileObj._id}' => '${destinationPath}'.`); + } else + args.jobLog('✔ No rename necessary.'); } else - args.jobLog('✔ No rename necessary.'); + args.jobLog(`✔ No ${arr === 'radarr' ? 'movie' : 'serie'} with a file named '${fileName}'.`); return destinationPath; }; @@ -133,7 +138,7 @@ const plugin = async (args: IpluginInputArgs): Promise => { let destinationPath = ''; if (arr === 'radarr') { destinationPath = await rename({ - getId: (parseRequestResult) => parseRequestResult.data.movie.movieFile.movieId, + getId: (parseRequestResult) => String(parseRequestResult.data?.movie?.movieFile?.movieId ?? -1), getPreviewRenameResquestUrl: (id, parseRequestResult) => `${arrHost}/api/v3/rename?movieId=${id}`, getFileToRename: (previewRenameRequestResult) => ((previewRenameRequestResult.data?.length ?? 0) > 0) ? @@ -143,16 +148,15 @@ const plugin = async (args: IpluginInputArgs): Promise => { } else if (arr === 'sonarr') { let episodeNumber = 0; destinationPath = await rename({ - getId: (parseRequestResult) => parseRequestResult.data.series.id, + getId: (parseRequestResult) => String(parseRequestResult.data?.series?.id ?? -1), getPreviewRenameResquestUrl: (id, parseRequestResult) => { episodeNumber = parseRequestResult.data.parsedEpisodeInfo.episodeNumbers[0]; return `${arrHost}/api/v3/rename?seriesId=${id}&seasonNumber=${parseRequestResult.data.parsedEpisodeInfo.seasonNumber}`; }, - getFileToRename: (previewRenameRequestResult) => { - return ((previewRenameRequestResult.data?.length ?? 0) > 0) ? + getFileToRename: (previewRenameRequestResult) => + ((previewRenameRequestResult.data?.length ?? 0) > 0) ? previewRenameRequestResult.data.find((episFile: { episodeNumbers: number[]; }) => ((episFile.episodeNumbers?.length ?? 0) > 0) ? episFile.episodeNumbers[0] === episodeNumber : false) : undefined - } }); } else { args.jobLog('No arr specified in plugin inputs.');