From bd585e42bb94561f00bd9b3faccc417b7616645c Mon Sep 17 00:00:00 2001 From: "jeanchristophe.mqt@gmail.com" Date: Sat, 11 Jan 2025 12:29:46 +0100 Subject: [PATCH] Code refactoring --- .../tools/notifyRadarrOrSonarr/3.0.0/index.js | 148 +++++++++------- .../tools/notifyRadarrOrSonarr/3.0.0/index.ts | 159 ++++++++++-------- 2 files changed, 173 insertions(+), 134 deletions(-) diff --git a/FlowPlugins/CommunityFlowPlugins/tools/notifyRadarrOrSonarr/3.0.0/index.js b/FlowPlugins/CommunityFlowPlugins/tools/notifyRadarrOrSonarr/3.0.0/index.js index 1639bf8e4..5700201aa 100644 --- a/FlowPlugins/CommunityFlowPlugins/tools/notifyRadarrOrSonarr/3.0.0/index.js +++ b/FlowPlugins/CommunityFlowPlugins/tools/notifyRadarrOrSonarr/3.0.0/index.js @@ -80,85 +80,103 @@ var details = function () { return ({ 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', + 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', }, ], outputs: [ - { - number: 1, - tooltip: 'Radarr or Sonarr notified', - }, - { - number: 2, - tooltip: 'Radarr or Sonarr do not know this file', - }, + { number: 1, tooltip: 'Radarr or Sonarr notified' }, + { number: 2, tooltip: 'Radarr or Sonarr do not know this file' }, ], }); }; exports.details = details; +var API_VERSION = 'v3'; +var CONTENT_TYPE = 'application/json'; +var arrConfigs = { + radarr: { + content: 'Movie', + buildRefreshRequest: function (id) { return JSON.stringify({ name: 'RefreshMovie', movieIds: [id] }); }, + }, + sonarr: { + content: 'Serie', + buildRefreshRequest: function (id) { return JSON.stringify({ name: 'RefreshSeries', seriesId: id }); }, + }, +}; +var normalizeHost = function (host) { + var trimmedHost = host.trim(); + return trimmedHost.endsWith('/') ? trimmedHost.slice(0, -1) : trimmedHost; +}; +var createArrApp = function (arrType, host, apiKey) { + var headers = { + 'Content-Type': CONTENT_TYPE, + 'X-Api-Key': apiKey, + Accept: CONTENT_TYPE, + }; + var config = arrConfigs[arrType]; + return { + name: arrType, + host: normalizeHost(host), + headers: headers, + content: config.content, + buildRefreshRequest: config.buildRefreshRequest, + }; +}; +var refreshArr = function (arrApp, id, args) { return __awaiter(void 0, void 0, void 0, function () { + var error_1; + return __generator(this, function (_a) { + switch (_a.label) { + case 0: + if (id === -1) { + args.jobLog('No valid ID found in variables'); + return [2 /*return*/, false]; + } + _a.label = 1; + case 1: + _a.trys.push([1, 3, , 4]); + return [4 /*yield*/, args.deps.axios({ + method: 'post', + url: "".concat(arrApp.host, "/api/").concat(API_VERSION, "/command"), + headers: arrApp.headers, + data: arrApp.buildRefreshRequest(id), + })]; + case 2: + _a.sent(); + args.jobLog("\u2714 ".concat(arrApp.content, " '").concat(id, "' refreshed in ").concat(arrApp.name, ".")); + return [2 /*return*/, true]; + case 3: + error_1 = _a.sent(); + args.jobLog("Error refreshing ".concat(arrApp.name, ": ").concat(error_1.message)); + return [2 /*return*/, false]; + case 4: return [2 /*return*/]; + } + }); +}); }; var plugin = function (args) { return __awaiter(void 0, void 0, void 0, function () { - var lib, refreshed, arr, arr_host, arrHost, headers, arrApp, id; - var _a; - return __generator(this, function (_b) { - switch (_b.label) { + var lib, _a, arr, arr_api_key, arr_host, arrApp, id, refreshed; + var _b; + return __generator(this, function (_c) { + switch (_c.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); - 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; - headers = { - 'Content-Type': 'application/json', - 'X-Api-Key': String(args.inputs.arr_api_key), - Accept: 'application/json', - }; - arrApp = arr === 'radarr' - ? { - name: arr, - host: arrHost, - headers: headers, - content: 'Movie', - delegates: { - buildRefreshResquestData: function (id) { return JSON.stringify({ name: 'RefreshMovie', movieIds: [id] }); }, - }, - } - : { - name: arr, - host: arrHost, - headers: headers, - content: 'Serie', - delegates: { - buildRefreshResquestData: function (id) { return JSON.stringify({ name: 'RefreshSeries', seriesId: id }); }, - }, - }; + _a = args.inputs, arr = _a.arr, arr_api_key = _a.arr_api_key, arr_host = _a.arr_host; + arrApp = createArrApp(arr, arr_host, arr_api_key); args.jobLog('Going to force scan'); args.jobLog("Refreshing ".concat(arrApp.name, "...")); - id = Number((_a = args.variables.user.ArrId) !== null && _a !== void 0 ? _a : -1); - if (!(id !== -1)) return [3 /*break*/, 2]; - // Using command endpoint to queue a refresh task - return [4 /*yield*/, args.deps.axios({ - method: 'post', - url: "".concat(arrApp.host, "/api/v3/command"), - headers: headers, - data: arrApp.delegates.buildRefreshResquestData(id), - })]; + id = Number((_b = args.variables.user.ArrId) !== null && _b !== void 0 ? _b : -1); + args.jobLog("ArrId ".concat(id, " read from flow variables")); + return [4 /*yield*/, refreshArr(arrApp, id, args)]; case 1: - // Using command endpoint to queue a refresh task - _b.sent(); - refreshed = true; - args.jobLog("\u2714 ".concat(arrApp.content, " '").concat(id, "' refreshed in ").concat(arrApp.name, ".")); - _b.label = 2; - case 2: return [2 /*return*/, { - outputFileObj: args.inputFileObj, - outputNumber: refreshed ? 1 : 2, - variables: args.variables, - }]; + refreshed = _c.sent(); + return [2 /*return*/, { + outputFileObj: args.inputFileObj, + outputNumber: refreshed ? 1 : 2, + variables: args.variables, + }]; } }); }); }; diff --git a/FlowPluginsTs/CommunityFlowPlugins/tools/notifyRadarrOrSonarr/3.0.0/index.ts b/FlowPluginsTs/CommunityFlowPlugins/tools/notifyRadarrOrSonarr/3.0.0/index.ts index 01d283a41..7a028ecc0 100644 --- a/FlowPluginsTs/CommunityFlowPlugins/tools/notifyRadarrOrSonarr/3.0.0/index.ts +++ b/FlowPluginsTs/CommunityFlowPlugins/tools/notifyRadarrOrSonarr/3.0.0/index.ts @@ -47,94 +47,118 @@ const details = (): IpluginDetails => ({ 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', + 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', }, ], outputs: [ - { - number: 1, - tooltip: 'Radarr or Sonarr notified', - }, - { - number: 2, - tooltip: 'Radarr or Sonarr do not know this file', - }, + { number: 1, tooltip: 'Radarr or Sonarr notified' }, + { number: 2, tooltip: 'Radarr or Sonarr do not know this file' }, ], }); interface IHTTPHeaders { - 'Content-Type': string, - 'X-Api-Key': string, - Accept: string, + 'Content-Type': string; + 'X-Api-Key': string; + Accept: string; } + interface IArrApp { - name: string, + name: string; + host: string; + headers: IHTTPHeaders; + content: string; + buildRefreshRequest: (id: number) => string; +} + +const API_VERSION = 'v3'; +const CONTENT_TYPE = 'application/json'; + +const arrConfigs = { + radarr: { + content: 'Movie', + buildRefreshRequest: (id: number) => JSON.stringify({ name: 'RefreshMovie', movieIds: [id] }), + }, + sonarr: { + content: 'Serie', + buildRefreshRequest: (id: number) => JSON.stringify({ name: 'RefreshSeries', seriesId: id }), + }, +} as const; + +const normalizeHost = (host: string): string => { + const trimmedHost = host.trim(); + return trimmedHost.endsWith('/') ? trimmedHost.slice(0, -1) : trimmedHost; +}; + +const createArrApp = ( + arrType: 'radarr' | 'sonarr', host: string, - headers: IHTTPHeaders, - content: string, - delegates: { - buildRefreshResquestData: (id: number) => string + apiKey: string, +): IArrApp => { + const headers: IHTTPHeaders = { + 'Content-Type': CONTENT_TYPE, + 'X-Api-Key': apiKey, + Accept: CONTENT_TYPE, + }; + + const config = arrConfigs[arrType]; + + return { + name: arrType, + host: normalizeHost(host), + headers, + content: config.content, + buildRefreshRequest: config.buildRefreshRequest, + }; +}; + +const refreshArr = async ( + arrApp: IArrApp, + id: number, + args: IpluginInputArgs, +): Promise => { + if (id === -1) { + args.jobLog('No valid ID found in variables'); + return false; } -} + + try { + await args.deps.axios({ + method: 'post', + url: `${arrApp.host}/api/${API_VERSION}/command`, + headers: arrApp.headers, + data: arrApp.buildRefreshRequest(id), + }); + + args.jobLog(`✔ ${arrApp.content} '${id}' refreshed in ${arrApp.name}.`); + return true; + } catch (error) { + args.jobLog(`Error refreshing ${arrApp.name}: ${(error as Error).message}`); + return false; + } +}; 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); - // 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 headers: IHTTPHeaders = { - 'Content-Type': 'application/json', - 'X-Api-Key': String(args.inputs.arr_api_key), - Accept: 'application/json', + const { arr, arr_api_key, arr_host } = args.inputs as { + arr: 'radarr' | 'sonarr'; + arr_api_key: string; + arr_host: string; }; - const arrApp: IArrApp = arr === 'radarr' - ? { - name: arr, - host: arrHost, - headers, - content: 'Movie', - delegates: { - buildRefreshResquestData: - (id) => JSON.stringify({ name: 'RefreshMovie', movieIds: [id] }), - }, - } - : { - name: arr, - host: arrHost, - headers, - content: 'Serie', - delegates: { - buildRefreshResquestData: - (id) => JSON.stringify({ name: 'RefreshSeries', seriesId: id }), - }, - }; + const arrApp = createArrApp(arr, arr_host, arr_api_key); args.jobLog('Going to force scan'); args.jobLog(`Refreshing ${arrApp.name}...`); const id = Number(args.variables.user.ArrId ?? -1); - if (id !== -1) { - // Using command endpoint to queue a refresh task - await args.deps.axios({ - method: 'post', - url: `${arrApp.host}/api/v3/command`, - headers, - data: arrApp.delegates.buildRefreshResquestData(id), - }); - - refreshed = true; - args.jobLog(`✔ ${arrApp.content} '${id}' refreshed in ${arrApp.name}.`); - } + args.jobLog(`ArrId ${id} read from flow variables`); + const refreshed = await refreshArr(arrApp, id, args); return { outputFileObj: args.inputFileObj, @@ -143,7 +167,4 @@ const plugin = async (args: IpluginInputArgs): Promise => { }; }; -export { - details, - plugin, -}; +export { details, plugin };