After thorough testing, some renaming and refactoring

make-only-subtitle-default
jeanchristophe.mqt@gmail.com 2 years ago
parent 5198d4b805
commit f9e9215bcb

@ -100,7 +100,7 @@ var details = function () { return ({
],
}); };
exports.details = details;
var getId = function (args, arr, arrHost, headers, fileName, refreshType) { return __awaiter(void 0, void 0, void 0, function () {
var getId = function (args, arrApp, fileName) { return __awaiter(void 0, void 0, void 0, function () {
var imdbId, id, _a, _b, _c, _d;
var _e, _f, _g, _h, _j;
return __generator(this, function (_k) {
@ -111,8 +111,8 @@ var getId = function (args, arr, arrHost, headers, fileName, refreshType) { retu
_b = Number;
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:
_a = _b.apply(void 0, [(_j = (_h = (_g = (_k.sent()).data) === null || _g === void 0 ? void 0 : _g.at(0)) === null || _h === void 0 ? void 0 : _h.id) !== null && _j !== void 0 ? _j : -1]);
@ -122,24 +122,24 @@ var getId = function (args, arr, arrHost, headers, fileName, refreshType) { retu
_k.label = 3;
case 3:
id = _a;
args.jobLog("".concat(refreshType.content, " ").concat(id !== -1 ? "".concat(id, " found") : 'not found', " for imdb '").concat(imdbId, "'"));
args.jobLog("".concat(arrApp.content, " ").concat(id !== -1 ? "'".concat(id, "' found") : 'not found', " for imdb '").concat(imdbId, "'"));
if (!(id === -1)) return [3 /*break*/, 5];
_d = (_c = refreshType.delegates).getIdFromParseResponse;
_d = (_c = arrApp.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,
url: "".concat(arrApp.host, "/api/v3/parse?title=").concat(encodeURIComponent((0, fileUtils_1.getFileName)(fileName))),
headers: arrApp.headers,
})];
case 4:
id = _d.apply(_c, [(_k.sent())]);
args.jobLog("".concat(refreshType.content, " ").concat(id !== -1 ? "".concat(id, " found") : 'not found', " for '").concat((0, fileUtils_1.getFileName)(fileName), "'"));
args.jobLog("".concat(arrApp.content, " ").concat(id !== -1 ? "'".concat(id, "' found") : 'not found', " for '").concat((0, fileUtils_1.getFileName)(fileName), "'"));
_k.label = 5;
case 5: return [2 /*return*/, id];
}
});
}); };
var plugin = function (args) { return __awaiter(void 0, void 0, void 0, function () {
var lib, refreshed, arr, arr_host, arrHost, originalFileName, currentFileName, headers, refreshType, id;
var lib, refreshed, arr, arr_host, arrHost, originalFileName, currentFileName, headers, arrApp, id;
var _a, _b, _c, _d;
return __generator(this, function (_e) {
switch (_e.label) {
@ -158,30 +158,34 @@ 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',
};
refreshType = arr === 'radarr'
arrApp = arr === 'radarr'
? {
appName: 'Radarr',
name: arr,
host: arrHost,
headers: headers,
content: 'Movie',
delegates: {
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); },
getIdFromParseResponse: function (parseResponse) { var _a, _b, _c; return Number((_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); },
buildRefreshResquestData: function (id) { return JSON.stringify({ name: 'RefreshMovie', movieIds: [id] }); },
},
}
: {
appName: 'Sonarr',
name: arr,
host: arrHost,
headers: headers,
content: 'Serie',
delegates: {
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); },
getIdFromParseResponse: function (parseResponse) { var _a, _b, _c; return Number((_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); },
buildRefreshResquestData: function (id) { return JSON.stringify({ name: 'RefreshSeries', seriesId: id }); },
},
};
args.jobLog('Going to force scan');
args.jobLog("Refreshing ".concat(refreshType.appName, "..."));
return [4 /*yield*/, getId(args, arr, arrHost, headers, originalFileName, refreshType)];
args.jobLog("Refreshing ".concat(arrApp.name, "..."));
return [4 /*yield*/, getId(args, arrApp, originalFileName)];
case 1:
id = _e.sent();
if (!(id === -1 && currentFileName !== originalFileName)) return [3 /*break*/, 3];
return [4 /*yield*/, getId(args, arr, arrHost, headers, currentFileName, refreshType)];
return [4 /*yield*/, getId(args, arrApp, currentFileName)];
case 2:
id = _e.sent();
_e.label = 3;
@ -190,15 +194,15 @@ var plugin = function (args) { return __awaiter(void 0, void 0, void 0, function
// Using command endpoint to queue a refresh task
return [4 /*yield*/, args.deps.axios({
method: 'post',
url: "".concat(arrHost, "/api/v3/command"),
url: "".concat(arrApp.host, "/api/v3/command"),
headers: headers,
data: refreshType.delegates.buildRefreshResquestData(id),
data: arrApp.delegates.buildRefreshResquestData(id),
})];
case 4:
// Using command endpoint to queue a refresh task
_e.sent();
refreshed = true;
args.jobLog("\u2714 ".concat(refreshType.content, " ").concat(id, " refreshed in ").concat(refreshType.appName, "."));
args.jobLog("\u2714 ".concat(arrApp.content, " '").concat(id, "' refreshed in ").concat(arrApp.name, "."));
_e.label = 5;
case 5: return [2 /*return*/, {
outputFileObj: args.inputFileObj,

@ -72,49 +72,47 @@ interface IHTTPHeaders {
'X-Api-Key': string,
Accept: string,
}
interface IParsedRequestResult {
interface IParseResponse {
data: {
movie?: { id: number },
series?: { id: number },
},
}
interface IRefreshType {
appName: string,
interface IArrApp {
name: string,
host: string,
headers: IHTTPHeaders,
content: string,
delegates: {
getIdFromParseResponse: (parseRequestResult: IParsedRequestResult) => number,
getIdFromParseResponse: (parseResponse: IParseResponse) => number,
buildRefreshResquestData: (id: number) => string
}
}
const getId = async (
args: IpluginInputArgs,
arr: string,
arrHost: string, headers: IHTTPHeaders,
arrApp: IArrApp,
fileName: string,
refreshType: IRefreshType,
)
: Promise<number> => {
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({
? Number((await args.deps.axios({
method: 'get',
url: `${arrHost}/api/v3/${arr === 'radarr' ? 'movie' : 'series'}/lookup?term=imdb:${imdbId}`,
headers,
})).data?.at(0)?.id ?? -1,
)
url: `${arrApp.host}/api/v3/${arrApp.name === 'radarr' ? 'movie' : 'series'}/lookup?term=imdb:${imdbId}`,
headers: arrApp.headers,
})).data?.at(0)?.id ?? -1)
: -1;
args.jobLog(`${refreshType.content} ${id !== -1 ? `${id} found` : 'not found'} for imdb '${imdbId}'`);
args.jobLog(`${arrApp.content} ${id !== -1 ? `'${id}' found` : 'not found'} for imdb '${imdbId}'`);
if (id === -1) {
id = refreshType.delegates.getIdFromParseResponse(
id = arrApp.delegates.getIdFromParseResponse(
(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,
})),
);
args.jobLog(`${refreshType.content} ${id !== -1 ? `${id} found` : 'not found'} for '${getFileName(fileName)}'`);
args.jobLog(`${arrApp.content} ${id !== -1 ? `'${id}' found` : 'not found'} for '${getFileName(fileName)}'`);
}
return id;
};
@ -136,35 +134,39 @@ const plugin = async (args: IpluginInputArgs): Promise<IpluginOutputArgs> => {
'X-Api-Key': String(args.inputs.arr_api_key),
Accept: 'application/json',
};
const refreshType: IRefreshType = arr === 'radarr'
const arrApp: IArrApp = arr === 'radarr'
? {
appName: 'Radarr',
name: arr,
host: arrHost,
headers,
content: 'Movie',
delegates: {
getIdFromParseResponse:
(parseRequestResult: IParsedRequestResult) => Number(parseRequestResult.data?.movie?.id ?? -1),
(parseResponse: IParseResponse) => Number(parseResponse?.data?.movie?.id ?? -1),
buildRefreshResquestData:
(id) => JSON.stringify({ name: 'RefreshMovie', movieIds: [id] }),
},
}
: {
appName: 'Sonarr',
name: arr,
host: arrHost,
headers,
content: 'Serie',
delegates: {
getIdFromParseResponse:
(parseRequestResult: IParsedRequestResult) => Number(parseRequestResult.data?.series?.id ?? -1),
(parseResponse: IParseResponse) => Number(parseResponse?.data?.series?.id ?? -1),
buildRefreshResquestData:
(id) => JSON.stringify({ name: 'RefreshSeries', seriesId: id }),
},
};
args.jobLog('Going to force scan');
args.jobLog(`Refreshing ${refreshType.appName}...`);
args.jobLog(`Refreshing ${arrApp.name}...`);
let id = await getId(args, arr, arrHost, headers, originalFileName, refreshType);
let id = await getId(args, arrApp, originalFileName);
// Useful in some edge cases
if (id === -1 && currentFileName !== originalFileName) {
id = await getId(args, arr, arrHost, headers, currentFileName, refreshType);
id = await getId(args, arrApp, currentFileName);
}
// Checking that the file has been found
@ -172,13 +174,13 @@ const plugin = async (args: IpluginInputArgs): Promise<IpluginOutputArgs> => {
// Using command endpoint to queue a refresh task
await args.deps.axios({
method: 'post',
url: `${arrHost}/api/v3/command`,
url: `${arrApp.host}/api/v3/command`,
headers,
data: refreshType.delegates.buildRefreshResquestData(id),
data: arrApp.delegates.buildRefreshResquestData(id),
});
refreshed = true;
args.jobLog(`${refreshType.content} ${id} refreshed in ${refreshType.appName}.`);
args.jobLog(`${arrApp.content} '${id}' refreshed in ${arrApp.name}.`);
}
return {

Loading…
Cancel
Save