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] 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, }; };