diff --git a/FlowPlugins/CommunityFlowPlugins/tools/setFlowVariablesFromRadarrOrSonarr/1.0.0/index.js b/FlowPlugins/CommunityFlowPlugins/tools/setFlowVariablesFromRadarrOrSonarr/1.0.0/index.js index 724761d18..da3a95270 100644 --- a/FlowPlugins/CommunityFlowPlugins/tools/setFlowVariablesFromRadarrOrSonarr/1.0.0/index.js +++ b/FlowPlugins/CommunityFlowPlugins/tools/setFlowVariablesFromRadarrOrSonarr/1.0.0/index.js @@ -162,6 +162,33 @@ var getFileInfo = function (args, arrApp, fileName) { return __awaiter(void 0, v } }); }); }; +var getLanguageCode = function (args, languageName) { return __awaiter(void 0, void 0, void 0, function () { + var url, response, languages; + var _a, _b; + return __generator(this, function (_c) { + switch (_c.label) { + case 0: + url = 'https://data.opendatasoft.com/api/explore/v2.1/catalog/datasets/' + + 'iso-language-codes-639-1-and-639-2@public/' + + "records?select=alpha3_b&where=english%20%3D%20%22".concat(languageName, "%22&limit=1"); + return [4 /*yield*/, fetch(url)]; + case 1: + response = _c.sent(); + if (!response.ok) { + args.jobLog('Failed to fetch language data'); + return [2 /*return*/, null]; + } + return [4 /*yield*/, response.json()]; + case 2: + languages = _c.sent(); + if (languages.total_count !== 1) { + args.jobLog('Failed to fetch language data'); + return [2 /*return*/, null]; + } + return [2 /*return*/, (_b = ((_a = languages.results[0]) === null || _a === void 0 ? void 0 : _a.alpha3_b)) !== null && _b !== void 0 ? _b : null]; + } + }); +}); }; // eslint-disable-next-line @typescript-eslint/no-unused-vars var plugin = function (args) { return __awaiter(void 0, void 0, void 0, function () { var lib, isSuccessful, arr, arr_host, arrHost, originalFileName, currentFileName, headers, arrApp, fInfo; @@ -194,25 +221,35 @@ var plugin = function (args) { return __awaiter(void 0, void 0, void 0, function var _a, _b, _c, _d, _e, _f, _g; 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), - languageCode: (_g = (_f = (_e = (_d = lookupResponse === null || lookupResponse === void 0 ? void 0 : lookupResponse.data) === null || _d === void 0 ? void 0 : _d.at(0)) === null || _e === void 0 ? void 0 : _e.originalLanguage) === null || _f === void 0 ? void 0 : _f.name) !== null && _g !== void 0 ? _g : '', + languageName: (_g = (_f = (_e = (_d = lookupResponse === null || lookupResponse === void 0 ? void 0 : lookupResponse.data) === null || _d === void 0 ? void 0 : _d.at(0)) === null || _e === void 0 ? void 0 : _e.originalLanguage) === null || _f === void 0 ? void 0 : _f.name) !== null && _g !== void 0 ? _g : '', }); }, getFileInfoFromParseResponse: function (parseResponse) { var _a, _b, _c, _d, _e, _f, _g; 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), - languageCode: (_g = (_f = (_e = (_d = parseResponse === null || parseResponse === void 0 ? void 0 : parseResponse.data) === null || _d === void 0 ? void 0 : _d.movie) === null || _e === void 0 ? void 0 : _e.originalLanguage) === null || _f === void 0 ? void 0 : _f.name) !== null && _g !== void 0 ? _g : '', + languageName: (_g = (_f = (_e = (_d = parseResponse === null || parseResponse === void 0 ? void 0 : parseResponse.data) === null || _d === void 0 ? void 0 : _d.movie) === null || _e === void 0 ? void 0 : _e.originalLanguage) === null || _f === void 0 ? void 0 : _f.name) !== null && _g !== void 0 ? _g : '', }); }, - setFlowVariables: function (fInfo) { + setFlowVariables: function (fInfo) { return __awaiter(void 0, void 0, void 0, function () { + var languageCode; var _a, _b; - // eslint-disable-next-line no-param-reassign - args.variables.user.ArrId = fInfo.id; - args.jobLog("Setting variable ArrId to ".concat(fInfo.id)); - // eslint-disable-next-line no-param-reassign - args.variables.user.ArrOriginalLanguageCode = (_a = fInfo.languageCode) !== null && _a !== void 0 ? _a : ''; - args.jobLog("Setting variable ArrOriginalLanguageCode to ".concat((_b = fInfo.languageCode) !== null && _b !== void 0 ? _b : '')); - }, + return __generator(this, function (_c) { + switch (_c.label) { + case 0: + // eslint-disable-next-line no-param-reassign + args.variables.user.ArrId = fInfo.id; + args.jobLog("Setting variable ArrId to ".concat(fInfo.id)); + return [4 /*yield*/, getLanguageCode(args, (_a = fInfo.languageName) !== null && _a !== void 0 ? _a : '')]; + case 1: + languageCode = (_b = (_c.sent())) !== null && _b !== void 0 ? _b : ''; + // eslint-disable-next-line no-param-reassign + args.variables.user.ArrOriginalLanguageCode = languageCode; + args.jobLog("Setting variable ArrOriginalLanguageCode to ".concat(languageCode)); + return [2 /*return*/]; + } + }); + }); }, }, } : { @@ -225,7 +262,7 @@ var plugin = function (args) { return __awaiter(void 0, void 0, void 0, function var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o; 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), - languageCode: (_g = (_f = (_e = (_d = lookupResponse === null || lookupResponse === void 0 ? void 0 : lookupResponse.data) === null || _d === void 0 ? void 0 : _d.at(0)) === null || _e === void 0 ? void 0 : _e.originalLanguage) === null || _f === void 0 ? void 0 : _f.name) !== null && _g !== void 0 ? _g : '', + languageName: (_g = (_f = (_e = (_d = lookupResponse === null || lookupResponse === void 0 ? void 0 : lookupResponse.data) === null || _d === void 0 ? void 0 : _d.at(0)) === null || _e === void 0 ? void 0 : _e.originalLanguage) === null || _f === void 0 ? void 0 : _f.name) !== null && _g !== void 0 ? _g : '', }; if (fInfo.id !== '-1') { var seasonEpisodenumber = (_j = (_h = /\bS\d{1,3}E\d{1,4}\b/i.exec(fileName)) === null || _h === void 0 ? void 0 : _h.at(0)) !== null && _j !== void 0 ? _j : ''; @@ -242,24 +279,34 @@ var plugin = function (args) { return __awaiter(void 0, void 0, void 0, function 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, - languageCode: (_p = (_o = (_m = (_l = parseResponse === null || parseResponse === void 0 ? void 0 : parseResponse.data) === null || _l === void 0 ? void 0 : _l.series) === null || _m === void 0 ? void 0 : _m.originalLanguage) === null || _o === void 0 ? void 0 : _o.name) !== null && _p !== void 0 ? _p : '', + languageName: (_p = (_o = (_m = (_l = parseResponse === null || parseResponse === void 0 ? void 0 : parseResponse.data) === null || _l === void 0 ? void 0 : _l.series) === null || _m === void 0 ? void 0 : _m.originalLanguage) === null || _o === void 0 ? void 0 : _o.name) !== null && _p !== void 0 ? _p : '', }); }, - setFlowVariables: function (fInfo) { + setFlowVariables: function (fInfo) { return __awaiter(void 0, void 0, void 0, function () { + var languageCode; var _a, _b, _c, _d, _e, _f; - // eslint-disable-next-line no-param-reassign - args.variables.user.ArrId = fInfo.id; - args.jobLog("Setting variable ArrId to ".concat(fInfo.id)); - // eslint-disable-next-line no-param-reassign - args.variables.user.ArrSeasonNumber = String((_a = fInfo.seasonNumber) !== null && _a !== void 0 ? _a : 0); - args.jobLog("Setting variable ArrSeasonNumber to ".concat(String((_b = fInfo.seasonNumber) !== null && _b !== void 0 ? _b : 0))); - // eslint-disable-next-line no-param-reassign - args.variables.user.ArrEpisodeNumber = String((_c = fInfo.episodeNumber) !== null && _c !== void 0 ? _c : 0); - args.jobLog("Setting variable ArrEpisodeNumber to ".concat((_d = fInfo.episodeNumber) !== null && _d !== void 0 ? _d : 0)); - // eslint-disable-next-line no-param-reassign - args.variables.user.ArrOriginalLanguageCode = (_e = fInfo.languageCode) !== null && _e !== void 0 ? _e : ''; - args.jobLog("Setting variable ArrOriginalLanguageCode to ".concat((_f = fInfo.languageCode) !== null && _f !== void 0 ? _f : '')); - }, + return __generator(this, function (_g) { + switch (_g.label) { + case 0: + // eslint-disable-next-line no-param-reassign + args.variables.user.ArrId = fInfo.id; + args.jobLog("Setting variable ArrId to ".concat(fInfo.id)); + return [4 /*yield*/, getLanguageCode(args, (_a = fInfo.languageName) !== null && _a !== void 0 ? _a : '')]; + case 1: + languageCode = (_b = (_g.sent())) !== null && _b !== void 0 ? _b : ''; + // eslint-disable-next-line no-param-reassign + args.variables.user.ArrOriginalLanguageCode = languageCode; + args.jobLog("Setting variable ArrOriginalLanguageCode to ".concat(languageCode)); + // eslint-disable-next-line no-param-reassign + args.variables.user.ArrSeasonNumber = String((_c = fInfo.seasonNumber) !== null && _c !== void 0 ? _c : 0); + args.jobLog("Setting variable ArrSeasonNumber to ".concat(String((_d = fInfo.seasonNumber) !== null && _d !== void 0 ? _d : 0))); + // eslint-disable-next-line no-param-reassign + args.variables.user.ArrEpisodeNumber = String((_e = fInfo.episodeNumber) !== null && _e !== void 0 ? _e : 0); + args.jobLog("Setting variable ArrEpisodeNumber to ".concat((_f = fInfo.episodeNumber) !== null && _f !== void 0 ? _f : 0)); + return [2 /*return*/]; + } + }); + }); }, }, }; return [4 /*yield*/, getFileInfo(args, arrApp, originalFileName)]; @@ -271,20 +318,21 @@ var plugin = function (args) { return __awaiter(void 0, void 0, void 0, function fInfo = _e.sent(); _e.label = 3; case 3: - // Checking that the file has been found - if (fInfo.id !== '-1') { - if (!args.variables.user) { - // eslint-disable-next-line no-param-reassign - args.variables.user = {}; - } - arrApp.delegates.setFlowVariables(fInfo); - isSuccessful = true; + if (!(fInfo.id !== '-1')) return [3 /*break*/, 5]; + if (!args.variables.user) { + // eslint-disable-next-line no-param-reassign + args.variables.user = {}; } - return [2 /*return*/, { - outputFileObj: args.inputFileObj, - outputNumber: isSuccessful ? 1 : 2, - variables: args.variables, - }]; + return [4 /*yield*/, arrApp.delegates.setFlowVariables(fInfo)]; + case 4: + _e.sent(); + isSuccessful = true; + _e.label = 5; + case 5: return [2 /*return*/, { + outputFileObj: args.inputFileObj, + outputNumber: isSuccessful ? 1 : 2, + variables: args.variables, + }]; } }); }); }; diff --git a/FlowPluginsTs/CommunityFlowPlugins/tools/setFlowVariablesFromRadarrOrSonarr/1.0.0/index.ts b/FlowPluginsTs/CommunityFlowPlugins/tools/setFlowVariablesFromRadarrOrSonarr/1.0.0/index.ts index 3ce74a8b2..30f460179 100644 --- a/FlowPluginsTs/CommunityFlowPlugins/tools/setFlowVariablesFromRadarrOrSonarr/1.0.0/index.ts +++ b/FlowPluginsTs/CommunityFlowPlugins/tools/setFlowVariablesFromRadarrOrSonarr/1.0.0/index.ts @@ -83,7 +83,7 @@ interface IFileInfo { id: string, seasonNumber?: number, episodeNumber?: number, - languageCode?: string, + languageName?: string, } interface IOriginalLanguage { id: number, @@ -122,8 +122,14 @@ interface IArrApp { getFileInfoFromParseResponse: (parseResponse: IParseResponse) => IFileInfo, setFlowVariables: - (fInfo: IFileInfo) => void + (fInfo: IFileInfo) => Promise } +}interface ILanguage { + alpha3_b: string; +} +interface ILanguagesResponse { + total_count: number, + results: ILanguage[] } const getFileInfoFromLookup = async ( @@ -176,6 +182,28 @@ const getFileInfo = async ( ? getFileInfoFromParse(args, arrApp, fileName) : fInfo; }; +const getLanguageCode = async ( + args: IpluginInputArgs, + languageName: string, +) + : Promise => { + const url = 'https://data.opendatasoft.com/api/explore/v2.1/catalog/datasets/' + + 'iso-language-codes-639-1-and-639-2@public/' + + `records?select=alpha3_b&where=english%20%3D%20%22${languageName}%22&limit=1`; + const response = await fetch(url); + if (!response.ok) { + args.jobLog('Failed to fetch language data'); + return null; + } + + const languages: ILanguagesResponse = await response.json(); + if (languages.total_count !== 1) { + args.jobLog('Failed to fetch language data'); + return null; + } + + return (languages.results[0]?.alpha3_b) ?? null; +}; // eslint-disable-next-line @typescript-eslint/no-unused-vars const plugin = async (args: IpluginInputArgs): Promise => { @@ -205,21 +233,23 @@ const plugin = async (args: IpluginInputArgs): Promise => { getFileInfoFromLookupResponse: (lookupResponse) => ({ id: String(lookupResponse?.data?.at(0)?.id ?? -1), - languageCode: lookupResponse?.data?.at(0)?.originalLanguage?.name ?? '', + languageName: lookupResponse?.data?.at(0)?.originalLanguage?.name ?? '', }), getFileInfoFromParseResponse: (parseResponse) => ({ id: String(parseResponse?.data?.movie?.id ?? -1), - languageCode: parseResponse?.data?.movie?.originalLanguage?.name ?? '', + languageName: parseResponse?.data?.movie?.originalLanguage?.name ?? '', }), setFlowVariables: - (fInfo) => { + async (fInfo) => { // eslint-disable-next-line no-param-reassign args.variables.user.ArrId = fInfo.id; args.jobLog(`Setting variable ArrId to ${fInfo.id}`); + + const languageCode = (await getLanguageCode(args, fInfo.languageName ?? '')) ?? ''; // eslint-disable-next-line no-param-reassign - args.variables.user.ArrOriginalLanguageCode = fInfo.languageCode ?? ''; - args.jobLog(`Setting variable ArrOriginalLanguageCode to ${fInfo.languageCode ?? ''}`); + args.variables.user.ArrOriginalLanguageCode = languageCode; + args.jobLog(`Setting variable ArrOriginalLanguageCode to ${languageCode}`); }, }, } @@ -233,7 +263,7 @@ const plugin = async (args: IpluginInputArgs): Promise => { (lookupResponse, fileName) => { const fInfo: IFileInfo = { id: String(lookupResponse?.data?.at(0)?.id ?? -1), - languageCode: lookupResponse?.data?.at(0)?.originalLanguage?.name ?? '', + languageName: lookupResponse?.data?.at(0)?.originalLanguage?.name ?? '', }; if (fInfo.id !== '-1') { const seasonEpisodenumber = /\bS\d{1,3}E\d{1,4}\b/i.exec(fileName)?.at(0) ?? ''; @@ -250,22 +280,26 @@ const plugin = async (args: IpluginInputArgs): Promise => { id: String(parseResponse?.data?.series?.id ?? -1), seasonNumber: parseResponse?.data?.parsedEpisodeInfo?.seasonNumber ?? 1, episodeNumber: parseResponse?.data?.parsedEpisodeInfo?.episodeNumbers?.at(0) ?? 1, - languageCode: parseResponse?.data?.series?.originalLanguage?.name ?? '', + languageName: parseResponse?.data?.series?.originalLanguage?.name ?? '', }), setFlowVariables: - (fInfo) => { + async (fInfo) => { // eslint-disable-next-line no-param-reassign args.variables.user.ArrId = fInfo.id; args.jobLog(`Setting variable ArrId to ${fInfo.id}`); + + const languageCode = (await getLanguageCode(args, fInfo.languageName ?? '')) ?? ''; + // eslint-disable-next-line no-param-reassign + args.variables.user.ArrOriginalLanguageCode = languageCode; + args.jobLog(`Setting variable ArrOriginalLanguageCode to ${languageCode}`); + // eslint-disable-next-line no-param-reassign args.variables.user.ArrSeasonNumber = String(fInfo.seasonNumber ?? 0); args.jobLog(`Setting variable ArrSeasonNumber to ${String(fInfo.seasonNumber ?? 0)}`); + // eslint-disable-next-line no-param-reassign args.variables.user.ArrEpisodeNumber = String(fInfo.episodeNumber ?? 0); args.jobLog(`Setting variable ArrEpisodeNumber to ${fInfo.episodeNumber ?? 0}`); - // eslint-disable-next-line no-param-reassign - args.variables.user.ArrOriginalLanguageCode = fInfo.languageCode ?? ''; - args.jobLog(`Setting variable ArrOriginalLanguageCode to ${fInfo.languageCode ?? ''}`); }, }, }; @@ -284,7 +318,7 @@ const plugin = async (args: IpluginInputArgs): Promise => { args.variables.user = {}; } - arrApp.delegates.setFlowVariables(fInfo); + await arrApp.delegates.setFlowVariables(fInfo); isSuccessful = true; }