diff --git a/FlowPlugins/CommunityFlowPlugins/tools/notifyRadarrOrSonarr/2.0.0/index.js b/FlowPlugins/CommunityFlowPlugins/tools/notifyRadarrOrSonarr/2.0.0/index.js index 4c3e840..458a5d4 100644 --- a/FlowPlugins/CommunityFlowPlugins/tools/notifyRadarrOrSonarr/2.0.0/index.js +++ b/FlowPlugins/CommunityFlowPlugins/tools/notifyRadarrOrSonarr/2.0.0/index.js @@ -35,8 +35,12 @@ 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 path_1 = __importDefault(require("path")); var fileUtils_1 = require("../../../../FlowHelpers/1.0.0/fileUtils"); var details = function () { return ({ name: 'Notify Radarr or Sonarr', @@ -100,110 +104,114 @@ var details = function () { return ({ ], }); }; exports.details = details; +var getId = function (args, arrHost, headers, fileName, refreshType) { return __awaiter(void 0, void 0, void 0, function () { + var imdbId, id, _a, _b, _c, _d; + var _e, _f, _g, _h; + return __generator(this, function (_j) { + switch (_j.label) { + case 0: + imdbId = (_f = (_e = /\b(tt|nm|co|ev|ch|ni)\d{7,10}\b/i.exec(fileName)) === null || _e === void 0 ? void 0 : _e.at(0)) !== null && _f !== void 0 ? _f : ''; + if (!(imdbId !== '')) return [3 /*break*/, 2]; + _b = Number; + return [4 /*yield*/, args.deps.axios({ + method: 'get', + url: "".concat(arrHost, "/api/v3/movie/lookup?terms=imdb:").concat(imdbId), + headers: headers, + })]; + case 1: + _a = _b.apply(void 0, [(_h = (_g = (_j.sent()).data) === null || _g === void 0 ? void 0 : _g.id) !== null && _h !== void 0 ? _h : -1]); + return [3 /*break*/, 3]; + case 2: + _a = -1; + _j.label = 3; + case 3: + id = _a; + if (!(id === -1)) return [3 /*break*/, 5]; + _d = (_c = refreshType.delegates).getIdFromParseResponse; + 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: + id = _d.apply(_c, [(_j.sent())]); + _j.label = 5; + case 5: + args.jobLog("".concat(refreshType.content, " ").concat(id !== -1 ? "".concat(id, " found") : 'not found', " for '").concat(fileName, "', imdb '").concat(imdbId, "'")); + return [2 /*return*/, id]; + } + }); +}); }; var plugin = function (args) { return __awaiter(void 0, void 0, void 0, function () { - var lib, _a, arr, arr_api_key, arr_host, arrHost, fileNames, refresh, refreshTypes, refreshed; - var _b, _c, _d, _e; - return __generator(this, function (_f) { - switch (_f.label) { + var lib, refreshed, arr, arr_host, arrHost, absoluteFileDir, fileNames, headers, refreshType, id; + 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; + refreshed = 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; + absoluteFileDir = (0, fileUtils_1.getFileAbosluteDir)((_b = (_a = args.originalLibraryFile) === null || _a === void 0 ? void 0 : _a._id) !== null && _b !== void 0 ? _b : ''); fileNames = { - originalFileName: (0, fileUtils_1.getFileName)((_c = (_b = args.originalLibraryFile) === null || _b === void 0 ? void 0 : _b._id) !== null && _c !== void 0 ? _c : ''), - currentFileName: (0, fileUtils_1.getFileName)((_e = (_d = args.inputFileObj) === null || _d === void 0 ? void 0 : _d._id) !== null && _e !== void 0 ? _e : ''), + originalFileName: path_1.default.join(absoluteFileDir, (0, fileUtils_1.getFileName)((_d = (_c = args.originalLibraryFile) === null || _c === void 0 ? void 0 : _c._id) !== null && _d !== void 0 ? _d : '')), + currentFileName: path_1.default.join(absoluteFileDir, (0, fileUtils_1.getFileName)((_f = (_e = args.inputFileObj) === null || _e === void 0 ? void 0 : _e._id) !== null && _f !== void 0 ? _f : '')), }; - refresh = function (refreshType) { return __awaiter(void 0, void 0, void 0, function () { - var refreshed, headers, getId, fileName, id, refreshResquestConfig; - return __generator(this, function (_a) { - switch (_a.label) { - case 0: - args.jobLog('Going to force scan'); - args.jobLog("Refreshing ".concat(refreshType.appName, "...")); - refreshed = false; - headers = { - 'Content-Type': 'application/json', - 'X-Api-Key': arr_api_key, - Accept: 'application/json', - }; - getId = 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 = refreshType.delegates.getIdFromParseRequestResult(parseRequestResult); - args.jobLog(id !== -1 - ? "Found ".concat(refreshType.contentName, " ").concat(id, " with a file named '").concat(fileName, "'") - : "Didn't find ".concat(refreshType.contentName, " with a file named '").concat(fileName, "' in ").concat(arrHost, ".")); - return [2 /*return*/, id]; - } - }); - }); }; - fileName = fileNames.originalFileName; - return [4 /*yield*/, getId(fileName)]; - case 1: - id = _a.sent(); - if (!(id === -1 && fileNames.currentFileName !== fileNames.originalFileName)) return [3 /*break*/, 3]; - fileName = fileNames.currentFileName; - return [4 /*yield*/, getId(fileName)]; - case 2: - id = _a.sent(); - _a.label = 3; - case 3: - if (!(id !== -1)) return [3 /*break*/, 5]; - refreshResquestConfig = { - method: 'post', - url: "".concat(arrHost, "/api/v3/command"), - headers: headers, - data: refreshType.delegates.buildRefreshResquestData(id), - }; - return [4 /*yield*/, args.deps.axios(refreshResquestConfig)]; - case 4: - _a.sent(); - refreshed = true; - args.jobLog("\u2714 Refreshed ".concat(refreshType.contentName, " ").concat(id, " in ").concat(refreshType.appName, ".")); - _a.label = 5; - case 5: return [2 /*return*/, refreshed]; - } - }); - }); }; - refreshTypes = { - radarr: { + headers = { + 'Content-Type': 'application/json', + 'X-Api-Key': String(args.inputs.arr_api_key), + Accept: 'application/json', + }; + refreshType = arr === 'radarr' + ? { appName: 'Radarr', - contentName: 'movie', + content: 'Movie', delegates: { - getIdFromParseRequestResult: function (parseRequestResult) { var _a, _b, _c, _d; return Number((_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); }, + getIdFromParseResponse: function (parseRequestResult) { var _a, _b, _c; return Number((_c = (_b = (_a = parseRequestResult.data) === null || _a === void 0 ? void 0 : _a.movie) === null || _b === void 0 ? void 0 : _b.id) !== null && _c !== void 0 ? _c : -1); }, buildRefreshResquestData: function (id) { return JSON.stringify({ name: 'RefreshMovie', movieIds: [id] }); }, }, - }, - sonarr: { + } + : { appName: 'Sonarr', - contentName: 'serie', + content: 'Serie', delegates: { - getIdFromParseRequestResult: function (parseRequestResult) { var _a, _b, _c; return Number((_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); }, + getIdFromParseResponse: function (parseRequestResult) { var _a, _b, _c; return Number((_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); }, buildRefreshResquestData: function (id) { return JSON.stringify({ name: 'RefreshSeries', seriesId: id }); }, }, - }, - }; - return [4 /*yield*/, refresh(arr === 'radarr' ? refreshTypes.radarr : refreshTypes.sonarr)]; + }; + args.jobLog('Going to force scan'); + args.jobLog("Refreshing ".concat(refreshType.appName, "...")); + return [4 /*yield*/, getId(args, arrHost, headers, fileNames.originalFileName, refreshType)]; case 1: - refreshed = _f.sent(); - return [2 /*return*/, { - outputFileObj: args.inputFileObj, - outputNumber: refreshed ? 1 : 2, - variables: args.variables, - }]; + id = _g.sent(); + if (!(id === -1 && fileNames.currentFileName !== fileNames.originalFileName)) return [3 /*break*/, 3]; + return [4 /*yield*/, getId(args, arrHost, headers, fileNames.currentFileName, refreshType)]; + case 2: + id = _g.sent(); + _g.label = 3; + case 3: + if (!(id !== -1)) return [3 /*break*/, 5]; + // Using command endpoint to queue a refresh task + return [4 /*yield*/, args.deps.axios({ + method: 'post', + url: "".concat(arrHost, "/api/v3/command"), + headers: headers, + data: refreshType.delegates.buildRefreshResquestData(id), + })]; + case 4: + // Using command endpoint to queue a refresh task + _g.sent(); + refreshed = true; + args.jobLog("\u2714 Refreshed ".concat(refreshType.content, " ").concat(id, " in ").concat(refreshType.appName, ".")); + _g.label = 5; + case 5: return [2 /*return*/, { + outputFileObj: args.inputFileObj, + outputNumber: refreshed ? 1 : 2, + variables: args.variables, + }]; } }); }); }; diff --git a/FlowPluginsTs/CommunityFlowPlugins/tools/notifyRadarrOrSonarr/2.0.0/index.ts b/FlowPluginsTs/CommunityFlowPlugins/tools/notifyRadarrOrSonarr/2.0.0/index.ts index a7a0dd0..7b294c3 100644 --- a/FlowPluginsTs/CommunityFlowPlugins/tools/notifyRadarrOrSonarr/2.0.0/index.ts +++ b/FlowPluginsTs/CommunityFlowPlugins/tools/notifyRadarrOrSonarr/2.0.0/index.ts @@ -1,4 +1,5 @@ -import { getFileName } from '../../../../FlowHelpers/1.0.0/fileUtils'; +import path from 'path'; +import { getFileAbosluteDir, getFileName } from '../../../../FlowHelpers/1.0.0/fileUtils'; import { IpluginDetails, IpluginInputArgs, @@ -67,127 +68,121 @@ const details = (): IpluginDetails => ({ ], }); -interface IFileNames { - originalFileName: string, - currentFileName: string +interface IHTTPHeaders { + 'Content-Type': string, + 'X-Api-Key': string, + Accept: string, } interface IParsedRequestResult { data: { - movie?: { - movieFile: { - movieId: number - }, - }, - series?: { - id: number - }, + movie?: { id: number }, + series?: { id: number }, }, } interface IRefreshDelegates { - getIdFromParseRequestResult: (parseRequestResult: IParsedRequestResult) => number, + getIdFromParseResponse: (parseRequestResult: IParsedRequestResult) => number, buildRefreshResquestData: (id: number) => string } interface IRefreshType { appName: string, - contentName: string, + content: string, delegates: IRefreshDelegates } -interface IRefreshTypes { - radarr: IRefreshType, - sonarr: IRefreshType -} + +const getId = async ( + args: IpluginInputArgs, + arrHost: string, headers: IHTTPHeaders, + fileName: string, + refreshType: IRefreshType, +) + : Promise => { + const imdbId = /\b(tt|nm|co|ev|ch|ni)\d{7,10}\b/i.exec(fileName)?.at(0) ?? ''; + let id = (imdbId !== '') + ? Number( + (await args.deps.axios({ + method: 'get', + url: `${arrHost}/api/v3/movie/lookup?terms=imdb:${imdbId}`, + headers, + })).data?.id ?? -1, + ) + : -1; + if (id === -1) { + id = refreshType.delegates.getIdFromParseResponse( + (await args.deps.axios({ + method: 'get', + url: `${arrHost}/api/v3/parse?title=${encodeURIComponent(getFileName(fileName))}`, + headers, + })), + ); + } + args.jobLog(`${refreshType.content} ${id !== -1 ? `${id} found` : 'not found'} for '${fileName}', imdb '${imdbId}'`); + return id; +}; 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; + // Variables initialization + let refreshed = 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 fileNames: IFileNames = { - originalFileName: getFileName(args.originalLibraryFile?._id ?? ''), - currentFileName: getFileName(args.inputFileObj?._id ?? ''), + const absoluteFileDir = getFileAbosluteDir(args.originalLibraryFile?._id ?? ''); + const fileNames = { + originalFileName: path.join(absoluteFileDir, getFileName(args.originalLibraryFile?._id ?? '')), + currentFileName: path.join(absoluteFileDir, getFileName(args.inputFileObj?._id ?? '')), }; - - const refresh = async (refreshType: IRefreshType) - : Promise => { - args.jobLog('Going to force scan'); - args.jobLog(`Refreshing ${refreshType.appName}...`); - - let refreshed = false; - const headers = { - 'Content-Type': 'application/json', - 'X-Api-Key': arr_api_key, - Accept: 'application/json', - }; - - const getId = 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 = refreshType.delegates.getIdFromParseRequestResult(parseRequestResult); - args.jobLog(id !== -1 - ? `Found ${refreshType.contentName} ${id} with a file named '${fileName}'` - : `Didn't find ${refreshType.contentName} with a file named '${fileName}' in ${arrHost}.`); - return id; - }; - - let fileName = fileNames.originalFileName; - let id = await getId(fileName); - // In case there has been a name change and the arr app already noticed it. - if (id === -1 && fileNames.currentFileName !== fileNames.originalFileName) { - fileName = fileNames.currentFileName; - id = await getId(fileName); - } - - // Checking that the file has been found. - if (id !== -1) { - // Using refresh command endpoint to force rescan. - const refreshResquestConfig = { - method: 'post', - url: `${arrHost}/api/v3/command`, - headers, - data: refreshType.delegates.buildRefreshResquestData(id), - }; - await args.deps.axios(refreshResquestConfig); - - refreshed = true; - args.jobLog(`✔ Refreshed ${refreshType.contentName} ${id} in ${refreshType.appName}.`); - } - - return refreshed; + const headers: IHTTPHeaders = { + 'Content-Type': 'application/json', + 'X-Api-Key': String(args.inputs.arr_api_key), + Accept: 'application/json', }; - - const refreshTypes: IRefreshTypes = { - radarr: { + const refreshType: IRefreshType = arr === 'radarr' + ? { appName: 'Radarr', - contentName: 'movie', + content: 'Movie', delegates: { - getIdFromParseRequestResult: - (parseRequestResult) => Number(parseRequestResult.data?.movie?.movieFile?.movieId ?? -1), + getIdFromParseResponse: + (parseRequestResult: IParsedRequestResult) => Number(parseRequestResult.data?.movie?.id ?? -1), buildRefreshResquestData: (id) => JSON.stringify({ name: 'RefreshMovie', movieIds: [id] }), }, - }, - sonarr: { + } + : { appName: 'Sonarr', - contentName: 'serie', + content: 'Serie', delegates: { - getIdFromParseRequestResult: - (parseRequestResult) => Number(parseRequestResult.data?.series?.id ?? -1), + getIdFromParseResponse: + (parseRequestResult: IParsedRequestResult) => Number(parseRequestResult.data?.series?.id ?? -1), buildRefreshResquestData: (id) => JSON.stringify({ name: 'RefreshSeries', seriesId: id }), }, - }, - }; + }; + + args.jobLog('Going to force scan'); + args.jobLog(`Refreshing ${refreshType.appName}...`); + + let id = await getId(args, arrHost, headers, fileNames.originalFileName, refreshType); + // Useful in some edge cases + if (id === -1 && fileNames.currentFileName !== fileNames.originalFileName) { + id = await getId(args, arrHost, headers, fileNames.currentFileName, refreshType); + } + + // Checking that the file has been found + if (id !== -1) { + // Using command endpoint to queue a refresh task + await args.deps.axios({ + method: 'post', + url: `${arrHost}/api/v3/command`, + headers, + data: refreshType.delegates.buildRefreshResquestData(id), + }); - const refreshed = await refresh(arr === 'radarr' ? refreshTypes.radarr : refreshTypes.sonarr); + refreshed = true; + args.jobLog(`✔ Refreshed ${refreshType.content} ${id} in ${refreshType.appName}.`); + } return { outputFileObj: args.inputFileObj,