From 9ade8f4b58635e0825cfa393d0172ad687a8154b Mon Sep 17 00:00:00 2001 From: "jeanchristophe.mqt@gmail.com" Date: Fri, 19 Jan 2024 14:21:44 +0100 Subject: [PATCH 01/34] Created Flow plugin for Radarr and Sonarr renaming --- .../1.0.0/index.ts | 159 ++++++++++++++++++ 1 file changed, 159 insertions(+) create mode 100644 FlowPluginsTs/CommunityFlowPlugins/tools/forceFileRenameRadarrOrSonarr/1.0.0/index.ts diff --git a/FlowPluginsTs/CommunityFlowPlugins/tools/forceFileRenameRadarrOrSonarr/1.0.0/index.ts b/FlowPluginsTs/CommunityFlowPlugins/tools/forceFileRenameRadarrOrSonarr/1.0.0/index.ts new file mode 100644 index 0000000..d8d788c --- /dev/null +++ b/FlowPluginsTs/CommunityFlowPlugins/tools/forceFileRenameRadarrOrSonarr/1.0.0/index.ts @@ -0,0 +1,159 @@ +import { + IpluginDetails, + IpluginInputArgs, + IpluginOutputArgs, +} from '../../../../FlowHelpers/1.0.0/interfaces/interfaces'; + +const details = (): IpluginDetails => ({ + name: 'Force File Rename Radarr or Sonarr', + description: 'Force Radarr or Sonarr to rename a file according to the naming policy', + style: { + borderColor: 'green', + }, + tags: '', + isStartPlugin: false, + pType: '', + requiresVersion: '2.11.01', + sidebarPosition: -1, + icon: 'faBell', + inputs: [ + { + label: 'Arr', + name: 'arr', + type: 'string', + defaultValue: 'radarr', + inputUI: { + type: 'dropdown', + options: ['radarr', 'sonarr'], + }, + tooltip: 'Specify which arr to use', + }, + { + label: 'Arr API Key', + name: 'arr_api_key', + type: 'string', + defaultValue: '', + inputUI: { + type: 'text', + }, + tooltip: 'Input your arr api key here', + }, + { + label: 'Arr Host', + name: 'arr_host', + type: 'string', + defaultValue: 'http://192.168.1.1:7878', + inputUI: { + type: 'text', + }, + tooltip: 'Input your arr host here.' + + '\\nExample:\\n' + + 'http://192.168.1.1:7878\\n' + + 'http://192.168.1.1:8989\\n' + + 'https://radarr.domain.com\\n' + + 'https://sonarr.domain.com\\n', + }, + ], + outputs: [ + { + number: 1, + tooltip: 'Continue to next plugin', + }, + ], +}); + +const plugin = async (args: IpluginInputArgs): Promise => { + const lib = require('../../../../../methods/lib')(); + // eslint-disable-next-line @typescript-eslint/no-unused-vars,no-param-reassign + args.inputs = lib.loadDefaultValues(args.inputs, details); + + const { arr, arr_api_key } = args.inputs; + const arr_host = String(args.inputs.arr_host).trim(); + + const fileName = args.originalLibraryFile?.meta?.FileName || ''; + + const arrHost = arr_host.endsWith('/') ? arr_host.slice(0, -1) : arr_host; + + const headers = { + 'Content-Type': 'application/json', + 'X-Api-Key': arr_api_key, + Accept: 'application/json', + }; + + args.jobLog('Going to force rename'); + + const rename = async ( + getId: (parseRequestResult: any) => any, + getPreviewRenameResquestUrl: (id: any, parseRequestResult: any) => any, + getRenameResquestData: (id: any, previewRenameRequestResult: any) => any) + : Promise => { + args.jobLog(`Renaming ${arr === 'radarr' ? 'Radarr' : 'Sonarr'}...`); + + // Using parse endpoint to get the movie/serie's id. + const parseRequestConfig = { + method: 'get', + url: `${arrHost}/api/v3/parse?title=${encodeURIComponent(fileName)}`, + headers, + }; + const parseRequestResult = await args.deps.axios(parseRequestConfig); + const id = getId(parseRequestResult); + + // Using rename endpoint to get ids of all the files that need renaming. + const previewRenameRequestConfig = { + method: 'get', + url: getPreviewRenameResquestUrl(id, parseRequestResult), + headers, + }; + const previewRenameRequestResult = await args.deps.axios(previewRenameRequestConfig); + + // Using command endpoint to schedule the renames. + const renameRequestConfig = { + method: 'post', + url: `${arrHost}/api/v3/command`, + headers, + data: JSON.stringify(getRenameResquestData(id, previewRenameRequestResult)) + }; + await args.deps.axios(renameRequestConfig); + + args.jobLog(`✔ Renamed ${arr === 'radarr' ? 'movie' : 'serie'} ${id} in ${arr === 'radarr' ? 'Radarr' : 'Sonarr'}.`); + }; + + if (arr === 'radarr') { + await rename( + (parseRequestResult) => parseRequestResult.data.movie.movieFile.movieId, + (id, parseRequestResult) => `${arrHost}/api/v3/rename?movieId=${id}`, + (id, previewRenameRequestResult) => { + return { + name: 'RenameFiles', + movieId: id, + files: previewRenameRequestResult.data.map((movieFile: { movieFileId: any; }) => movieFile.movieFileId) + }; + } + ); + } else if (arr === 'sonarr') { + await rename( + (parseRequestResult) => parseRequestResult.data.series.id, + (id, parseRequestResult) => `${arrHost}/api/v3/rename?seriesId=${id}&seasonNumber=${parseRequestResult.data.parsedEpisodeInfo.seasonNumber}`, + (id, previewRenameRequestResult) => { + return { + name: 'RenameFiles', + seriesId: id, + files: previewRenameRequestResult.data.map((episodeFile: { episodeFileId: any; }) => episodeFile.episodeFileId) + }; + } + ); + } else { + args.jobLog('No arr specified in plugin inputs.'); + } + + return { + outputFileObj: args.inputFileObj, + outputNumber: 1, + variables: args.variables, + }; +}; + +export { + details, + plugin, +}; From aa79f27f5a0d3bd6ef48c1f13525ac807efe0ac2 Mon Sep 17 00:00:00 2001 From: "jeanchristophe.mqt@gmail.com" Date: Fri, 19 Jan 2024 14:47:52 +0100 Subject: [PATCH 02/34] Compiled Flow plugin for Radarr and Sonarr renaming --- .../1.0.0/index.js | 188 ++++++++++++++++++ 1 file changed, 188 insertions(+) create mode 100644 FlowPlugins/CommunityFlowPlugins/tools/forceFileRenameRadarrOrSonarr/1.0.0/index.js diff --git a/FlowPlugins/CommunityFlowPlugins/tools/forceFileRenameRadarrOrSonarr/1.0.0/index.js b/FlowPlugins/CommunityFlowPlugins/tools/forceFileRenameRadarrOrSonarr/1.0.0/index.js new file mode 100644 index 0000000..cb731b0 --- /dev/null +++ b/FlowPlugins/CommunityFlowPlugins/tools/forceFileRenameRadarrOrSonarr/1.0.0/index.js @@ -0,0 +1,188 @@ +"use strict"; +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } + return new (P || (P = Promise))(function (resolve, reject) { + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } + function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } + step((generator = generator.apply(thisArg, _arguments || [])).next()); + }); +}; +var __generator = (this && this.__generator) || function (thisArg, body) { + var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; + return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; + function verb(n) { return function (v) { return step([n, v]); }; } + function step(op) { + if (f) throw new TypeError("Generator is already executing."); + while (g && (g = 0, op[0] && (_ = 0)), _) try { + if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; + if (y = 0, t) op = [op[0] & 2, t.value]; + switch (op[0]) { + case 0: case 1: t = op; break; + case 4: _.label++; return { value: op[1], done: false }; + case 5: _.label++; y = op[1]; op = [0]; continue; + case 7: op = _.ops.pop(); _.trys.pop(); continue; + default: + if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } + if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } + if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } + if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } + if (t[2]) _.ops.pop(); + _.trys.pop(); continue; + } + op = body.call(thisArg, _); + } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } + if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; + } +}; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.plugin = exports.details = void 0; +var details = function () { return ({ + name: 'Force File Rename Radarr or Sonarr', + description: 'Force Radarr or Sonarr to rename a file according to the naming policy', + style: { + borderColor: 'green', + }, + tags: '', + isStartPlugin: false, + pType: '', + requiresVersion: '2.11.01', + sidebarPosition: -1, + icon: 'faBell', + inputs: [ + { + label: 'Arr', + name: 'arr', + type: 'string', + defaultValue: 'radarr', + inputUI: { + type: 'dropdown', + options: ['radarr', 'sonarr'], + }, + tooltip: 'Specify which arr to use', + }, + { + label: 'Arr API Key', + name: 'arr_api_key', + type: 'string', + defaultValue: '', + inputUI: { + type: 'text', + }, + tooltip: 'Input your arr api key here', + }, + { + label: 'Arr Host', + name: 'arr_host', + type: 'string', + defaultValue: 'http://192.168.1.1:7878', + inputUI: { + type: 'text', + }, + tooltip: 'Input your arr host here.' + + '\\nExample:\\n' + + 'http://192.168.1.1:7878\\n' + + 'http://192.168.1.1:8989\\n' + + 'https://radarr.domain.com\\n' + + 'https://sonarr.domain.com\\n', + }, + ], + outputs: [ + { + number: 1, + tooltip: 'Continue to next plugin', + }, + ], +}); }; +exports.details = details; +var plugin = function (args) { return __awaiter(void 0, void 0, void 0, function () { + var lib, _a, arr, arr_api_key, arr_host, fileName, arrHost, headers, rename; + var _b, _c; + return __generator(this, function (_d) { + switch (_d.label) { + case 0: + lib = require('../../../../../methods/lib')(); + // eslint-disable-next-line @typescript-eslint/no-unused-vars,no-param-reassign + args.inputs = lib.loadDefaultValues(args.inputs, details); + _a = args.inputs, arr = _a.arr, arr_api_key = _a.arr_api_key; + arr_host = String(args.inputs.arr_host).trim(); + fileName = ((_c = (_b = args.originalLibraryFile) === null || _b === void 0 ? void 0 : _b.meta) === null || _c === void 0 ? void 0 : _c.FileName) || ''; + arrHost = arr_host.endsWith('/') ? arr_host.slice(0, -1) : arr_host; + headers = { + 'Content-Type': 'application/json', + 'X-Api-Key': arr_api_key, + Accept: 'application/json', + }; + args.jobLog('Going to force rename'); + rename = function (getId, getPreviewRenameResquestUrl, getRenameResquestData) { return __awaiter(void 0, void 0, void 0, function () { + var parseRequestConfig, parseRequestResult, id, previewRenameRequestConfig, previewRenameRequestResult, renameRequestConfig; + return __generator(this, function (_a) { + switch (_a.label) { + case 0: + args.jobLog("Renaming ".concat(arr === 'radarr' ? 'Radarr' : 'Sonarr', "...")); + parseRequestConfig = { + method: 'get', + url: "".concat(arrHost, "/api/v3/parse?title=").concat(encodeURIComponent(fileName)), + headers: headers, + }; + return [4 /*yield*/, args.deps.axios(parseRequestConfig)]; + case 1: + parseRequestResult = _a.sent(); + id = getId(parseRequestResult); + previewRenameRequestConfig = { + method: 'get', + url: getPreviewRenameResquestUrl(id, parseRequestResult), + headers: headers, + }; + return [4 /*yield*/, args.deps.axios(previewRenameRequestConfig)]; + case 2: + previewRenameRequestResult = _a.sent(); + renameRequestConfig = { + method: 'post', + url: "".concat(arrHost, "/api/v3/command"), + headers: headers, + data: JSON.stringify(getRenameResquestData(id, previewRenameRequestResult)) + }; + return [4 /*yield*/, args.deps.axios(renameRequestConfig)]; + case 3: + _a.sent(); + args.jobLog("\u2714 Renamed ".concat(arr === 'radarr' ? 'movie' : 'serie', " ").concat(id, " in ").concat(arr === 'radarr' ? 'Radarr' : 'Sonarr', ".")); + return [2 /*return*/]; + } + }); + }); }; + if (!(arr === 'radarr')) return [3 /*break*/, 2]; + return [4 /*yield*/, rename(function (parseRequestResult) { return parseRequestResult.data.movie.movieFile.movieId; }, function (id, parseRequestResult) { return "".concat(arrHost, "/api/v3/rename?movieId=").concat(id); }, function (id, previewRenameRequestResult) { + return { + name: 'RenameFiles', + movieId: id, + files: previewRenameRequestResult.data.map(function (movieFile) { return movieFile.movieFileId; }) + }; + })]; + case 1: + _d.sent(); + return [3 /*break*/, 5]; + case 2: + if (!(arr === 'sonarr')) return [3 /*break*/, 4]; + return [4 /*yield*/, rename(function (parseRequestResult) { return parseRequestResult.data.series.id; }, function (id, parseRequestResult) { return "".concat(arrHost, "/api/v3/rename?seriesId=").concat(id, "&seasonNumber=").concat(parseRequestResult.data.parsedEpisodeInfo.seasonNumber); }, function (id, previewRenameRequestResult) { + return { + name: 'RenameFiles', + seriesId: id, + files: previewRenameRequestResult.data.map(function (episodeFile) { return episodeFile.episodeFileId; }) + }; + })]; + case 3: + _d.sent(); + return [3 /*break*/, 5]; + case 4: + args.jobLog('No arr specified in plugin inputs.'); + _d.label = 5; + case 5: return [2 /*return*/, { + outputFileObj: args.inputFileObj, + outputNumber: 1, + variables: args.variables, + }]; + } + }); +}); }; +exports.plugin = plugin; From 4be4bc7169b583a801b81cccad3e6940a4955ea4 Mon Sep 17 00:00:00 2001 From: "jeanchristophe.mqt@gmail.com" Date: Fri, 19 Jan 2024 16:41:36 +0100 Subject: [PATCH 03/34] Rename with radarr/sonarr Flow plugin now updates the file id --- .../1.0.0/index.js | 34 +++++++++++++------ .../1.0.0/index.ts | 25 ++++++++++---- 2 files changed, 43 insertions(+), 16 deletions(-) diff --git a/FlowPlugins/CommunityFlowPlugins/tools/forceFileRenameRadarrOrSonarr/1.0.0/index.js b/FlowPlugins/CommunityFlowPlugins/tools/forceFileRenameRadarrOrSonarr/1.0.0/index.js index cb731b0..426ba5b 100644 --- a/FlowPlugins/CommunityFlowPlugins/tools/forceFileRenameRadarrOrSonarr/1.0.0/index.js +++ b/FlowPlugins/CommunityFlowPlugins/tools/forceFileRenameRadarrOrSonarr/1.0.0/index.js @@ -96,7 +96,7 @@ var details = function () { return ({ }); }; exports.details = details; var plugin = function (args) { return __awaiter(void 0, void 0, void 0, function () { - var lib, _a, arr, arr_api_key, arr_host, fileName, arrHost, headers, rename; + var lib, _a, arr, arr_api_key, arr_host, fileName, arrHost, headers, rename, existingPath, newPath, episodeNumber_1, newFileId; var _b, _c; return __generator(this, function (_d) { switch (_d.label) { @@ -113,12 +113,12 @@ var plugin = function (args) { return __awaiter(void 0, void 0, void 0, function 'X-Api-Key': arr_api_key, Accept: 'application/json', }; - args.jobLog('Going to force rename'); rename = function (getId, getPreviewRenameResquestUrl, getRenameResquestData) { return __awaiter(void 0, void 0, void 0, function () { var parseRequestConfig, parseRequestResult, id, previewRenameRequestConfig, previewRenameRequestResult, renameRequestConfig; return __generator(this, function (_a) { switch (_a.label) { case 0: + args.jobLog('Going to force rename'); args.jobLog("Renaming ".concat(arr === 'radarr' ? 'Radarr' : 'Sonarr', "...")); parseRequestConfig = { method: 'get', @@ -151,12 +151,15 @@ var plugin = function (args) { return __awaiter(void 0, void 0, void 0, function } }); }); }; + newPath = ''; if (!(arr === 'radarr')) return [3 /*break*/, 2]; return [4 /*yield*/, rename(function (parseRequestResult) { return parseRequestResult.data.movie.movieFile.movieId; }, function (id, parseRequestResult) { return "".concat(arrHost, "/api/v3/rename?movieId=").concat(id); }, function (id, previewRenameRequestResult) { + var movieFile = previewRenameRequestResult.data[0]; + (existingPath = movieFile.existingPath, newPath = movieFile.newPath); return { name: 'RenameFiles', movieId: id, - files: previewRenameRequestResult.data.map(function (movieFile) { return movieFile.movieFileId; }) + files: [movieFile.movieFileId] }; })]; case 1: @@ -164,11 +167,17 @@ var plugin = function (args) { return __awaiter(void 0, void 0, void 0, function return [3 /*break*/, 5]; case 2: if (!(arr === 'sonarr')) return [3 /*break*/, 4]; - return [4 /*yield*/, rename(function (parseRequestResult) { return parseRequestResult.data.series.id; }, function (id, parseRequestResult) { return "".concat(arrHost, "/api/v3/rename?seriesId=").concat(id, "&seasonNumber=").concat(parseRequestResult.data.parsedEpisodeInfo.seasonNumber); }, function (id, previewRenameRequestResult) { + episodeNumber_1 = 0; + return [4 /*yield*/, rename(function (parseRequestResult) { return parseRequestResult.data.series.id; }, 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); + }, function (id, previewRenameRequestResult) { + var episodeFile = previewRenameRequestResult.find(function (episFile) { return episFile.episodeNumbers[0] === episodeNumber_1; }); + (existingPath = episodeFile.existingPath, newPath = episodeFile.newPath); return { name: 'RenameFiles', seriesId: id, - files: previewRenameRequestResult.data.map(function (episodeFile) { return episodeFile.episodeFileId; }) + files: [episodeFile.episodeFileId] }; })]; case 3: @@ -177,11 +186,16 @@ var plugin = function (args) { return __awaiter(void 0, void 0, void 0, function case 4: args.jobLog('No arr specified in plugin inputs.'); _d.label = 5; - case 5: return [2 /*return*/, { - outputFileObj: args.inputFileObj, - outputNumber: 1, - variables: args.variables, - }]; + case 5: + newFileId = args.inputFileObj.replace(existingPath, newPath); + args.jobLog("New file iid ".concat(newFileId)); + return [2 /*return*/, { + outputFileObj: { + _id: newFileId + }, + outputNumber: 1, + variables: args.variables, + }]; } }); }); }; diff --git a/FlowPluginsTs/CommunityFlowPlugins/tools/forceFileRenameRadarrOrSonarr/1.0.0/index.ts b/FlowPluginsTs/CommunityFlowPlugins/tools/forceFileRenameRadarrOrSonarr/1.0.0/index.ts index d8d788c..69d472b 100644 --- a/FlowPluginsTs/CommunityFlowPlugins/tools/forceFileRenameRadarrOrSonarr/1.0.0/index.ts +++ b/FlowPluginsTs/CommunityFlowPlugins/tools/forceFileRenameRadarrOrSonarr/1.0.0/index.ts @@ -80,13 +80,13 @@ const plugin = async (args: IpluginInputArgs): Promise => { Accept: 'application/json', }; - args.jobLog('Going to force rename'); - const rename = async ( getId: (parseRequestResult: any) => any, getPreviewRenameResquestUrl: (id: any, parseRequestResult: any) => any, getRenameResquestData: (id: any, previewRenameRequestResult: any) => any) : Promise => { + args.jobLog('Going to force rename'); + args.jobLog(`Renaming ${arr === 'radarr' ? 'Radarr' : 'Sonarr'}...`); // Using parse endpoint to get the movie/serie's id. @@ -118,27 +118,36 @@ const plugin = async (args: IpluginInputArgs): Promise => { args.jobLog(`✔ Renamed ${arr === 'radarr' ? 'movie' : 'serie'} ${id} in ${arr === 'radarr' ? 'Radarr' : 'Sonarr'}.`); }; + let existingPath, newPath = ''; if (arr === 'radarr') { await rename( (parseRequestResult) => parseRequestResult.data.movie.movieFile.movieId, (id, parseRequestResult) => `${arrHost}/api/v3/rename?movieId=${id}`, (id, previewRenameRequestResult) => { + const movieFile = previewRenameRequestResult.data[0]; + ({ existingPath, newPath } = movieFile); return { name: 'RenameFiles', movieId: id, - files: previewRenameRequestResult.data.map((movieFile: { movieFileId: any; }) => movieFile.movieFileId) + files: [movieFile.movieFileId] }; } ); } else if (arr === 'sonarr') { + let episodeNumber = 0; await rename( (parseRequestResult) => parseRequestResult.data.series.id, - (id, parseRequestResult) => `${arrHost}/api/v3/rename?seriesId=${id}&seasonNumber=${parseRequestResult.data.parsedEpisodeInfo.seasonNumber}`, + (id, parseRequestResult) => { + episodeNumber = parseRequestResult.data.parsedEpisodeInfo.episodeNumbers[0]; + return `${arrHost}/api/v3/rename?seriesId=${id}&seasonNumber=${parseRequestResult.data.parsedEpisodeInfo.seasonNumber}`; + }, (id, previewRenameRequestResult) => { + const episodeFile = previewRenameRequestResult.find((episFile: { episodeNumbers: number[]; }) => episFile.episodeNumbers[0] === episodeNumber); + ({ existingPath, newPath } = episodeFile); return { name: 'RenameFiles', seriesId: id, - files: previewRenameRequestResult.data.map((episodeFile: { episodeFileId: any; }) => episodeFile.episodeFileId) + files: [episodeFile.episodeFileId] }; } ); @@ -146,8 +155,12 @@ const plugin = async (args: IpluginInputArgs): Promise => { args.jobLog('No arr specified in plugin inputs.'); } + const newFileId = args.inputFileObj.replace(existingPath, newPath); + args.jobLog(`New file iid ${newFileId}`); return { - outputFileObj: args.inputFileObj, + outputFileObj: { + _id: newFileId + }, outputNumber: 1, variables: args.variables, }; From 06e1213c719144166079ee026db491d31f5deff5 Mon Sep 17 00:00:00 2001 From: "jeanchristophe.mqt@gmail.com" Date: Fri, 19 Jan 2024 16:59:30 +0100 Subject: [PATCH 04/34] Force rename with radarr/sonarr : corrected mistake --- .../tools/forceFileRenameRadarrOrSonarr/1.0.0/index.js | 8 ++++---- .../tools/forceFileRenameRadarrOrSonarr/1.0.0/index.ts | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/FlowPlugins/CommunityFlowPlugins/tools/forceFileRenameRadarrOrSonarr/1.0.0/index.js b/FlowPlugins/CommunityFlowPlugins/tools/forceFileRenameRadarrOrSonarr/1.0.0/index.js index 426ba5b..3f2393d 100644 --- a/FlowPlugins/CommunityFlowPlugins/tools/forceFileRenameRadarrOrSonarr/1.0.0/index.js +++ b/FlowPlugins/CommunityFlowPlugins/tools/forceFileRenameRadarrOrSonarr/1.0.0/index.js @@ -113,7 +113,7 @@ var plugin = function (args) { return __awaiter(void 0, void 0, void 0, function 'X-Api-Key': arr_api_key, Accept: 'application/json', }; - rename = function (getId, getPreviewRenameResquestUrl, getRenameResquestData) { return __awaiter(void 0, void 0, void 0, function () { + rename = function (getId, getPreviewRenameResquestUrl, getRenameResquestConfigData) { return __awaiter(void 0, void 0, void 0, function () { var parseRequestConfig, parseRequestResult, id, previewRenameRequestConfig, previewRenameRequestResult, renameRequestConfig; return __generator(this, function (_a) { switch (_a.label) { @@ -141,7 +141,7 @@ var plugin = function (args) { return __awaiter(void 0, void 0, void 0, function method: 'post', url: "".concat(arrHost, "/api/v3/command"), headers: headers, - data: JSON.stringify(getRenameResquestData(id, previewRenameRequestResult)) + data: JSON.stringify(getRenameResquestConfigData(id, previewRenameRequestResult)) }; return [4 /*yield*/, args.deps.axios(renameRequestConfig)]; case 3: @@ -172,7 +172,7 @@ var plugin = function (args) { return __awaiter(void 0, void 0, void 0, function episodeNumber_1 = parseRequestResult.data.parsedEpisodeInfo.episodeNumbers[0]; return "".concat(arrHost, "/api/v3/rename?seriesId=").concat(id, "&seasonNumber=").concat(parseRequestResult.data.parsedEpisodeInfo.seasonNumber); }, function (id, previewRenameRequestResult) { - var episodeFile = previewRenameRequestResult.find(function (episFile) { return episFile.episodeNumbers[0] === episodeNumber_1; }); + var episodeFile = previewRenameRequestResult.data.find(function (episFile) { return episFile.episodeNumbers[0] === episodeNumber_1; }); (existingPath = episodeFile.existingPath, newPath = episodeFile.newPath); return { name: 'RenameFiles', @@ -188,7 +188,7 @@ var plugin = function (args) { return __awaiter(void 0, void 0, void 0, function _d.label = 5; case 5: newFileId = args.inputFileObj.replace(existingPath, newPath); - args.jobLog("New file iid ".concat(newFileId)); + args.jobLog("New file id ".concat(newFileId)); return [2 /*return*/, { outputFileObj: { _id: newFileId diff --git a/FlowPluginsTs/CommunityFlowPlugins/tools/forceFileRenameRadarrOrSonarr/1.0.0/index.ts b/FlowPluginsTs/CommunityFlowPlugins/tools/forceFileRenameRadarrOrSonarr/1.0.0/index.ts index 69d472b..4b6eb58 100644 --- a/FlowPluginsTs/CommunityFlowPlugins/tools/forceFileRenameRadarrOrSonarr/1.0.0/index.ts +++ b/FlowPluginsTs/CommunityFlowPlugins/tools/forceFileRenameRadarrOrSonarr/1.0.0/index.ts @@ -83,7 +83,7 @@ const plugin = async (args: IpluginInputArgs): Promise => { const rename = async ( getId: (parseRequestResult: any) => any, getPreviewRenameResquestUrl: (id: any, parseRequestResult: any) => any, - getRenameResquestData: (id: any, previewRenameRequestResult: any) => any) + getRenameResquestConfigData: (id: any, previewRenameRequestResult: any) => any) : Promise => { args.jobLog('Going to force rename'); @@ -111,7 +111,7 @@ const plugin = async (args: IpluginInputArgs): Promise => { method: 'post', url: `${arrHost}/api/v3/command`, headers, - data: JSON.stringify(getRenameResquestData(id, previewRenameRequestResult)) + data: JSON.stringify(getRenameResquestConfigData(id, previewRenameRequestResult)) }; await args.deps.axios(renameRequestConfig); @@ -142,7 +142,7 @@ const plugin = async (args: IpluginInputArgs): Promise => { return `${arrHost}/api/v3/rename?seriesId=${id}&seasonNumber=${parseRequestResult.data.parsedEpisodeInfo.seasonNumber}`; }, (id, previewRenameRequestResult) => { - const episodeFile = previewRenameRequestResult.find((episFile: { episodeNumbers: number[]; }) => episFile.episodeNumbers[0] === episodeNumber); + const episodeFile = previewRenameRequestResult.data.find((episFile: { episodeNumbers: number[]; }) => episFile.episodeNumbers[0] === episodeNumber); ({ existingPath, newPath } = episodeFile); return { name: 'RenameFiles', @@ -156,7 +156,7 @@ const plugin = async (args: IpluginInputArgs): Promise => { } const newFileId = args.inputFileObj.replace(existingPath, newPath); - args.jobLog(`New file iid ${newFileId}`); + args.jobLog(`New file id ${newFileId}`); return { outputFileObj: { _id: newFileId From a9388db03519bb2b29858da75c9c12d8d33d09eb Mon Sep 17 00:00:00 2001 From: "jeanchristophe.mqt@gmail.com" Date: Fri, 19 Jan 2024 17:23:46 +0100 Subject: [PATCH 05/34] Force rename with radarr/sonarr : handling no rename needed --- .../1.0.0/index.js | 72 +++++++++++-------- .../1.0.0/index.ts | 67 ++++++++++------- 2 files changed, 82 insertions(+), 57 deletions(-) diff --git a/FlowPlugins/CommunityFlowPlugins/tools/forceFileRenameRadarrOrSonarr/1.0.0/index.js b/FlowPlugins/CommunityFlowPlugins/tools/forceFileRenameRadarrOrSonarr/1.0.0/index.js index 3f2393d..2d63db5 100644 --- a/FlowPlugins/CommunityFlowPlugins/tools/forceFileRenameRadarrOrSonarr/1.0.0/index.js +++ b/FlowPlugins/CommunityFlowPlugins/tools/forceFileRenameRadarrOrSonarr/1.0.0/index.js @@ -96,30 +96,32 @@ var details = function () { return ({ }); }; exports.details = details; var plugin = function (args) { return __awaiter(void 0, void 0, void 0, function () { - var lib, _a, arr, arr_api_key, arr_host, fileName, arrHost, headers, rename, existingPath, newPath, episodeNumber_1, newFileId; + var lib, _a, arr, arr_api_key, arr_host, fileName, arrHost, headers, rename, existingPath, newPath, episodeNumber_1; var _b, _c; - return __generator(this, function (_d) { - switch (_d.label) { + var _d, _e; + return __generator(this, function (_f) { + switch (_f.label) { case 0: lib = require('../../../../../methods/lib')(); // eslint-disable-next-line @typescript-eslint/no-unused-vars,no-param-reassign args.inputs = lib.loadDefaultValues(args.inputs, details); _a = args.inputs, arr = _a.arr, arr_api_key = _a.arr_api_key; arr_host = String(args.inputs.arr_host).trim(); - fileName = ((_c = (_b = args.originalLibraryFile) === null || _b === void 0 ? void 0 : _b.meta) === null || _c === void 0 ? void 0 : _c.FileName) || ''; + fileName = ((_e = (_d = args.originalLibraryFile) === null || _d === void 0 ? void 0 : _d.meta) === null || _e === void 0 ? void 0 : _e.FileName) || ''; arrHost = arr_host.endsWith('/') ? arr_host.slice(0, -1) : arr_host; headers = { 'Content-Type': 'application/json', 'X-Api-Key': arr_api_key, Accept: 'application/json', }; - rename = function (getId, getPreviewRenameResquestUrl, getRenameResquestConfigData) { return __awaiter(void 0, void 0, void 0, function () { - var parseRequestConfig, parseRequestResult, id, previewRenameRequestConfig, previewRenameRequestResult, renameRequestConfig; + rename = function (getId, getPreviewRenameResquestUrl, getFileToRename, getRenameResquestConfigData) { return __awaiter(void 0, void 0, void 0, function () { + var existingPath, newPath, parseRequestConfig, parseRequestResult, id, previewRenameRequestConfig, previewRenameRequestResult, fileToRename, renameRequestConfig; return __generator(this, function (_a) { switch (_a.label) { case 0: args.jobLog('Going to force rename'); args.jobLog("Renaming ".concat(arr === 'radarr' ? 'Radarr' : 'Sonarr', "...")); + newPath = ''; parseRequestConfig = { method: 'get', url: "".concat(arrHost, "/api/v3/parse?title=").concat(encodeURIComponent(fileName)), @@ -137,33 +139,43 @@ var plugin = function (args) { return __awaiter(void 0, void 0, void 0, function return [4 /*yield*/, args.deps.axios(previewRenameRequestConfig)]; case 2: previewRenameRequestResult = _a.sent(); + fileToRename = getFileToRename(previewRenameRequestResult); + if (!(fileToRename !== undefined)) return [3 /*break*/, 4]; + (existingPath = fileToRename.existingPath, newPath = fileToRename.newPath); renameRequestConfig = { method: 'post', url: "".concat(arrHost, "/api/v3/command"), headers: headers, - data: JSON.stringify(getRenameResquestConfigData(id, previewRenameRequestResult)) + data: JSON.stringify(getRenameResquestConfigData(id, fileToRename)) }; return [4 /*yield*/, args.deps.axios(renameRequestConfig)]; case 3: _a.sent(); - args.jobLog("\u2714 Renamed ".concat(arr === 'radarr' ? 'movie' : 'serie', " ").concat(id, " in ").concat(arr === 'radarr' ? 'Radarr' : 'Sonarr', ".")); - return [2 /*return*/]; + args.jobLog("\u2714 Renamed ".concat(arr === 'radarr' ? 'movie' : 'serie', " ").concat(id, " in ").concat(arr === 'radarr' ? 'Radarr' : 'Sonarr', " : '").concat(existingPath, "' => '").concat(newPath, "'.")); + return [3 /*break*/, 5]; + case 4: + args.jobLog("\u2714 No rename necessary."); + _a.label = 5; + case 5: return [2 /*return*/, { existingPath: existingPath, newPath: newPath }]; } }); }); }; newPath = ''; if (!(arr === 'radarr')) return [3 /*break*/, 2]; - return [4 /*yield*/, rename(function (parseRequestResult) { return parseRequestResult.data.movie.movieFile.movieId; }, function (id, parseRequestResult) { return "".concat(arrHost, "/api/v3/rename?movieId=").concat(id); }, function (id, previewRenameRequestResult) { - var movieFile = previewRenameRequestResult.data[0]; - (existingPath = movieFile.existingPath, newPath = movieFile.newPath); + return [4 /*yield*/, rename(function (parseRequestResult) { return parseRequestResult.data.movie.movieFile.movieId; }, function (id, parseRequestResult) { return "".concat(arrHost, "/api/v3/rename?movieId=").concat(id); }, function (previewRenameRequestResult) { + var _a, _b; + return (((_b = (_a = previewRenameRequestResult.data) === null || _a === void 0 ? void 0 : _a.lenght) !== null && _b !== void 0 ? _b : 0) > 0) ? + previewRenameRequestResult.data[0] + : undefined; + }, function (id, fileToRename) { return { name: 'RenameFiles', movieId: id, - files: [movieFile.movieFileId] + files: [fileToRename.movieFileId] }; })]; case 1: - _d.sent(); + (_b = _f.sent(), existingPath = _b.existingPath, newPath = _b.newPath); return [3 /*break*/, 5]; case 2: if (!(arr === 'sonarr')) return [3 /*break*/, 4]; @@ -171,31 +183,31 @@ var plugin = function (args) { return __awaiter(void 0, void 0, void 0, function return [4 /*yield*/, rename(function (parseRequestResult) { return parseRequestResult.data.series.id; }, 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); - }, function (id, previewRenameRequestResult) { - var episodeFile = previewRenameRequestResult.data.find(function (episFile) { return episFile.episodeNumbers[0] === episodeNumber_1; }); - (existingPath = episodeFile.existingPath, newPath = episodeFile.newPath); + }, function (previewRenameRequestResult) { + var _a, _b; + return (((_b = (_a = previewRenameRequestResult.data) === null || _a === void 0 ? void 0 : _a.lenght) !== null && _b !== void 0 ? _b : 0) > 0) ? + previewRenameRequestResult.data.find(function (episFile) { return episFile.episodeNumbers[0] === episodeNumber_1; }) + : undefined; + }, function (id, fileToRename) { return { name: 'RenameFiles', seriesId: id, - files: [episodeFile.episodeFileId] + files: [fileToRename.episodeFileId] }; })]; case 3: - _d.sent(); + (_c = _f.sent(), existingPath = _c.existingPath, newPath = _c.newPath); return [3 /*break*/, 5]; case 4: args.jobLog('No arr specified in plugin inputs.'); - _d.label = 5; - case 5: - newFileId = args.inputFileObj.replace(existingPath, newPath); - args.jobLog("New file id ".concat(newFileId)); - return [2 /*return*/, { - outputFileObj: { - _id: newFileId - }, - outputNumber: 1, - variables: args.variables, - }]; + _f.label = 5; + case 5: return [2 /*return*/, { + outputFileObj: { + _id: args.inputFileObj.replace(existingPath, newPath) + }, + outputNumber: 1, + variables: args.variables, + }]; } }); }); }; diff --git a/FlowPluginsTs/CommunityFlowPlugins/tools/forceFileRenameRadarrOrSonarr/1.0.0/index.ts b/FlowPluginsTs/CommunityFlowPlugins/tools/forceFileRenameRadarrOrSonarr/1.0.0/index.ts index 4b6eb58..60aeb00 100644 --- a/FlowPluginsTs/CommunityFlowPlugins/tools/forceFileRenameRadarrOrSonarr/1.0.0/index.ts +++ b/FlowPluginsTs/CommunityFlowPlugins/tools/forceFileRenameRadarrOrSonarr/1.0.0/index.ts @@ -83,12 +83,14 @@ const plugin = async (args: IpluginInputArgs): Promise => { const rename = async ( getId: (parseRequestResult: any) => any, getPreviewRenameResquestUrl: (id: any, parseRequestResult: any) => any, - getRenameResquestConfigData: (id: any, previewRenameRequestResult: any) => any) - : Promise => { + getFileToRename: (previewRenameRequestResult: any) => any, + getRenameResquestConfigData: (id: any, fileToRename: any) => any) + : Promise<{ existingPath: any, newPath: any }> => { args.jobLog('Going to force rename'); - args.jobLog(`Renaming ${arr === 'radarr' ? 'Radarr' : 'Sonarr'}...`); + let existingPath, newPath = ''; + // Using parse endpoint to get the movie/serie's id. const parseRequestConfig = { method: 'get', @@ -105,61 +107,72 @@ const plugin = async (args: IpluginInputArgs): Promise => { headers, }; const previewRenameRequestResult = await args.deps.axios(previewRenameRequestConfig); + const fileToRename = getFileToRename(previewRenameRequestResult); - // Using command endpoint to schedule the renames. - const renameRequestConfig = { - method: 'post', - url: `${arrHost}/api/v3/command`, - headers, - data: JSON.stringify(getRenameResquestConfigData(id, previewRenameRequestResult)) - }; - await args.deps.axios(renameRequestConfig); + // Only if there is a rename to execute + if (fileToRename !== undefined) { + ({ existingPath, newPath } = fileToRename); + + // Using command endpoint to schedule the renames. + const renameRequestConfig = { + method: 'post', + url: `${arrHost}/api/v3/command`, + headers, + data: JSON.stringify(getRenameResquestConfigData(id, fileToRename)) + }; + await args.deps.axios(renameRequestConfig); + + args.jobLog(`✔ Renamed ${arr === 'radarr' ? 'movie' : 'serie'} ${id} in ${arr === 'radarr' ? 'Radarr' : 'Sonarr'} : '${existingPath}' => '${newPath}'.`); + } else + args.jobLog(`✔ No rename necessary.`); - args.jobLog(`✔ Renamed ${arr === 'radarr' ? 'movie' : 'serie'} ${id} in ${arr === 'radarr' ? 'Radarr' : 'Sonarr'}.`); + return { existingPath, newPath }; }; let existingPath, newPath = ''; if (arr === 'radarr') { - await rename( + ({ existingPath, newPath } = await rename( (parseRequestResult) => parseRequestResult.data.movie.movieFile.movieId, (id, parseRequestResult) => `${arrHost}/api/v3/rename?movieId=${id}`, - (id, previewRenameRequestResult) => { - const movieFile = previewRenameRequestResult.data[0]; - ({ existingPath, newPath } = movieFile); + (previewRenameRequestResult) => + ((previewRenameRequestResult.data?.lenght ?? 0) > 0) ? + previewRenameRequestResult.data[0] + : undefined, + (id, fileToRename) => { return { name: 'RenameFiles', movieId: id, - files: [movieFile.movieFileId] + files: [fileToRename.movieFileId] }; } - ); + )); } else if (arr === 'sonarr') { let episodeNumber = 0; - await rename( + ({ existingPath, newPath } = await rename( (parseRequestResult) => parseRequestResult.data.series.id, (id, parseRequestResult) => { episodeNumber = parseRequestResult.data.parsedEpisodeInfo.episodeNumbers[0]; return `${arrHost}/api/v3/rename?seriesId=${id}&seasonNumber=${parseRequestResult.data.parsedEpisodeInfo.seasonNumber}`; }, - (id, previewRenameRequestResult) => { - const episodeFile = previewRenameRequestResult.data.find((episFile: { episodeNumbers: number[]; }) => episFile.episodeNumbers[0] === episodeNumber); - ({ existingPath, newPath } = episodeFile); + (previewRenameRequestResult) => + ((previewRenameRequestResult.data?.lenght ?? 0) > 0) ? + previewRenameRequestResult.data.find((episFile: { episodeNumbers: number[]; }) => episFile.episodeNumbers[0] === episodeNumber) + : undefined, + (id, fileToRename) => { return { name: 'RenameFiles', seriesId: id, - files: [episodeFile.episodeFileId] + files: [fileToRename.episodeFileId] }; } - ); + )); } else { args.jobLog('No arr specified in plugin inputs.'); } - const newFileId = args.inputFileObj.replace(existingPath, newPath); - args.jobLog(`New file id ${newFileId}`); return { outputFileObj: { - _id: newFileId + _id: args.inputFileObj.replace(existingPath, newPath) }, outputNumber: 1, variables: args.variables, From 9916ed8a0932c3e00772f9b14944c8ee00e0d508 Mon Sep 17 00:00:00 2001 From: "jeanchristophe.mqt@gmail.com" Date: Fri, 19 Jan 2024 17:34:53 +0100 Subject: [PATCH 06/34] Force rename with radarr/sonarr : improve readability --- .../1.0.0/index.js | 70 +++++++++++-------- .../1.0.0/index.ts | 39 ++++++----- 2 files changed, 61 insertions(+), 48 deletions(-) diff --git a/FlowPlugins/CommunityFlowPlugins/tools/forceFileRenameRadarrOrSonarr/1.0.0/index.js b/FlowPlugins/CommunityFlowPlugins/tools/forceFileRenameRadarrOrSonarr/1.0.0/index.js index 2d63db5..67007b6 100644 --- a/FlowPlugins/CommunityFlowPlugins/tools/forceFileRenameRadarrOrSonarr/1.0.0/index.js +++ b/FlowPlugins/CommunityFlowPlugins/tools/forceFileRenameRadarrOrSonarr/1.0.0/index.js @@ -114,7 +114,7 @@ var plugin = function (args) { return __awaiter(void 0, void 0, void 0, function 'X-Api-Key': arr_api_key, Accept: 'application/json', }; - rename = function (getId, getPreviewRenameResquestUrl, getFileToRename, getRenameResquestConfigData) { return __awaiter(void 0, void 0, void 0, function () { + rename = function (delegates) { return __awaiter(void 0, void 0, void 0, function () { var existingPath, newPath, parseRequestConfig, parseRequestResult, id, previewRenameRequestConfig, previewRenameRequestResult, fileToRename, renameRequestConfig; return __generator(this, function (_a) { switch (_a.label) { @@ -130,23 +130,23 @@ var plugin = function (args) { return __awaiter(void 0, void 0, void 0, function return [4 /*yield*/, args.deps.axios(parseRequestConfig)]; case 1: parseRequestResult = _a.sent(); - id = getId(parseRequestResult); + id = delegates.getId(parseRequestResult); previewRenameRequestConfig = { method: 'get', - url: getPreviewRenameResquestUrl(id, parseRequestResult), + url: delegates.getPreviewRenameResquestUrl(id, parseRequestResult), headers: headers, }; return [4 /*yield*/, args.deps.axios(previewRenameRequestConfig)]; case 2: previewRenameRequestResult = _a.sent(); - fileToRename = getFileToRename(previewRenameRequestResult); + fileToRename = delegates.getFileToRename(previewRenameRequestResult); if (!(fileToRename !== undefined)) return [3 /*break*/, 4]; (existingPath = fileToRename.existingPath, newPath = fileToRename.newPath); renameRequestConfig = { method: 'post', url: "".concat(arrHost, "/api/v3/command"), headers: headers, - data: JSON.stringify(getRenameResquestConfigData(id, fileToRename)) + data: JSON.stringify(delegates.getRenameResquestConfigData(id, fileToRename)) }; return [4 /*yield*/, args.deps.axios(renameRequestConfig)]; case 3: @@ -162,17 +162,22 @@ var plugin = function (args) { return __awaiter(void 0, void 0, void 0, function }); }; newPath = ''; if (!(arr === 'radarr')) return [3 /*break*/, 2]; - return [4 /*yield*/, rename(function (parseRequestResult) { return parseRequestResult.data.movie.movieFile.movieId; }, function (id, parseRequestResult) { return "".concat(arrHost, "/api/v3/rename?movieId=").concat(id); }, function (previewRenameRequestResult) { - var _a, _b; - return (((_b = (_a = previewRenameRequestResult.data) === null || _a === void 0 ? void 0 : _a.lenght) !== null && _b !== void 0 ? _b : 0) > 0) ? - previewRenameRequestResult.data[0] - : undefined; - }, function (id, fileToRename) { - return { - name: 'RenameFiles', - movieId: id, - files: [fileToRename.movieFileId] - }; + return [4 /*yield*/, rename({ + getId: function (parseRequestResult) { return parseRequestResult.data.movie.movieFile.movieId; }, + getPreviewRenameResquestUrl: function (id, parseRequestResult) { return "".concat(arrHost, "/api/v3/rename?movieId=").concat(id); }, + getFileToRename: function (previewRenameRequestResult) { + var _a, _b; + return (((_b = (_a = previewRenameRequestResult.data) === null || _a === void 0 ? void 0 : _a.lenght) !== null && _b !== void 0 ? _b : 0) > 0) ? + previewRenameRequestResult.data[0] + : undefined; + }, + getRenameResquestConfigData: function (id, fileToRename) { + return { + name: 'RenameFiles', + movieId: id, + files: [fileToRename.movieFileId] + }; + } })]; case 1: (_b = _f.sent(), existingPath = _b.existingPath, newPath = _b.newPath); @@ -180,20 +185,25 @@ var plugin = function (args) { return __awaiter(void 0, void 0, void 0, function case 2: if (!(arr === 'sonarr')) return [3 /*break*/, 4]; episodeNumber_1 = 0; - return [4 /*yield*/, rename(function (parseRequestResult) { return parseRequestResult.data.series.id; }, 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); - }, function (previewRenameRequestResult) { - var _a, _b; - return (((_b = (_a = previewRenameRequestResult.data) === null || _a === void 0 ? void 0 : _a.lenght) !== null && _b !== void 0 ? _b : 0) > 0) ? - previewRenameRequestResult.data.find(function (episFile) { return episFile.episodeNumbers[0] === episodeNumber_1; }) - : undefined; - }, function (id, fileToRename) { - return { - name: 'RenameFiles', - seriesId: id, - files: [fileToRename.episodeFileId] - }; + return [4 /*yield*/, rename({ + getId: function (parseRequestResult) { return parseRequestResult.data.series.id; }, + 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); + }, + getFileToRename: function (previewRenameRequestResult) { + var _a, _b; + return (((_b = (_a = previewRenameRequestResult.data) === null || _a === void 0 ? void 0 : _a.lenght) !== null && _b !== void 0 ? _b : 0) > 0) ? + previewRenameRequestResult.data.find(function (episFile) { return episFile.episodeNumbers[0] === episodeNumber_1; }) + : undefined; + }, + getRenameResquestConfigData: function (id, fileToRename) { + return { + name: 'RenameFiles', + seriesId: id, + files: [fileToRename.episodeFileId] + }; + } })]; case 3: (_c = _f.sent(), existingPath = _c.existingPath, newPath = _c.newPath); diff --git a/FlowPluginsTs/CommunityFlowPlugins/tools/forceFileRenameRadarrOrSonarr/1.0.0/index.ts b/FlowPluginsTs/CommunityFlowPlugins/tools/forceFileRenameRadarrOrSonarr/1.0.0/index.ts index 60aeb00..1ba0996 100644 --- a/FlowPluginsTs/CommunityFlowPlugins/tools/forceFileRenameRadarrOrSonarr/1.0.0/index.ts +++ b/FlowPluginsTs/CommunityFlowPlugins/tools/forceFileRenameRadarrOrSonarr/1.0.0/index.ts @@ -80,11 +80,14 @@ const plugin = async (args: IpluginInputArgs): Promise => { Accept: 'application/json', }; - const rename = async ( + interface IRenameDelegates { getId: (parseRequestResult: any) => any, getPreviewRenameResquestUrl: (id: any, parseRequestResult: any) => any, getFileToRename: (previewRenameRequestResult: any) => any, - getRenameResquestConfigData: (id: any, fileToRename: any) => any) + getRenameResquestConfigData: (id: any, fileToRename: any) => any + } + + const rename = async (delegates: IRenameDelegates) : Promise<{ existingPath: any, newPath: any }> => { args.jobLog('Going to force rename'); args.jobLog(`Renaming ${arr === 'radarr' ? 'Radarr' : 'Sonarr'}...`); @@ -98,16 +101,16 @@ const plugin = async (args: IpluginInputArgs): Promise => { headers, }; const parseRequestResult = await args.deps.axios(parseRequestConfig); - const id = getId(parseRequestResult); + const id = delegates.getId(parseRequestResult); // Using rename endpoint to get ids of all the files that need renaming. const previewRenameRequestConfig = { method: 'get', - url: getPreviewRenameResquestUrl(id, parseRequestResult), + url: delegates.getPreviewRenameResquestUrl(id, parseRequestResult), headers, }; const previewRenameRequestResult = await args.deps.axios(previewRenameRequestConfig); - const fileToRename = getFileToRename(previewRenameRequestResult); + const fileToRename = delegates.getFileToRename(previewRenameRequestResult); // Only if there is a rename to execute if (fileToRename !== undefined) { @@ -118,7 +121,7 @@ const plugin = async (args: IpluginInputArgs): Promise => { method: 'post', url: `${arrHost}/api/v3/command`, headers, - data: JSON.stringify(getRenameResquestConfigData(id, fileToRename)) + data: JSON.stringify(delegates.getRenameResquestConfigData(id, fileToRename)) }; await args.deps.axios(renameRequestConfig); @@ -131,41 +134,41 @@ const plugin = async (args: IpluginInputArgs): Promise => { let existingPath, newPath = ''; if (arr === 'radarr') { - ({ existingPath, newPath } = await rename( - (parseRequestResult) => parseRequestResult.data.movie.movieFile.movieId, - (id, parseRequestResult) => `${arrHost}/api/v3/rename?movieId=${id}`, - (previewRenameRequestResult) => + ({ existingPath, newPath } = await rename({ + getId: (parseRequestResult) => parseRequestResult.data.movie.movieFile.movieId, + getPreviewRenameResquestUrl: (id, parseRequestResult) => `${arrHost}/api/v3/rename?movieId=${id}`, + getFileToRename: (previewRenameRequestResult) => ((previewRenameRequestResult.data?.lenght ?? 0) > 0) ? previewRenameRequestResult.data[0] : undefined, - (id, fileToRename) => { + getRenameResquestConfigData: (id, fileToRename) => { return { name: 'RenameFiles', movieId: id, files: [fileToRename.movieFileId] }; } - )); + })); } else if (arr === 'sonarr') { let episodeNumber = 0; - ({ existingPath, newPath } = await rename( - (parseRequestResult) => parseRequestResult.data.series.id, - (id, parseRequestResult) => { + ({ existingPath, newPath } = await rename({ + getId: (parseRequestResult) => parseRequestResult.data.series.id, + getPreviewRenameResquestUrl: (id, parseRequestResult) => { episodeNumber = parseRequestResult.data.parsedEpisodeInfo.episodeNumbers[0]; return `${arrHost}/api/v3/rename?seriesId=${id}&seasonNumber=${parseRequestResult.data.parsedEpisodeInfo.seasonNumber}`; }, - (previewRenameRequestResult) => + getFileToRename: (previewRenameRequestResult) => ((previewRenameRequestResult.data?.lenght ?? 0) > 0) ? previewRenameRequestResult.data.find((episFile: { episodeNumbers: number[]; }) => episFile.episodeNumbers[0] === episodeNumber) : undefined, - (id, fileToRename) => { + getRenameResquestConfigData: (id, fileToRename) => { return { name: 'RenameFiles', seriesId: id, files: [fileToRename.episodeFileId] }; } - )); + })); } else { args.jobLog('No arr specified in plugin inputs.'); } From 87578facad461519d05df0385e428e9bae9e19a7 Mon Sep 17 00:00:00 2001 From: "jeanchristophe.mqt@gmail.com" Date: Fri, 19 Jan 2024 17:53:32 +0100 Subject: [PATCH 07/34] Force rename with radarr/sonarr : corrected mistake --- .../tools/forceFileRenameRadarrOrSonarr/1.0.0/index.js | 8 ++++---- .../tools/forceFileRenameRadarrOrSonarr/1.0.0/index.ts | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/FlowPlugins/CommunityFlowPlugins/tools/forceFileRenameRadarrOrSonarr/1.0.0/index.js b/FlowPlugins/CommunityFlowPlugins/tools/forceFileRenameRadarrOrSonarr/1.0.0/index.js index 67007b6..71cb64b 100644 --- a/FlowPlugins/CommunityFlowPlugins/tools/forceFileRenameRadarrOrSonarr/1.0.0/index.js +++ b/FlowPlugins/CommunityFlowPlugins/tools/forceFileRenameRadarrOrSonarr/1.0.0/index.js @@ -167,7 +167,7 @@ var plugin = function (args) { return __awaiter(void 0, void 0, void 0, function getPreviewRenameResquestUrl: function (id, parseRequestResult) { return "".concat(arrHost, "/api/v3/rename?movieId=").concat(id); }, getFileToRename: function (previewRenameRequestResult) { var _a, _b; - return (((_b = (_a = previewRenameRequestResult.data) === null || _a === void 0 ? void 0 : _a.lenght) !== null && _b !== void 0 ? _b : 0) > 0) ? + return (((_b = (_a = previewRenameRequestResult.data) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0) > 0) ? previewRenameRequestResult.data[0] : undefined; }, @@ -193,8 +193,8 @@ var plugin = function (args) { return __awaiter(void 0, void 0, void 0, function }, getFileToRename: function (previewRenameRequestResult) { var _a, _b; - return (((_b = (_a = previewRenameRequestResult.data) === null || _a === void 0 ? void 0 : _a.lenght) !== null && _b !== void 0 ? _b : 0) > 0) ? - previewRenameRequestResult.data.find(function (episFile) { return episFile.episodeNumbers[0] === episodeNumber_1; }) + return (((_b = (_a = previewRenameRequestResult.data) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0) > 0) ? + previewRenameRequestResult.data.find(function (episFile) { var _a, _b; return (((_b = (_a = episFile.episodeNumbers) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0) > 0) ? episFile.episodeNumbers[0] === episodeNumber_1 : false; }) : undefined; }, getRenameResquestConfigData: function (id, fileToRename) { @@ -213,7 +213,7 @@ var plugin = function (args) { return __awaiter(void 0, void 0, void 0, function _f.label = 5; case 5: return [2 /*return*/, { outputFileObj: { - _id: args.inputFileObj.replace(existingPath, newPath) + _id: args.inputFileObj._id.replace(existingPath, newPath) }, outputNumber: 1, variables: args.variables, diff --git a/FlowPluginsTs/CommunityFlowPlugins/tools/forceFileRenameRadarrOrSonarr/1.0.0/index.ts b/FlowPluginsTs/CommunityFlowPlugins/tools/forceFileRenameRadarrOrSonarr/1.0.0/index.ts index 1ba0996..7f07519 100644 --- a/FlowPluginsTs/CommunityFlowPlugins/tools/forceFileRenameRadarrOrSonarr/1.0.0/index.ts +++ b/FlowPluginsTs/CommunityFlowPlugins/tools/forceFileRenameRadarrOrSonarr/1.0.0/index.ts @@ -138,7 +138,7 @@ const plugin = async (args: IpluginInputArgs): Promise => { getId: (parseRequestResult) => parseRequestResult.data.movie.movieFile.movieId, getPreviewRenameResquestUrl: (id, parseRequestResult) => `${arrHost}/api/v3/rename?movieId=${id}`, getFileToRename: (previewRenameRequestResult) => - ((previewRenameRequestResult.data?.lenght ?? 0) > 0) ? + ((previewRenameRequestResult.data?.length ?? 0) > 0) ? previewRenameRequestResult.data[0] : undefined, getRenameResquestConfigData: (id, fileToRename) => { @@ -158,8 +158,8 @@ const plugin = async (args: IpluginInputArgs): Promise => { return `${arrHost}/api/v3/rename?seriesId=${id}&seasonNumber=${parseRequestResult.data.parsedEpisodeInfo.seasonNumber}`; }, getFileToRename: (previewRenameRequestResult) => - ((previewRenameRequestResult.data?.lenght ?? 0) > 0) ? - previewRenameRequestResult.data.find((episFile: { episodeNumbers: number[]; }) => episFile.episodeNumbers[0] === episodeNumber) + ((previewRenameRequestResult.data?.length ?? 0) > 0) ? + previewRenameRequestResult.data.find((episFile: { episodeNumbers: number[]; }) => ((episFile.episodeNumbers?.length ?? 0) > 0) ? episFile.episodeNumbers[0] === episodeNumber : false) : undefined, getRenameResquestConfigData: (id, fileToRename) => { return { @@ -175,7 +175,7 @@ const plugin = async (args: IpluginInputArgs): Promise => { return { outputFileObj: { - _id: args.inputFileObj.replace(existingPath, newPath) + _id: args.inputFileObj._id.replace(existingPath, newPath) }, outputNumber: 1, variables: args.variables, From c3ad020b4b22feac512d694ad681335df376429f Mon Sep 17 00:00:00 2001 From: "jeanchristophe.mqt@gmail.com" Date: Fri, 19 Jan 2024 18:52:20 +0100 Subject: [PATCH 08/34] Force rename with radarr/sonarr : added file move after rename --- .../1.0.0/index.js | 58 ++++++++++++------- .../1.0.0/index.ts | 52 +++++++++++------ 2 files changed, 73 insertions(+), 37 deletions(-) diff --git a/FlowPlugins/CommunityFlowPlugins/tools/forceFileRenameRadarrOrSonarr/1.0.0/index.js b/FlowPlugins/CommunityFlowPlugins/tools/forceFileRenameRadarrOrSonarr/1.0.0/index.js index 71cb64b..734b971 100644 --- a/FlowPlugins/CommunityFlowPlugins/tools/forceFileRenameRadarrOrSonarr/1.0.0/index.js +++ b/FlowPlugins/CommunityFlowPlugins/tools/forceFileRenameRadarrOrSonarr/1.0.0/index.js @@ -35,8 +35,13 @@ var __generator = (this && this.__generator) || function (thisArg, body) { if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; Object.defineProperty(exports, "__esModule", { value: true }); exports.plugin = exports.details = void 0; +var fileMoveOrCopy_1 = __importDefault(require("../../../../FlowHelpers/1.0.0/fileMoveOrCopy")); +var fileUtils_1 = require("../../../../FlowHelpers/1.0.0/fileUtils"); var details = function () { return ({ name: 'Force File Rename Radarr or Sonarr', description: 'Force Radarr or Sonarr to rename a file according to the naming policy', @@ -96,32 +101,31 @@ var details = function () { return ({ }); }; exports.details = details; var plugin = function (args) { return __awaiter(void 0, void 0, void 0, function () { - var lib, _a, arr, arr_api_key, arr_host, fileName, arrHost, headers, rename, existingPath, newPath, episodeNumber_1; + var lib, _a, arr, arr_api_key, arr_host, arrHost, fileName, rename, existingPath, newPath, episodeNumber_1, outputFileObj, destinationPath; var _b, _c; - var _d, _e; - return __generator(this, function (_f) { - switch (_f.label) { + return __generator(this, function (_d) { + switch (_d.label) { case 0: lib = require('../../../../../methods/lib')(); // eslint-disable-next-line @typescript-eslint/no-unused-vars,no-param-reassign args.inputs = lib.loadDefaultValues(args.inputs, details); _a = args.inputs, arr = _a.arr, arr_api_key = _a.arr_api_key; arr_host = String(args.inputs.arr_host).trim(); - fileName = ((_e = (_d = args.originalLibraryFile) === null || _d === void 0 ? void 0 : _d.meta) === null || _e === void 0 ? void 0 : _e.FileName) || ''; arrHost = arr_host.endsWith('/') ? arr_host.slice(0, -1) : arr_host; - headers = { - 'Content-Type': 'application/json', - 'X-Api-Key': arr_api_key, - Accept: 'application/json', - }; + fileName = (0, fileUtils_1.getFileName)(args.inputFileObj._id); rename = function (delegates) { return __awaiter(void 0, void 0, void 0, function () { - var existingPath, newPath, parseRequestConfig, parseRequestResult, id, previewRenameRequestConfig, previewRenameRequestResult, fileToRename, renameRequestConfig; + var existingPath, newPath, headers, parseRequestConfig, parseRequestResult, id, previewRenameRequestConfig, previewRenameRequestResult, fileToRename, renameRequestConfig; return __generator(this, function (_a) { switch (_a.label) { case 0: args.jobLog('Going to force rename'); args.jobLog("Renaming ".concat(arr === 'radarr' ? 'Radarr' : 'Sonarr', "...")); - newPath = ''; + existingPath = '', newPath = ''; + headers = { + 'Content-Type': 'application/json', + 'X-Api-Key': arr_api_key, + Accept: 'application/json', + }; parseRequestConfig = { method: 'get', url: "".concat(arrHost, "/api/v3/parse?title=").concat(encodeURIComponent(fileName)), @@ -154,7 +158,7 @@ var plugin = function (args) { return __awaiter(void 0, void 0, void 0, function args.jobLog("\u2714 Renamed ".concat(arr === 'radarr' ? 'movie' : 'serie', " ").concat(id, " in ").concat(arr === 'radarr' ? 'Radarr' : 'Sonarr', " : '").concat(existingPath, "' => '").concat(newPath, "'.")); return [3 /*break*/, 5]; case 4: - args.jobLog("\u2714 No rename necessary."); + args.jobLog('✔ No rename necessary.'); _a.label = 5; case 5: return [2 /*return*/, { existingPath: existingPath, newPath: newPath }]; } @@ -180,7 +184,7 @@ var plugin = function (args) { return __awaiter(void 0, void 0, void 0, function } })]; case 1: - (_b = _f.sent(), existingPath = _b.existingPath, newPath = _b.newPath); + (_b = _d.sent(), existingPath = _b.existingPath, newPath = _b.newPath); return [3 /*break*/, 5]; case 2: if (!(arr === 'sonarr')) return [3 /*break*/, 4]; @@ -193,6 +197,7 @@ var plugin = function (args) { return __awaiter(void 0, void 0, void 0, function }, getFileToRename: function (previewRenameRequestResult) { var _a, _b; + args.jobLog(JSON.stringify(previewRenameRequestResult)); return (((_b = (_a = previewRenameRequestResult.data) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0) > 0) ? previewRenameRequestResult.data.find(function (episFile) { var _a, _b; return (((_b = (_a = episFile.episodeNumbers) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0) > 0) ? episFile.episodeNumbers[0] === episodeNumber_1 : false; }) : undefined; @@ -206,15 +211,28 @@ var plugin = function (args) { return __awaiter(void 0, void 0, void 0, function } })]; case 3: - (_c = _f.sent(), existingPath = _c.existingPath, newPath = _c.newPath); + (_c = _d.sent(), existingPath = _c.existingPath, newPath = _c.newPath); return [3 /*break*/, 5]; case 4: args.jobLog('No arr specified in plugin inputs.'); - _f.label = 5; - case 5: return [2 /*return*/, { - outputFileObj: { - _id: args.inputFileObj._id.replace(existingPath, newPath) - }, + _d.label = 5; + case 5: + outputFileObj = args.inputFileObj; + if (!(existingPath !== newPath)) return [3 /*break*/, 7]; + destinationPath = "".concat((0, fileUtils_1.getFileAbosluteDir)(args.inputFileObj._id), "/").concat((0, fileUtils_1.getFileName)(newPath)); + return [4 /*yield*/, (0, fileMoveOrCopy_1.default)({ + operation: 'move', + sourcePath: args.inputFileObj._id, + destinationPath: destinationPath, + args: args, + })]; + case 6: + _d.sent(); + args.jobLog("\u2714 File moved : '".concat(args.inputFileObj._id, "' => '").concat(destinationPath, "'.")); + outputFileObj = { _id: destinationPath }; + _d.label = 7; + case 7: return [2 /*return*/, { + outputFileObj: outputFileObj, outputNumber: 1, variables: args.variables, }]; diff --git a/FlowPluginsTs/CommunityFlowPlugins/tools/forceFileRenameRadarrOrSonarr/1.0.0/index.ts b/FlowPluginsTs/CommunityFlowPlugins/tools/forceFileRenameRadarrOrSonarr/1.0.0/index.ts index 7f07519..9607989 100644 --- a/FlowPluginsTs/CommunityFlowPlugins/tools/forceFileRenameRadarrOrSonarr/1.0.0/index.ts +++ b/FlowPluginsTs/CommunityFlowPlugins/tools/forceFileRenameRadarrOrSonarr/1.0.0/index.ts @@ -1,3 +1,7 @@ +import fileMoveOrCopy from '../../../../FlowHelpers/1.0.0/fileMoveOrCopy'; +import { + getFileAbosluteDir, getFileName, +} from '../../../../FlowHelpers/1.0.0/fileUtils'; import { IpluginDetails, IpluginInputArgs, @@ -69,16 +73,8 @@ const plugin = async (args: IpluginInputArgs): Promise => { const { arr, arr_api_key } = args.inputs; const arr_host = String(args.inputs.arr_host).trim(); - - const fileName = args.originalLibraryFile?.meta?.FileName || ''; - const arrHost = arr_host.endsWith('/') ? arr_host.slice(0, -1) : arr_host; - - const headers = { - 'Content-Type': 'application/json', - 'X-Api-Key': arr_api_key, - Accept: 'application/json', - }; + const fileName = getFileName(args.inputFileObj._id); interface IRenameDelegates { getId: (parseRequestResult: any) => any, @@ -92,7 +88,13 @@ const plugin = async (args: IpluginInputArgs): Promise => { args.jobLog('Going to force rename'); args.jobLog(`Renaming ${arr === 'radarr' ? 'Radarr' : 'Sonarr'}...`); - let existingPath, newPath = ''; + let existingPath = '', newPath = ''; + + const headers = { + 'Content-Type': 'application/json', + 'X-Api-Key': arr_api_key, + Accept: 'application/json', + }; // Using parse endpoint to get the movie/serie's id. const parseRequestConfig = { @@ -127,7 +129,7 @@ const plugin = async (args: IpluginInputArgs): Promise => { args.jobLog(`✔ Renamed ${arr === 'radarr' ? 'movie' : 'serie'} ${id} in ${arr === 'radarr' ? 'Radarr' : 'Sonarr'} : '${existingPath}' => '${newPath}'.`); } else - args.jobLog(`✔ No rename necessary.`); + args.jobLog('✔ No rename necessary.'); return { existingPath, newPath }; }; @@ -157,10 +159,12 @@ const plugin = async (args: IpluginInputArgs): Promise => { episodeNumber = parseRequestResult.data.parsedEpisodeInfo.episodeNumbers[0]; return `${arrHost}/api/v3/rename?seriesId=${id}&seasonNumber=${parseRequestResult.data.parsedEpisodeInfo.seasonNumber}`; }, - getFileToRename: (previewRenameRequestResult) => - ((previewRenameRequestResult.data?.length ?? 0) > 0) ? + getFileToRename: (previewRenameRequestResult) => { + args.jobLog(JSON.stringify(previewRenameRequestResult)); + return ((previewRenameRequestResult.data?.length ?? 0) > 0) ? previewRenameRequestResult.data.find((episFile: { episodeNumbers: number[]; }) => ((episFile.episodeNumbers?.length ?? 0) > 0) ? episFile.episodeNumbers[0] === episodeNumber : false) - : undefined, + : undefined + }, getRenameResquestConfigData: (id, fileToRename) => { return { name: 'RenameFiles', @@ -173,10 +177,24 @@ const plugin = async (args: IpluginInputArgs): Promise => { args.jobLog('No arr specified in plugin inputs.'); } + // If file has been renamed, move the transcoded file accordingly + let outputFileObj: {_id : string} = args.inputFileObj; + if (existingPath !== newPath) { + const destinationPath = `${getFileAbosluteDir(args.inputFileObj._id)}/${getFileName(newPath)}`; + + await fileMoveOrCopy({ + operation: 'move', + sourcePath: args.inputFileObj._id, + destinationPath: destinationPath, + args, + }); + args.jobLog(`✔ File moved : '${args.inputFileObj._id}' => '${destinationPath}'.`); + + outputFileObj = { _id: destinationPath }; + } + return { - outputFileObj: { - _id: args.inputFileObj._id.replace(existingPath, newPath) - }, + outputFileObj: outputFileObj, outputNumber: 1, variables: args.variables, }; From d6ac4ba9fa650bad284bfdeda9915abd926c889b Mon Sep 17 00:00:00 2001 From: "jeanchristophe.mqt@gmail.com" Date: Fri, 19 Jan 2024 18:57:18 +0100 Subject: [PATCH 09/34] Force rename radarr.sonarr : remove log --- .../tools/forceFileRenameRadarrOrSonarr/1.0.0/index.js | 1 - .../tools/forceFileRenameRadarrOrSonarr/1.0.0/index.ts | 1 - 2 files changed, 2 deletions(-) diff --git a/FlowPlugins/CommunityFlowPlugins/tools/forceFileRenameRadarrOrSonarr/1.0.0/index.js b/FlowPlugins/CommunityFlowPlugins/tools/forceFileRenameRadarrOrSonarr/1.0.0/index.js index 734b971..dd82d6e 100644 --- a/FlowPlugins/CommunityFlowPlugins/tools/forceFileRenameRadarrOrSonarr/1.0.0/index.js +++ b/FlowPlugins/CommunityFlowPlugins/tools/forceFileRenameRadarrOrSonarr/1.0.0/index.js @@ -197,7 +197,6 @@ var plugin = function (args) { return __awaiter(void 0, void 0, void 0, function }, getFileToRename: function (previewRenameRequestResult) { var _a, _b; - args.jobLog(JSON.stringify(previewRenameRequestResult)); return (((_b = (_a = previewRenameRequestResult.data) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0) > 0) ? previewRenameRequestResult.data.find(function (episFile) { var _a, _b; return (((_b = (_a = episFile.episodeNumbers) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0) > 0) ? episFile.episodeNumbers[0] === episodeNumber_1 : false; }) : undefined; diff --git a/FlowPluginsTs/CommunityFlowPlugins/tools/forceFileRenameRadarrOrSonarr/1.0.0/index.ts b/FlowPluginsTs/CommunityFlowPlugins/tools/forceFileRenameRadarrOrSonarr/1.0.0/index.ts index 9607989..1b21250 100644 --- a/FlowPluginsTs/CommunityFlowPlugins/tools/forceFileRenameRadarrOrSonarr/1.0.0/index.ts +++ b/FlowPluginsTs/CommunityFlowPlugins/tools/forceFileRenameRadarrOrSonarr/1.0.0/index.ts @@ -160,7 +160,6 @@ const plugin = async (args: IpluginInputArgs): Promise => { return `${arrHost}/api/v3/rename?seriesId=${id}&seasonNumber=${parseRequestResult.data.parsedEpisodeInfo.seasonNumber}`; }, getFileToRename: (previewRenameRequestResult) => { - args.jobLog(JSON.stringify(previewRenameRequestResult)); return ((previewRenameRequestResult.data?.length ?? 0) > 0) ? previewRenameRequestResult.data.find((episFile: { episodeNumbers: number[]; }) => ((episFile.episodeNumbers?.length ?? 0) > 0) ? episFile.episodeNumbers[0] === episodeNumber : false) : undefined From 713923cc1ed18fcb2bca833ded0e8d3e00d48f1b Mon Sep 17 00:00:00 2001 From: "jeanchristophe.mqt@gmail.com" Date: Fri, 19 Jan 2024 19:25:31 +0100 Subject: [PATCH 10/34] Force rename Radarr/Sonarr logic simplified to just apply the naming policy --- .../1.0.0/index.js | 75 +++++------------ .../1.0.0/index.ts | 82 ++++++------------- 2 files changed, 46 insertions(+), 111 deletions(-) rename FlowPlugins/CommunityFlowPlugins/tools/{forceFileRenameRadarrOrSonarr => applyRadarrOrSonarrNamingPolicy}/1.0.0/index.js (75%) rename FlowPluginsTs/CommunityFlowPlugins/tools/{forceFileRenameRadarrOrSonarr => applyRadarrOrSonarrNamingPolicy}/1.0.0/index.ts (69%) diff --git a/FlowPlugins/CommunityFlowPlugins/tools/forceFileRenameRadarrOrSonarr/1.0.0/index.js b/FlowPlugins/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.js similarity index 75% rename from FlowPlugins/CommunityFlowPlugins/tools/forceFileRenameRadarrOrSonarr/1.0.0/index.js rename to FlowPlugins/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.js index dd82d6e..9d4cab8 100644 --- a/FlowPlugins/CommunityFlowPlugins/tools/forceFileRenameRadarrOrSonarr/1.0.0/index.js +++ b/FlowPlugins/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.js @@ -43,8 +43,8 @@ exports.plugin = exports.details = void 0; var fileMoveOrCopy_1 = __importDefault(require("../../../../FlowHelpers/1.0.0/fileMoveOrCopy")); var fileUtils_1 = require("../../../../FlowHelpers/1.0.0/fileUtils"); var details = function () { return ({ - name: 'Force File Rename Radarr or Sonarr', - description: 'Force Radarr or Sonarr to rename a file according to the naming policy', + name: 'Apply Radarr or Sonarr naming policy', + description: 'Apply Radarr or Sonarr naming policy to a file. This plugin should be called after the original file has been replaced and Radarr or Sonarr has been notified. Radarr or Sonarr should also be notified after this plugin.', style: { borderColor: 'green', }, @@ -101,10 +101,9 @@ var details = function () { return ({ }); }; exports.details = details; var plugin = function (args) { return __awaiter(void 0, void 0, void 0, function () { - var lib, _a, arr, arr_api_key, arr_host, arrHost, fileName, rename, existingPath, newPath, episodeNumber_1, outputFileObj, destinationPath; - var _b, _c; - return __generator(this, function (_d) { - switch (_d.label) { + var lib, _a, arr, arr_api_key, arr_host, arrHost, fileName, rename, destinationPath, episodeNumber_1; + return __generator(this, function (_b) { + switch (_b.label) { case 0: lib = require('../../../../../methods/lib')(); // eslint-disable-next-line @typescript-eslint/no-unused-vars,no-param-reassign @@ -114,13 +113,13 @@ var plugin = function (args) { return __awaiter(void 0, void 0, void 0, function arrHost = arr_host.endsWith('/') ? arr_host.slice(0, -1) : arr_host; fileName = (0, fileUtils_1.getFileName)(args.inputFileObj._id); rename = function (delegates) { return __awaiter(void 0, void 0, void 0, function () { - var existingPath, newPath, headers, parseRequestConfig, parseRequestResult, id, previewRenameRequestConfig, previewRenameRequestResult, fileToRename, renameRequestConfig; + var destinationPath, headers, parseRequestConfig, parseRequestResult, id, previewRenameRequestConfig, previewRenameRequestResult, fileToRename; return __generator(this, function (_a) { switch (_a.label) { case 0: + destinationPath = ''; args.jobLog('Going to force rename'); args.jobLog("Renaming ".concat(arr === 'radarr' ? 'Radarr' : 'Sonarr', "...")); - existingPath = '', newPath = ''; headers = { 'Content-Type': 'application/json', 'X-Api-Key': arr_api_key, @@ -145,26 +144,25 @@ var plugin = function (args) { return __awaiter(void 0, void 0, void 0, function previewRenameRequestResult = _a.sent(); fileToRename = delegates.getFileToRename(previewRenameRequestResult); if (!(fileToRename !== undefined)) return [3 /*break*/, 4]; - (existingPath = fileToRename.existingPath, newPath = fileToRename.newPath); - renameRequestConfig = { - method: 'post', - url: "".concat(arrHost, "/api/v3/command"), - headers: headers, - data: JSON.stringify(delegates.getRenameResquestConfigData(id, fileToRename)) - }; - return [4 /*yield*/, args.deps.axios(renameRequestConfig)]; + destinationPath = "".concat((0, fileUtils_1.getFileAbosluteDir)(args.inputFileObj._id), "/").concat((0, fileUtils_1.getFileName)(fileToRename.newPath), ".").concat((0, fileUtils_1.getContainer)(fileToRename.newPath)); + return [4 /*yield*/, (0, fileMoveOrCopy_1.default)({ + operation: 'move', + sourcePath: args.inputFileObj._id, + destinationPath: destinationPath, + args: args, + })]; case 3: _a.sent(); - args.jobLog("\u2714 Renamed ".concat(arr === 'radarr' ? 'movie' : 'serie', " ").concat(id, " in ").concat(arr === 'radarr' ? 'Radarr' : 'Sonarr', " : '").concat(existingPath, "' => '").concat(newPath, "'.")); + args.jobLog("\u2714 Renamed ".concat(arr === 'radarr' ? 'movie' : 'serie', " ").concat(id, " : '").concat(args.inputFileObj._id, "' => '").concat(destinationPath, "'.")); return [3 /*break*/, 5]; case 4: args.jobLog('✔ No rename necessary.'); _a.label = 5; - case 5: return [2 /*return*/, { existingPath: existingPath, newPath: newPath }]; + case 5: return [2 /*return*/, destinationPath]; } }); }); }; - newPath = ''; + destinationPath = ''; if (!(arr === 'radarr')) return [3 /*break*/, 2]; return [4 /*yield*/, rename({ getId: function (parseRequestResult) { return parseRequestResult.data.movie.movieFile.movieId; }, @@ -174,17 +172,10 @@ var plugin = function (args) { return __awaiter(void 0, void 0, void 0, function return (((_b = (_a = previewRenameRequestResult.data) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0) > 0) ? previewRenameRequestResult.data[0] : undefined; - }, - getRenameResquestConfigData: function (id, fileToRename) { - return { - name: 'RenameFiles', - movieId: id, - files: [fileToRename.movieFileId] - }; } })]; case 1: - (_b = _d.sent(), existingPath = _b.existingPath, newPath = _b.newPath); + destinationPath = _b.sent(); return [3 /*break*/, 5]; case 2: if (!(arr === 'sonarr')) return [3 /*break*/, 4]; @@ -200,38 +191,16 @@ var plugin = function (args) { return __awaiter(void 0, void 0, void 0, function return (((_b = (_a = previewRenameRequestResult.data) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0) > 0) ? previewRenameRequestResult.data.find(function (episFile) { var _a, _b; return (((_b = (_a = episFile.episodeNumbers) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0) > 0) ? episFile.episodeNumbers[0] === episodeNumber_1 : false; }) : undefined; - }, - getRenameResquestConfigData: function (id, fileToRename) { - return { - name: 'RenameFiles', - seriesId: id, - files: [fileToRename.episodeFileId] - }; } })]; case 3: - (_c = _d.sent(), existingPath = _c.existingPath, newPath = _c.newPath); + destinationPath = _b.sent(); return [3 /*break*/, 5]; case 4: args.jobLog('No arr specified in plugin inputs.'); - _d.label = 5; - case 5: - outputFileObj = args.inputFileObj; - if (!(existingPath !== newPath)) return [3 /*break*/, 7]; - destinationPath = "".concat((0, fileUtils_1.getFileAbosluteDir)(args.inputFileObj._id), "/").concat((0, fileUtils_1.getFileName)(newPath)); - return [4 /*yield*/, (0, fileMoveOrCopy_1.default)({ - operation: 'move', - sourcePath: args.inputFileObj._id, - destinationPath: destinationPath, - args: args, - })]; - case 6: - _d.sent(); - args.jobLog("\u2714 File moved : '".concat(args.inputFileObj._id, "' => '").concat(destinationPath, "'.")); - outputFileObj = { _id: destinationPath }; - _d.label = 7; - case 7: return [2 /*return*/, { - outputFileObj: outputFileObj, + _b.label = 5; + case 5: return [2 /*return*/, { + outputFileObj: destinationPath !== '' ? { _id: destinationPath } : args.inputFileObj, outputNumber: 1, variables: args.variables, }]; diff --git a/FlowPluginsTs/CommunityFlowPlugins/tools/forceFileRenameRadarrOrSonarr/1.0.0/index.ts b/FlowPluginsTs/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.ts similarity index 69% rename from FlowPluginsTs/CommunityFlowPlugins/tools/forceFileRenameRadarrOrSonarr/1.0.0/index.ts rename to FlowPluginsTs/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.ts index 1b21250..e778c13 100644 --- a/FlowPluginsTs/CommunityFlowPlugins/tools/forceFileRenameRadarrOrSonarr/1.0.0/index.ts +++ b/FlowPluginsTs/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.ts @@ -1,6 +1,6 @@ import fileMoveOrCopy from '../../../../FlowHelpers/1.0.0/fileMoveOrCopy'; import { - getFileAbosluteDir, getFileName, + getContainer, getFileAbosluteDir, getFileName, } from '../../../../FlowHelpers/1.0.0/fileUtils'; import { IpluginDetails, @@ -9,8 +9,8 @@ import { } from '../../../../FlowHelpers/1.0.0/interfaces/interfaces'; const details = (): IpluginDetails => ({ - name: 'Force File Rename Radarr or Sonarr', - description: 'Force Radarr or Sonarr to rename a file according to the naming policy', + name: 'Apply Radarr or Sonarr naming policy', + description: 'Apply Radarr or Sonarr naming policy to a file. This plugin should be called after the original file has been replaced and Radarr or Sonarr has been notified. Radarr or Sonarr should also be notified after this plugin.', style: { borderColor: 'green', }, @@ -79,17 +79,16 @@ const plugin = async (args: IpluginInputArgs): Promise => { interface IRenameDelegates { getId: (parseRequestResult: any) => any, getPreviewRenameResquestUrl: (id: any, parseRequestResult: any) => any, - getFileToRename: (previewRenameRequestResult: any) => any, - getRenameResquestConfigData: (id: any, fileToRename: any) => any + getFileToRename: (previewRenameRequestResult: any) => any } const rename = async (delegates: IRenameDelegates) - : Promise<{ existingPath: any, newPath: any }> => { + : Promise => { + let destinationPath = ''; + args.jobLog('Going to force rename'); args.jobLog(`Renaming ${arr === 'radarr' ? 'Radarr' : 'Sonarr'}...`); - let existingPath = '', newPath = ''; - const headers = { 'Content-Type': 'application/json', 'X-Api-Key': arr_api_key, @@ -116,44 +115,34 @@ const plugin = async (args: IpluginInputArgs): Promise => { // Only if there is a rename to execute if (fileToRename !== undefined) { - ({ existingPath, newPath } = fileToRename); - - // Using command endpoint to schedule the renames. - const renameRequestConfig = { - method: 'post', - url: `${arrHost}/api/v3/command`, - headers, - data: JSON.stringify(delegates.getRenameResquestConfigData(id, fileToRename)) - }; - await args.deps.axios(renameRequestConfig); - - args.jobLog(`✔ Renamed ${arr === 'radarr' ? 'movie' : 'serie'} ${id} in ${arr === 'radarr' ? 'Radarr' : 'Sonarr'} : '${existingPath}' => '${newPath}'.`); + 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}'.`); } else args.jobLog('✔ No rename necessary.'); - return { existingPath, newPath }; + return destinationPath; }; - let existingPath, newPath = ''; + let destinationPath = ''; if (arr === 'radarr') { - ({ existingPath, newPath } = await rename({ + destinationPath = await rename({ getId: (parseRequestResult) => parseRequestResult.data.movie.movieFile.movieId, getPreviewRenameResquestUrl: (id, parseRequestResult) => `${arrHost}/api/v3/rename?movieId=${id}`, getFileToRename: (previewRenameRequestResult) => ((previewRenameRequestResult.data?.length ?? 0) > 0) ? previewRenameRequestResult.data[0] - : undefined, - getRenameResquestConfigData: (id, fileToRename) => { - return { - name: 'RenameFiles', - movieId: id, - files: [fileToRename.movieFileId] - }; - } - })); + : undefined + }); } else if (arr === 'sonarr') { let episodeNumber = 0; - ({ existingPath, newPath } = await rename({ + destinationPath = await rename({ getId: (parseRequestResult) => parseRequestResult.data.series.id, getPreviewRenameResquestUrl: (id, parseRequestResult) => { episodeNumber = parseRequestResult.data.parsedEpisodeInfo.episodeNumbers[0]; @@ -163,37 +152,14 @@ const plugin = async (args: IpluginInputArgs): Promise => { return ((previewRenameRequestResult.data?.length ?? 0) > 0) ? previewRenameRequestResult.data.find((episFile: { episodeNumbers: number[]; }) => ((episFile.episodeNumbers?.length ?? 0) > 0) ? episFile.episodeNumbers[0] === episodeNumber : false) : undefined - }, - getRenameResquestConfigData: (id, fileToRename) => { - return { - name: 'RenameFiles', - seriesId: id, - files: [fileToRename.episodeFileId] - }; } - })); + }); } else { args.jobLog('No arr specified in plugin inputs.'); } - // If file has been renamed, move the transcoded file accordingly - let outputFileObj: {_id : string} = args.inputFileObj; - if (existingPath !== newPath) { - const destinationPath = `${getFileAbosluteDir(args.inputFileObj._id)}/${getFileName(newPath)}`; - - await fileMoveOrCopy({ - operation: 'move', - sourcePath: args.inputFileObj._id, - destinationPath: destinationPath, - args, - }); - args.jobLog(`✔ File moved : '${args.inputFileObj._id}' => '${destinationPath}'.`); - - outputFileObj = { _id: destinationPath }; - } - return { - outputFileObj: outputFileObj, + outputFileObj: destinationPath !== '' ? { _id: destinationPath } : args.inputFileObj, outputNumber: 1, variables: args.variables, }; From b62e38bfde13f83708149b541787a370a90d4b96 Mon Sep 17 00:00:00 2001 From: "jeanchristophe.mqt@gmail.com" Date: Fri, 19 Jan 2024 20:27:33 +0100 Subject: [PATCH 11/34] Added some logs for compare file size --- .../CommunityFlowPlugins/file/compareFileSize/1.0.0/index.js | 3 +++ .../CommunityFlowPlugins/file/compareFileSize/1.0.0/index.ts | 3 +++ 2 files changed, 6 insertions(+) diff --git a/FlowPlugins/CommunityFlowPlugins/file/compareFileSize/1.0.0/index.js b/FlowPlugins/CommunityFlowPlugins/file/compareFileSize/1.0.0/index.js index e403445..f9ee076 100644 --- a/FlowPlugins/CommunityFlowPlugins/file/compareFileSize/1.0.0/index.js +++ b/FlowPlugins/CommunityFlowPlugins/file/compareFileSize/1.0.0/index.js @@ -38,12 +38,15 @@ var plugin = function (args) { args.inputs = lib.loadDefaultValues(args.inputs, details); var outputNumber = 1; if (args.inputFileObj.file_size < args.originalLibraryFile.file_size) { + args.jobLog('Working file is smaller than original file.'); outputNumber = 1; } else if (args.inputFileObj.file_size === args.originalLibraryFile.file_size) { + args.jobLog('Working file is same size as original file.'); outputNumber = 2; } else if (args.inputFileObj.file_size > args.originalLibraryFile.file_size) { + args.jobLog('Working file is larger than original file.'); outputNumber = 3; } return { diff --git a/FlowPluginsTs/CommunityFlowPlugins/file/compareFileSize/1.0.0/index.ts b/FlowPluginsTs/CommunityFlowPlugins/file/compareFileSize/1.0.0/index.ts index c1a041e..25e2326 100644 --- a/FlowPluginsTs/CommunityFlowPlugins/file/compareFileSize/1.0.0/index.ts +++ b/FlowPluginsTs/CommunityFlowPlugins/file/compareFileSize/1.0.0/index.ts @@ -45,10 +45,13 @@ const plugin = (args: IpluginInputArgs): IpluginOutputArgs => { let outputNumber = 1; if (args.inputFileObj.file_size < args.originalLibraryFile.file_size) { + args.jobLog('Working file is smaller than original file.'); outputNumber = 1; } else if (args.inputFileObj.file_size === args.originalLibraryFile.file_size) { + args.jobLog('Working file is same size as original file.'); outputNumber = 2; } else if (args.inputFileObj.file_size > args.originalLibraryFile.file_size) { + args.jobLog('Working file is larger than original file.'); outputNumber = 3; } From bfb13e907af53d4b636d238a842427f83a176597 Mon Sep 17 00:00:00 2001 From: "jeanchristophe.mqt@gmail.com" Date: Fri, 19 Jan 2024 20:30:15 +0100 Subject: [PATCH 12/34] Enhanced logs for campare file size --- .../file/compareFileSize/1.0.0/index.js | 6 +++--- .../file/compareFileSize/1.0.0/index.ts | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/FlowPlugins/CommunityFlowPlugins/file/compareFileSize/1.0.0/index.js b/FlowPlugins/CommunityFlowPlugins/file/compareFileSize/1.0.0/index.js index f9ee076..05f7dc3 100644 --- a/FlowPlugins/CommunityFlowPlugins/file/compareFileSize/1.0.0/index.js +++ b/FlowPlugins/CommunityFlowPlugins/file/compareFileSize/1.0.0/index.js @@ -38,15 +38,15 @@ var plugin = function (args) { args.inputs = lib.loadDefaultValues(args.inputs, details); var outputNumber = 1; if (args.inputFileObj.file_size < args.originalLibraryFile.file_size) { - args.jobLog('Working file is smaller than original file.'); + args.jobLog("Working file (".concat(args.inputFileObj.file_size, ") is smaller than original file (").concat(args.originalLibraryFile.file_size, ").")); outputNumber = 1; } else if (args.inputFileObj.file_size === args.originalLibraryFile.file_size) { - args.jobLog('Working file is same size as original file.'); + args.jobLog("Working file (".concat(args.inputFileObj.file_size, ") is same size as original file (").concat(args.originalLibraryFile.file_size, ").")); outputNumber = 2; } else if (args.inputFileObj.file_size > args.originalLibraryFile.file_size) { - args.jobLog('Working file is larger than original file.'); + args.jobLog("Working file (".concat(args.inputFileObj.file_size, ") is larger than original file (").concat(args.originalLibraryFile.file_size, ").")); outputNumber = 3; } return { diff --git a/FlowPluginsTs/CommunityFlowPlugins/file/compareFileSize/1.0.0/index.ts b/FlowPluginsTs/CommunityFlowPlugins/file/compareFileSize/1.0.0/index.ts index 25e2326..30e87a4 100644 --- a/FlowPluginsTs/CommunityFlowPlugins/file/compareFileSize/1.0.0/index.ts +++ b/FlowPluginsTs/CommunityFlowPlugins/file/compareFileSize/1.0.0/index.ts @@ -45,13 +45,13 @@ const plugin = (args: IpluginInputArgs): IpluginOutputArgs => { let outputNumber = 1; if (args.inputFileObj.file_size < args.originalLibraryFile.file_size) { - args.jobLog('Working file is smaller than original file.'); + args.jobLog(`Working file (${args.inputFileObj.file_size}) is smaller than original file (${args.originalLibraryFile.file_size}).`); outputNumber = 1; } else if (args.inputFileObj.file_size === args.originalLibraryFile.file_size) { - args.jobLog('Working file is same size as original file.'); + args.jobLog(`Working file (${args.inputFileObj.file_size}) is same size as original file (${args.originalLibraryFile.file_size}).`); outputNumber = 2; } else if (args.inputFileObj.file_size > args.originalLibraryFile.file_size) { - args.jobLog('Working file is larger than original file.'); + args.jobLog(`Working file (${args.inputFileObj.file_size}) is larger than original file (${args.originalLibraryFile.file_size}).`); outputNumber = 3; } 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 13/34] 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.'); From ad6694862231aa7975a8804040c4f0c96d8b7a1d Mon Sep 17 00:00:00 2001 From: "jeanchristophe.mqt@gmail.com" Date: Thu, 25 Jan 2024 23:48:01 +0100 Subject: [PATCH 14/34] Revert "Added some logs for compare file size" This reverts commit b62e38bfde13f83708149b541787a370a90d4b96. --- .../CommunityFlowPlugins/file/compareFileSize/1.0.0/index.js | 3 --- .../CommunityFlowPlugins/file/compareFileSize/1.0.0/index.ts | 3 --- 2 files changed, 6 deletions(-) diff --git a/FlowPlugins/CommunityFlowPlugins/file/compareFileSize/1.0.0/index.js b/FlowPlugins/CommunityFlowPlugins/file/compareFileSize/1.0.0/index.js index 05f7dc3..e403445 100644 --- a/FlowPlugins/CommunityFlowPlugins/file/compareFileSize/1.0.0/index.js +++ b/FlowPlugins/CommunityFlowPlugins/file/compareFileSize/1.0.0/index.js @@ -38,15 +38,12 @@ var plugin = function (args) { args.inputs = lib.loadDefaultValues(args.inputs, details); var outputNumber = 1; if (args.inputFileObj.file_size < args.originalLibraryFile.file_size) { - args.jobLog("Working file (".concat(args.inputFileObj.file_size, ") is smaller than original file (").concat(args.originalLibraryFile.file_size, ").")); outputNumber = 1; } else if (args.inputFileObj.file_size === args.originalLibraryFile.file_size) { - args.jobLog("Working file (".concat(args.inputFileObj.file_size, ") is same size as original file (").concat(args.originalLibraryFile.file_size, ").")); outputNumber = 2; } else if (args.inputFileObj.file_size > args.originalLibraryFile.file_size) { - args.jobLog("Working file (".concat(args.inputFileObj.file_size, ") is larger than original file (").concat(args.originalLibraryFile.file_size, ").")); outputNumber = 3; } return { diff --git a/FlowPluginsTs/CommunityFlowPlugins/file/compareFileSize/1.0.0/index.ts b/FlowPluginsTs/CommunityFlowPlugins/file/compareFileSize/1.0.0/index.ts index 30e87a4..c1a041e 100644 --- a/FlowPluginsTs/CommunityFlowPlugins/file/compareFileSize/1.0.0/index.ts +++ b/FlowPluginsTs/CommunityFlowPlugins/file/compareFileSize/1.0.0/index.ts @@ -45,13 +45,10 @@ const plugin = (args: IpluginInputArgs): IpluginOutputArgs => { let outputNumber = 1; if (args.inputFileObj.file_size < args.originalLibraryFile.file_size) { - args.jobLog(`Working file (${args.inputFileObj.file_size}) is smaller than original file (${args.originalLibraryFile.file_size}).`); outputNumber = 1; } else if (args.inputFileObj.file_size === args.originalLibraryFile.file_size) { - args.jobLog(`Working file (${args.inputFileObj.file_size}) is same size as original file (${args.originalLibraryFile.file_size}).`); outputNumber = 2; } else if (args.inputFileObj.file_size > args.originalLibraryFile.file_size) { - args.jobLog(`Working file (${args.inputFileObj.file_size}) is larger than original file (${args.originalLibraryFile.file_size}).`); outputNumber = 3; } From 09ba9b5dc6bd80808d1d48b56176fbb95d3773f6 Mon Sep 17 00:00:00 2001 From: "jeanchristophe.mqt@gmail.com" Date: Fri, 26 Jan 2024 00:01:32 +0100 Subject: [PATCH 15/34] Variables and functions names refactoring --- .../1.0.0/index.js | 39 ++++++++++++------- .../1.0.0/index.ts | 22 +++++------ 2 files changed, 36 insertions(+), 25 deletions(-) diff --git a/FlowPlugins/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.js b/FlowPlugins/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.js index 78c335e..ad897eb 100644 --- a/FlowPlugins/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.js +++ b/FlowPlugins/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.js @@ -1,4 +1,15 @@ "use strict"; +var __assign = (this && this.__assign) || function () { + __assign = Object.assign || function(t) { + for (var s, i = 1, n = arguments.length; i < n; i++) { + s = arguments[i]; + for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) + t[p] = s[p]; + } + return t; + }; + return __assign.apply(this, arguments); +}; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { @@ -101,7 +112,7 @@ var details = function () { return ({ }); }; exports.details = details; var plugin = function (args) { return __awaiter(void 0, void 0, void 0, function () { - var lib, _a, arr, arr_api_key, arr_host, arrHost, fileName, rename, destinationPath, episodeNumber_1; + var lib, _a, arr, arr_api_key, arr_host, arrHost, fileName, getNewPath, pathWithNewName, episodeNumber_1; return __generator(this, function (_b) { switch (_b.label) { case 0: @@ -112,12 +123,12 @@ var plugin = function (args) { return __awaiter(void 0, void 0, void 0, function arr_host = String(args.inputs.arr_host).trim(); arrHost = arr_host.endsWith('/') ? arr_host.slice(0, -1) : arr_host; fileName = (0, fileUtils_1.getFileName)(args.inputFileObj._id); - rename = function (delegates) { return __awaiter(void 0, void 0, void 0, function () { - var destinationPath, headers, parseRequestConfig, parseRequestResult, id, previewRenameRequestConfig, previewRenameRequestResult, fileToRename; + getNewPath = function (delegates) { return __awaiter(void 0, void 0, void 0, function () { + var pathWithNewName, headers, parseRequestConfig, parseRequestResult, id, previewRenameRequestConfig, previewRenameRequestResult, fileToRename; return __generator(this, function (_a) { switch (_a.label) { case 0: - destinationPath = ''; + pathWithNewName = ''; args.jobLog('Going to force rename'); args.jobLog("Renaming ".concat(arr === 'radarr' ? 'Radarr' : 'Sonarr', "...")); headers = { @@ -145,16 +156,16 @@ var plugin = function (args) { return __awaiter(void 0, void 0, void 0, function previewRenameRequestResult = _a.sent(); fileToRename = delegates.getFileToRename(previewRenameRequestResult); if (!(fileToRename !== undefined)) return [3 /*break*/, 4]; - destinationPath = "".concat((0, fileUtils_1.getFileAbosluteDir)(args.inputFileObj._id), "/").concat((0, fileUtils_1.getFileName)(fileToRename.newPath), ".").concat((0, fileUtils_1.getContainer)(fileToRename.newPath)); + pathWithNewName = "".concat((0, fileUtils_1.getFileAbosluteDir)(args.inputFileObj._id), "/").concat((0, fileUtils_1.getFileName)(fileToRename.newPath), ".").concat((0, fileUtils_1.getContainer)(fileToRename.newPath)); return [4 /*yield*/, (0, fileMoveOrCopy_1.default)({ operation: 'move', sourcePath: args.inputFileObj._id, - destinationPath: destinationPath, + destinationPath: pathWithNewName, args: args, })]; case 3: _a.sent(); - args.jobLog("\u2714 Renamed ".concat(arr === 'radarr' ? 'movie' : 'serie', " ").concat(id, " : '").concat(args.inputFileObj._id, "' => '").concat(destinationPath, "'.")); + args.jobLog("\u2714 Renamed ".concat(arr === 'radarr' ? 'movie' : 'serie', " ").concat(id, " : '").concat(args.inputFileObj._id, "' => '").concat(pathWithNewName, "'.")); return [3 /*break*/, 5]; case 4: args.jobLog('✔ No rename necessary.'); @@ -163,13 +174,13 @@ var plugin = function (args) { return __awaiter(void 0, void 0, void 0, function 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]; + case 7: return [2 /*return*/, pathWithNewName]; } }); }); }; - destinationPath = ''; + pathWithNewName = ''; if (!(arr === 'radarr')) return [3 /*break*/, 2]; - return [4 /*yield*/, rename({ + return [4 /*yield*/, getNewPath({ 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) { @@ -180,12 +191,12 @@ var plugin = function (args) { return __awaiter(void 0, void 0, void 0, function } })]; case 1: - destinationPath = _b.sent(); + pathWithNewName = _b.sent(); return [3 /*break*/, 5]; case 2: if (!(arr === 'sonarr')) return [3 /*break*/, 4]; episodeNumber_1 = 0; - return [4 /*yield*/, rename({ + return [4 /*yield*/, getNewPath({ 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]; @@ -199,13 +210,13 @@ var plugin = function (args) { return __awaiter(void 0, void 0, void 0, function } })]; case 3: - destinationPath = _b.sent(); + pathWithNewName = _b.sent(); return [3 /*break*/, 5]; case 4: args.jobLog('No arr specified in plugin inputs.'); _b.label = 5; case 5: return [2 /*return*/, { - outputFileObj: destinationPath !== '' ? { _id: destinationPath } : args.inputFileObj, + outputFileObj: pathWithNewName !== '' ? __assign(__assign({}, args.inputFileObj), { _id: pathWithNewName }) : args.inputFileObj, outputNumber: 1, variables: args.variables, }]; diff --git a/FlowPluginsTs/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.ts b/FlowPluginsTs/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.ts index 46523d7..c5cfde0 100644 --- a/FlowPluginsTs/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.ts +++ b/FlowPluginsTs/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.ts @@ -76,15 +76,15 @@ const plugin = async (args: IpluginInputArgs): Promise => { const arrHost = arr_host.endsWith('/') ? arr_host.slice(0, -1) : arr_host; const fileName = getFileName(args.inputFileObj._id); - interface IRenameDelegates { + interface IGetNewPathDelegates { getId: (parseRequestResult: any) => string, getPreviewRenameResquestUrl: (id: string, parseRequestResult: any) => string, getFileToRename: (previewRenameRequestResult: any) => any } - const rename = async (delegates: IRenameDelegates) + const getNewPath = async (delegates: IGetNewPathDelegates) : Promise => { - let destinationPath = ''; + let pathWithNewName = ''; args.jobLog('Going to force rename'); args.jobLog(`Renaming ${arr === 'radarr' ? 'Radarr' : 'Sonarr'}...`); @@ -118,26 +118,26 @@ const plugin = async (args: IpluginInputArgs): Promise => { // Only if there is a rename to execute if (fileToRename !== undefined) { - destinationPath = `${getFileAbosluteDir(args.inputFileObj._id)}/${getFileName(fileToRename.newPath)}.${getContainer(fileToRename.newPath)}`; + pathWithNewName = `${getFileAbosluteDir(args.inputFileObj._id)}/${getFileName(fileToRename.newPath)}.${getContainer(fileToRename.newPath)}`; await fileMoveOrCopy({ operation: 'move', sourcePath: args.inputFileObj._id, - destinationPath: destinationPath, + destinationPath: pathWithNewName, args, }); - args.jobLog(`✔ Renamed ${arr === 'radarr' ? 'movie' : 'serie'} ${id} : '${args.inputFileObj._id}' => '${destinationPath}'.`); + args.jobLog(`✔ Renamed ${arr === 'radarr' ? 'movie' : 'serie'} ${id} : '${args.inputFileObj._id}' => '${pathWithNewName}'.`); } else args.jobLog('✔ No rename necessary.'); } else args.jobLog(`✔ No ${arr === 'radarr' ? 'movie' : 'serie'} with a file named '${fileName}'.`); - return destinationPath; + return pathWithNewName; }; - let destinationPath = ''; + let pathWithNewName = ''; if (arr === 'radarr') { - destinationPath = await rename({ + pathWithNewName = await getNewPath({ getId: (parseRequestResult) => String(parseRequestResult.data?.movie?.movieFile?.movieId ?? -1), getPreviewRenameResquestUrl: (id, parseRequestResult) => `${arrHost}/api/v3/rename?movieId=${id}`, getFileToRename: (previewRenameRequestResult) => @@ -147,7 +147,7 @@ const plugin = async (args: IpluginInputArgs): Promise => { }); } else if (arr === 'sonarr') { let episodeNumber = 0; - destinationPath = await rename({ + pathWithNewName = await getNewPath({ getId: (parseRequestResult) => String(parseRequestResult.data?.series?.id ?? -1), getPreviewRenameResquestUrl: (id, parseRequestResult) => { episodeNumber = parseRequestResult.data.parsedEpisodeInfo.episodeNumbers[0]; @@ -163,7 +163,7 @@ const plugin = async (args: IpluginInputArgs): Promise => { } return { - outputFileObj: destinationPath !== '' ? { _id: destinationPath } : args.inputFileObj, + outputFileObj: pathWithNewName !== '' ? { ...args.inputFileObj, _id: pathWithNewName } : args.inputFileObj, outputNumber: 1, variables: args.variables, }; From 5c2d2814a6e09d2752dc5663743ad9780480f052 Mon Sep 17 00:00:00 2001 From: "jeanchristophe.mqt@gmail.com" Date: Fri, 26 Jan 2024 11:00:15 +0100 Subject: [PATCH 16/34] Little more refactoring --- .../1.0.0/index.js | 4 ++-- .../1.0.0/index.ts | 16 ++++++++-------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/FlowPlugins/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.js b/FlowPlugins/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.js index ad897eb..7ec5846 100644 --- a/FlowPlugins/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.js +++ b/FlowPlugins/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.js @@ -172,7 +172,7 @@ var plugin = function (args) { return __awaiter(void 0, void 0, void 0, function _a.label = 5; case 5: return [3 /*break*/, 7]; case 6: - args.jobLog("\u2714 No ".concat(arr === 'radarr' ? 'movie' : 'serie', " with a file named '").concat(fileName, "'.")); + args.jobLog("No ".concat(arr === 'radarr' ? 'movie' : 'serie', " with a file named '").concat(fileName, "'.")); _a.label = 7; case 7: return [2 /*return*/, pathWithNewName]; } @@ -205,7 +205,7 @@ var plugin = function (args) { return __awaiter(void 0, void 0, void 0, function getFileToRename: function (previewRenameRequestResult) { var _a, _b; return (((_b = (_a = previewRenameRequestResult.data) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0) > 0) ? - previewRenameRequestResult.data.find(function (episFile) { var _a, _b; return (((_b = (_a = episFile.episodeNumbers) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0) > 0) ? episFile.episodeNumbers[0] === episodeNumber_1 : false; }) + previewRenameRequestResult.data.find(function (episodeFile) { var _a, _b; return (((_b = (_a = episodeFile.episodeNumbers) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0) > 0) ? episodeFile.episodeNumbers[0] === episodeNumber_1 : false; }) : undefined; } })]; diff --git a/FlowPluginsTs/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.ts b/FlowPluginsTs/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.ts index c5cfde0..8ced451 100644 --- a/FlowPluginsTs/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.ts +++ b/FlowPluginsTs/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.ts @@ -66,6 +66,12 @@ const details = (): IpluginDetails => ({ ], }); +interface IGetNewPathDelegates { + getId: (parseRequestResult: any) => string, + getPreviewRenameResquestUrl: (id: string, parseRequestResult: any) => string, + getFileToRename: (previewRenameRequestResult: any) => any +} + const plugin = async (args: IpluginInputArgs): Promise => { const lib = require('../../../../../methods/lib')(); // eslint-disable-next-line @typescript-eslint/no-unused-vars,no-param-reassign @@ -76,12 +82,6 @@ const plugin = async (args: IpluginInputArgs): Promise => { const arrHost = arr_host.endsWith('/') ? arr_host.slice(0, -1) : arr_host; const fileName = getFileName(args.inputFileObj._id); - interface IGetNewPathDelegates { - getId: (parseRequestResult: any) => string, - getPreviewRenameResquestUrl: (id: string, parseRequestResult: any) => string, - getFileToRename: (previewRenameRequestResult: any) => any - } - const getNewPath = async (delegates: IGetNewPathDelegates) : Promise => { let pathWithNewName = ''; @@ -130,7 +130,7 @@ const plugin = async (args: IpluginInputArgs): Promise => { } else args.jobLog('✔ No rename necessary.'); } else - args.jobLog(`✔ No ${arr === 'radarr' ? 'movie' : 'serie'} with a file named '${fileName}'.`); + args.jobLog(`No ${arr === 'radarr' ? 'movie' : 'serie'} with a file named '${fileName}'.`); return pathWithNewName; }; @@ -155,7 +155,7 @@ const plugin = async (args: IpluginInputArgs): Promise => { }, getFileToRename: (previewRenameRequestResult) => ((previewRenameRequestResult.data?.length ?? 0) > 0) ? - previewRenameRequestResult.data.find((episFile: { episodeNumbers: number[]; }) => ((episFile.episodeNumbers?.length ?? 0) > 0) ? episFile.episodeNumbers[0] === episodeNumber : false) + previewRenameRequestResult.data.find((episodeFile: { episodeNumbers: number[]; }) => ((episodeFile.episodeNumbers?.length ?? 0) > 0) ? episodeFile.episodeNumbers[0] === episodeNumber : false) : undefined }); } else { From 3937c799dd178653a3fa839cf4adb256f759b14c Mon Sep 17 00:00:00 2001 From: "jeanchristophe.mqt@gmail.com" Date: Mon, 29 Jan 2024 11:41:37 +0100 Subject: [PATCH 17/34] Renamed some methods to clarify the expected behavior --- .../1.0.0/index.js | 18 +++++++------- .../1.0.0/index.ts | 24 +++++++++---------- 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/FlowPlugins/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.js b/FlowPlugins/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.js index 7ec5846..2c6c2ff 100644 --- a/FlowPlugins/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.js +++ b/FlowPlugins/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.js @@ -144,17 +144,17 @@ var plugin = function (args) { return __awaiter(void 0, void 0, void 0, function return [4 /*yield*/, args.deps.axios(parseRequestConfig)]; case 1: parseRequestResult = _a.sent(); - id = delegates.getId(parseRequestResult); + id = delegates.getIdFromParseRequestResult(parseRequestResult); if (!(id !== '-1')) return [3 /*break*/, 6]; previewRenameRequestConfig = { method: 'get', - url: delegates.getPreviewRenameResquestUrl(id, parseRequestResult), + url: delegates.buildPreviewRenameResquestUrl(id, parseRequestResult), headers: headers, }; return [4 /*yield*/, args.deps.axios(previewRenameRequestConfig)]; case 2: previewRenameRequestResult = _a.sent(); - fileToRename = delegates.getFileToRename(previewRenameRequestResult); + fileToRename = delegates.getFileToRenameFromPreviewRenameRequestResult(previewRenameRequestResult); if (!(fileToRename !== undefined)) return [3 /*break*/, 4]; pathWithNewName = "".concat((0, fileUtils_1.getFileAbosluteDir)(args.inputFileObj._id), "/").concat((0, fileUtils_1.getFileName)(fileToRename.newPath), ".").concat((0, fileUtils_1.getContainer)(fileToRename.newPath)); return [4 /*yield*/, (0, fileMoveOrCopy_1.default)({ @@ -181,9 +181,9 @@ var plugin = function (args) { return __awaiter(void 0, void 0, void 0, function pathWithNewName = ''; if (!(arr === 'radarr')) return [3 /*break*/, 2]; return [4 /*yield*/, getNewPath({ - 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) { + getIdFromParseRequestResult: 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); }, + buildPreviewRenameResquestUrl: function (id, parseRequestResult) { return "".concat(arrHost, "/api/v3/rename?movieId=").concat(id); }, + getFileToRenameFromPreviewRenameRequestResult: function (previewRenameRequestResult) { var _a, _b; return (((_b = (_a = previewRenameRequestResult.data) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0) > 0) ? previewRenameRequestResult.data[0] @@ -197,12 +197,12 @@ 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*/, getNewPath({ - 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) { + getIdFromParseRequestResult: 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); }, + buildPreviewRenameResquestUrl: 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); }, - getFileToRename: function (previewRenameRequestResult) { + getFileToRenameFromPreviewRenameRequestResult: function (previewRenameRequestResult) { var _a, _b; return (((_b = (_a = previewRenameRequestResult.data) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0) > 0) ? previewRenameRequestResult.data.find(function (episodeFile) { var _a, _b; return (((_b = (_a = episodeFile.episodeNumbers) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0) > 0) ? episodeFile.episodeNumbers[0] === episodeNumber_1 : false; }) diff --git a/FlowPluginsTs/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.ts b/FlowPluginsTs/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.ts index 8ced451..2d6216c 100644 --- a/FlowPluginsTs/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.ts +++ b/FlowPluginsTs/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.ts @@ -67,9 +67,9 @@ const details = (): IpluginDetails => ({ }); interface IGetNewPathDelegates { - getId: (parseRequestResult: any) => string, - getPreviewRenameResquestUrl: (id: string, parseRequestResult: any) => string, - getFileToRename: (previewRenameRequestResult: any) => any + getIdFromParseRequestResult: (parseRequestResult: any) => string, + buildPreviewRenameResquestUrl: (id: string, parseRequestResult: any) => string, + getFileToRenameFromPreviewRenameRequestResult: (previewRenameRequestResult: any) => any } const plugin = async (args: IpluginInputArgs): Promise => { @@ -102,7 +102,7 @@ const plugin = async (args: IpluginInputArgs): Promise => { headers, }; const parseRequestResult = await args.deps.axios(parseRequestConfig); - const id = delegates.getId(parseRequestResult); + const id = delegates.getIdFromParseRequestResult(parseRequestResult); // 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. @@ -110,11 +110,11 @@ const plugin = async (args: IpluginInputArgs): Promise => { // Using rename endpoint to get ids of all the files that need renaming. const previewRenameRequestConfig = { method: 'get', - url: delegates.getPreviewRenameResquestUrl(id, parseRequestResult), + url: delegates.buildPreviewRenameResquestUrl(id, parseRequestResult), headers, }; const previewRenameRequestResult = await args.deps.axios(previewRenameRequestConfig); - const fileToRename = delegates.getFileToRename(previewRenameRequestResult); + const fileToRename = delegates.getFileToRenameFromPreviewRenameRequestResult(previewRenameRequestResult); // Only if there is a rename to execute if (fileToRename !== undefined) { @@ -138,9 +138,9 @@ const plugin = async (args: IpluginInputArgs): Promise => { let pathWithNewName = ''; if (arr === 'radarr') { pathWithNewName = await getNewPath({ - getId: (parseRequestResult) => String(parseRequestResult.data?.movie?.movieFile?.movieId ?? -1), - getPreviewRenameResquestUrl: (id, parseRequestResult) => `${arrHost}/api/v3/rename?movieId=${id}`, - getFileToRename: (previewRenameRequestResult) => + getIdFromParseRequestResult: (parseRequestResult) => String(parseRequestResult.data?.movie?.movieFile?.movieId ?? -1), + buildPreviewRenameResquestUrl: (id, parseRequestResult) => `${arrHost}/api/v3/rename?movieId=${id}`, + getFileToRenameFromPreviewRenameRequestResult: (previewRenameRequestResult) => ((previewRenameRequestResult.data?.length ?? 0) > 0) ? previewRenameRequestResult.data[0] : undefined @@ -148,12 +148,12 @@ const plugin = async (args: IpluginInputArgs): Promise => { } else if (arr === 'sonarr') { let episodeNumber = 0; pathWithNewName = await getNewPath({ - getId: (parseRequestResult) => String(parseRequestResult.data?.series?.id ?? -1), - getPreviewRenameResquestUrl: (id, parseRequestResult) => { + getIdFromParseRequestResult: (parseRequestResult) => String(parseRequestResult.data?.series?.id ?? -1), + buildPreviewRenameResquestUrl: (id, parseRequestResult) => { episodeNumber = parseRequestResult.data.parsedEpisodeInfo.episodeNumbers[0]; return `${arrHost}/api/v3/rename?seriesId=${id}&seasonNumber=${parseRequestResult.data.parsedEpisodeInfo.seasonNumber}`; }, - getFileToRename: (previewRenameRequestResult) => + getFileToRenameFromPreviewRenameRequestResult: (previewRenameRequestResult) => ((previewRenameRequestResult.data?.length ?? 0) > 0) ? previewRenameRequestResult.data.find((episodeFile: { episodeNumbers: number[]; }) => ((episodeFile.episodeNumbers?.length ?? 0) > 0) ? episodeFile.episodeNumbers[0] === episodeNumber : false) : undefined From 4b7e10f30461587260e525f64c2aa894a16e35a6 Mon Sep 17 00:00:00 2001 From: "jeanchristophe.mqt@gmail.com" Date: Sat, 3 Feb 2024 13:11:17 +0100 Subject: [PATCH 18/34] Added new output to the plugin for the case when file is not found --- .../1.0.0/index.js | 143 ++++++++++-------- .../1.0.0/index.ts | 118 +++++++++------ 2 files changed, 150 insertions(+), 111 deletions(-) diff --git a/FlowPlugins/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.js b/FlowPlugins/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.js index 2c6c2ff..7df7d73 100644 --- a/FlowPlugins/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.js +++ b/FlowPlugins/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.js @@ -106,15 +106,20 @@ var details = function () { return ({ outputs: [ { number: 1, - tooltip: 'Continue to next plugin', + tooltip: 'Radarr or Sonnar notified', }, - ], + { + number: 2, + tooltip: 'Radarr or Sonnar do not know this file', + } + ] }); }; exports.details = details; var plugin = function (args) { return __awaiter(void 0, void 0, void 0, function () { - var lib, _a, arr, arr_api_key, arr_host, arrHost, fileName, getNewPath, pathWithNewName, episodeNumber_1; - return __generator(this, function (_b) { - switch (_b.label) { + var lib, _a, arr, arr_api_key, arr_host, arrHost, filePath, fileName, getNewPath, episodeNumber, getNewPathTypes, newPathOutput; + var _b, _c; + return __generator(this, function (_d) { + switch (_d.label) { case 0: lib = require('../../../../../methods/lib')(); // eslint-disable-next-line @typescript-eslint/no-unused-vars,no-param-reassign @@ -122,15 +127,19 @@ var plugin = function (args) { return __awaiter(void 0, void 0, void 0, function _a = args.inputs, arr = _a.arr, arr_api_key = _a.arr_api_key; arr_host = String(args.inputs.arr_host).trim(); arrHost = arr_host.endsWith('/') ? arr_host.slice(0, -1) : arr_host; - fileName = (0, fileUtils_1.getFileName)(args.inputFileObj._id); - getNewPath = function (delegates) { return __awaiter(void 0, void 0, void 0, function () { - var pathWithNewName, headers, parseRequestConfig, parseRequestResult, id, previewRenameRequestConfig, previewRenameRequestResult, fileToRename; - return __generator(this, function (_a) { - switch (_a.label) { + filePath = (_c = (_b = args.originalLibraryFile) === null || _b === void 0 ? void 0 : _b._id) !== null && _c !== void 0 ? _c : ''; + fileName = (0, fileUtils_1.getFileName)(filePath); + getNewPath = function (getNewPathType) { return __awaiter(void 0, void 0, void 0, function () { + var output, headers, parseRequestConfig, parseRequestResult, id, previewRenameRequestConfig, previewRenameRequestResult, fileToRename, _a; + return __generator(this, function (_b) { + switch (_b.label) { case 0: - pathWithNewName = ''; - args.jobLog('Going to force rename'); - args.jobLog("Renaming ".concat(arr === 'radarr' ? 'Radarr' : 'Sonarr', "...")); + output = { + newPath: '', + isSuccessful: false + }; + args.jobLog('Going to apply new name'); + args.jobLog("Renaming ".concat(getNewPathType.appName, "...")); headers = { 'Content-Type': 'application/json', 'X-Api-Key': arr_api_key, @@ -143,83 +152,85 @@ var plugin = function (args) { return __awaiter(void 0, void 0, void 0, function }; return [4 /*yield*/, args.deps.axios(parseRequestConfig)]; case 1: - parseRequestResult = _a.sent(); - id = delegates.getIdFromParseRequestResult(parseRequestResult); + parseRequestResult = _b.sent(); + id = getNewPathType.delegates.getIdFromParseRequestResult(parseRequestResult); if (!(id !== '-1')) return [3 /*break*/, 6]; previewRenameRequestConfig = { method: 'get', - url: delegates.buildPreviewRenameResquestUrl(id, parseRequestResult), + url: getNewPathType.delegates.buildPreviewRenameResquestUrl(id, parseRequestResult), headers: headers, }; return [4 /*yield*/, args.deps.axios(previewRenameRequestConfig)]; case 2: - previewRenameRequestResult = _a.sent(); - fileToRename = delegates.getFileToRenameFromPreviewRenameRequestResult(previewRenameRequestResult); + previewRenameRequestResult = _b.sent(); + fileToRename = getNewPathType.delegates.getFileToRenameFromPreviewRenameRequestResult(previewRenameRequestResult); if (!(fileToRename !== undefined)) return [3 /*break*/, 4]; - pathWithNewName = "".concat((0, fileUtils_1.getFileAbosluteDir)(args.inputFileObj._id), "/").concat((0, fileUtils_1.getFileName)(fileToRename.newPath), ".").concat((0, fileUtils_1.getContainer)(fileToRename.newPath)); + output.newPath = "".concat((0, fileUtils_1.getFileAbosluteDir)(args.inputFileObj._id), "/").concat((0, fileUtils_1.getFileName)(fileToRename.newPath), ".").concat((0, fileUtils_1.getContainer)(fileToRename.newPath)); + _a = output; return [4 /*yield*/, (0, fileMoveOrCopy_1.default)({ operation: 'move', sourcePath: args.inputFileObj._id, - destinationPath: pathWithNewName, + destinationPath: output.newPath, args: args, })]; case 3: - _a.sent(); - args.jobLog("\u2714 Renamed ".concat(arr === 'radarr' ? 'movie' : 'serie', " ").concat(id, " : '").concat(args.inputFileObj._id, "' => '").concat(pathWithNewName, "'.")); + _a.isSuccessful = _b.sent(); + args.jobLog("\u2714 Renamed ".concat(getNewPathType.contentName, " ").concat(id, " : '").concat(filePath, "' => '").concat(output.newPath, "'.")); return [3 /*break*/, 5]; case 4: + output.isSuccessful = true; args.jobLog('✔ No rename necessary.'); - _a.label = 5; + _b.label = 5; case 5: return [3 /*break*/, 7]; case 6: - args.jobLog("No ".concat(arr === 'radarr' ? 'movie' : 'serie', " with a file named '").concat(fileName, "'.")); - _a.label = 7; - case 7: return [2 /*return*/, pathWithNewName]; + args.jobLog("No ".concat(getNewPathType.appName, " with a file named '").concat(fileName, "'.")); + _b.label = 7; + case 7: return [2 /*return*/, output]; } }); }); }; - pathWithNewName = ''; - if (!(arr === 'radarr')) return [3 /*break*/, 2]; - return [4 /*yield*/, getNewPath({ - getIdFromParseRequestResult: 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); }, - buildPreviewRenameResquestUrl: function (id, parseRequestResult) { return "".concat(arrHost, "/api/v3/rename?movieId=").concat(id); }, - getFileToRenameFromPreviewRenameRequestResult: function (previewRenameRequestResult) { - var _a, _b; - return (((_b = (_a = previewRenameRequestResult.data) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0) > 0) ? - previewRenameRequestResult.data[0] - : undefined; + episodeNumber = 0; + getNewPathTypes = { + radarr: { + appName: 'Radarr', + contentName: 'movie', + delegates: { + getIdFromParseRequestResult: 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); }, + buildPreviewRenameResquestUrl: function (id, parseRequestResult) { return "".concat(arrHost, "/api/v3/rename?movieId=").concat(id); }, + getFileToRenameFromPreviewRenameRequestResult: function (previewRenameRequestResult) { + var _a, _b; + return (((_b = (_a = previewRenameRequestResult.data) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0) > 0) ? + previewRenameRequestResult.data[0] + : undefined; + } } - })]; - case 1: - pathWithNewName = _b.sent(); - return [3 /*break*/, 5]; - case 2: - if (!(arr === 'sonarr')) return [3 /*break*/, 4]; - episodeNumber_1 = 0; - return [4 /*yield*/, getNewPath({ - getIdFromParseRequestResult: 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); }, - buildPreviewRenameResquestUrl: 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); - }, - getFileToRenameFromPreviewRenameRequestResult: function (previewRenameRequestResult) { - var _a, _b; - return (((_b = (_a = previewRenameRequestResult.data) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0) > 0) ? - previewRenameRequestResult.data.find(function (episodeFile) { var _a, _b; return (((_b = (_a = episodeFile.episodeNumbers) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0) > 0) ? episodeFile.episodeNumbers[0] === episodeNumber_1 : false; }) - : undefined; + }, + sonarr: { + appName: 'Sonarr', + contentName: 'serie', + delegates: { + getIdFromParseRequestResult: 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); }, + buildPreviewRenameResquestUrl: function (id, parseRequestResult) { + episodeNumber = parseRequestResult.data.parsedEpisodeInfo.episodeNumbers[0]; + return "".concat(arrHost, "/api/v3/rename?seriesId=").concat(id, "&seasonNumber=").concat(parseRequestResult.data.parsedEpisodeInfo.seasonNumber); + }, + getFileToRenameFromPreviewRenameRequestResult: function (previewRenameRequestResult) { + var _a, _b; + return (((_b = (_a = previewRenameRequestResult.data) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0) > 0) ? + previewRenameRequestResult.data.find(function (episodeFile) { var _a, _b; return (((_b = (_a = episodeFile.episodeNumbers) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0) > 0) ? episodeFile.episodeNumbers[0] === episodeNumber : false; }) + : undefined; + } } - })]; - case 3: - pathWithNewName = _b.sent(); - return [3 /*break*/, 5]; - case 4: - args.jobLog('No arr specified in plugin inputs.'); - _b.label = 5; - case 5: return [2 /*return*/, { - outputFileObj: pathWithNewName !== '' ? __assign(__assign({}, args.inputFileObj), { _id: pathWithNewName }) : args.inputFileObj, - outputNumber: 1, - variables: args.variables, - }]; + } + }; + return [4 /*yield*/, getNewPath(arr === 'radarr' ? getNewPathTypes.radarr : getNewPathTypes.sonarr)]; + case 1: + newPathOutput = _d.sent(); + return [2 /*return*/, { + outputFileObj: newPathOutput.isSuccessful ? __assign(__assign({}, args.inputFileObj), { _id: newPathOutput.newPath }) : args.inputFileObj, + outputNumber: newPathOutput.isSuccessful ? 1 : 2, + variables: args.variables, + }]; } }); }); }; diff --git a/FlowPluginsTs/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.ts b/FlowPluginsTs/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.ts index 2d6216c..1a0d08f 100644 --- a/FlowPluginsTs/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.ts +++ b/FlowPluginsTs/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.ts @@ -61,9 +61,13 @@ const details = (): IpluginDetails => ({ outputs: [ { number: 1, - tooltip: 'Continue to next plugin', + tooltip: 'Radarr or Sonnar notified', }, - ], + { + number: 2, + tooltip: 'Radarr or Sonnar do not know this file', + } + ] }); interface IGetNewPathDelegates { @@ -71,6 +75,19 @@ interface IGetNewPathDelegates { buildPreviewRenameResquestUrl: (id: string, parseRequestResult: any) => string, getFileToRenameFromPreviewRenameRequestResult: (previewRenameRequestResult: any) => any } +interface IGetNewPathType { + appName: string, + contentName: string, + delegates: IGetNewPathDelegates +} +interface IGetNewPathTypes { + radarr: IGetNewPathType, + sonarr: IGetNewPathType +} +interface IGetNewPathOutput { + newPath: string, + isSuccessful: boolean +} const plugin = async (args: IpluginInputArgs): Promise => { const lib = require('../../../../../methods/lib')(); @@ -80,14 +97,18 @@ const plugin = async (args: IpluginInputArgs): Promise => { const { arr, arr_api_key } = args.inputs; const arr_host = String(args.inputs.arr_host).trim(); const arrHost = arr_host.endsWith('/') ? arr_host.slice(0, -1) : arr_host; - const fileName = getFileName(args.inputFileObj._id); + const filePath = args.originalLibraryFile?._id ?? ''; + const fileName = getFileName(filePath); - const getNewPath = async (delegates: IGetNewPathDelegates) - : Promise => { - let pathWithNewName = ''; + const getNewPath = async (getNewPathType: IGetNewPathType) + : Promise => { + const output : IGetNewPathOutput = { + newPath: '', + isSuccessful: false + } - args.jobLog('Going to force rename'); - args.jobLog(`Renaming ${arr === 'radarr' ? 'Radarr' : 'Sonarr'}...`); + args.jobLog('Going to apply new name'); + args.jobLog(`Renaming ${getNewPathType.appName}...`); const headers = { 'Content-Type': 'application/json', @@ -102,7 +123,7 @@ const plugin = async (args: IpluginInputArgs): Promise => { headers, }; const parseRequestResult = await args.deps.axios(parseRequestConfig); - const id = delegates.getIdFromParseRequestResult(parseRequestResult); + const id = getNewPathType.delegates.getIdFromParseRequestResult(parseRequestResult); // 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. @@ -110,61 +131,68 @@ const plugin = async (args: IpluginInputArgs): Promise => { // Using rename endpoint to get ids of all the files that need renaming. const previewRenameRequestConfig = { method: 'get', - url: delegates.buildPreviewRenameResquestUrl(id, parseRequestResult), + url: getNewPathType.delegates.buildPreviewRenameResquestUrl(id, parseRequestResult), headers, }; const previewRenameRequestResult = await args.deps.axios(previewRenameRequestConfig); - const fileToRename = delegates.getFileToRenameFromPreviewRenameRequestResult(previewRenameRequestResult); + const fileToRename = getNewPathType.delegates.getFileToRenameFromPreviewRenameRequestResult(previewRenameRequestResult); // Only if there is a rename to execute if (fileToRename !== undefined) { - pathWithNewName = `${getFileAbosluteDir(args.inputFileObj._id)}/${getFileName(fileToRename.newPath)}.${getContainer(fileToRename.newPath)}`; + output.newPath = `${getFileAbosluteDir(args.inputFileObj._id)}/${getFileName(fileToRename.newPath)}.${getContainer(fileToRename.newPath)}`; - await fileMoveOrCopy({ + output.isSuccessful = await fileMoveOrCopy({ operation: 'move', sourcePath: args.inputFileObj._id, - destinationPath: pathWithNewName, + destinationPath: output.newPath, args, }); - args.jobLog(`✔ Renamed ${arr === 'radarr' ? 'movie' : 'serie'} ${id} : '${args.inputFileObj._id}' => '${pathWithNewName}'.`); - } else + args.jobLog(`✔ Renamed ${getNewPathType.contentName} ${id} : '${filePath}' => '${output.newPath}'.`); + } else { + output.isSuccessful = true; args.jobLog('✔ No rename necessary.'); + } } else - args.jobLog(`No ${arr === 'radarr' ? 'movie' : 'serie'} with a file named '${fileName}'.`); + args.jobLog(`No ${getNewPathType.appName} with a file named '${fileName}'.`); - return pathWithNewName; + return output; }; - let pathWithNewName = ''; - if (arr === 'radarr') { - pathWithNewName = await getNewPath({ - getIdFromParseRequestResult: (parseRequestResult) => String(parseRequestResult.data?.movie?.movieFile?.movieId ?? -1), - buildPreviewRenameResquestUrl: (id, parseRequestResult) => `${arrHost}/api/v3/rename?movieId=${id}`, - getFileToRenameFromPreviewRenameRequestResult: (previewRenameRequestResult) => - ((previewRenameRequestResult.data?.length ?? 0) > 0) ? - previewRenameRequestResult.data[0] - : undefined - }); - } else if (arr === 'sonarr') { - let episodeNumber = 0; - pathWithNewName = await getNewPath({ - getIdFromParseRequestResult: (parseRequestResult) => String(parseRequestResult.data?.series?.id ?? -1), - buildPreviewRenameResquestUrl: (id, parseRequestResult) => { - episodeNumber = parseRequestResult.data.parsedEpisodeInfo.episodeNumbers[0]; - return `${arrHost}/api/v3/rename?seriesId=${id}&seasonNumber=${parseRequestResult.data.parsedEpisodeInfo.seasonNumber}`; - }, - getFileToRenameFromPreviewRenameRequestResult: (previewRenameRequestResult) => - ((previewRenameRequestResult.data?.length ?? 0) > 0) ? - previewRenameRequestResult.data.find((episodeFile: { episodeNumbers: number[]; }) => ((episodeFile.episodeNumbers?.length ?? 0) > 0) ? episodeFile.episodeNumbers[0] === episodeNumber : false) - : undefined - }); - } else { - args.jobLog('No arr specified in plugin inputs.'); + let episodeNumber = 0; + const getNewPathTypes: IGetNewPathTypes = { + radarr: { + appName: 'Radarr', + contentName: 'movie', + delegates: { + getIdFromParseRequestResult: (parseRequestResult) => String(parseRequestResult.data?.movie?.movieFile?.movieId ?? -1), + buildPreviewRenameResquestUrl: (id, parseRequestResult) => `${arrHost}/api/v3/rename?movieId=${id}`, + getFileToRenameFromPreviewRenameRequestResult: (previewRenameRequestResult) => + ((previewRenameRequestResult.data?.length ?? 0) > 0) ? + previewRenameRequestResult.data[0] + : undefined + } + }, + sonarr: { + appName: 'Sonarr', + contentName: 'serie', + delegates: { + getIdFromParseRequestResult: (parseRequestResult) => String(parseRequestResult.data?.series?.id ?? -1), + buildPreviewRenameResquestUrl: (id, parseRequestResult) => { + episodeNumber = parseRequestResult.data.parsedEpisodeInfo.episodeNumbers[0]; + return `${arrHost}/api/v3/rename?seriesId=${id}&seasonNumber=${parseRequestResult.data.parsedEpisodeInfo.seasonNumber}`; + }, + getFileToRenameFromPreviewRenameRequestResult: (previewRenameRequestResult) => + ((previewRenameRequestResult.data?.length ?? 0) > 0) ? + previewRenameRequestResult.data.find((episodeFile: { episodeNumbers: number[]; }) => ((episodeFile.episodeNumbers?.length ?? 0) > 0) ? episodeFile.episodeNumbers[0] === episodeNumber : false) + : undefined + } + } } + const newPathOutput = await getNewPath(arr === 'radarr' ? getNewPathTypes.radarr : getNewPathTypes.sonarr); return { - outputFileObj: pathWithNewName !== '' ? { ...args.inputFileObj, _id: pathWithNewName } : args.inputFileObj, - outputNumber: 1, + outputFileObj: newPathOutput.isSuccessful ? { ...args.inputFileObj, _id: newPathOutput.newPath } : args.inputFileObj, + outputNumber: newPathOutput.isSuccessful ? 1 : 2, variables: args.variables, }; }; From 88227593f51212c618358f94bbde87e964fdc00d Mon Sep 17 00:00:00 2001 From: "jeanchristophe.mqt@gmail.com" Date: Sat, 10 Feb 2024 20:42:56 +0100 Subject: [PATCH 19/34] Corrected final check --- .../tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.js | 2 +- .../tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/FlowPlugins/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.js b/FlowPlugins/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.js index 7df7d73..030faf8 100644 --- a/FlowPlugins/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.js +++ b/FlowPlugins/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.js @@ -227,7 +227,7 @@ var plugin = function (args) { return __awaiter(void 0, void 0, void 0, function case 1: newPathOutput = _d.sent(); return [2 /*return*/, { - outputFileObj: newPathOutput.isSuccessful ? __assign(__assign({}, args.inputFileObj), { _id: newPathOutput.newPath }) : args.inputFileObj, + outputFileObj: newPathOutput.isSuccessful && newPathOutput.newPath ? __assign(__assign({}, args.inputFileObj), { _id: newPathOutput.newPath }) : args.inputFileObj, outputNumber: newPathOutput.isSuccessful ? 1 : 2, variables: args.variables, }]; diff --git a/FlowPluginsTs/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.ts b/FlowPluginsTs/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.ts index 1a0d08f..bc606a6 100644 --- a/FlowPluginsTs/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.ts +++ b/FlowPluginsTs/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.ts @@ -191,7 +191,7 @@ const plugin = async (args: IpluginInputArgs): Promise => { const newPathOutput = await getNewPath(arr === 'radarr' ? getNewPathTypes.radarr : getNewPathTypes.sonarr); return { - outputFileObj: newPathOutput.isSuccessful ? { ...args.inputFileObj, _id: newPathOutput.newPath } : args.inputFileObj, + outputFileObj: newPathOutput.isSuccessful && newPathOutput.newPath ? { ...args.inputFileObj, _id: newPathOutput.newPath } : args.inputFileObj, outputNumber: newPathOutput.isSuccessful ? 1 : 2, variables: args.variables, }; From da5806b00f145d10cc84f472b1d2d8b05378cce3 Mon Sep 17 00:00:00 2001 From: "jeanchristophe.mqt@gmail.com" Date: Sat, 10 Feb 2024 20:46:12 +0100 Subject: [PATCH 20/34] Final test completed --- .../tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.js | 2 +- .../tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/FlowPlugins/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.js b/FlowPlugins/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.js index 030faf8..30a7b3d 100644 --- a/FlowPlugins/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.js +++ b/FlowPlugins/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.js @@ -227,7 +227,7 @@ var plugin = function (args) { return __awaiter(void 0, void 0, void 0, function case 1: newPathOutput = _d.sent(); return [2 /*return*/, { - outputFileObj: newPathOutput.isSuccessful && newPathOutput.newPath ? __assign(__assign({}, args.inputFileObj), { _id: newPathOutput.newPath }) : args.inputFileObj, + outputFileObj: newPathOutput.isSuccessful && newPathOutput.newPath !== '' ? __assign(__assign({}, args.inputFileObj), { _id: newPathOutput.newPath }) : args.inputFileObj, outputNumber: newPathOutput.isSuccessful ? 1 : 2, variables: args.variables, }]; diff --git a/FlowPluginsTs/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.ts b/FlowPluginsTs/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.ts index bc606a6..9960cb9 100644 --- a/FlowPluginsTs/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.ts +++ b/FlowPluginsTs/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.ts @@ -191,7 +191,7 @@ const plugin = async (args: IpluginInputArgs): Promise => { const newPathOutput = await getNewPath(arr === 'radarr' ? getNewPathTypes.radarr : getNewPathTypes.sonarr); return { - outputFileObj: newPathOutput.isSuccessful && newPathOutput.newPath ? { ...args.inputFileObj, _id: newPathOutput.newPath } : args.inputFileObj, + outputFileObj: newPathOutput.isSuccessful && newPathOutput.newPath !== '' ? { ...args.inputFileObj, _id: newPathOutput.newPath } : args.inputFileObj, outputNumber: newPathOutput.isSuccessful ? 1 : 2, variables: args.variables, }; From 2a5b73a8ebc551f6d48f4a8482a5495bd69697e2 Mon Sep 17 00:00:00 2001 From: "jeanchristophe.mqt@gmail.com" Date: Sun, 25 Feb 2024 16:33:54 +0100 Subject: [PATCH 21/34] Added more checks and fallbacks for name checking --- .../1.0.0/index.js | 88 ++++++++++++------- .../1.0.0/index.ts | 66 +++++++++----- 2 files changed, 102 insertions(+), 52 deletions(-) diff --git a/FlowPlugins/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.js b/FlowPlugins/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.js index 30a7b3d..b5d4fa7 100644 --- a/FlowPlugins/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.js +++ b/FlowPlugins/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.js @@ -116,10 +116,10 @@ var details = function () { return ({ }); }; exports.details = details; var plugin = function (args) { return __awaiter(void 0, void 0, void 0, function () { - var lib, _a, arr, arr_api_key, arr_host, arrHost, filePath, fileName, getNewPath, episodeNumber, getNewPathTypes, newPathOutput; - var _b, _c; - return __generator(this, function (_d) { - switch (_d.label) { + var lib, _a, arr, arr_api_key, arr_host, arrHost, filePath, fileNames, getNewPath, episodeNumber, getNewPathTypes, newPathOutput; + var _b, _c, _d, _e, _f, _g; + return __generator(this, function (_h) { + switch (_h.label) { case 0: lib = require('../../../../../methods/lib')(); // eslint-disable-next-line @typescript-eslint/no-unused-vars,no-param-reassign @@ -128,9 +128,12 @@ var plugin = function (args) { return __awaiter(void 0, void 0, void 0, function arr_host = String(args.inputs.arr_host).trim(); arrHost = arr_host.endsWith('/') ? arr_host.slice(0, -1) : arr_host; filePath = (_c = (_b = args.originalLibraryFile) === null || _b === void 0 ? void 0 : _b._id) !== null && _c !== void 0 ? _c : ''; - fileName = (0, fileUtils_1.getFileName)(filePath); + fileNames = { + originalFileName: (0, fileUtils_1.getFileName)((_e = (_d = args.originalLibraryFile) === null || _d === void 0 ? void 0 : _d._id) !== null && _e !== void 0 ? _e : ''), + currentFileName: (0, fileUtils_1.getFileName)((_g = (_f = args.inputFileObj) === null || _f === void 0 ? void 0 : _f._id) !== null && _g !== void 0 ? _g : '') + }; getNewPath = function (getNewPathType) { return __awaiter(void 0, void 0, void 0, function () { - var output, headers, parseRequestConfig, parseRequestResult, id, previewRenameRequestConfig, previewRenameRequestResult, fileToRename, _a; + var output, headers, getParseRequestResult, fileName, parseRequestResult, previewRenameRequestConfig, previewRenameRequestResult, fileToRename, _a; return __generator(this, function (_b) { switch (_b.label) { case 0: @@ -145,26 +148,49 @@ var plugin = function (args) { return __awaiter(void 0, void 0, void 0, function 'X-Api-Key': arr_api_key, Accept: 'application/json', }; - parseRequestConfig = { - method: 'get', - url: "".concat(arrHost, "/api/v3/parse?title=").concat(encodeURIComponent(fileName)), - headers: headers, - }; - return [4 /*yield*/, args.deps.axios(parseRequestConfig)]; + getParseRequestResult = function (fileName) { return __awaiter(void 0, void 0, void 0, function () { + var parseRequestConfig, parseRequestResult, id; + return __generator(this, function (_a) { + switch (_a.label) { + case 0: + parseRequestConfig = { + method: 'get', + url: "".concat(arrHost, "/api/v3/parse?title=").concat(encodeURIComponent(fileName)), + headers: headers, + }; + return [4 /*yield*/, args.deps.axios(parseRequestConfig)]; + case 1: + parseRequestResult = _a.sent(); + id = getNewPathType.delegates.getIdFromParseRequestResult(parseRequestResult); + args.jobLog(id !== '-1' ? + "Found ".concat(getNewPathType.contentName, " ").concat(id, " with a file named '").concat(fileName, "'") + : "Didn't find ".concat(getNewPathType.contentName, " with a file named '").concat(fileName, "' in ").concat(arrHost, ".")); + return [2 /*return*/, { requestResult: parseRequestResult, id: id }]; + } + }); + }); }; + fileName = fileNames.originalFileName; + return [4 /*yield*/, getParseRequestResult(fileName)]; case 1: parseRequestResult = _b.sent(); - id = getNewPathType.delegates.getIdFromParseRequestResult(parseRequestResult); - if (!(id !== '-1')) return [3 /*break*/, 6]; + if (!(parseRequestResult.id == '-1' && fileNames.currentFileName !== fileNames.originalFileName)) return [3 /*break*/, 3]; + fileName = fileNames.currentFileName; + return [4 /*yield*/, getParseRequestResult(fileName)]; + case 2: + parseRequestResult = _b.sent(); + _b.label = 3; + case 3: + if (!(parseRequestResult.id !== '-1')) return [3 /*break*/, 8]; previewRenameRequestConfig = { method: 'get', - url: getNewPathType.delegates.buildPreviewRenameResquestUrl(id, parseRequestResult), + url: getNewPathType.delegates.buildPreviewRenameResquestUrl(parseRequestResult), headers: headers, }; return [4 /*yield*/, args.deps.axios(previewRenameRequestConfig)]; - case 2: + case 4: previewRenameRequestResult = _b.sent(); fileToRename = getNewPathType.delegates.getFileToRenameFromPreviewRenameRequestResult(previewRenameRequestResult); - if (!(fileToRename !== undefined)) return [3 /*break*/, 4]; + if (!(fileToRename !== undefined)) return [3 /*break*/, 6]; output.newPath = "".concat((0, fileUtils_1.getFileAbosluteDir)(args.inputFileObj._id), "/").concat((0, fileUtils_1.getFileName)(fileToRename.newPath), ".").concat((0, fileUtils_1.getContainer)(fileToRename.newPath)); _a = output; return [4 /*yield*/, (0, fileMoveOrCopy_1.default)({ @@ -173,19 +199,19 @@ var plugin = function (args) { return __awaiter(void 0, void 0, void 0, function destinationPath: output.newPath, args: args, })]; - case 3: + case 5: _a.isSuccessful = _b.sent(); - args.jobLog("\u2714 Renamed ".concat(getNewPathType.contentName, " ").concat(id, " : '").concat(filePath, "' => '").concat(output.newPath, "'.")); - return [3 /*break*/, 5]; - case 4: + args.jobLog("\u2714 Renamed ".concat(getNewPathType.contentName, " ").concat(parseRequestResult, " : '").concat(filePath, "' => '").concat(output.newPath, "'.")); + return [3 /*break*/, 7]; + case 6: output.isSuccessful = true; args.jobLog('✔ No rename necessary.'); - _b.label = 5; - case 5: return [3 /*break*/, 7]; - case 6: - args.jobLog("No ".concat(getNewPathType.appName, " with a file named '").concat(fileName, "'.")); _b.label = 7; - case 7: return [2 /*return*/, output]; + case 7: return [3 /*break*/, 9]; + case 8: + args.jobLog("No ".concat(getNewPathType.appName, " with a file named '").concat(fileName, "'.")); + _b.label = 9; + case 9: return [2 /*return*/, output]; } }); }); }; @@ -196,7 +222,7 @@ var plugin = function (args) { return __awaiter(void 0, void 0, void 0, function contentName: 'movie', delegates: { getIdFromParseRequestResult: 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); }, - buildPreviewRenameResquestUrl: function (id, parseRequestResult) { return "".concat(arrHost, "/api/v3/rename?movieId=").concat(id); }, + buildPreviewRenameResquestUrl: function (parseRequestResult) { return "".concat(arrHost, "/api/v3/rename?movieId=").concat(parseRequestResult.id); }, getFileToRenameFromPreviewRenameRequestResult: function (previewRenameRequestResult) { var _a, _b; return (((_b = (_a = previewRenameRequestResult.data) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0) > 0) ? @@ -210,9 +236,9 @@ var plugin = function (args) { return __awaiter(void 0, void 0, void 0, function contentName: 'serie', delegates: { getIdFromParseRequestResult: 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); }, - buildPreviewRenameResquestUrl: function (id, parseRequestResult) { - episodeNumber = parseRequestResult.data.parsedEpisodeInfo.episodeNumbers[0]; - return "".concat(arrHost, "/api/v3/rename?seriesId=").concat(id, "&seasonNumber=").concat(parseRequestResult.data.parsedEpisodeInfo.seasonNumber); + buildPreviewRenameResquestUrl: function (parseRequestResult) { + episodeNumber = parseRequestResult.requestResult.data.parsedEpisodeInfo.episodeNumbers[0]; + return "".concat(arrHost, "/api/v3/rename?seriesId=").concat(parseRequestResult.id, "&seasonNumber=").concat(parseRequestResult.requestResult.data.parsedEpisodeInfo.seasonNumber); }, getFileToRenameFromPreviewRenameRequestResult: function (previewRenameRequestResult) { var _a, _b; @@ -225,7 +251,7 @@ var plugin = function (args) { return __awaiter(void 0, void 0, void 0, function }; return [4 /*yield*/, getNewPath(arr === 'radarr' ? getNewPathTypes.radarr : getNewPathTypes.sonarr)]; case 1: - newPathOutput = _d.sent(); + newPathOutput = _h.sent(); return [2 /*return*/, { outputFileObj: newPathOutput.isSuccessful && newPathOutput.newPath !== '' ? __assign(__assign({}, args.inputFileObj), { _id: newPathOutput.newPath }) : args.inputFileObj, outputNumber: newPathOutput.isSuccessful ? 1 : 2, diff --git a/FlowPluginsTs/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.ts b/FlowPluginsTs/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.ts index 9960cb9..c465ddd 100644 --- a/FlowPluginsTs/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.ts +++ b/FlowPluginsTs/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.ts @@ -70,9 +70,17 @@ const details = (): IpluginDetails => ({ ] }); +interface IFileNames { + originalFileName: string, + currentFileName: string +} +interface IParseRequestResult { + requestResult: any, + id: string +} interface IGetNewPathDelegates { getIdFromParseRequestResult: (parseRequestResult: any) => string, - buildPreviewRenameResquestUrl: (id: string, parseRequestResult: any) => string, + buildPreviewRenameResquestUrl: (parseRequestResult: IParseRequestResult) => string, getFileToRenameFromPreviewRenameRequestResult: (previewRenameRequestResult: any) => any } interface IGetNewPathType { @@ -98,15 +106,17 @@ const plugin = async (args: IpluginInputArgs): Promise => { const arr_host = String(args.inputs.arr_host).trim(); const arrHost = arr_host.endsWith('/') ? arr_host.slice(0, -1) : arr_host; const filePath = args.originalLibraryFile?._id ?? ''; - const fileName = getFileName(filePath); + const fileNames: IFileNames = { + originalFileName: getFileName(args.originalLibraryFile?._id ?? ''), + currentFileName: getFileName(args.inputFileObj?._id ?? '') + }; const getNewPath = async (getNewPathType: IGetNewPathType) : Promise => { - const output : IGetNewPathOutput = { + const output: IGetNewPathOutput = { newPath: '', isSuccessful: false } - args.jobLog('Going to apply new name'); args.jobLog(`Renaming ${getNewPathType.appName}...`); @@ -116,22 +126,36 @@ const plugin = async (args: IpluginInputArgs): Promise => { Accept: 'application/json', }; - // Using parse endpoint to get the movie/serie's id. - const parseRequestConfig = { - method: 'get', - url: `${arrHost}/api/v3/parse?title=${encodeURIComponent(fileName)}`, - headers, - }; - const parseRequestResult = await args.deps.axios(parseRequestConfig); - const id = getNewPathType.delegates.getIdFromParseRequestResult(parseRequestResult); + const getParseRequestResult = async (fileName: string) + : Promise => { + // Using parse endpoint to get the movie/serie's id. + const parseRequestConfig = { + method: 'get', + url: `${arrHost}/api/v3/parse?title=${encodeURIComponent(fileName)}`, + headers, + }; + const parseRequestResult = await args.deps.axios(parseRequestConfig); + const id = getNewPathType.delegates.getIdFromParseRequestResult(parseRequestResult); + args.jobLog(id !== '-1' ? + `Found ${getNewPathType.contentName} ${id} with a file named '${fileName}'` + : `Didn't find ${getNewPathType.contentName} with a file named '${fileName}' in ${arrHost}.`); + return { requestResult: parseRequestResult, id: id }; + } + + let fileName = fileNames.originalFileName; + let parseRequestResult = await getParseRequestResult(fileName); + // In case there has been a name change and the arr app already noticed it. + if (parseRequestResult.id == '-1' && fileNames.currentFileName !== fileNames.originalFileName) { + fileName = fileNames.currentFileName; + parseRequestResult = await getParseRequestResult(fileName); + } - // 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') { + // Checking that the file has been found. + if (parseRequestResult.id !== '-1') { // Using rename endpoint to get ids of all the files that need renaming. const previewRenameRequestConfig = { method: 'get', - url: getNewPathType.delegates.buildPreviewRenameResquestUrl(id, parseRequestResult), + url: getNewPathType.delegates.buildPreviewRenameResquestUrl(parseRequestResult), headers, }; const previewRenameRequestResult = await args.deps.axios(previewRenameRequestConfig); @@ -147,7 +171,7 @@ const plugin = async (args: IpluginInputArgs): Promise => { destinationPath: output.newPath, args, }); - args.jobLog(`✔ Renamed ${getNewPathType.contentName} ${id} : '${filePath}' => '${output.newPath}'.`); + args.jobLog(`✔ Renamed ${getNewPathType.contentName} ${parseRequestResult} : '${filePath}' => '${output.newPath}'.`); } else { output.isSuccessful = true; args.jobLog('✔ No rename necessary.'); @@ -165,7 +189,7 @@ const plugin = async (args: IpluginInputArgs): Promise => { contentName: 'movie', delegates: { getIdFromParseRequestResult: (parseRequestResult) => String(parseRequestResult.data?.movie?.movieFile?.movieId ?? -1), - buildPreviewRenameResquestUrl: (id, parseRequestResult) => `${arrHost}/api/v3/rename?movieId=${id}`, + buildPreviewRenameResquestUrl: (parseRequestResult) => `${arrHost}/api/v3/rename?movieId=${parseRequestResult.id}`, getFileToRenameFromPreviewRenameRequestResult: (previewRenameRequestResult) => ((previewRenameRequestResult.data?.length ?? 0) > 0) ? previewRenameRequestResult.data[0] @@ -177,9 +201,9 @@ const plugin = async (args: IpluginInputArgs): Promise => { contentName: 'serie', delegates: { getIdFromParseRequestResult: (parseRequestResult) => String(parseRequestResult.data?.series?.id ?? -1), - buildPreviewRenameResquestUrl: (id, parseRequestResult) => { - episodeNumber = parseRequestResult.data.parsedEpisodeInfo.episodeNumbers[0]; - return `${arrHost}/api/v3/rename?seriesId=${id}&seasonNumber=${parseRequestResult.data.parsedEpisodeInfo.seasonNumber}`; + buildPreviewRenameResquestUrl: (parseRequestResult) => { + episodeNumber = parseRequestResult.requestResult.data.parsedEpisodeInfo.episodeNumbers[0]; + return `${arrHost}/api/v3/rename?seriesId=${parseRequestResult.id}&seasonNumber=${parseRequestResult.requestResult.data.parsedEpisodeInfo.seasonNumber}`; }, getFileToRenameFromPreviewRenameRequestResult: (previewRenameRequestResult) => ((previewRenameRequestResult.data?.length ?? 0) > 0) ? From 9d5f7136f83f8f09885dbbe254f1c5b7e3c7ab28 Mon Sep 17 00:00:00 2001 From: "jeanchristophe.mqt@gmail.com" Date: Sun, 25 Feb 2024 16:40:16 +0100 Subject: [PATCH 22/34] Minor correction --- .../tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.js | 2 +- .../tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/FlowPlugins/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.js b/FlowPlugins/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.js index b5d4fa7..8dc1ff4 100644 --- a/FlowPlugins/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.js +++ b/FlowPlugins/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.js @@ -201,7 +201,7 @@ var plugin = function (args) { return __awaiter(void 0, void 0, void 0, function })]; case 5: _a.isSuccessful = _b.sent(); - args.jobLog("\u2714 Renamed ".concat(getNewPathType.contentName, " ").concat(parseRequestResult, " : '").concat(filePath, "' => '").concat(output.newPath, "'.")); + args.jobLog("\u2714 Renamed ".concat(getNewPathType.contentName, " ").concat(parseRequestResult.id, " : '").concat(filePath, "' => '").concat(output.newPath, "'.")); return [3 /*break*/, 7]; case 6: output.isSuccessful = true; diff --git a/FlowPluginsTs/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.ts b/FlowPluginsTs/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.ts index c465ddd..a3c3099 100644 --- a/FlowPluginsTs/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.ts +++ b/FlowPluginsTs/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.ts @@ -171,7 +171,7 @@ const plugin = async (args: IpluginInputArgs): Promise => { destinationPath: output.newPath, args, }); - args.jobLog(`✔ Renamed ${getNewPathType.contentName} ${parseRequestResult} : '${filePath}' => '${output.newPath}'.`); + args.jobLog(`✔ Renamed ${getNewPathType.contentName} ${parseRequestResult.id} : '${filePath}' => '${output.newPath}'.`); } else { output.isSuccessful = true; args.jobLog('✔ No rename necessary.'); From 9ca5be4a2d7cacd834e0effc96329e8c54d23eee Mon Sep 17 00:00:00 2001 From: "jeanchristophe.mqt@gmail.com" Date: Sun, 25 Feb 2024 17:01:58 +0100 Subject: [PATCH 23/34] Removed useless log --- .../tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.js | 8 ++------ .../tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.ts | 3 +-- 2 files changed, 3 insertions(+), 8 deletions(-) diff --git a/FlowPlugins/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.js b/FlowPlugins/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.js index 8dc1ff4..40b46d9 100644 --- a/FlowPlugins/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.js +++ b/FlowPlugins/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.js @@ -180,7 +180,7 @@ var plugin = function (args) { return __awaiter(void 0, void 0, void 0, function parseRequestResult = _b.sent(); _b.label = 3; case 3: - if (!(parseRequestResult.id !== '-1')) return [3 /*break*/, 8]; + if (!(parseRequestResult.id !== '-1')) return [3 /*break*/, 7]; previewRenameRequestConfig = { method: 'get', url: getNewPathType.delegates.buildPreviewRenameResquestUrl(parseRequestResult), @@ -207,11 +207,7 @@ var plugin = function (args) { return __awaiter(void 0, void 0, void 0, function output.isSuccessful = true; args.jobLog('✔ No rename necessary.'); _b.label = 7; - case 7: return [3 /*break*/, 9]; - case 8: - args.jobLog("No ".concat(getNewPathType.appName, " with a file named '").concat(fileName, "'.")); - _b.label = 9; - case 9: return [2 /*return*/, output]; + case 7: return [2 /*return*/, output]; } }); }); }; diff --git a/FlowPluginsTs/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.ts b/FlowPluginsTs/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.ts index a3c3099..456996a 100644 --- a/FlowPluginsTs/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.ts +++ b/FlowPluginsTs/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.ts @@ -176,8 +176,7 @@ const plugin = async (args: IpluginInputArgs): Promise => { output.isSuccessful = true; args.jobLog('✔ No rename necessary.'); } - } else - args.jobLog(`No ${getNewPathType.appName} with a file named '${fileName}'.`); + } return output; }; From d27955ed90bffe6a039b41c03d40276f8cf7d824 Mon Sep 17 00:00:00 2001 From: "jeanchristophe.mqt@gmail.com" Date: Fri, 22 Mar 2024 23:05:28 +0100 Subject: [PATCH 24/34] Fix eslint warnings and errors --- .../1.0.0/index.js | 57 +++++++----- .../1.0.0/index.ts | 87 +++++++++++-------- 2 files changed, 87 insertions(+), 57 deletions(-) diff --git a/FlowPlugins/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.js b/FlowPlugins/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.js index 40b46d9..feff620 100644 --- a/FlowPlugins/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.js +++ b/FlowPlugins/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.js @@ -55,7 +55,8 @@ var fileMoveOrCopy_1 = __importDefault(require("../../../../FlowHelpers/1.0.0/fi var fileUtils_1 = require("../../../../FlowHelpers/1.0.0/fileUtils"); var details = function () { return ({ name: 'Apply Radarr or Sonarr naming policy', - description: 'Apply Radarr or Sonarr naming policy to a file. This plugin should be called after the original file has been replaced and Radarr or Sonarr has been notified. Radarr or Sonarr should also be notified after this plugin.', + description: 'Apply Radarr or Sonarr naming policy to a file. This plugin should be called after the original file has been ' + + 'replaced and Radarr or Sonarr has been notified. Radarr or Sonarr should also be notified after this plugin.', style: { borderColor: 'green', }, @@ -111,8 +112,8 @@ var details = function () { return ({ { number: 2, tooltip: 'Radarr or Sonnar do not know this file', - } - ] + }, + ], }); }; exports.details = details; var plugin = function (args) { return __awaiter(void 0, void 0, void 0, function () { @@ -130,7 +131,7 @@ var plugin = function (args) { return __awaiter(void 0, void 0, void 0, function filePath = (_c = (_b = args.originalLibraryFile) === null || _b === void 0 ? void 0 : _b._id) !== null && _c !== void 0 ? _c : ''; fileNames = { originalFileName: (0, fileUtils_1.getFileName)((_e = (_d = args.originalLibraryFile) === null || _d === void 0 ? void 0 : _d._id) !== null && _e !== void 0 ? _e : ''), - currentFileName: (0, fileUtils_1.getFileName)((_g = (_f = args.inputFileObj) === null || _f === void 0 ? void 0 : _f._id) !== null && _g !== void 0 ? _g : '') + currentFileName: (0, fileUtils_1.getFileName)((_g = (_f = args.inputFileObj) === null || _f === void 0 ? void 0 : _f._id) !== null && _g !== void 0 ? _g : ''), }; getNewPath = function (getNewPathType) { return __awaiter(void 0, void 0, void 0, function () { var output, headers, getParseRequestResult, fileName, parseRequestResult, previewRenameRequestConfig, previewRenameRequestResult, fileToRename, _a; @@ -139,7 +140,7 @@ var plugin = function (args) { return __awaiter(void 0, void 0, void 0, function case 0: output = { newPath: '', - isSuccessful: false + isSuccessful: false, }; args.jobLog('Going to apply new name'); args.jobLog("Renaming ".concat(getNewPathType.appName, "...")); @@ -162,8 +163,8 @@ var plugin = function (args) { return __awaiter(void 0, void 0, void 0, function case 1: parseRequestResult = _a.sent(); id = getNewPathType.delegates.getIdFromParseRequestResult(parseRequestResult); - args.jobLog(id !== '-1' ? - "Found ".concat(getNewPathType.contentName, " ").concat(id, " with a file named '").concat(fileName, "'") + args.jobLog(id !== '-1' + ? "Found ".concat(getNewPathType.contentName, " ").concat(id, " with a file named '").concat(fileName, "'") : "Didn't find ".concat(getNewPathType.contentName, " with a file named '").concat(fileName, "' in ").concat(arrHost, ".")); return [2 /*return*/, { requestResult: parseRequestResult, id: id }]; } @@ -173,7 +174,7 @@ var plugin = function (args) { return __awaiter(void 0, void 0, void 0, function return [4 /*yield*/, getParseRequestResult(fileName)]; case 1: parseRequestResult = _b.sent(); - if (!(parseRequestResult.id == '-1' && fileNames.currentFileName !== fileNames.originalFileName)) return [3 /*break*/, 3]; + if (!(parseRequestResult.id === '-1' && fileNames.currentFileName !== fileNames.originalFileName)) return [3 /*break*/, 3]; fileName = fileNames.currentFileName; return [4 /*yield*/, getParseRequestResult(fileName)]; case 2: @@ -189,7 +190,8 @@ var plugin = function (args) { return __awaiter(void 0, void 0, void 0, function return [4 /*yield*/, args.deps.axios(previewRenameRequestConfig)]; case 4: previewRenameRequestResult = _b.sent(); - fileToRename = getNewPathType.delegates.getFileToRenameFromPreviewRenameRequestResult(previewRenameRequestResult); + fileToRename = getNewPathType.delegates + .getFileToRenameFromPreviewRenameRequestResult(previewRenameRequestResult); if (!(fileToRename !== undefined)) return [3 /*break*/, 6]; output.newPath = "".concat((0, fileUtils_1.getFileAbosluteDir)(args.inputFileObj._id), "/").concat((0, fileUtils_1.getFileName)(fileToRename.newPath), ".").concat((0, fileUtils_1.getContainer)(fileToRename.newPath)); _a = output; @@ -201,7 +203,8 @@ var plugin = function (args) { return __awaiter(void 0, void 0, void 0, function })]; case 5: _a.isSuccessful = _b.sent(); - args.jobLog("\u2714 Renamed ".concat(getNewPathType.contentName, " ").concat(parseRequestResult.id, " : '").concat(filePath, "' => '").concat(output.newPath, "'.")); + args.jobLog("\u2714 Renamed ".concat(getNewPathType.contentName, " ").concat(parseRequestResult.id, " : ") + + "'".concat(filePath, "' => '").concat(output.newPath, "'.")); return [3 /*break*/, 7]; case 6: output.isSuccessful = true; @@ -221,11 +224,11 @@ var plugin = function (args) { return __awaiter(void 0, void 0, void 0, function buildPreviewRenameResquestUrl: function (parseRequestResult) { return "".concat(arrHost, "/api/v3/rename?movieId=").concat(parseRequestResult.id); }, getFileToRenameFromPreviewRenameRequestResult: function (previewRenameRequestResult) { var _a, _b; - return (((_b = (_a = previewRenameRequestResult.data) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0) > 0) ? - previewRenameRequestResult.data[0] - : undefined; - } - } + return ((((_b = (_a = previewRenameRequestResult.data) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0) > 0) + ? previewRenameRequestResult.data[0] + : undefined); + }, + }, }, sonarr: { appName: 'Sonarr', @@ -234,22 +237,30 @@ var plugin = function (args) { return __awaiter(void 0, void 0, void 0, function getIdFromParseRequestResult: 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); }, buildPreviewRenameResquestUrl: function (parseRequestResult) { episodeNumber = parseRequestResult.requestResult.data.parsedEpisodeInfo.episodeNumbers[0]; - return "".concat(arrHost, "/api/v3/rename?seriesId=").concat(parseRequestResult.id, "&seasonNumber=").concat(parseRequestResult.requestResult.data.parsedEpisodeInfo.seasonNumber); + return "".concat(arrHost, "/api/v3/rename?") + + "seriesId=".concat(parseRequestResult.id) + + "&seasonNumber=".concat(parseRequestResult.requestResult.data.parsedEpisodeInfo.seasonNumber); }, getFileToRenameFromPreviewRenameRequestResult: function (previewRenameRequestResult) { var _a, _b; - return (((_b = (_a = previewRenameRequestResult.data) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0) > 0) ? - previewRenameRequestResult.data.find(function (episodeFile) { var _a, _b; return (((_b = (_a = episodeFile.episodeNumbers) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0) > 0) ? episodeFile.episodeNumbers[0] === episodeNumber : false; }) - : undefined; - } - } - } + return ((((_b = (_a = previewRenameRequestResult.data) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0) > 0) + ? previewRenameRequestResult.data.find(function (episodeFile) { + var _a, _b; + return ((((_b = (_a = episodeFile.episodeNumbers) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0) > 0) + ? episodeFile.episodeNumbers[0] === episodeNumber + : false); + }) + : undefined); + }, + }, + }, }; return [4 /*yield*/, getNewPath(arr === 'radarr' ? getNewPathTypes.radarr : getNewPathTypes.sonarr)]; case 1: newPathOutput = _h.sent(); return [2 /*return*/, { - outputFileObj: newPathOutput.isSuccessful && newPathOutput.newPath !== '' ? __assign(__assign({}, args.inputFileObj), { _id: newPathOutput.newPath }) : args.inputFileObj, + outputFileObj: newPathOutput.isSuccessful && newPathOutput.newPath !== '' + ? __assign(__assign({}, args.inputFileObj), { _id: newPathOutput.newPath }) : args.inputFileObj, outputNumber: newPathOutput.isSuccessful ? 1 : 2, variables: args.variables, }]; diff --git a/FlowPluginsTs/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.ts b/FlowPluginsTs/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.ts index 456996a..2937157 100644 --- a/FlowPluginsTs/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.ts +++ b/FlowPluginsTs/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.ts @@ -10,7 +10,9 @@ import { const details = (): IpluginDetails => ({ name: 'Apply Radarr or Sonarr naming policy', - description: 'Apply Radarr or Sonarr naming policy to a file. This plugin should be called after the original file has been replaced and Radarr or Sonarr has been notified. Radarr or Sonarr should also be notified after this plugin.', + description: + 'Apply Radarr or Sonarr naming policy to a file. This plugin should be called after the original file has been ' + + 'replaced and Radarr or Sonarr has been notified. Radarr or Sonarr should also be notified after this plugin.', style: { borderColor: 'green', }, @@ -66,8 +68,8 @@ const details = (): IpluginDetails => ({ { number: 2, tooltip: 'Radarr or Sonnar do not know this file', - } - ] + }, + ], }); interface IFileNames { @@ -108,15 +110,15 @@ const plugin = async (args: IpluginInputArgs): Promise => { const filePath = args.originalLibraryFile?._id ?? ''; const fileNames: IFileNames = { originalFileName: getFileName(args.originalLibraryFile?._id ?? ''), - currentFileName: getFileName(args.inputFileObj?._id ?? '') + currentFileName: getFileName(args.inputFileObj?._id ?? ''), }; const getNewPath = async (getNewPathType: IGetNewPathType) : Promise => { const output: IGetNewPathOutput = { newPath: '', - isSuccessful: false - } + isSuccessful: false, + }; args.jobLog('Going to apply new name'); args.jobLog(`Renaming ${getNewPathType.appName}...`); @@ -136,16 +138,16 @@ const plugin = async (args: IpluginInputArgs): Promise => { }; const parseRequestResult = await args.deps.axios(parseRequestConfig); const id = getNewPathType.delegates.getIdFromParseRequestResult(parseRequestResult); - args.jobLog(id !== '-1' ? - `Found ${getNewPathType.contentName} ${id} with a file named '${fileName}'` + args.jobLog(id !== '-1' + ? `Found ${getNewPathType.contentName} ${id} with a file named '${fileName}'` : `Didn't find ${getNewPathType.contentName} with a file named '${fileName}' in ${arrHost}.`); - return { requestResult: parseRequestResult, id: id }; - } + return { requestResult: parseRequestResult, id }; + }; let fileName = fileNames.originalFileName; let parseRequestResult = await getParseRequestResult(fileName); // In case there has been a name change and the arr app already noticed it. - if (parseRequestResult.id == '-1' && fileNames.currentFileName !== fileNames.originalFileName) { + if (parseRequestResult.id === '-1' && fileNames.currentFileName !== fileNames.originalFileName) { fileName = fileNames.currentFileName; parseRequestResult = await getParseRequestResult(fileName); } @@ -159,11 +161,14 @@ const plugin = async (args: IpluginInputArgs): Promise => { headers, }; const previewRenameRequestResult = await args.deps.axios(previewRenameRequestConfig); - const fileToRename = getNewPathType.delegates.getFileToRenameFromPreviewRenameRequestResult(previewRenameRequestResult); + const fileToRename = getNewPathType.delegates + .getFileToRenameFromPreviewRenameRequestResult(previewRenameRequestResult); // Only if there is a rename to execute if (fileToRename !== undefined) { - output.newPath = `${getFileAbosluteDir(args.inputFileObj._id)}/${getFileName(fileToRename.newPath)}.${getContainer(fileToRename.newPath)}`; + output.newPath = `${getFileAbosluteDir(args.inputFileObj._id) + }/${getFileName(fileToRename.newPath) + }.${getContainer(fileToRename.newPath)}`; output.isSuccessful = await fileMoveOrCopy({ operation: 'move', @@ -171,7 +176,8 @@ const plugin = async (args: IpluginInputArgs): Promise => { destinationPath: output.newPath, args, }); - args.jobLog(`✔ Renamed ${getNewPathType.contentName} ${parseRequestResult.id} : '${filePath}' => '${output.newPath}'.`); + args.jobLog(`✔ Renamed ${getNewPathType.contentName} ${parseRequestResult.id} : ` + + `'${filePath}' => '${output.newPath}'.`); } else { output.isSuccessful = true; args.jobLog('✔ No rename necessary.'); @@ -187,34 +193,47 @@ const plugin = async (args: IpluginInputArgs): Promise => { appName: 'Radarr', contentName: 'movie', delegates: { - getIdFromParseRequestResult: (parseRequestResult) => String(parseRequestResult.data?.movie?.movieFile?.movieId ?? -1), - buildPreviewRenameResquestUrl: (parseRequestResult) => `${arrHost}/api/v3/rename?movieId=${parseRequestResult.id}`, - getFileToRenameFromPreviewRenameRequestResult: (previewRenameRequestResult) => - ((previewRenameRequestResult.data?.length ?? 0) > 0) ? - previewRenameRequestResult.data[0] - : undefined - } + getIdFromParseRequestResult: + (parseRequestResult) => String(parseRequestResult.data?.movie?.movieFile?.movieId ?? -1), + buildPreviewRenameResquestUrl: + (parseRequestResult) => `${arrHost}/api/v3/rename?movieId=${parseRequestResult.id}`, + getFileToRenameFromPreviewRenameRequestResult: + (previewRenameRequestResult) => (((previewRenameRequestResult.data?.length ?? 0) > 0) + ? previewRenameRequestResult.data[0] + : undefined), + }, }, sonarr: { appName: 'Sonarr', contentName: 'serie', delegates: { - getIdFromParseRequestResult: (parseRequestResult) => String(parseRequestResult.data?.series?.id ?? -1), - buildPreviewRenameResquestUrl: (parseRequestResult) => { - episodeNumber = parseRequestResult.requestResult.data.parsedEpisodeInfo.episodeNumbers[0]; - return `${arrHost}/api/v3/rename?seriesId=${parseRequestResult.id}&seasonNumber=${parseRequestResult.requestResult.data.parsedEpisodeInfo.seasonNumber}`; - }, - getFileToRenameFromPreviewRenameRequestResult: (previewRenameRequestResult) => - ((previewRenameRequestResult.data?.length ?? 0) > 0) ? - previewRenameRequestResult.data.find((episodeFile: { episodeNumbers: number[]; }) => ((episodeFile.episodeNumbers?.length ?? 0) > 0) ? episodeFile.episodeNumbers[0] === episodeNumber : false) - : undefined - } - } - } + getIdFromParseRequestResult: + (parseRequestResult) => String(parseRequestResult.data?.series?.id ?? -1), + buildPreviewRenameResquestUrl: + (parseRequestResult) => { + [episodeNumber] = parseRequestResult.requestResult.data.parsedEpisodeInfo.episodeNumbers; + return `${arrHost}/api/v3/rename?` + + `seriesId=${parseRequestResult.id}` + + `&seasonNumber=${parseRequestResult.requestResult.data.parsedEpisodeInfo.seasonNumber}`; + }, + getFileToRenameFromPreviewRenameRequestResult: + (previewRenameRequestResult) => (((previewRenameRequestResult.data?.length ?? 0) > 0) + ? previewRenameRequestResult.data.find( + (episodeFile: { episodeNumbers: number[]; }) => (((episodeFile.episodeNumbers?.length ?? 0) > 0) + ? episodeFile.episodeNumbers[0] === episodeNumber + : false), + ) + : undefined), + }, + }, + }; const newPathOutput = await getNewPath(arr === 'radarr' ? getNewPathTypes.radarr : getNewPathTypes.sonarr); return { - outputFileObj: newPathOutput.isSuccessful && newPathOutput.newPath !== '' ? { ...args.inputFileObj, _id: newPathOutput.newPath } : args.inputFileObj, + outputFileObj: + newPathOutput.isSuccessful && newPathOutput.newPath !== '' + ? { ...args.inputFileObj, _id: newPathOutput.newPath } + : args.inputFileObj, outputNumber: newPathOutput.isSuccessful ? 1 : 2, variables: args.variables, }; From 5795a6df61e9c91ea7d62d13814e81d0f71f3d12 Mon Sep 17 00:00:00 2001 From: "jeanchristophe.mqt@gmail.com" Date: Sat, 23 Mar 2024 00:37:12 +0100 Subject: [PATCH 25/34] Replaced any by inferred interfaces --- .../1.0.0/index.js | 41 +++++------ .../1.0.0/index.ts | 70 ++++++++++++------- 2 files changed, 64 insertions(+), 47 deletions(-) diff --git a/FlowPlugins/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.js b/FlowPlugins/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.js index feff620..2476099 100644 --- a/FlowPlugins/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.js +++ b/FlowPlugins/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.js @@ -134,7 +134,7 @@ var plugin = function (args) { return __awaiter(void 0, void 0, void 0, function currentFileName: (0, fileUtils_1.getFileName)((_g = (_f = args.inputFileObj) === null || _f === void 0 ? void 0 : _f._id) !== null && _g !== void 0 ? _g : ''), }; getNewPath = function (getNewPathType) { return __awaiter(void 0, void 0, void 0, function () { - var output, headers, getParseRequestResult, fileName, parseRequestResult, previewRenameRequestConfig, previewRenameRequestResult, fileToRename, _a; + var output, headers, getParseRequestResultWrapper, fileName, parseRequestResultWrapper, previewRenameRequestConfig, previewRenameRequestResult, fileToRename, _a; return __generator(this, function (_b) { switch (_b.label) { case 0: @@ -149,7 +149,7 @@ var plugin = function (args) { return __awaiter(void 0, void 0, void 0, function 'X-Api-Key': arr_api_key, Accept: 'application/json', }; - getParseRequestResult = function (fileName) { return __awaiter(void 0, void 0, void 0, function () { + getParseRequestResultWrapper = function (fileName) { return __awaiter(void 0, void 0, void 0, function () { var parseRequestConfig, parseRequestResult, id; return __generator(this, function (_a) { switch (_a.label) { @@ -166,25 +166,25 @@ var plugin = function (args) { return __awaiter(void 0, void 0, void 0, function args.jobLog(id !== '-1' ? "Found ".concat(getNewPathType.contentName, " ").concat(id, " with a file named '").concat(fileName, "'") : "Didn't find ".concat(getNewPathType.contentName, " with a file named '").concat(fileName, "' in ").concat(arrHost, ".")); - return [2 /*return*/, { requestResult: parseRequestResult, id: id }]; + return [2 /*return*/, { parseRequestResult: parseRequestResult, id: id }]; } }); }); }; fileName = fileNames.originalFileName; - return [4 /*yield*/, getParseRequestResult(fileName)]; + return [4 /*yield*/, getParseRequestResultWrapper(fileName)]; case 1: - parseRequestResult = _b.sent(); - if (!(parseRequestResult.id === '-1' && fileNames.currentFileName !== fileNames.originalFileName)) return [3 /*break*/, 3]; + parseRequestResultWrapper = _b.sent(); + if (!(parseRequestResultWrapper.id === '-1' && fileNames.currentFileName !== fileNames.originalFileName)) return [3 /*break*/, 3]; fileName = fileNames.currentFileName; - return [4 /*yield*/, getParseRequestResult(fileName)]; + return [4 /*yield*/, getParseRequestResultWrapper(fileName)]; case 2: - parseRequestResult = _b.sent(); + parseRequestResultWrapper = _b.sent(); _b.label = 3; case 3: - if (!(parseRequestResult.id !== '-1')) return [3 /*break*/, 7]; + if (!(parseRequestResultWrapper.id !== '-1')) return [3 /*break*/, 7]; previewRenameRequestConfig = { method: 'get', - url: getNewPathType.delegates.buildPreviewRenameResquestUrl(parseRequestResult), + url: getNewPathType.delegates.buildPreviewRenameResquestUrl(parseRequestResultWrapper), headers: headers, }; return [4 /*yield*/, args.deps.axios(previewRenameRequestConfig)]; @@ -203,7 +203,7 @@ var plugin = function (args) { return __awaiter(void 0, void 0, void 0, function })]; case 5: _a.isSuccessful = _b.sent(); - args.jobLog("\u2714 Renamed ".concat(getNewPathType.contentName, " ").concat(parseRequestResult.id, " : ") + args.jobLog("\u2714 Renamed ".concat(getNewPathType.contentName, " ").concat(parseRequestResultWrapper.id, " : ") + "'".concat(filePath, "' => '").concat(output.newPath, "'.")); return [3 /*break*/, 7]; case 6: @@ -221,7 +221,7 @@ var plugin = function (args) { return __awaiter(void 0, void 0, void 0, function contentName: 'movie', delegates: { getIdFromParseRequestResult: 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); }, - buildPreviewRenameResquestUrl: function (parseRequestResult) { return "".concat(arrHost, "/api/v3/rename?movieId=").concat(parseRequestResult.id); }, + buildPreviewRenameResquestUrl: function (parseRequestResultWrapper) { return "".concat(arrHost, "/api/v3/rename?movieId=").concat(parseRequestResultWrapper.id); }, getFileToRenameFromPreviewRenameRequestResult: function (previewRenameRequestResult) { var _a, _b; return ((((_b = (_a = previewRenameRequestResult.data) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0) > 0) @@ -235,21 +235,16 @@ var plugin = function (args) { return __awaiter(void 0, void 0, void 0, function contentName: 'serie', delegates: { getIdFromParseRequestResult: 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); }, - buildPreviewRenameResquestUrl: function (parseRequestResult) { - episodeNumber = parseRequestResult.requestResult.data.parsedEpisodeInfo.episodeNumbers[0]; - return "".concat(arrHost, "/api/v3/rename?") - + "seriesId=".concat(parseRequestResult.id) - + "&seasonNumber=".concat(parseRequestResult.requestResult.data.parsedEpisodeInfo.seasonNumber); + buildPreviewRenameResquestUrl: function (parseRequestResultWrapper) { + var _a, _b, _c, _d; + episodeNumber = ((_b = (_a = parseRequestResultWrapper.parseRequestResult.data.parsedEpisodeInfo) === null || _a === void 0 ? void 0 : _a.episodeNumbers) !== null && _b !== void 0 ? _b : [1])[0]; + return "".concat(arrHost, "/api/v3/rename?seriesId=").concat(parseRequestResultWrapper.id, "&seasonNumber=") + + "".concat((_d = (_c = parseRequestResultWrapper.parseRequestResult.data.parsedEpisodeInfo) === null || _c === void 0 ? void 0 : _c.seasonNumber) !== null && _d !== void 0 ? _d : 1); }, getFileToRenameFromPreviewRenameRequestResult: function (previewRenameRequestResult) { var _a, _b; return ((((_b = (_a = previewRenameRequestResult.data) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0) > 0) - ? previewRenameRequestResult.data.find(function (episodeFile) { - var _a, _b; - return ((((_b = (_a = episodeFile.episodeNumbers) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0) > 0) - ? episodeFile.episodeNumbers[0] === episodeNumber - : false); - }) + ? previewRenameRequestResult.data.find(function (episodeFile) { var _a; return ((_a = episodeFile.episodeNumbers) === null || _a === void 0 ? void 0 : _a.at(0)) === episodeNumber; }) : undefined); }, }, diff --git a/FlowPluginsTs/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.ts b/FlowPluginsTs/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.ts index 2937157..c571892 100644 --- a/FlowPluginsTs/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.ts +++ b/FlowPluginsTs/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.ts @@ -77,13 +77,39 @@ interface IFileNames { currentFileName: string } interface IParseRequestResult { - requestResult: any, + data: { + movie?: { + movieFile: { + movieId: number + }, + }, + series?: { + id: number + }, + parsedEpisodeInfo?: { + episodeNumbers: number[], + seasonNumber: number + }, + }, +} +interface IParseRequestResultWrapper { + parseRequestResult: IParseRequestResult, id: string } +interface IFileToRename { + newPath: string + episodeNumbers?: number[] +} +interface IPreviewRenameRequestResult { + data: IFileToRename[] +} interface IGetNewPathDelegates { - getIdFromParseRequestResult: (parseRequestResult: any) => string, - buildPreviewRenameResquestUrl: (parseRequestResult: IParseRequestResult) => string, - getFileToRenameFromPreviewRenameRequestResult: (previewRenameRequestResult: any) => any + getIdFromParseRequestResult: + (parseRequestResult: IParseRequestResult) => string, + buildPreviewRenameResquestUrl: + (parseRequestResult: IParseRequestResultWrapper) => string, + getFileToRenameFromPreviewRenameRequestResult: + (previewRenameRequestResult: IPreviewRenameRequestResult) => IFileToRename | undefined } interface IGetNewPathType { appName: string, @@ -128,8 +154,8 @@ const plugin = async (args: IpluginInputArgs): Promise => { Accept: 'application/json', }; - const getParseRequestResult = async (fileName: string) - : Promise => { + const getParseRequestResultWrapper = async (fileName: string) + : Promise => { // Using parse endpoint to get the movie/serie's id. const parseRequestConfig = { method: 'get', @@ -141,23 +167,23 @@ const plugin = async (args: IpluginInputArgs): Promise => { args.jobLog(id !== '-1' ? `Found ${getNewPathType.contentName} ${id} with a file named '${fileName}'` : `Didn't find ${getNewPathType.contentName} with a file named '${fileName}' in ${arrHost}.`); - return { requestResult: parseRequestResult, id }; + return { parseRequestResult, id }; }; let fileName = fileNames.originalFileName; - let parseRequestResult = await getParseRequestResult(fileName); + let parseRequestResultWrapper = await getParseRequestResultWrapper(fileName); // In case there has been a name change and the arr app already noticed it. - if (parseRequestResult.id === '-1' && fileNames.currentFileName !== fileNames.originalFileName) { + if (parseRequestResultWrapper.id === '-1' && fileNames.currentFileName !== fileNames.originalFileName) { fileName = fileNames.currentFileName; - parseRequestResult = await getParseRequestResult(fileName); + parseRequestResultWrapper = await getParseRequestResultWrapper(fileName); } // Checking that the file has been found. - if (parseRequestResult.id !== '-1') { + if (parseRequestResultWrapper.id !== '-1') { // Using rename endpoint to get ids of all the files that need renaming. const previewRenameRequestConfig = { method: 'get', - url: getNewPathType.delegates.buildPreviewRenameResquestUrl(parseRequestResult), + url: getNewPathType.delegates.buildPreviewRenameResquestUrl(parseRequestResultWrapper), headers, }; const previewRenameRequestResult = await args.deps.axios(previewRenameRequestConfig); @@ -176,7 +202,7 @@ const plugin = async (args: IpluginInputArgs): Promise => { destinationPath: output.newPath, args, }); - args.jobLog(`✔ Renamed ${getNewPathType.contentName} ${parseRequestResult.id} : ` + args.jobLog(`✔ Renamed ${getNewPathType.contentName} ${parseRequestResultWrapper.id} : ` + `'${filePath}' => '${output.newPath}'.`); } else { output.isSuccessful = true; @@ -196,7 +222,7 @@ const plugin = async (args: IpluginInputArgs): Promise => { getIdFromParseRequestResult: (parseRequestResult) => String(parseRequestResult.data?.movie?.movieFile?.movieId ?? -1), buildPreviewRenameResquestUrl: - (parseRequestResult) => `${arrHost}/api/v3/rename?movieId=${parseRequestResult.id}`, + (parseRequestResultWrapper) => `${arrHost}/api/v3/rename?movieId=${parseRequestResultWrapper.id}`, getFileToRenameFromPreviewRenameRequestResult: (previewRenameRequestResult) => (((previewRenameRequestResult.data?.length ?? 0) > 0) ? previewRenameRequestResult.data[0] @@ -210,19 +236,15 @@ const plugin = async (args: IpluginInputArgs): Promise => { getIdFromParseRequestResult: (parseRequestResult) => String(parseRequestResult.data?.series?.id ?? -1), buildPreviewRenameResquestUrl: - (parseRequestResult) => { - [episodeNumber] = parseRequestResult.requestResult.data.parsedEpisodeInfo.episodeNumbers; - return `${arrHost}/api/v3/rename?` - + `seriesId=${parseRequestResult.id}` - + `&seasonNumber=${parseRequestResult.requestResult.data.parsedEpisodeInfo.seasonNumber}`; + (parseRequestResultWrapper) => { + [episodeNumber] = parseRequestResultWrapper.parseRequestResult.data.parsedEpisodeInfo?.episodeNumbers + ?? [1]; + return `${arrHost}/api/v3/rename?seriesId=${parseRequestResultWrapper.id}&seasonNumber=` + + `${parseRequestResultWrapper.parseRequestResult.data.parsedEpisodeInfo?.seasonNumber ?? 1}`; }, getFileToRenameFromPreviewRenameRequestResult: (previewRenameRequestResult) => (((previewRenameRequestResult.data?.length ?? 0) > 0) - ? previewRenameRequestResult.data.find( - (episodeFile: { episodeNumbers: number[]; }) => (((episodeFile.episodeNumbers?.length ?? 0) > 0) - ? episodeFile.episodeNumbers[0] === episodeNumber - : false), - ) + ? previewRenameRequestResult.data.find((episodeFile) => episodeFile.episodeNumbers?.at(0) === episodeNumber) : undefined), }, }, From d654454c5eba501d1088d7c3d53f75b0753175e1 Mon Sep 17 00:00:00 2001 From: "jeanchristophe.mqt@gmail.com" Date: Fri, 29 Mar 2024 19:44:32 +0100 Subject: [PATCH 26/34] Added the possibility to fin movieId from imdbId --- .../1.0.0/index.js | 262 ++++++++-------- .../1.0.0/index.ts | 283 +++++++++--------- 2 files changed, 279 insertions(+), 266 deletions(-) diff --git a/FlowPlugins/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.js b/FlowPlugins/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.js index 2476099..383397f 100644 --- a/FlowPlugins/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.js +++ b/FlowPlugins/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.js @@ -116,149 +116,163 @@ var details = function () { return ({ ], }); }; exports.details = details; -var plugin = function (args) { return __awaiter(void 0, void 0, void 0, function () { - var lib, _a, arr, arr_api_key, arr_host, arrHost, filePath, fileNames, getNewPath, episodeNumber, getNewPathTypes, newPathOutput; - var _b, _c, _d, _e, _f, _g; +var getMovieId = function (args, arrHost, headers, fileName, getNewPathType) { return __awaiter(void 0, void 0, void 0, function () { + var imdbId, id, _a, _b; + var _c, _d, _e, _f, _g; return __generator(this, function (_h) { switch (_h.label) { + case 0: + imdbId = (_d = (_c = /\b(tt|nm|co|ev|ch|ni)\d{7,10}\b/i.exec(fileName)) === null || _c === void 0 ? void 0 : _c.at(0)) !== null && _d !== void 0 ? _d : ''; + if (!(imdbId !== '')) return [3 /*break*/, 2]; + _b = String; + return [4 /*yield*/, args.deps.axios({ + method: 'get', + url: "".concat(arrHost, "/api/v3/movie/lookup?term=imdb:").concat(imdbId), + headers: headers, + })]; + case 1: + _a = _b.apply(void 0, [(_g = (_f = (_e = (_h.sent()).data) === null || _e === void 0 ? void 0 : _e.at(0)) === null || _f === void 0 ? void 0 : _f.id) !== null && _g !== void 0 ? _g : -1]); + return [3 /*break*/, 3]; + case 2: + _a = '-1'; + _h.label = 3; + case 3: + id = _a; + args.jobLog("".concat(getNewPathType.content, " ").concat(id !== '-1' ? "".concat(id, " found") : 'not found', " for imdb '").concat(imdbId, "'")); + return [2 /*return*/, id]; + } + }); +}); }; +var getFileDetailsWrapper = function (args, arr, arrHost, headers, fileName, renameType) { return __awaiter(void 0, void 0, void 0, function () { + var fdw, _a, _b; + var _c; + return __generator(this, function (_d) { + switch (_d.label) { + case 0: + _c = {}; + if (!(arr === 'radarr')) return [3 /*break*/, 2]; + return [4 /*yield*/, getMovieId(args, arrHost, headers, fileName, renameType)]; + case 1: + _a = _d.sent(); + return [3 /*break*/, 3]; + case 2: + _a = '-1'; + _d.label = 3; + case 3: + fdw = (_c.id = _a, + _c.fileDetails = undefined, + _c); + if (!(fdw.id === '-1')) return [3 /*break*/, 5]; + _b = fdw; + return [4 /*yield*/, args.deps.axios({ + method: 'get', + url: "".concat(arrHost, "/api/v3/parse?title=").concat(encodeURIComponent((0, fileUtils_1.getFileName)(fileName))), + headers: headers, + })]; + case 4: + _b.fileDetails = _d.sent(); + fdw.id = renameType.delegates.getIdFromParseResponse(fdw); + args.jobLog("".concat(renameType.content, " ").concat(fdw.id !== '-1' ? "".concat(fdw.id, " found") : 'not found', " for '") + + "".concat((0, fileUtils_1.getFileName)(fileName), "'")); + _d.label = 5; + case 5: return [2 /*return*/, fdw]; + } + }); +}); }; +var plugin = function (args) { return __awaiter(void 0, void 0, void 0, function () { + var lib, newPath, isSuccessful, arr, arr_host, arrHost, filePath, originalFileName, currentFileName, headers, episodeNumber, renameType, fileDetailsWrapper, previewRenameRequestResult, fileToRename; + var _a, _b, _c, _d, _e, _f; + return __generator(this, function (_g) { + switch (_g.label) { case 0: lib = require('../../../../../methods/lib')(); // eslint-disable-next-line @typescript-eslint/no-unused-vars,no-param-reassign args.inputs = lib.loadDefaultValues(args.inputs, details); - _a = args.inputs, arr = _a.arr, arr_api_key = _a.arr_api_key; + newPath = ''; + isSuccessful = false; + arr = String(args.inputs.arr); arr_host = String(args.inputs.arr_host).trim(); arrHost = arr_host.endsWith('/') ? arr_host.slice(0, -1) : arr_host; - filePath = (_c = (_b = args.originalLibraryFile) === null || _b === void 0 ? void 0 : _b._id) !== null && _c !== void 0 ? _c : ''; - fileNames = { - originalFileName: (0, fileUtils_1.getFileName)((_e = (_d = args.originalLibraryFile) === null || _d === void 0 ? void 0 : _d._id) !== null && _e !== void 0 ? _e : ''), - currentFileName: (0, fileUtils_1.getFileName)((_g = (_f = args.inputFileObj) === null || _f === void 0 ? void 0 : _f._id) !== null && _g !== void 0 ? _g : ''), + filePath = (_b = (_a = args.originalLibraryFile) === null || _a === void 0 ? void 0 : _a._id) !== null && _b !== void 0 ? _b : ''; + originalFileName = (_d = (_c = args.originalLibraryFile) === null || _c === void 0 ? void 0 : _c._id) !== null && _d !== void 0 ? _d : ''; + currentFileName = (_f = (_e = args.inputFileObj) === null || _e === void 0 ? void 0 : _e._id) !== null && _f !== void 0 ? _f : ''; + headers = { + 'Content-Type': 'application/json', + 'X-Api-Key': String(args.inputs.arr_api_key), + Accept: 'application/json', }; - getNewPath = function (getNewPathType) { return __awaiter(void 0, void 0, void 0, function () { - var output, headers, getParseRequestResultWrapper, fileName, parseRequestResultWrapper, previewRenameRequestConfig, previewRenameRequestResult, fileToRename, _a; - return __generator(this, function (_b) { - switch (_b.label) { - case 0: - output = { - newPath: '', - isSuccessful: false, - }; - args.jobLog('Going to apply new name'); - args.jobLog("Renaming ".concat(getNewPathType.appName, "...")); - headers = { - 'Content-Type': 'application/json', - 'X-Api-Key': arr_api_key, - Accept: 'application/json', - }; - getParseRequestResultWrapper = function (fileName) { return __awaiter(void 0, void 0, void 0, function () { - var parseRequestConfig, parseRequestResult, id; - return __generator(this, function (_a) { - switch (_a.label) { - case 0: - parseRequestConfig = { - method: 'get', - url: "".concat(arrHost, "/api/v3/parse?title=").concat(encodeURIComponent(fileName)), - headers: headers, - }; - return [4 /*yield*/, args.deps.axios(parseRequestConfig)]; - case 1: - parseRequestResult = _a.sent(); - id = getNewPathType.delegates.getIdFromParseRequestResult(parseRequestResult); - args.jobLog(id !== '-1' - ? "Found ".concat(getNewPathType.contentName, " ").concat(id, " with a file named '").concat(fileName, "'") - : "Didn't find ".concat(getNewPathType.contentName, " with a file named '").concat(fileName, "' in ").concat(arrHost, ".")); - return [2 /*return*/, { parseRequestResult: parseRequestResult, id: id }]; - } - }); - }); }; - fileName = fileNames.originalFileName; - return [4 /*yield*/, getParseRequestResultWrapper(fileName)]; - case 1: - parseRequestResultWrapper = _b.sent(); - if (!(parseRequestResultWrapper.id === '-1' && fileNames.currentFileName !== fileNames.originalFileName)) return [3 /*break*/, 3]; - fileName = fileNames.currentFileName; - return [4 /*yield*/, getParseRequestResultWrapper(fileName)]; - case 2: - parseRequestResultWrapper = _b.sent(); - _b.label = 3; - case 3: - if (!(parseRequestResultWrapper.id !== '-1')) return [3 /*break*/, 7]; - previewRenameRequestConfig = { - method: 'get', - url: getNewPathType.delegates.buildPreviewRenameResquestUrl(parseRequestResultWrapper), - headers: headers, - }; - return [4 /*yield*/, args.deps.axios(previewRenameRequestConfig)]; - case 4: - previewRenameRequestResult = _b.sent(); - fileToRename = getNewPathType.delegates - .getFileToRenameFromPreviewRenameRequestResult(previewRenameRequestResult); - if (!(fileToRename !== undefined)) return [3 /*break*/, 6]; - output.newPath = "".concat((0, fileUtils_1.getFileAbosluteDir)(args.inputFileObj._id), "/").concat((0, fileUtils_1.getFileName)(fileToRename.newPath), ".").concat((0, fileUtils_1.getContainer)(fileToRename.newPath)); - _a = output; - return [4 /*yield*/, (0, fileMoveOrCopy_1.default)({ - operation: 'move', - sourcePath: args.inputFileObj._id, - destinationPath: output.newPath, - args: args, - })]; - case 5: - _a.isSuccessful = _b.sent(); - args.jobLog("\u2714 Renamed ".concat(getNewPathType.contentName, " ").concat(parseRequestResultWrapper.id, " : ") - + "'".concat(filePath, "' => '").concat(output.newPath, "'.")); - return [3 /*break*/, 7]; - case 6: - output.isSuccessful = true; - args.jobLog('✔ No rename necessary.'); - _b.label = 7; - case 7: return [2 /*return*/, output]; - } - }); - }); }; episodeNumber = 0; - getNewPathTypes = { - radarr: { + renameType = arr === 'radarr' + ? { appName: 'Radarr', - contentName: 'movie', + content: 'Movie', delegates: { - getIdFromParseRequestResult: 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); }, - buildPreviewRenameResquestUrl: function (parseRequestResultWrapper) { return "".concat(arrHost, "/api/v3/rename?movieId=").concat(parseRequestResultWrapper.id); }, - getFileToRenameFromPreviewRenameRequestResult: function (previewRenameRequestResult) { - var _a, _b; - return ((((_b = (_a = previewRenameRequestResult.data) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0) > 0) - ? previewRenameRequestResult.data[0] - : undefined); - }, + getIdFromParseResponse: function (fileDetailsWrapper) { var _a, _b, _c, _d; return String((_d = (_c = (_b = (_a = fileDetailsWrapper.fileDetails) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.movie) === null || _c === void 0 ? void 0 : _c.id) !== null && _d !== void 0 ? _d : -1); }, + buildPreviewRenameResquestUrl: function (fileDetailsWrapper) { return "".concat(arrHost, "/api/v3/rename?movieId=").concat(fileDetailsWrapper.id); }, + getFileToRenameFromPreviewRenameResponse: function (previewRenameResponse) { var _a; return (_a = previewRenameResponse.data) === null || _a === void 0 ? void 0 : _a.at(0); }, }, - }, - sonarr: { + } + : { appName: 'Sonarr', - contentName: 'serie', + content: 'Serie', delegates: { - getIdFromParseRequestResult: 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); }, - buildPreviewRenameResquestUrl: function (parseRequestResultWrapper) { - var _a, _b, _c, _d; - episodeNumber = ((_b = (_a = parseRequestResultWrapper.parseRequestResult.data.parsedEpisodeInfo) === null || _a === void 0 ? void 0 : _a.episodeNumbers) !== null && _b !== void 0 ? _b : [1])[0]; - return "".concat(arrHost, "/api/v3/rename?seriesId=").concat(parseRequestResultWrapper.id, "&seasonNumber=") - + "".concat((_d = (_c = parseRequestResultWrapper.parseRequestResult.data.parsedEpisodeInfo) === null || _c === void 0 ? void 0 : _c.seasonNumber) !== null && _d !== void 0 ? _d : 1); + getIdFromParseResponse: function (fileDetailsWrapper) { var _a, _b, _c, _d; return String((_d = (_c = (_b = (_a = fileDetailsWrapper.fileDetails) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.series) === null || _c === void 0 ? void 0 : _c.id) !== null && _d !== void 0 ? _d : -1); }, + buildPreviewRenameResquestUrl: function (fileDetailsWrapper) { + var _a, _b, _c, _d, _e, _f; + episodeNumber = ((_c = (_b = (_a = fileDetailsWrapper.fileDetails) === null || _a === void 0 ? void 0 : _a.data.parsedEpisodeInfo) === null || _b === void 0 ? void 0 : _b.episodeNumbers) !== null && _c !== void 0 ? _c : [1])[0]; + return "".concat(arrHost, "/api/v3/rename?seriesId=").concat(fileDetailsWrapper.id, "&seasonNumber=") + + "".concat((_f = (_e = (_d = fileDetailsWrapper.fileDetails) === null || _d === void 0 ? void 0 : _d.data.parsedEpisodeInfo) === null || _e === void 0 ? void 0 : _e.seasonNumber) !== null && _f !== void 0 ? _f : 1); }, - getFileToRenameFromPreviewRenameRequestResult: function (previewRenameRequestResult) { - var _a, _b; - return ((((_b = (_a = previewRenameRequestResult.data) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0) > 0) - ? previewRenameRequestResult.data.find(function (episodeFile) { var _a; return ((_a = episodeFile.episodeNumbers) === null || _a === void 0 ? void 0 : _a.at(0)) === episodeNumber; }) - : undefined); + getFileToRenameFromPreviewRenameResponse: function (previewRenameResponse) { + var _a; + return (_a = previewRenameResponse.data) === null || _a === void 0 ? void 0 : _a.find(function (episodeFile) { var _a; return ((_a = episodeFile.episodeNumbers) === null || _a === void 0 ? void 0 : _a.at(0)) === episodeNumber; }); }, }, - }, - }; - return [4 /*yield*/, getNewPath(arr === 'radarr' ? getNewPathTypes.radarr : getNewPathTypes.sonarr)]; + }; + args.jobLog('Going to apply new name'); + args.jobLog("Renaming ".concat(renameType.appName, "...")); + return [4 /*yield*/, getFileDetailsWrapper(args, arr, arrHost, headers, originalFileName, renameType)]; case 1: - newPathOutput = _h.sent(); - return [2 /*return*/, { - outputFileObj: newPathOutput.isSuccessful && newPathOutput.newPath !== '' - ? __assign(__assign({}, args.inputFileObj), { _id: newPathOutput.newPath }) : args.inputFileObj, - outputNumber: newPathOutput.isSuccessful ? 1 : 2, - variables: args.variables, - }]; + fileDetailsWrapper = _g.sent(); + if (!(fileDetailsWrapper.id === '-1' && currentFileName !== originalFileName)) return [3 /*break*/, 3]; + return [4 /*yield*/, getFileDetailsWrapper(args, arr, arrHost, headers, currentFileName, renameType)]; + case 2: + fileDetailsWrapper = _g.sent(); + _g.label = 3; + case 3: + if (!(fileDetailsWrapper.id !== '-1')) return [3 /*break*/, 7]; + return [4 /*yield*/, args.deps.axios({ + method: 'get', + url: renameType.delegates.buildPreviewRenameResquestUrl(fileDetailsWrapper), + headers: headers, + })]; + case 4: + previewRenameRequestResult = _g.sent(); + fileToRename = renameType.delegates + .getFileToRenameFromPreviewRenameResponse(previewRenameRequestResult); + if (!(fileToRename !== undefined)) return [3 /*break*/, 6]; + newPath = "".concat((0, fileUtils_1.getFileAbosluteDir)(args.inputFileObj._id), "/").concat((0, fileUtils_1.getFileName)(fileToRename.newPath), ".").concat((0, fileUtils_1.getContainer)(fileToRename.newPath)); + return [4 /*yield*/, (0, fileMoveOrCopy_1.default)({ + operation: 'move', + sourcePath: args.inputFileObj._id, + destinationPath: newPath, + args: args, + })]; + case 5: + isSuccessful = _g.sent(); + args.jobLog("\u2714 ".concat(renameType.content, " renamed ").concat(fileDetailsWrapper.id, " : ") + + "'".concat(filePath, "' => '").concat(newPath, "'.")); + return [3 /*break*/, 7]; + case 6: + isSuccessful = true; + args.jobLog('✔ No rename necessary.'); + _g.label = 7; + case 7: return [2 /*return*/, { + outputFileObj: isSuccessful && newPath !== '' + ? __assign(__assign({}, args.inputFileObj), { _id: newPath }) : args.inputFileObj, + outputNumber: isSuccessful ? 1 : 2, + variables: args.variables, + }]; } }); }); }; diff --git a/FlowPluginsTs/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.ts b/FlowPluginsTs/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.ts index c571892..83bbc83 100644 --- a/FlowPluginsTs/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.ts +++ b/FlowPluginsTs/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.ts @@ -72,191 +72,190 @@ const details = (): IpluginDetails => ({ ], }); -interface IFileNames { - originalFileName: string, - currentFileName: string +interface IHTTPHeaders { + 'Content-Type': string, + 'X-Api-Key': string, + Accept: string, } -interface IParseRequestResult { +interface IFileDetails { data: { - movie?: { - movieFile: { - movieId: number - }, - }, - series?: { - id: number - }, + movie?: { id: number }, + series?: { id: number }, parsedEpisodeInfo?: { episodeNumbers: number[], seasonNumber: number }, }, } -interface IParseRequestResultWrapper { - parseRequestResult: IParseRequestResult, +interface IFileDetailsWrapper { id: string + fileDetails?: IFileDetails } interface IFileToRename { newPath: string episodeNumbers?: number[] } -interface IPreviewRenameRequestResult { +interface IPreviewRenameResponse { data: IFileToRename[] } -interface IGetNewPathDelegates { - getIdFromParseRequestResult: - (parseRequestResult: IParseRequestResult) => string, - buildPreviewRenameResquestUrl: - (parseRequestResult: IParseRequestResultWrapper) => string, - getFileToRenameFromPreviewRenameRequestResult: - (previewRenameRequestResult: IPreviewRenameRequestResult) => IFileToRename | undefined -} -interface IGetNewPathType { +interface IRenameType { appName: string, - contentName: string, - delegates: IGetNewPathDelegates -} -interface IGetNewPathTypes { - radarr: IGetNewPathType, - sonarr: IGetNewPathType -} -interface IGetNewPathOutput { - newPath: string, - isSuccessful: boolean + content: string, + delegates: { + getIdFromParseResponse: + (fileDetailsWrapper: IFileDetailsWrapper) => string, + buildPreviewRenameResquestUrl: + (fileDetailsWrapper: IFileDetailsWrapper) => string, + getFileToRenameFromPreviewRenameResponse: + (previewRenameResponse: IPreviewRenameResponse) => IFileToRename | undefined + } } +const getMovieId = async ( + args: IpluginInputArgs, + arrHost: string, + headers: IHTTPHeaders, + fileName: string, + getNewPathType: IRenameType, +) + : Promise => { + const imdbId = /\b(tt|nm|co|ev|ch|ni)\d{7,10}\b/i.exec(fileName)?.at(0) ?? ''; + const id = (imdbId !== '') + ? String( + (await args.deps.axios({ + method: 'get', + url: `${arrHost}/api/v3/movie/lookup?term=imdb:${imdbId}`, + headers, + })).data?.at(0)?.id ?? -1, + ) + : '-1'; + args.jobLog(`${getNewPathType.content} ${id !== '-1' ? `${id} found` : 'not found'} for imdb '${imdbId}'`); + return id; +}; + +const getFileDetailsWrapper = async ( + args: IpluginInputArgs, + arr: string, + arrHost: string, + headers: IHTTPHeaders, + fileName: string, + renameType: IRenameType, +) + : Promise => { + const fdw: IFileDetailsWrapper = { + id: arr === 'radarr' ? await getMovieId(args, arrHost, headers, fileName, renameType) : '-1', + fileDetails: undefined, + }; + if (fdw.id === '-1') { + fdw.fileDetails = await args.deps.axios({ + method: 'get', + url: `${arrHost}/api/v3/parse?title=${encodeURIComponent(getFileName(fileName))}`, + headers, + }); + fdw.id = renameType.delegates.getIdFromParseResponse(fdw); + args.jobLog(`${renameType.content} ${fdw.id !== '-1' ? `${fdw.id} found` : 'not found'} for '` + + `${getFileName(fileName)}'`); + } + return fdw; +}; + const plugin = async (args: IpluginInputArgs): Promise => { const lib = require('../../../../../methods/lib')(); // eslint-disable-next-line @typescript-eslint/no-unused-vars,no-param-reassign args.inputs = lib.loadDefaultValues(args.inputs, details); - const { arr, arr_api_key } = args.inputs; + let newPath = ''; + let isSuccessful = false; + const arr = String(args.inputs.arr); const arr_host = String(args.inputs.arr_host).trim(); const arrHost = arr_host.endsWith('/') ? arr_host.slice(0, -1) : arr_host; const filePath = args.originalLibraryFile?._id ?? ''; - const fileNames: IFileNames = { - originalFileName: getFileName(args.originalLibraryFile?._id ?? ''), - currentFileName: getFileName(args.inputFileObj?._id ?? ''), - }; - - const getNewPath = async (getNewPathType: IGetNewPathType) - : Promise => { - const output: IGetNewPathOutput = { - newPath: '', - isSuccessful: false, - }; - args.jobLog('Going to apply new name'); - args.jobLog(`Renaming ${getNewPathType.appName}...`); - - const headers = { - 'Content-Type': 'application/json', - 'X-Api-Key': arr_api_key, - Accept: 'application/json', - }; - - const getParseRequestResultWrapper = async (fileName: string) - : Promise => { - // Using parse endpoint to get the movie/serie's id. - const parseRequestConfig = { - method: 'get', - url: `${arrHost}/api/v3/parse?title=${encodeURIComponent(fileName)}`, - headers, - }; - const parseRequestResult = await args.deps.axios(parseRequestConfig); - const id = getNewPathType.delegates.getIdFromParseRequestResult(parseRequestResult); - args.jobLog(id !== '-1' - ? `Found ${getNewPathType.contentName} ${id} with a file named '${fileName}'` - : `Didn't find ${getNewPathType.contentName} with a file named '${fileName}' in ${arrHost}.`); - return { parseRequestResult, id }; - }; - - let fileName = fileNames.originalFileName; - let parseRequestResultWrapper = await getParseRequestResultWrapper(fileName); - // In case there has been a name change and the arr app already noticed it. - if (parseRequestResultWrapper.id === '-1' && fileNames.currentFileName !== fileNames.originalFileName) { - fileName = fileNames.currentFileName; - parseRequestResultWrapper = await getParseRequestResultWrapper(fileName); - } - - // Checking that the file has been found. - if (parseRequestResultWrapper.id !== '-1') { - // Using rename endpoint to get ids of all the files that need renaming. - const previewRenameRequestConfig = { - method: 'get', - url: getNewPathType.delegates.buildPreviewRenameResquestUrl(parseRequestResultWrapper), - headers, - }; - const previewRenameRequestResult = await args.deps.axios(previewRenameRequestConfig); - const fileToRename = getNewPathType.delegates - .getFileToRenameFromPreviewRenameRequestResult(previewRenameRequestResult); - - // Only if there is a rename to execute - if (fileToRename !== undefined) { - output.newPath = `${getFileAbosluteDir(args.inputFileObj._id) - }/${getFileName(fileToRename.newPath) - }.${getContainer(fileToRename.newPath)}`; - - output.isSuccessful = await fileMoveOrCopy({ - operation: 'move', - sourcePath: args.inputFileObj._id, - destinationPath: output.newPath, - args, - }); - args.jobLog(`✔ Renamed ${getNewPathType.contentName} ${parseRequestResultWrapper.id} : ` - + `'${filePath}' => '${output.newPath}'.`); - } else { - output.isSuccessful = true; - args.jobLog('✔ No rename necessary.'); - } - } - - return output; + const originalFileName = args.originalLibraryFile?._id ?? ''; + const currentFileName = args.inputFileObj?._id ?? ''; + const headers = { + 'Content-Type': 'application/json', + 'X-Api-Key': String(args.inputs.arr_api_key), + Accept: 'application/json', }; let episodeNumber = 0; - const getNewPathTypes: IGetNewPathTypes = { - radarr: { + const renameType: IRenameType = arr === 'radarr' + ? { appName: 'Radarr', - contentName: 'movie', + content: 'Movie', delegates: { - getIdFromParseRequestResult: - (parseRequestResult) => String(parseRequestResult.data?.movie?.movieFile?.movieId ?? -1), + getIdFromParseResponse: + (fileDetailsWrapper) => String(fileDetailsWrapper.fileDetails?.data?.movie?.id ?? -1), buildPreviewRenameResquestUrl: - (parseRequestResultWrapper) => `${arrHost}/api/v3/rename?movieId=${parseRequestResultWrapper.id}`, - getFileToRenameFromPreviewRenameRequestResult: - (previewRenameRequestResult) => (((previewRenameRequestResult.data?.length ?? 0) > 0) - ? previewRenameRequestResult.data[0] - : undefined), + (fileDetailsWrapper) => `${arrHost}/api/v3/rename?movieId=${fileDetailsWrapper.id}`, + getFileToRenameFromPreviewRenameResponse: + (previewRenameResponse) => previewRenameResponse.data?.at(0), }, - }, - sonarr: { + } + : { appName: 'Sonarr', - contentName: 'serie', + content: 'Serie', delegates: { - getIdFromParseRequestResult: - (parseRequestResult) => String(parseRequestResult.data?.series?.id ?? -1), + getIdFromParseResponse: + (fileDetailsWrapper) => String(fileDetailsWrapper.fileDetails?.data?.series?.id ?? -1), buildPreviewRenameResquestUrl: - (parseRequestResultWrapper) => { - [episodeNumber] = parseRequestResultWrapper.parseRequestResult.data.parsedEpisodeInfo?.episodeNumbers - ?? [1]; - return `${arrHost}/api/v3/rename?seriesId=${parseRequestResultWrapper.id}&seasonNumber=` - + `${parseRequestResultWrapper.parseRequestResult.data.parsedEpisodeInfo?.seasonNumber ?? 1}`; + (fileDetailsWrapper) => { + [episodeNumber] = fileDetailsWrapper.fileDetails?.data.parsedEpisodeInfo?.episodeNumbers ?? [1]; + return `${arrHost}/api/v3/rename?seriesId=${fileDetailsWrapper.id}&seasonNumber=` + + `${fileDetailsWrapper.fileDetails?.data.parsedEpisodeInfo?.seasonNumber ?? 1}`; }, - getFileToRenameFromPreviewRenameRequestResult: - (previewRenameRequestResult) => (((previewRenameRequestResult.data?.length ?? 0) > 0) - ? previewRenameRequestResult.data.find((episodeFile) => episodeFile.episodeNumbers?.at(0) === episodeNumber) - : undefined), + getFileToRenameFromPreviewRenameResponse: + (previewRenameResponse) => previewRenameResponse.data + ?.find((episodeFile) => episodeFile.episodeNumbers?.at(0) === episodeNumber), }, - }, - }; - const newPathOutput = await getNewPath(arr === 'radarr' ? getNewPathTypes.radarr : getNewPathTypes.sonarr); + }; + + args.jobLog('Going to apply new name'); + args.jobLog(`Renaming ${renameType.appName}...`); + + let fileDetailsWrapper = await getFileDetailsWrapper(args, arr, arrHost, headers, originalFileName, renameType); + // Useful in some edge cases + if (fileDetailsWrapper.id === '-1' && currentFileName !== originalFileName) { + fileDetailsWrapper = await getFileDetailsWrapper(args, arr, arrHost, headers, currentFileName, renameType); + } + + // Checking that the file has been found + if (fileDetailsWrapper.id !== '-1') { + // Using rename endpoint to get ids of all the files that need renaming + const previewRenameRequestResult = await args.deps.axios({ + method: 'get', + url: renameType.delegates.buildPreviewRenameResquestUrl(fileDetailsWrapper), + headers, + }); + const fileToRename = renameType.delegates + .getFileToRenameFromPreviewRenameResponse(previewRenameRequestResult); + + // Only if there is a rename to execute + if (fileToRename !== undefined) { + newPath = `${getFileAbosluteDir(args.inputFileObj._id) + }/${getFileName(fileToRename.newPath) + }.${getContainer(fileToRename.newPath)}`; + + isSuccessful = await fileMoveOrCopy({ + operation: 'move', + sourcePath: args.inputFileObj._id, + destinationPath: newPath, + args, + }); + args.jobLog(`✔ ${renameType.content} renamed ${fileDetailsWrapper.id} : ` + + `'${filePath}' => '${newPath}'.`); + } else { + isSuccessful = true; + args.jobLog('✔ No rename necessary.'); + } + } return { outputFileObj: - newPathOutput.isSuccessful && newPathOutput.newPath !== '' - ? { ...args.inputFileObj, _id: newPathOutput.newPath } + isSuccessful && newPath !== '' + ? { ...args.inputFileObj, _id: newPath } : args.inputFileObj, - outputNumber: newPathOutput.isSuccessful ? 1 : 2, + outputNumber: isSuccessful ? 1 : 2, variables: args.variables, }; }; From 22a474104a690c45355982a979333cd469a34f12 Mon Sep 17 00:00:00 2001 From: "jeanchristophe.mqt@gmail.com" Date: Fri, 29 Mar 2024 19:58:08 +0100 Subject: [PATCH 27/34] Log corrected --- .../tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.js | 2 +- .../tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/FlowPlugins/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.js b/FlowPlugins/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.js index 383397f..3f31cec 100644 --- a/FlowPlugins/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.js +++ b/FlowPlugins/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.js @@ -260,7 +260,7 @@ var plugin = function (args) { return __awaiter(void 0, void 0, void 0, function })]; case 5: isSuccessful = _g.sent(); - args.jobLog("\u2714 ".concat(renameType.content, " renamed ").concat(fileDetailsWrapper.id, " : ") + args.jobLog("\u2714 ".concat(renameType.content, " ").concat(fileDetailsWrapper.id, " renamed : ") + "'".concat(filePath, "' => '").concat(newPath, "'.")); return [3 /*break*/, 7]; case 6: diff --git a/FlowPluginsTs/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.ts b/FlowPluginsTs/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.ts index 83bbc83..1124bb6 100644 --- a/FlowPluginsTs/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.ts +++ b/FlowPluginsTs/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.ts @@ -242,7 +242,7 @@ const plugin = async (args: IpluginInputArgs): Promise => { destinationPath: newPath, args, }); - args.jobLog(`✔ ${renameType.content} renamed ${fileDetailsWrapper.id} : ` + args.jobLog(`✔ ${renameType.content} ${fileDetailsWrapper.id} renamed : ` + `'${filePath}' => '${newPath}'.`); } else { isSuccessful = true; From b85f59da2c20709ca1b548c64fce4cc804cb186a Mon Sep 17 00:00:00 2001 From: "jeanchristophe.mqt@gmail.com" Date: Sat, 30 Mar 2024 10:45:01 +0100 Subject: [PATCH 28/34] Minor corrections --- .../1.0.0/index.js | 31 +++++++++---------- .../1.0.0/index.ts | 7 ++--- 2 files changed, 18 insertions(+), 20 deletions(-) diff --git a/FlowPlugins/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.js b/FlowPlugins/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.js index 3f31cec..10095bd 100644 --- a/FlowPlugins/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.js +++ b/FlowPlugins/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.js @@ -180,10 +180,10 @@ var getFileDetailsWrapper = function (args, arr, arrHost, headers, fileName, ren }); }); }; var plugin = function (args) { return __awaiter(void 0, void 0, void 0, function () { - var lib, newPath, isSuccessful, arr, arr_host, arrHost, filePath, originalFileName, currentFileName, headers, episodeNumber, renameType, fileDetailsWrapper, previewRenameRequestResult, fileToRename; - var _a, _b, _c, _d, _e, _f; - return __generator(this, function (_g) { - switch (_g.label) { + var lib, newPath, isSuccessful, arr, arr_host, arrHost, originalFileName, currentFileName, headers, episodeNumber, renameType, fileDetailsWrapper, previewRenameRequestResult, fileToRename; + var _a, _b, _c, _d; + return __generator(this, function (_e) { + switch (_e.label) { case 0: lib = require('../../../../../methods/lib')(); // eslint-disable-next-line @typescript-eslint/no-unused-vars,no-param-reassign @@ -193,9 +193,8 @@ var plugin = function (args) { return __awaiter(void 0, void 0, void 0, function arr = String(args.inputs.arr); arr_host = String(args.inputs.arr_host).trim(); arrHost = arr_host.endsWith('/') ? arr_host.slice(0, -1) : arr_host; - filePath = (_b = (_a = args.originalLibraryFile) === null || _a === void 0 ? void 0 : _a._id) !== null && _b !== void 0 ? _b : ''; - originalFileName = (_d = (_c = args.originalLibraryFile) === null || _c === void 0 ? void 0 : _c._id) !== null && _d !== void 0 ? _d : ''; - currentFileName = (_f = (_e = args.inputFileObj) === null || _e === void 0 ? void 0 : _e._id) !== null && _f !== void 0 ? _f : ''; + originalFileName = (_b = (_a = args.originalLibraryFile) === null || _a === void 0 ? void 0 : _a._id) !== null && _b !== void 0 ? _b : ''; + currentFileName = (_d = (_c = args.inputFileObj) === null || _c === void 0 ? void 0 : _c._id) !== null && _d !== void 0 ? _d : ''; headers = { 'Content-Type': 'application/json', 'X-Api-Key': String(args.inputs.arr_api_key), @@ -233,12 +232,12 @@ var plugin = function (args) { return __awaiter(void 0, void 0, void 0, function args.jobLog("Renaming ".concat(renameType.appName, "...")); return [4 /*yield*/, getFileDetailsWrapper(args, arr, arrHost, headers, originalFileName, renameType)]; case 1: - fileDetailsWrapper = _g.sent(); + fileDetailsWrapper = _e.sent(); if (!(fileDetailsWrapper.id === '-1' && currentFileName !== originalFileName)) return [3 /*break*/, 3]; return [4 /*yield*/, getFileDetailsWrapper(args, arr, arrHost, headers, currentFileName, renameType)]; case 2: - fileDetailsWrapper = _g.sent(); - _g.label = 3; + fileDetailsWrapper = _e.sent(); + _e.label = 3; case 3: if (!(fileDetailsWrapper.id !== '-1')) return [3 /*break*/, 7]; return [4 /*yield*/, args.deps.axios({ @@ -247,26 +246,26 @@ var plugin = function (args) { return __awaiter(void 0, void 0, void 0, function headers: headers, })]; case 4: - previewRenameRequestResult = _g.sent(); + previewRenameRequestResult = _e.sent(); fileToRename = renameType.delegates .getFileToRenameFromPreviewRenameResponse(previewRenameRequestResult); if (!(fileToRename !== undefined)) return [3 /*break*/, 6]; - newPath = "".concat((0, fileUtils_1.getFileAbosluteDir)(args.inputFileObj._id), "/").concat((0, fileUtils_1.getFileName)(fileToRename.newPath), ".").concat((0, fileUtils_1.getContainer)(fileToRename.newPath)); + newPath = "".concat((0, fileUtils_1.getFileAbosluteDir)(currentFileName), "/").concat((0, fileUtils_1.getFileName)(fileToRename.newPath), ".").concat((0, fileUtils_1.getContainer)(fileToRename.newPath)); return [4 /*yield*/, (0, fileMoveOrCopy_1.default)({ operation: 'move', - sourcePath: args.inputFileObj._id, + sourcePath: currentFileName, destinationPath: newPath, args: args, })]; case 5: - isSuccessful = _g.sent(); + isSuccessful = _e.sent(); args.jobLog("\u2714 ".concat(renameType.content, " ").concat(fileDetailsWrapper.id, " renamed : ") - + "'".concat(filePath, "' => '").concat(newPath, "'.")); + + "'".concat(originalFileName, "' => '").concat(newPath, "'.")); return [3 /*break*/, 7]; case 6: isSuccessful = true; args.jobLog('✔ No rename necessary.'); - _g.label = 7; + _e.label = 7; case 7: return [2 /*return*/, { outputFileObj: isSuccessful && newPath !== '' ? __assign(__assign({}, args.inputFileObj), { _id: newPath }) : args.inputFileObj, diff --git a/FlowPluginsTs/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.ts b/FlowPluginsTs/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.ts index 1124bb6..79adbe5 100644 --- a/FlowPluginsTs/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.ts +++ b/FlowPluginsTs/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.ts @@ -169,7 +169,6 @@ const plugin = async (args: IpluginInputArgs): Promise => { const arr = String(args.inputs.arr); const arr_host = String(args.inputs.arr_host).trim(); const arrHost = arr_host.endsWith('/') ? arr_host.slice(0, -1) : arr_host; - const filePath = args.originalLibraryFile?._id ?? ''; const originalFileName = args.originalLibraryFile?._id ?? ''; const currentFileName = args.inputFileObj?._id ?? ''; const headers = { @@ -232,18 +231,18 @@ const plugin = async (args: IpluginInputArgs): Promise => { // Only if there is a rename to execute if (fileToRename !== undefined) { - newPath = `${getFileAbosluteDir(args.inputFileObj._id) + newPath = `${getFileAbosluteDir(currentFileName) }/${getFileName(fileToRename.newPath) }.${getContainer(fileToRename.newPath)}`; isSuccessful = await fileMoveOrCopy({ operation: 'move', - sourcePath: args.inputFileObj._id, + sourcePath: currentFileName, destinationPath: newPath, args, }); args.jobLog(`✔ ${renameType.content} ${fileDetailsWrapper.id} renamed : ` - + `'${filePath}' => '${newPath}'.`); + + `'${originalFileName}' => '${newPath}'.`); } else { isSuccessful = true; args.jobLog('✔ No rename necessary.'); From ecb519576a4dbbe490fdd0dc135d7b3dd4587b97 Mon Sep 17 00:00:00 2001 From: "jeanchristophe.mqt@gmail.com" Date: Sat, 30 Mar 2024 22:57:15 +0100 Subject: [PATCH 29/34] Added the possibility to find serieId from imdbId --- .../1.0.0/index.js | 138 ++++++++-------- .../1.0.0/index.ts | 155 +++++++++++------- 2 files changed, 165 insertions(+), 128 deletions(-) diff --git a/FlowPlugins/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.js b/FlowPlugins/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.js index 10095bd..187a609 100644 --- a/FlowPlugins/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.js +++ b/FlowPlugins/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.js @@ -116,71 +116,65 @@ var details = function () { return ({ ], }); }; exports.details = details; -var getMovieId = function (args, arrHost, headers, fileName, getNewPathType) { return __awaiter(void 0, void 0, void 0, function () { - var imdbId, id, _a, _b; - var _c, _d, _e, _f, _g; - return __generator(this, function (_h) { - switch (_h.label) { +var getFileInfoFromLookup = function (args, arr, arrHost, headers, fileName, renameType) { return __awaiter(void 0, void 0, void 0, function () { + var fInfo, imdbId, lookupResponse; + var _a, _b; + return __generator(this, function (_c) { + switch (_c.label) { case 0: - imdbId = (_d = (_c = /\b(tt|nm|co|ev|ch|ni)\d{7,10}\b/i.exec(fileName)) === null || _c === void 0 ? void 0 : _c.at(0)) !== null && _d !== void 0 ? _d : ''; + fInfo = { id: '-1' }; + imdbId = (_b = (_a = /\b(tt|nm|co|ev|ch|ni)\d{7,10}\b/i.exec(fileName)) === null || _a === void 0 ? void 0 : _a.at(0)) !== null && _b !== void 0 ? _b : ''; if (!(imdbId !== '')) return [3 /*break*/, 2]; - _b = String; return [4 /*yield*/, args.deps.axios({ method: 'get', - url: "".concat(arrHost, "/api/v3/movie/lookup?term=imdb:").concat(imdbId), + url: "".concat(arrHost, "/api/v3/").concat(arr === 'radarr' ? 'movie' : 'series', "/lookup?term=imdb:").concat(imdbId), headers: headers, })]; case 1: - _a = _b.apply(void 0, [(_g = (_f = (_e = (_h.sent()).data) === null || _e === void 0 ? void 0 : _e.at(0)) === null || _f === void 0 ? void 0 : _f.id) !== null && _g !== void 0 ? _g : -1]); - return [3 /*break*/, 3]; - case 2: - _a = '-1'; - _h.label = 3; - case 3: - id = _a; - args.jobLog("".concat(getNewPathType.content, " ").concat(id !== '-1' ? "".concat(id, " found") : 'not found', " for imdb '").concat(imdbId, "'")); - return [2 /*return*/, id]; + lookupResponse = _c.sent(); + fInfo = renameType.delegates.getFileInfoFromLookupResponse(lookupResponse, fileName); + args.jobLog("".concat(renameType.content, " ").concat(fInfo.id !== '-1' ? "'".concat(fInfo.id, "' found") : 'not found') + + " for imdb '".concat(imdbId, "'")); + _c.label = 2; + case 2: return [2 /*return*/, fInfo]; } }); }); }; -var getFileDetailsWrapper = function (args, arr, arrHost, headers, fileName, renameType) { return __awaiter(void 0, void 0, void 0, function () { - var fdw, _a, _b; - var _c; - return __generator(this, function (_d) { - switch (_d.label) { +var getFileInfoFromParse = function (args, arr, arrHost, headers, fileName, renameType) { return __awaiter(void 0, void 0, void 0, function () { + var fInfo, parseResponse; + return __generator(this, function (_a) { + switch (_a.label) { case 0: - _c = {}; - if (!(arr === 'radarr')) return [3 /*break*/, 2]; - return [4 /*yield*/, getMovieId(args, arrHost, headers, fileName, renameType)]; - case 1: - _a = _d.sent(); - return [3 /*break*/, 3]; - case 2: - _a = '-1'; - _d.label = 3; - case 3: - fdw = (_c.id = _a, - _c.fileDetails = undefined, - _c); - if (!(fdw.id === '-1')) return [3 /*break*/, 5]; - _b = fdw; + fInfo = { id: '-1' }; return [4 /*yield*/, args.deps.axios({ method: 'get', url: "".concat(arrHost, "/api/v3/parse?title=").concat(encodeURIComponent((0, fileUtils_1.getFileName)(fileName))), headers: headers, })]; - case 4: - _b.fileDetails = _d.sent(); - fdw.id = renameType.delegates.getIdFromParseResponse(fdw); - args.jobLog("".concat(renameType.content, " ").concat(fdw.id !== '-1' ? "".concat(fdw.id, " found") : 'not found', " for '") - + "".concat((0, fileUtils_1.getFileName)(fileName), "'")); - _d.label = 5; - case 5: return [2 /*return*/, fdw]; + case 1: + parseResponse = _a.sent(); + fInfo = renameType.delegates.getFileInfoFromParseResponse(parseResponse); + args.jobLog("".concat(renameType.content, " ").concat(fInfo.id !== '-1' ? "'".concat(fInfo.id, "' found") : 'not found') + + " for '".concat((0, fileUtils_1.getFileName)(fileName), "'")); + return [2 /*return*/, fInfo]; + } + }); +}); }; +var getFileInfo = function (args, arr, arrHost, headers, fileName, renameType) { return __awaiter(void 0, void 0, void 0, function () { + var fInfo; + return __generator(this, function (_a) { + switch (_a.label) { + case 0: return [4 /*yield*/, getFileInfoFromLookup(args, arr, arrHost, headers, fileName, renameType)]; + case 1: + fInfo = _a.sent(); + return [2 /*return*/, (fInfo.id === '-1' || (arr === 'sonarr' && (fInfo.seasonNumber === -1 || fInfo.episodeNumber === -1))) + ? getFileInfoFromParse(args, arr, arrHost, headers, fileName, renameType) + : fInfo]; } }); }); }; var plugin = function (args) { return __awaiter(void 0, void 0, void 0, function () { - var lib, newPath, isSuccessful, arr, arr_host, arrHost, originalFileName, currentFileName, headers, episodeNumber, renameType, fileDetailsWrapper, previewRenameRequestResult, fileToRename; + var lib, newPath, isSuccessful, arr, arr_host, arrHost, originalFileName, currentFileName, headers, renameType, fInfo, previewRenameRequestResult, fileToRename; var _a, _b, _c, _d; return __generator(this, function (_e) { switch (_e.label) { @@ -200,14 +194,14 @@ var plugin = function (args) { return __awaiter(void 0, void 0, void 0, function 'X-Api-Key': String(args.inputs.arr_api_key), Accept: 'application/json', }; - episodeNumber = 0; renameType = arr === 'radarr' ? { appName: 'Radarr', content: 'Movie', delegates: { - getIdFromParseResponse: function (fileDetailsWrapper) { var _a, _b, _c, _d; return String((_d = (_c = (_b = (_a = fileDetailsWrapper.fileDetails) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.movie) === null || _c === void 0 ? void 0 : _c.id) !== null && _d !== void 0 ? _d : -1); }, - buildPreviewRenameResquestUrl: function (fileDetailsWrapper) { return "".concat(arrHost, "/api/v3/rename?movieId=").concat(fileDetailsWrapper.id); }, + getFileInfoFromLookupResponse: function (lookupResponse) { var _a, _b, _c; return ({ id: String((_c = (_b = (_a = lookupResponse === null || lookupResponse === void 0 ? void 0 : lookupResponse.data) === null || _a === void 0 ? void 0 : _a.at(0)) === null || _b === void 0 ? void 0 : _b.id) !== null && _c !== void 0 ? _c : -1) }); }, + getFileInfoFromParseResponse: function (parseResponse) { var _a, _b, _c; return ({ id: String((_c = (_b = (_a = parseResponse === null || parseResponse === void 0 ? void 0 : parseResponse.data) === null || _a === void 0 ? void 0 : _a.movie) === null || _b === void 0 ? void 0 : _b.id) !== null && _c !== void 0 ? _c : -1) }); }, + buildPreviewRenameResquestUrl: function (fInfo) { return "".concat(arrHost, "/api/v3/rename?movieId=").concat(fInfo.id); }, getFileToRenameFromPreviewRenameResponse: function (previewRenameResponse) { var _a; return (_a = previewRenameResponse.data) === null || _a === void 0 ? void 0 : _a.at(0); }, }, } @@ -215,40 +209,54 @@ var plugin = function (args) { return __awaiter(void 0, void 0, void 0, function appName: 'Sonarr', content: 'Serie', delegates: { - getIdFromParseResponse: function (fileDetailsWrapper) { var _a, _b, _c, _d; return String((_d = (_c = (_b = (_a = fileDetailsWrapper.fileDetails) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.series) === null || _c === void 0 ? void 0 : _c.id) !== null && _d !== void 0 ? _d : -1); }, - buildPreviewRenameResquestUrl: function (fileDetailsWrapper) { - var _a, _b, _c, _d, _e, _f; - episodeNumber = ((_c = (_b = (_a = fileDetailsWrapper.fileDetails) === null || _a === void 0 ? void 0 : _a.data.parsedEpisodeInfo) === null || _b === void 0 ? void 0 : _b.episodeNumbers) !== null && _c !== void 0 ? _c : [1])[0]; - return "".concat(arrHost, "/api/v3/rename?seriesId=").concat(fileDetailsWrapper.id, "&seasonNumber=") - + "".concat((_f = (_e = (_d = fileDetailsWrapper.fileDetails) === null || _d === void 0 ? void 0 : _d.data.parsedEpisodeInfo) === null || _e === void 0 ? void 0 : _e.seasonNumber) !== null && _f !== void 0 ? _f : 1); + getFileInfoFromLookupResponse: function (lookupResponse, fileName) { + var _a, _b, _c, _d, _e, _f, _g, _h, _j; + var fInfo = { id: String((_c = (_b = (_a = lookupResponse === null || lookupResponse === void 0 ? void 0 : lookupResponse.data) === null || _a === void 0 ? void 0 : _a.at(0)) === null || _b === void 0 ? void 0 : _b.id) !== null && _c !== void 0 ? _c : -1) }; + if (fInfo.id !== '-1') { + var seasonEpisodenumber = (_e = (_d = /\bS\d{1,3}E\d{1,4}\b/i.exec(fileName)) === null || _d === void 0 ? void 0 : _d.at(0)) !== null && _e !== void 0 ? _e : ''; + var episodeNumber = (_g = (_f = /\d{1,4}$/i.exec(seasonEpisodenumber)) === null || _f === void 0 ? void 0 : _f.at(0)) !== null && _g !== void 0 ? _g : ''; + fInfo.seasonNumber = Number((_j = (_h = /\d{1,3}/i + .exec(seasonEpisodenumber.slice(0, -episodeNumber.length))) === null || _h === void 0 ? void 0 : _h.at(0)) !== null && _j !== void 0 ? _j : '-1'); + fInfo.episodeNumber = Number(episodeNumber !== '' ? episodeNumber : -1); + } + return fInfo; + }, + getFileInfoFromParseResponse: function (parseResponse) { + var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k; + return ({ + id: String((_c = (_b = (_a = parseResponse === null || parseResponse === void 0 ? void 0 : parseResponse.data) === null || _a === void 0 ? void 0 : _a.series) === null || _b === void 0 ? void 0 : _b.id) !== null && _c !== void 0 ? _c : -1), + seasonNumber: (_f = (_e = (_d = parseResponse === null || parseResponse === void 0 ? void 0 : parseResponse.data) === null || _d === void 0 ? void 0 : _d.parsedEpisodeInfo) === null || _e === void 0 ? void 0 : _e.seasonNumber) !== null && _f !== void 0 ? _f : 1, + episodeNumber: (_k = (_j = (_h = (_g = parseResponse === null || parseResponse === void 0 ? void 0 : parseResponse.data) === null || _g === void 0 ? void 0 : _g.parsedEpisodeInfo) === null || _h === void 0 ? void 0 : _h.episodeNumbers) === null || _j === void 0 ? void 0 : _j.at(0)) !== null && _k !== void 0 ? _k : 1, + }); }, - getFileToRenameFromPreviewRenameResponse: function (previewRenameResponse) { + buildPreviewRenameResquestUrl: function (fInfo) { return "".concat(arrHost, "/api/v3/rename?seriesId=").concat(fInfo.id, "&seasonNumber=").concat(fInfo.seasonNumber); }, + getFileToRenameFromPreviewRenameResponse: function (previewRenameResponse, fInfo) { var _a; - return (_a = previewRenameResponse.data) === null || _a === void 0 ? void 0 : _a.find(function (episodeFile) { var _a; return ((_a = episodeFile.episodeNumbers) === null || _a === void 0 ? void 0 : _a.at(0)) === episodeNumber; }); + return (_a = previewRenameResponse.data) === null || _a === void 0 ? void 0 : _a.find(function (episodeFile) { var _a; return ((_a = episodeFile.episodeNumbers) === null || _a === void 0 ? void 0 : _a.at(0)) === fInfo.episodeNumber; }); }, }, }; args.jobLog('Going to apply new name'); args.jobLog("Renaming ".concat(renameType.appName, "...")); - return [4 /*yield*/, getFileDetailsWrapper(args, arr, arrHost, headers, originalFileName, renameType)]; + return [4 /*yield*/, getFileInfo(args, arr, arrHost, headers, originalFileName, renameType)]; case 1: - fileDetailsWrapper = _e.sent(); - if (!(fileDetailsWrapper.id === '-1' && currentFileName !== originalFileName)) return [3 /*break*/, 3]; - return [4 /*yield*/, getFileDetailsWrapper(args, arr, arrHost, headers, currentFileName, renameType)]; + fInfo = _e.sent(); + if (!(fInfo.id === '-1' && currentFileName !== originalFileName)) return [3 /*break*/, 3]; + return [4 /*yield*/, getFileInfo(args, arr, arrHost, headers, currentFileName, renameType)]; case 2: - fileDetailsWrapper = _e.sent(); + fInfo = _e.sent(); _e.label = 3; case 3: - if (!(fileDetailsWrapper.id !== '-1')) return [3 /*break*/, 7]; + if (!(fInfo.id !== '-1')) return [3 /*break*/, 7]; return [4 /*yield*/, args.deps.axios({ method: 'get', - url: renameType.delegates.buildPreviewRenameResquestUrl(fileDetailsWrapper), + url: renameType.delegates.buildPreviewRenameResquestUrl(fInfo), headers: headers, })]; case 4: previewRenameRequestResult = _e.sent(); fileToRename = renameType.delegates - .getFileToRenameFromPreviewRenameResponse(previewRenameRequestResult); + .getFileToRenameFromPreviewRenameResponse(previewRenameRequestResult, fInfo); if (!(fileToRename !== undefined)) return [3 /*break*/, 6]; newPath = "".concat((0, fileUtils_1.getFileAbosluteDir)(currentFileName), "/").concat((0, fileUtils_1.getFileName)(fileToRename.newPath), ".").concat((0, fileUtils_1.getContainer)(fileToRename.newPath)); return [4 /*yield*/, (0, fileMoveOrCopy_1.default)({ @@ -259,8 +267,6 @@ var plugin = function (args) { return __awaiter(void 0, void 0, void 0, function })]; case 5: isSuccessful = _e.sent(); - args.jobLog("\u2714 ".concat(renameType.content, " ").concat(fileDetailsWrapper.id, " renamed : ") - + "'".concat(originalFileName, "' => '").concat(newPath, "'.")); return [3 /*break*/, 7]; case 6: isSuccessful = true; diff --git a/FlowPluginsTs/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.ts b/FlowPluginsTs/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.ts index 79adbe5..0a44294 100644 --- a/FlowPluginsTs/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.ts +++ b/FlowPluginsTs/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.ts @@ -77,7 +77,15 @@ interface IHTTPHeaders { 'X-Api-Key': string, Accept: string, } -interface IFileDetails { +interface IFileInfo { + id: string, + seasonNumber?: number, + episodeNumber?: number +} +interface ILookupResponse { + data: [{ id: number }], +} +interface IParseResponse { data: { movie?: { id: number }, series?: { id: number }, @@ -87,12 +95,8 @@ interface IFileDetails { }, }, } -interface IFileDetailsWrapper { - id: string - fileDetails?: IFileDetails -} interface IFileToRename { - newPath: string + newPath: string, episodeNumbers?: number[] } interface IPreviewRenameResponse { @@ -102,38 +106,42 @@ interface IRenameType { appName: string, content: string, delegates: { - getIdFromParseResponse: - (fileDetailsWrapper: IFileDetailsWrapper) => string, + getFileInfoFromLookupResponse: + (lookupResponse: ILookupResponse, fileName: string) => IFileInfo, + getFileInfoFromParseResponse: + (parseResponse: IParseResponse) => IFileInfo, buildPreviewRenameResquestUrl: - (fileDetailsWrapper: IFileDetailsWrapper) => string, + (fileInfo: IFileInfo) => string, getFileToRenameFromPreviewRenameResponse: - (previewRenameResponse: IPreviewRenameResponse) => IFileToRename | undefined + (previewRenameResponse: IPreviewRenameResponse, fileInfo: IFileInfo) => IFileToRename | undefined } } -const getMovieId = async ( +const getFileInfoFromLookup = async ( args: IpluginInputArgs, + arr: string, arrHost: string, headers: IHTTPHeaders, fileName: string, - getNewPathType: IRenameType, + renameType: IRenameType, ) - : Promise => { + : Promise => { + let fInfo: IFileInfo = { id: '-1' }; const imdbId = /\b(tt|nm|co|ev|ch|ni)\d{7,10}\b/i.exec(fileName)?.at(0) ?? ''; - const id = (imdbId !== '') - ? String( - (await args.deps.axios({ - method: 'get', - url: `${arrHost}/api/v3/movie/lookup?term=imdb:${imdbId}`, - headers, - })).data?.at(0)?.id ?? -1, - ) - : '-1'; - args.jobLog(`${getNewPathType.content} ${id !== '-1' ? `${id} found` : 'not found'} for imdb '${imdbId}'`); - return id; + if (imdbId !== '') { + const lookupResponse: ILookupResponse = await args.deps.axios({ + method: 'get', + url: `${arrHost}/api/v3/${arr === 'radarr' ? 'movie' : 'series'}/lookup?term=imdb:${imdbId}`, + headers, + }); + fInfo = renameType.delegates.getFileInfoFromLookupResponse(lookupResponse, fileName); + args.jobLog(`${renameType.content} ${fInfo.id !== '-1' ? `'${fInfo.id}' found` : 'not found'}` + + ` for imdb '${imdbId}'`); + } + return fInfo; }; -const getFileDetailsWrapper = async ( +const getFileInfoFromParse = async ( args: IpluginInputArgs, arr: string, arrHost: string, @@ -141,22 +149,32 @@ const getFileDetailsWrapper = async ( fileName: string, renameType: IRenameType, ) - : Promise => { - const fdw: IFileDetailsWrapper = { - id: arr === 'radarr' ? await getMovieId(args, arrHost, headers, fileName, renameType) : '-1', - fileDetails: undefined, - }; - if (fdw.id === '-1') { - fdw.fileDetails = await args.deps.axios({ - method: 'get', - url: `${arrHost}/api/v3/parse?title=${encodeURIComponent(getFileName(fileName))}`, - headers, - }); - fdw.id = renameType.delegates.getIdFromParseResponse(fdw); - args.jobLog(`${renameType.content} ${fdw.id !== '-1' ? `${fdw.id} found` : 'not found'} for '` - + `${getFileName(fileName)}'`); - } - return fdw; + : Promise => { + let fInfo: IFileInfo = { id: '-1' }; + const parseResponse: IParseResponse = await args.deps.axios({ + method: 'get', + url: `${arrHost}/api/v3/parse?title=${encodeURIComponent(getFileName(fileName))}`, + headers, + }); + fInfo = renameType.delegates.getFileInfoFromParseResponse(parseResponse); + args.jobLog(`${renameType.content} ${fInfo.id !== '-1' ? `'${fInfo.id}' found` : 'not found'}` + + ` for '${getFileName(fileName)}'`); + return fInfo; +}; + +const getFileInfo = async ( + args: IpluginInputArgs, + arr: string, + arrHost: string, + headers: IHTTPHeaders, + fileName: string, + renameType: IRenameType, +) + : Promise => { + const fInfo = await getFileInfoFromLookup(args, arr, arrHost, headers, fileName, renameType); + return (fInfo.id === '-1' || (arr === 'sonarr' && (fInfo.seasonNumber === -1 || fInfo.episodeNumber === -1))) + ? getFileInfoFromParse(args, arr, arrHost, headers, fileName, renameType) + : fInfo; }; const plugin = async (args: IpluginInputArgs): Promise => { @@ -177,16 +195,17 @@ const plugin = async (args: IpluginInputArgs): Promise => { Accept: 'application/json', }; - let episodeNumber = 0; const renameType: IRenameType = arr === 'radarr' ? { appName: 'Radarr', content: 'Movie', delegates: { - getIdFromParseResponse: - (fileDetailsWrapper) => String(fileDetailsWrapper.fileDetails?.data?.movie?.id ?? -1), + getFileInfoFromLookupResponse: + (lookupResponse) => ({ id: String(lookupResponse?.data?.at(0)?.id ?? -1) }), + getFileInfoFromParseResponse: + (parseResponse) => ({ id: String(parseResponse?.data?.movie?.id ?? -1) }), buildPreviewRenameResquestUrl: - (fileDetailsWrapper) => `${arrHost}/api/v3/rename?movieId=${fileDetailsWrapper.id}`, + (fInfo) => `${arrHost}/api/v3/rename?movieId=${fInfo.id}`, getFileToRenameFromPreviewRenameResponse: (previewRenameResponse) => previewRenameResponse.data?.at(0), }, @@ -195,39 +214,53 @@ const plugin = async (args: IpluginInputArgs): Promise => { appName: 'Sonarr', content: 'Serie', delegates: { - getIdFromParseResponse: - (fileDetailsWrapper) => String(fileDetailsWrapper.fileDetails?.data?.series?.id ?? -1), - buildPreviewRenameResquestUrl: - (fileDetailsWrapper) => { - [episodeNumber] = fileDetailsWrapper.fileDetails?.data.parsedEpisodeInfo?.episodeNumbers ?? [1]; - return `${arrHost}/api/v3/rename?seriesId=${fileDetailsWrapper.id}&seasonNumber=` - + `${fileDetailsWrapper.fileDetails?.data.parsedEpisodeInfo?.seasonNumber ?? 1}`; + getFileInfoFromLookupResponse: + (lookupResponse, fileName) => { + const fInfo: IFileInfo = { id: String(lookupResponse?.data?.at(0)?.id ?? -1) }; + if (fInfo.id !== '-1') { + const seasonEpisodenumber = /\bS\d{1,3}E\d{1,4}\b/i.exec(fileName)?.at(0) ?? ''; + const episodeNumber = /\d{1,4}$/i.exec(seasonEpisodenumber)?.at(0) ?? ''; + fInfo.seasonNumber = Number(/\d{1,3}/i + .exec(seasonEpisodenumber.slice(0, -episodeNumber.length)) + ?.at(0) ?? '-1'); + fInfo.episodeNumber = Number(episodeNumber !== '' ? episodeNumber : -1); + } + return fInfo; }, + getFileInfoFromParseResponse: + (parseResponse) => ({ + id: String(parseResponse?.data?.series?.id ?? -1), + seasonNumber: parseResponse?.data?.parsedEpisodeInfo?.seasonNumber ?? 1, + episodeNumber: parseResponse?.data?.parsedEpisodeInfo?.episodeNumbers?.at(0) ?? 1, + }), + buildPreviewRenameResquestUrl: + (fInfo) => `${arrHost}/api/v3/rename?seriesId=${fInfo.id}&seasonNumber=${fInfo.seasonNumber}`, getFileToRenameFromPreviewRenameResponse: - (previewRenameResponse) => previewRenameResponse.data - ?.find((episodeFile) => episodeFile.episodeNumbers?.at(0) === episodeNumber), + (previewRenameResponse, fInfo) => previewRenameResponse.data + ?.find((episodeFile) => episodeFile.episodeNumbers?.at(0) === fInfo.episodeNumber), }, }; args.jobLog('Going to apply new name'); args.jobLog(`Renaming ${renameType.appName}...`); - let fileDetailsWrapper = await getFileDetailsWrapper(args, arr, arrHost, headers, originalFileName, renameType); + // Retrieving movie or serie id, plus season and episode number for serie + let fInfo = await getFileInfo(args, arr, arrHost, headers, originalFileName, renameType); // Useful in some edge cases - if (fileDetailsWrapper.id === '-1' && currentFileName !== originalFileName) { - fileDetailsWrapper = await getFileDetailsWrapper(args, arr, arrHost, headers, currentFileName, renameType); + if (fInfo.id === '-1' && currentFileName !== originalFileName) { + fInfo = await getFileInfo(args, arr, arrHost, headers, currentFileName, renameType); } // Checking that the file has been found - if (fileDetailsWrapper.id !== '-1') { + if (fInfo.id !== '-1') { // Using rename endpoint to get ids of all the files that need renaming const previewRenameRequestResult = await args.deps.axios({ method: 'get', - url: renameType.delegates.buildPreviewRenameResquestUrl(fileDetailsWrapper), + url: renameType.delegates.buildPreviewRenameResquestUrl(fInfo), headers, }); const fileToRename = renameType.delegates - .getFileToRenameFromPreviewRenameResponse(previewRenameRequestResult); + .getFileToRenameFromPreviewRenameResponse(previewRenameRequestResult, fInfo); // Only if there is a rename to execute if (fileToRename !== undefined) { @@ -241,8 +274,6 @@ const plugin = async (args: IpluginInputArgs): Promise => { destinationPath: newPath, args, }); - args.jobLog(`✔ ${renameType.content} ${fileDetailsWrapper.id} renamed : ` - + `'${originalFileName}' => '${newPath}'.`); } else { isSuccessful = true; args.jobLog('✔ No rename necessary.'); From 2903603a5d4242e1cc1d7c35307d626204d61bd5 Mon Sep 17 00:00:00 2001 From: "jeanchristophe.mqt@gmail.com" Date: Sun, 31 Mar 2024 11:13:08 +0200 Subject: [PATCH 30/34] After thorough testing, some renaming and refactoring --- .../1.0.0/index.js | 50 ++++++++------- .../1.0.0/index.ts | 63 +++++++++---------- 2 files changed, 57 insertions(+), 56 deletions(-) diff --git a/FlowPlugins/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.js b/FlowPlugins/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.js index 187a609..aef2f8d 100644 --- a/FlowPlugins/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.js +++ b/FlowPlugins/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.js @@ -116,7 +116,7 @@ var details = function () { return ({ ], }); }; exports.details = details; -var getFileInfoFromLookup = function (args, arr, arrHost, headers, fileName, renameType) { return __awaiter(void 0, void 0, void 0, function () { +var getFileInfoFromLookup = function (args, arrApp, fileName) { return __awaiter(void 0, void 0, void 0, function () { var fInfo, imdbId, lookupResponse; var _a, _b; return __generator(this, function (_c) { @@ -127,20 +127,20 @@ var getFileInfoFromLookup = function (args, arr, arrHost, headers, fileName, ren if (!(imdbId !== '')) return [3 /*break*/, 2]; return [4 /*yield*/, args.deps.axios({ method: 'get', - url: "".concat(arrHost, "/api/v3/").concat(arr === 'radarr' ? 'movie' : 'series', "/lookup?term=imdb:").concat(imdbId), - headers: headers, + url: "".concat(arrApp.host, "/api/v3/").concat(arrApp.name === 'radarr' ? 'movie' : 'series', "/lookup?term=imdb:").concat(imdbId), + headers: arrApp.headers, })]; case 1: lookupResponse = _c.sent(); - fInfo = renameType.delegates.getFileInfoFromLookupResponse(lookupResponse, fileName); - args.jobLog("".concat(renameType.content, " ").concat(fInfo.id !== '-1' ? "'".concat(fInfo.id, "' found") : 'not found') + fInfo = arrApp.delegates.getFileInfoFromLookupResponse(lookupResponse, fileName); + args.jobLog("".concat(arrApp.content, " ").concat(fInfo.id !== '-1' ? "'".concat(fInfo.id, "' found") : 'not found') + " for imdb '".concat(imdbId, "'")); _c.label = 2; case 2: return [2 /*return*/, fInfo]; } }); }); }; -var getFileInfoFromParse = function (args, arr, arrHost, headers, fileName, renameType) { return __awaiter(void 0, void 0, void 0, function () { +var getFileInfoFromParse = function (args, arrApp, fileName) { return __awaiter(void 0, void 0, void 0, function () { var fInfo, parseResponse; return __generator(this, function (_a) { switch (_a.label) { @@ -148,33 +148,33 @@ var getFileInfoFromParse = function (args, arr, arrHost, headers, fileName, rena fInfo = { id: '-1' }; return [4 /*yield*/, args.deps.axios({ method: 'get', - url: "".concat(arrHost, "/api/v3/parse?title=").concat(encodeURIComponent((0, fileUtils_1.getFileName)(fileName))), - headers: headers, + url: "".concat(arrApp.host, "/api/v3/parse?title=").concat(encodeURIComponent((0, fileUtils_1.getFileName)(fileName))), + headers: arrApp.headers, })]; case 1: parseResponse = _a.sent(); - fInfo = renameType.delegates.getFileInfoFromParseResponse(parseResponse); - args.jobLog("".concat(renameType.content, " ").concat(fInfo.id !== '-1' ? "'".concat(fInfo.id, "' found") : 'not found') + fInfo = arrApp.delegates.getFileInfoFromParseResponse(parseResponse); + args.jobLog("".concat(arrApp.content, " ").concat(fInfo.id !== '-1' ? "'".concat(fInfo.id, "' found") : 'not found') + " for '".concat((0, fileUtils_1.getFileName)(fileName), "'")); return [2 /*return*/, fInfo]; } }); }); }; -var getFileInfo = function (args, arr, arrHost, headers, fileName, renameType) { return __awaiter(void 0, void 0, void 0, function () { +var getFileInfo = function (args, arrApp, fileName) { return __awaiter(void 0, void 0, void 0, function () { var fInfo; return __generator(this, function (_a) { switch (_a.label) { - case 0: return [4 /*yield*/, getFileInfoFromLookup(args, arr, arrHost, headers, fileName, renameType)]; + case 0: return [4 /*yield*/, getFileInfoFromLookup(args, arrApp, fileName)]; case 1: fInfo = _a.sent(); - return [2 /*return*/, (fInfo.id === '-1' || (arr === 'sonarr' && (fInfo.seasonNumber === -1 || fInfo.episodeNumber === -1))) - ? getFileInfoFromParse(args, arr, arrHost, headers, fileName, renameType) + return [2 /*return*/, (fInfo.id === '-1' || (arrApp.name === 'sonarr' && (fInfo.seasonNumber === -1 || fInfo.episodeNumber === -1))) + ? getFileInfoFromParse(args, arrApp, fileName) : fInfo]; } }); }); }; var plugin = function (args) { return __awaiter(void 0, void 0, void 0, function () { - var lib, newPath, isSuccessful, arr, arr_host, arrHost, originalFileName, currentFileName, headers, renameType, fInfo, previewRenameRequestResult, fileToRename; + var lib, newPath, isSuccessful, arr, arr_host, arrHost, originalFileName, currentFileName, headers, arrApp, fInfo, previewRenameRequestResult, fileToRename; var _a, _b, _c, _d; return __generator(this, function (_e) { switch (_e.label) { @@ -194,9 +194,11 @@ var plugin = function (args) { return __awaiter(void 0, void 0, void 0, function 'X-Api-Key': String(args.inputs.arr_api_key), Accept: 'application/json', }; - renameType = arr === 'radarr' + arrApp = arr === 'radarr' ? { - appName: 'Radarr', + name: arr, + host: arrHost, + headers: headers, content: 'Movie', delegates: { getFileInfoFromLookupResponse: function (lookupResponse) { var _a, _b, _c; return ({ id: String((_c = (_b = (_a = lookupResponse === null || lookupResponse === void 0 ? void 0 : lookupResponse.data) === null || _a === void 0 ? void 0 : _a.at(0)) === null || _b === void 0 ? void 0 : _b.id) !== null && _c !== void 0 ? _c : -1) }); }, @@ -206,7 +208,9 @@ var plugin = function (args) { return __awaiter(void 0, void 0, void 0, function }, } : { - appName: 'Sonarr', + name: arr, + host: arrHost, + headers: headers, content: 'Serie', delegates: { getFileInfoFromLookupResponse: function (lookupResponse, fileName) { @@ -237,12 +241,12 @@ var plugin = function (args) { return __awaiter(void 0, void 0, void 0, function }, }; args.jobLog('Going to apply new name'); - args.jobLog("Renaming ".concat(renameType.appName, "...")); - return [4 /*yield*/, getFileInfo(args, arr, arrHost, headers, originalFileName, renameType)]; + args.jobLog("Renaming ".concat(arrApp.name, "...")); + return [4 /*yield*/, getFileInfo(args, arrApp, originalFileName)]; case 1: fInfo = _e.sent(); if (!(fInfo.id === '-1' && currentFileName !== originalFileName)) return [3 /*break*/, 3]; - return [4 /*yield*/, getFileInfo(args, arr, arrHost, headers, currentFileName, renameType)]; + return [4 /*yield*/, getFileInfo(args, arrApp, currentFileName)]; case 2: fInfo = _e.sent(); _e.label = 3; @@ -250,12 +254,12 @@ var plugin = function (args) { return __awaiter(void 0, void 0, void 0, function if (!(fInfo.id !== '-1')) return [3 /*break*/, 7]; return [4 /*yield*/, args.deps.axios({ method: 'get', - url: renameType.delegates.buildPreviewRenameResquestUrl(fInfo), + url: arrApp.delegates.buildPreviewRenameResquestUrl(fInfo), headers: headers, })]; case 4: previewRenameRequestResult = _e.sent(); - fileToRename = renameType.delegates + fileToRename = arrApp.delegates .getFileToRenameFromPreviewRenameResponse(previewRenameRequestResult, fInfo); if (!(fileToRename !== undefined)) return [3 /*break*/, 6]; newPath = "".concat((0, fileUtils_1.getFileAbosluteDir)(currentFileName), "/").concat((0, fileUtils_1.getFileName)(fileToRename.newPath), ".").concat((0, fileUtils_1.getContainer)(fileToRename.newPath)); diff --git a/FlowPluginsTs/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.ts b/FlowPluginsTs/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.ts index 0a44294..fd3531a 100644 --- a/FlowPluginsTs/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.ts +++ b/FlowPluginsTs/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.ts @@ -102,8 +102,10 @@ interface IFileToRename { interface IPreviewRenameResponse { data: IFileToRename[] } -interface IRenameType { - appName: string, +interface IArrApp { + name: string, + host: string, + headers: IHTTPHeaders, content: string, delegates: { getFileInfoFromLookupResponse: @@ -119,11 +121,8 @@ interface IRenameType { const getFileInfoFromLookup = async ( args: IpluginInputArgs, - arr: string, - arrHost: string, - headers: IHTTPHeaders, + arrApp: IArrApp, fileName: string, - renameType: IRenameType, ) : Promise => { let fInfo: IFileInfo = { id: '-1' }; @@ -131,11 +130,11 @@ const getFileInfoFromLookup = async ( if (imdbId !== '') { const lookupResponse: ILookupResponse = await args.deps.axios({ method: 'get', - url: `${arrHost}/api/v3/${arr === 'radarr' ? 'movie' : 'series'}/lookup?term=imdb:${imdbId}`, - headers, + url: `${arrApp.host}/api/v3/${arrApp.name === 'radarr' ? 'movie' : 'series'}/lookup?term=imdb:${imdbId}`, + headers: arrApp.headers, }); - fInfo = renameType.delegates.getFileInfoFromLookupResponse(lookupResponse, fileName); - args.jobLog(`${renameType.content} ${fInfo.id !== '-1' ? `'${fInfo.id}' found` : 'not found'}` + fInfo = arrApp.delegates.getFileInfoFromLookupResponse(lookupResponse, fileName); + args.jobLog(`${arrApp.content} ${fInfo.id !== '-1' ? `'${fInfo.id}' found` : 'not found'}` + ` for imdb '${imdbId}'`); } return fInfo; @@ -143,37 +142,31 @@ const getFileInfoFromLookup = async ( const getFileInfoFromParse = async ( args: IpluginInputArgs, - arr: string, - arrHost: string, - headers: IHTTPHeaders, + arrApp: IArrApp, fileName: string, - renameType: IRenameType, ) : Promise => { let fInfo: IFileInfo = { id: '-1' }; const parseResponse: IParseResponse = await args.deps.axios({ method: 'get', - url: `${arrHost}/api/v3/parse?title=${encodeURIComponent(getFileName(fileName))}`, - headers, + url: `${arrApp.host}/api/v3/parse?title=${encodeURIComponent(getFileName(fileName))}`, + headers: arrApp.headers, }); - fInfo = renameType.delegates.getFileInfoFromParseResponse(parseResponse); - args.jobLog(`${renameType.content} ${fInfo.id !== '-1' ? `'${fInfo.id}' found` : 'not found'}` + fInfo = arrApp.delegates.getFileInfoFromParseResponse(parseResponse); + args.jobLog(`${arrApp.content} ${fInfo.id !== '-1' ? `'${fInfo.id}' found` : 'not found'}` + ` for '${getFileName(fileName)}'`); return fInfo; }; const getFileInfo = async ( args: IpluginInputArgs, - arr: string, - arrHost: string, - headers: IHTTPHeaders, + arrApp: IArrApp, fileName: string, - renameType: IRenameType, ) : Promise => { - const fInfo = await getFileInfoFromLookup(args, arr, arrHost, headers, fileName, renameType); - return (fInfo.id === '-1' || (arr === 'sonarr' && (fInfo.seasonNumber === -1 || fInfo.episodeNumber === -1))) - ? getFileInfoFromParse(args, arr, arrHost, headers, fileName, renameType) + const fInfo = await getFileInfoFromLookup(args, arrApp, fileName); + return (fInfo.id === '-1' || (arrApp.name === 'sonarr' && (fInfo.seasonNumber === -1 || fInfo.episodeNumber === -1))) + ? getFileInfoFromParse(args, arrApp, fileName) : fInfo; }; @@ -195,9 +188,11 @@ const plugin = async (args: IpluginInputArgs): Promise => { Accept: 'application/json', }; - const renameType: IRenameType = arr === 'radarr' + const arrApp: IArrApp = arr === 'radarr' ? { - appName: 'Radarr', + name: arr, + host: arrHost, + headers, content: 'Movie', delegates: { getFileInfoFromLookupResponse: @@ -211,7 +206,9 @@ const plugin = async (args: IpluginInputArgs): Promise => { }, } : { - appName: 'Sonarr', + name: arr, + host: arrHost, + headers, content: 'Serie', delegates: { getFileInfoFromLookupResponse: @@ -242,13 +239,13 @@ const plugin = async (args: IpluginInputArgs): Promise => { }; args.jobLog('Going to apply new name'); - args.jobLog(`Renaming ${renameType.appName}...`); + args.jobLog(`Renaming ${arrApp.name}...`); // Retrieving movie or serie id, plus season and episode number for serie - let fInfo = await getFileInfo(args, arr, arrHost, headers, originalFileName, renameType); + let fInfo = await getFileInfo(args, arrApp, originalFileName); // Useful in some edge cases if (fInfo.id === '-1' && currentFileName !== originalFileName) { - fInfo = await getFileInfo(args, arr, arrHost, headers, currentFileName, renameType); + fInfo = await getFileInfo(args, arrApp, currentFileName); } // Checking that the file has been found @@ -256,10 +253,10 @@ const plugin = async (args: IpluginInputArgs): Promise => { // Using rename endpoint to get ids of all the files that need renaming const previewRenameRequestResult = await args.deps.axios({ method: 'get', - url: renameType.delegates.buildPreviewRenameResquestUrl(fInfo), + url: arrApp.delegates.buildPreviewRenameResquestUrl(fInfo), headers, }); - const fileToRename = renameType.delegates + const fileToRename = arrApp.delegates .getFileToRenameFromPreviewRenameResponse(previewRenameRequestResult, fInfo); // Only if there is a rename to execute From 3a03b7ed462190d95a50c1ab347f43e8c686455d Mon Sep 17 00:00:00 2001 From: "jeanchristophe.mqt@gmail.com" Date: Sun, 31 Mar 2024 14:09:17 +0200 Subject: [PATCH 31/34] Icon changed to fa-pen-to-square --- .../tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.js | 2 +- .../tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/FlowPlugins/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.js b/FlowPlugins/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.js index aef2f8d..cc6ce1e 100644 --- a/FlowPlugins/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.js +++ b/FlowPlugins/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.js @@ -65,7 +65,7 @@ var details = function () { return ({ pType: '', requiresVersion: '2.11.01', sidebarPosition: -1, - icon: 'faBell', + icon: 'faPenToSquare', inputs: [ { label: 'Arr', diff --git a/FlowPluginsTs/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.ts b/FlowPluginsTs/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.ts index fd3531a..c9468e0 100644 --- a/FlowPluginsTs/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.ts +++ b/FlowPluginsTs/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.ts @@ -21,7 +21,7 @@ const details = (): IpluginDetails => ({ pType: '', requiresVersion: '2.11.01', sidebarPosition: -1, - icon: 'faBell', + icon: 'faPenToSquare', inputs: [ { label: 'Arr', From 86c536252a8eb17f9be4dd107a7865e3cc854d1e Mon Sep 17 00:00:00 2001 From: "jeanchristophe.mqt@gmail.com" Date: Fri, 12 Apr 2024 10:18:26 +0200 Subject: [PATCH 32/34] Made imdb regex ungreedy --- .../tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.js | 2 +- .../tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/FlowPlugins/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.js b/FlowPlugins/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.js index cc6ce1e..9c32bd0 100644 --- a/FlowPlugins/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.js +++ b/FlowPlugins/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.js @@ -123,7 +123,7 @@ var getFileInfoFromLookup = function (args, arrApp, fileName) { return __awaiter switch (_c.label) { case 0: fInfo = { id: '-1' }; - imdbId = (_b = (_a = /\b(tt|nm|co|ev|ch|ni)\d{7,10}\b/i.exec(fileName)) === null || _a === void 0 ? void 0 : _a.at(0)) !== null && _b !== void 0 ? _b : ''; + imdbId = (_b = (_a = /\b(tt|nm|co|ev|ch|ni)\d{7,10}?\b/i.exec(fileName)) === null || _a === void 0 ? void 0 : _a.at(0)) !== null && _b !== void 0 ? _b : ''; if (!(imdbId !== '')) return [3 /*break*/, 2]; return [4 /*yield*/, args.deps.axios({ method: 'get', diff --git a/FlowPluginsTs/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.ts b/FlowPluginsTs/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.ts index c9468e0..97b939c 100644 --- a/FlowPluginsTs/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.ts +++ b/FlowPluginsTs/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.ts @@ -126,7 +126,7 @@ const getFileInfoFromLookup = async ( ) : Promise => { let fInfo: IFileInfo = { id: '-1' }; - const imdbId = /\b(tt|nm|co|ev|ch|ni)\d{7,10}\b/i.exec(fileName)?.at(0) ?? ''; + const imdbId = /\b(tt|nm|co|ev|ch|ni)\d{7,10}?\b/i.exec(fileName)?.at(0) ?? ''; if (imdbId !== '') { const lookupResponse: ILookupResponse = await args.deps.axios({ method: 'get', From ac32fd8075684a5de07ccb0bd08808ba4d4f2443 Mon Sep 17 00:00:00 2001 From: HaveAGitGat <43864057+HaveAGitGat@users.noreply.github.com> Date: Wed, 8 May 2024 06:48:09 +0100 Subject: [PATCH 33/34] Fix Sonarr typo --- .../tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/FlowPluginsTs/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.ts b/FlowPluginsTs/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.ts index 97b939c..2cecd76 100644 --- a/FlowPluginsTs/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.ts +++ b/FlowPluginsTs/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.ts @@ -63,11 +63,11 @@ const details = (): IpluginDetails => ({ outputs: [ { number: 1, - tooltip: 'Radarr or Sonnar notified', + tooltip: 'Radarr or Sonarr notified', }, { number: 2, - tooltip: 'Radarr or Sonnar do not know this file', + tooltip: 'Radarr or Sonarr do not know this file', }, ], }); From 6e67b97875f3ce811220b41271952a33380da173 Mon Sep 17 00:00:00 2001 From: HaveAGitGat <43864057+HaveAGitGat@users.noreply.github.com> Date: Wed, 8 May 2024 06:50:26 +0100 Subject: [PATCH 34/34] Add compiled changes --- .../tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/FlowPlugins/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.js b/FlowPlugins/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.js index 9c32bd0..dfe4ac1 100644 --- a/FlowPlugins/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.js +++ b/FlowPlugins/CommunityFlowPlugins/tools/applyRadarrOrSonarrNamingPolicy/1.0.0/index.js @@ -107,11 +107,11 @@ var details = function () { return ({ outputs: [ { number: 1, - tooltip: 'Radarr or Sonnar notified', + tooltip: 'Radarr or Sonarr notified', }, { number: 2, - tooltip: 'Radarr or Sonnar do not know this file', + tooltip: 'Radarr or Sonarr do not know this file', }, ], }); };