Added the possibility to find serieId from imdbId

make-only-subtitle-default
jeanchristophe.mqt@gmail.com 2 years ago
parent b85f59da2c
commit ecb519576a

@ -116,71 +116,65 @@ var details = function () { return ({
], ],
}); }; }); };
exports.details = details; exports.details = details;
var getMovieId = function (args, arrHost, headers, fileName, getNewPathType) { return __awaiter(void 0, void 0, void 0, function () { var getFileInfoFromLookup = function (args, arr, arrHost, headers, fileName, renameType) { return __awaiter(void 0, void 0, void 0, function () {
var imdbId, id, _a, _b; var fInfo, imdbId, lookupResponse;
var _c, _d, _e, _f, _g; var _a, _b;
return __generator(this, function (_h) { return __generator(this, function (_c) {
switch (_h.label) { switch (_c.label) {
case 0: 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 : ''; fInfo = { id: '-1' };
imdbId = (_b = (_a = /\b(tt|nm|co|ev|ch|ni)\d{7,10}\b/i.exec(fileName)) === null || _a === void 0 ? void 0 : _a.at(0)) !== null && _b !== void 0 ? _b : '';
if (!(imdbId !== '')) return [3 /*break*/, 2]; if (!(imdbId !== '')) return [3 /*break*/, 2];
_b = String;
return [4 /*yield*/, args.deps.axios({ return [4 /*yield*/, args.deps.axios({
method: 'get', method: 'get',
url: "".concat(arrHost, "/api/v3/movie/lookup?term=imdb:").concat(imdbId), url: "".concat(arrHost, "/api/v3/").concat(arr === 'radarr' ? 'movie' : 'series', "/lookup?term=imdb:").concat(imdbId),
headers: headers, headers: headers,
})]; })];
case 1: 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]); lookupResponse = _c.sent();
return [3 /*break*/, 3]; fInfo = renameType.delegates.getFileInfoFromLookupResponse(lookupResponse, fileName);
case 2: args.jobLog("".concat(renameType.content, " ").concat(fInfo.id !== '-1' ? "'".concat(fInfo.id, "' found") : 'not found')
_a = '-1'; + " for imdb '".concat(imdbId, "'"));
_h.label = 3; _c.label = 2;
case 3: case 2: return [2 /*return*/, fInfo];
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 getFileInfoFromParse = function (args, arr, arrHost, headers, fileName, renameType) { return __awaiter(void 0, void 0, void 0, function () {
var fdw, _a, _b; var fInfo, parseResponse;
var _c; return __generator(this, function (_a) {
return __generator(this, function (_d) { switch (_a.label) {
switch (_d.label) {
case 0: case 0:
_c = {}; fInfo = { id: '-1' };
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({ return [4 /*yield*/, args.deps.axios({
method: 'get', method: 'get',
url: "".concat(arrHost, "/api/v3/parse?title=").concat(encodeURIComponent((0, fileUtils_1.getFileName)(fileName))), url: "".concat(arrHost, "/api/v3/parse?title=").concat(encodeURIComponent((0, fileUtils_1.getFileName)(fileName))),
headers: headers, headers: headers,
})]; })];
case 4: case 1:
_b.fileDetails = _d.sent(); parseResponse = _a.sent();
fdw.id = renameType.delegates.getIdFromParseResponse(fdw); fInfo = renameType.delegates.getFileInfoFromParseResponse(parseResponse);
args.jobLog("".concat(renameType.content, " ").concat(fdw.id !== '-1' ? "".concat(fdw.id, " found") : 'not found', " for '") args.jobLog("".concat(renameType.content, " ").concat(fInfo.id !== '-1' ? "'".concat(fInfo.id, "' found") : 'not found')
+ "".concat((0, fileUtils_1.getFileName)(fileName), "'")); + " for '".concat((0, fileUtils_1.getFileName)(fileName), "'"));
_d.label = 5; return [2 /*return*/, fInfo];
case 5: return [2 /*return*/, fdw]; }
});
}); };
var getFileInfo = function (args, arr, arrHost, headers, fileName, renameType) { return __awaiter(void 0, void 0, void 0, function () {
var fInfo;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, getFileInfoFromLookup(args, arr, arrHost, headers, fileName, renameType)];
case 1:
fInfo = _a.sent();
return [2 /*return*/, (fInfo.id === '-1' || (arr === 'sonarr' && (fInfo.seasonNumber === -1 || fInfo.episodeNumber === -1)))
? getFileInfoFromParse(args, arr, arrHost, headers, fileName, renameType)
: fInfo];
} }
}); });
}); }; }); };
var plugin = function (args) { return __awaiter(void 0, void 0, void 0, function () { var plugin = function (args) { return __awaiter(void 0, void 0, void 0, function () {
var lib, newPath, isSuccessful, arr, arr_host, arrHost, originalFileName, currentFileName, headers, episodeNumber, renameType, fileDetailsWrapper, previewRenameRequestResult, fileToRename; var lib, newPath, isSuccessful, arr, arr_host, arrHost, originalFileName, currentFileName, headers, renameType, fInfo, previewRenameRequestResult, fileToRename;
var _a, _b, _c, _d; var _a, _b, _c, _d;
return __generator(this, function (_e) { return __generator(this, function (_e) {
switch (_e.label) { switch (_e.label) {
@ -200,14 +194,14 @@ var plugin = function (args) { return __awaiter(void 0, void 0, void 0, function
'X-Api-Key': String(args.inputs.arr_api_key), 'X-Api-Key': String(args.inputs.arr_api_key),
Accept: 'application/json', Accept: 'application/json',
}; };
episodeNumber = 0;
renameType = arr === 'radarr' renameType = arr === 'radarr'
? { ? {
appName: 'Radarr', appName: 'Radarr',
content: 'Movie', content: 'Movie',
delegates: { delegates: {
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); }, getFileInfoFromLookupResponse: function (lookupResponse) { var _a, _b, _c; 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) }); },
buildPreviewRenameResquestUrl: function (fileDetailsWrapper) { return "".concat(arrHost, "/api/v3/rename?movieId=").concat(fileDetailsWrapper.id); }, getFileInfoFromParseResponse: function (parseResponse) { var _a, _b, _c; 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) }); },
buildPreviewRenameResquestUrl: function (fInfo) { return "".concat(arrHost, "/api/v3/rename?movieId=").concat(fInfo.id); },
getFileToRenameFromPreviewRenameResponse: function (previewRenameResponse) { var _a; return (_a = previewRenameResponse.data) === null || _a === void 0 ? void 0 : _a.at(0); }, getFileToRenameFromPreviewRenameResponse: function (previewRenameResponse) { var _a; return (_a = previewRenameResponse.data) === null || _a === void 0 ? void 0 : _a.at(0); },
}, },
} }
@ -215,40 +209,54 @@ var plugin = function (args) { return __awaiter(void 0, void 0, void 0, function
appName: 'Sonarr', appName: 'Sonarr',
content: 'Serie', content: 'Serie',
delegates: { delegates: {
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); }, getFileInfoFromLookupResponse: function (lookupResponse, fileName) {
buildPreviewRenameResquestUrl: function (fileDetailsWrapper) { var _a, _b, _c, _d, _e, _f, _g, _h, _j;
var _a, _b, _c, _d, _e, _f; 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) };
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]; if (fInfo.id !== '-1') {
return "".concat(arrHost, "/api/v3/rename?seriesId=").concat(fileDetailsWrapper.id, "&seasonNumber=") var seasonEpisodenumber = (_e = (_d = /\bS\d{1,3}E\d{1,4}\b/i.exec(fileName)) === null || _d === void 0 ? void 0 : _d.at(0)) !== null && _e !== void 0 ? _e : '';
+ "".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); var episodeNumber = (_g = (_f = /\d{1,4}$/i.exec(seasonEpisodenumber)) === null || _f === void 0 ? void 0 : _f.at(0)) !== null && _g !== void 0 ? _g : '';
fInfo.seasonNumber = Number((_j = (_h = /\d{1,3}/i
.exec(seasonEpisodenumber.slice(0, -episodeNumber.length))) === null || _h === void 0 ? void 0 : _h.at(0)) !== null && _j !== void 0 ? _j : '-1');
fInfo.episodeNumber = Number(episodeNumber !== '' ? episodeNumber : -1);
}
return fInfo;
},
getFileInfoFromParseResponse: function (parseResponse) {
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
return ({
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,
});
}, },
getFileToRenameFromPreviewRenameResponse: function (previewRenameResponse) { buildPreviewRenameResquestUrl: function (fInfo) { return "".concat(arrHost, "/api/v3/rename?seriesId=").concat(fInfo.id, "&seasonNumber=").concat(fInfo.seasonNumber); },
getFileToRenameFromPreviewRenameResponse: function (previewRenameResponse, fInfo) {
var _a; 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 (_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)) === fInfo.episodeNumber; });
}, },
}, },
}; };
args.jobLog('Going to apply new name'); args.jobLog('Going to apply new name');
args.jobLog("Renaming ".concat(renameType.appName, "...")); args.jobLog("Renaming ".concat(renameType.appName, "..."));
return [4 /*yield*/, getFileDetailsWrapper(args, arr, arrHost, headers, originalFileName, renameType)]; return [4 /*yield*/, getFileInfo(args, arr, arrHost, headers, originalFileName, renameType)];
case 1: case 1:
fileDetailsWrapper = _e.sent(); fInfo = _e.sent();
if (!(fileDetailsWrapper.id === '-1' && currentFileName !== originalFileName)) return [3 /*break*/, 3]; if (!(fInfo.id === '-1' && currentFileName !== originalFileName)) return [3 /*break*/, 3];
return [4 /*yield*/, getFileDetailsWrapper(args, arr, arrHost, headers, currentFileName, renameType)]; return [4 /*yield*/, getFileInfo(args, arr, arrHost, headers, currentFileName, renameType)];
case 2: case 2:
fileDetailsWrapper = _e.sent(); fInfo = _e.sent();
_e.label = 3; _e.label = 3;
case 3: case 3:
if (!(fileDetailsWrapper.id !== '-1')) return [3 /*break*/, 7]; if (!(fInfo.id !== '-1')) return [3 /*break*/, 7];
return [4 /*yield*/, args.deps.axios({ return [4 /*yield*/, args.deps.axios({
method: 'get', method: 'get',
url: renameType.delegates.buildPreviewRenameResquestUrl(fileDetailsWrapper), url: renameType.delegates.buildPreviewRenameResquestUrl(fInfo),
headers: headers, headers: headers,
})]; })];
case 4: case 4:
previewRenameRequestResult = _e.sent(); previewRenameRequestResult = _e.sent();
fileToRename = renameType.delegates fileToRename = renameType.delegates
.getFileToRenameFromPreviewRenameResponse(previewRenameRequestResult); .getFileToRenameFromPreviewRenameResponse(previewRenameRequestResult, fInfo);
if (!(fileToRename !== undefined)) return [3 /*break*/, 6]; if (!(fileToRename !== undefined)) return [3 /*break*/, 6];
newPath = "".concat((0, fileUtils_1.getFileAbosluteDir)(currentFileName), "/").concat((0, fileUtils_1.getFileName)(fileToRename.newPath), ".").concat((0, fileUtils_1.getContainer)(fileToRename.newPath)); newPath = "".concat((0, fileUtils_1.getFileAbosluteDir)(currentFileName), "/").concat((0, fileUtils_1.getFileName)(fileToRename.newPath), ".").concat((0, fileUtils_1.getContainer)(fileToRename.newPath));
return [4 /*yield*/, (0, fileMoveOrCopy_1.default)({ return [4 /*yield*/, (0, fileMoveOrCopy_1.default)({
@ -259,8 +267,6 @@ var plugin = function (args) { return __awaiter(void 0, void 0, void 0, function
})]; })];
case 5: case 5:
isSuccessful = _e.sent(); isSuccessful = _e.sent();
args.jobLog("\u2714 ".concat(renameType.content, " ").concat(fileDetailsWrapper.id, " renamed : ")
+ "'".concat(originalFileName, "' => '").concat(newPath, "'."));
return [3 /*break*/, 7]; return [3 /*break*/, 7];
case 6: case 6:
isSuccessful = true; isSuccessful = true;

@ -77,7 +77,15 @@ interface IHTTPHeaders {
'X-Api-Key': string, 'X-Api-Key': string,
Accept: string, Accept: string,
} }
interface IFileDetails { interface IFileInfo {
id: string,
seasonNumber?: number,
episodeNumber?: number
}
interface ILookupResponse {
data: [{ id: number }],
}
interface IParseResponse {
data: { data: {
movie?: { id: number }, movie?: { id: number },
series?: { id: number }, series?: { id: number },
@ -87,12 +95,8 @@ interface IFileDetails {
}, },
}, },
} }
interface IFileDetailsWrapper {
id: string
fileDetails?: IFileDetails
}
interface IFileToRename { interface IFileToRename {
newPath: string newPath: string,
episodeNumbers?: number[] episodeNumbers?: number[]
} }
interface IPreviewRenameResponse { interface IPreviewRenameResponse {
@ -102,38 +106,42 @@ interface IRenameType {
appName: string, appName: string,
content: string, content: string,
delegates: { delegates: {
getIdFromParseResponse: getFileInfoFromLookupResponse:
(fileDetailsWrapper: IFileDetailsWrapper) => string, (lookupResponse: ILookupResponse, fileName: string) => IFileInfo,
getFileInfoFromParseResponse:
(parseResponse: IParseResponse) => IFileInfo,
buildPreviewRenameResquestUrl: buildPreviewRenameResquestUrl:
(fileDetailsWrapper: IFileDetailsWrapper) => string, (fileInfo: IFileInfo) => string,
getFileToRenameFromPreviewRenameResponse: getFileToRenameFromPreviewRenameResponse:
(previewRenameResponse: IPreviewRenameResponse) => IFileToRename | undefined (previewRenameResponse: IPreviewRenameResponse, fileInfo: IFileInfo) => IFileToRename | undefined
} }
} }
const getMovieId = async ( const getFileInfoFromLookup = async (
args: IpluginInputArgs, args: IpluginInputArgs,
arr: string,
arrHost: string, arrHost: string,
headers: IHTTPHeaders, headers: IHTTPHeaders,
fileName: string, fileName: string,
getNewPathType: IRenameType, renameType: IRenameType,
) )
: Promise<string> => { : Promise<IFileInfo> => {
let fInfo: IFileInfo = { id: '-1' };
const imdbId = /\b(tt|nm|co|ev|ch|ni)\d{7,10}\b/i.exec(fileName)?.at(0) ?? ''; const imdbId = /\b(tt|nm|co|ev|ch|ni)\d{7,10}\b/i.exec(fileName)?.at(0) ?? '';
const id = (imdbId !== '') if (imdbId !== '') {
? String( const lookupResponse: ILookupResponse = await args.deps.axios({
(await args.deps.axios({ method: 'get',
method: 'get', url: `${arrHost}/api/v3/${arr === 'radarr' ? 'movie' : 'series'}/lookup?term=imdb:${imdbId}`,
url: `${arrHost}/api/v3/movie/lookup?term=imdb:${imdbId}`, headers,
headers, });
})).data?.at(0)?.id ?? -1, fInfo = renameType.delegates.getFileInfoFromLookupResponse(lookupResponse, fileName);
) args.jobLog(`${renameType.content} ${fInfo.id !== '-1' ? `'${fInfo.id}' found` : 'not found'}`
: '-1'; + ` for imdb '${imdbId}'`);
args.jobLog(`${getNewPathType.content} ${id !== '-1' ? `${id} found` : 'not found'} for imdb '${imdbId}'`); }
return id; return fInfo;
}; };
const getFileDetailsWrapper = async ( const getFileInfoFromParse = async (
args: IpluginInputArgs, args: IpluginInputArgs,
arr: string, arr: string,
arrHost: string, arrHost: string,
@ -141,22 +149,32 @@ const getFileDetailsWrapper = async (
fileName: string, fileName: string,
renameType: IRenameType, renameType: IRenameType,
) )
: Promise<IFileDetailsWrapper> => { : Promise<IFileInfo> => {
const fdw: IFileDetailsWrapper = { let fInfo: IFileInfo = { id: '-1' };
id: arr === 'radarr' ? await getMovieId(args, arrHost, headers, fileName, renameType) : '-1', const parseResponse: IParseResponse = await args.deps.axios({
fileDetails: undefined, method: 'get',
}; url: `${arrHost}/api/v3/parse?title=${encodeURIComponent(getFileName(fileName))}`,
if (fdw.id === '-1') { headers,
fdw.fileDetails = await args.deps.axios({ });
method: 'get', fInfo = renameType.delegates.getFileInfoFromParseResponse(parseResponse);
url: `${arrHost}/api/v3/parse?title=${encodeURIComponent(getFileName(fileName))}`, args.jobLog(`${renameType.content} ${fInfo.id !== '-1' ? `'${fInfo.id}' found` : 'not found'}`
headers, + ` for '${getFileName(fileName)}'`);
}); return fInfo;
fdw.id = renameType.delegates.getIdFromParseResponse(fdw); };
args.jobLog(`${renameType.content} ${fdw.id !== '-1' ? `${fdw.id} found` : 'not found'} for '`
+ `${getFileName(fileName)}'`); const getFileInfo = async (
} args: IpluginInputArgs,
return fdw; arr: string,
arrHost: string,
headers: IHTTPHeaders,
fileName: string,
renameType: IRenameType,
)
: Promise<IFileInfo> => {
const fInfo = await getFileInfoFromLookup(args, arr, arrHost, headers, fileName, renameType);
return (fInfo.id === '-1' || (arr === 'sonarr' && (fInfo.seasonNumber === -1 || fInfo.episodeNumber === -1)))
? getFileInfoFromParse(args, arr, arrHost, headers, fileName, renameType)
: fInfo;
}; };
const plugin = async (args: IpluginInputArgs): Promise<IpluginOutputArgs> => { const plugin = async (args: IpluginInputArgs): Promise<IpluginOutputArgs> => {
@ -177,16 +195,17 @@ const plugin = async (args: IpluginInputArgs): Promise<IpluginOutputArgs> => {
Accept: 'application/json', Accept: 'application/json',
}; };
let episodeNumber = 0;
const renameType: IRenameType = arr === 'radarr' const renameType: IRenameType = arr === 'radarr'
? { ? {
appName: 'Radarr', appName: 'Radarr',
content: 'Movie', content: 'Movie',
delegates: { delegates: {
getIdFromParseResponse: getFileInfoFromLookupResponse:
(fileDetailsWrapper) => String(fileDetailsWrapper.fileDetails?.data?.movie?.id ?? -1), (lookupResponse) => ({ id: String(lookupResponse?.data?.at(0)?.id ?? -1) }),
getFileInfoFromParseResponse:
(parseResponse) => ({ id: String(parseResponse?.data?.movie?.id ?? -1) }),
buildPreviewRenameResquestUrl: buildPreviewRenameResquestUrl:
(fileDetailsWrapper) => `${arrHost}/api/v3/rename?movieId=${fileDetailsWrapper.id}`, (fInfo) => `${arrHost}/api/v3/rename?movieId=${fInfo.id}`,
getFileToRenameFromPreviewRenameResponse: getFileToRenameFromPreviewRenameResponse:
(previewRenameResponse) => previewRenameResponse.data?.at(0), (previewRenameResponse) => previewRenameResponse.data?.at(0),
}, },
@ -195,39 +214,53 @@ const plugin = async (args: IpluginInputArgs): Promise<IpluginOutputArgs> => {
appName: 'Sonarr', appName: 'Sonarr',
content: 'Serie', content: 'Serie',
delegates: { delegates: {
getIdFromParseResponse: getFileInfoFromLookupResponse:
(fileDetailsWrapper) => String(fileDetailsWrapper.fileDetails?.data?.series?.id ?? -1), (lookupResponse, fileName) => {
buildPreviewRenameResquestUrl: const fInfo: IFileInfo = { id: String(lookupResponse?.data?.at(0)?.id ?? -1) };
(fileDetailsWrapper) => { if (fInfo.id !== '-1') {
[episodeNumber] = fileDetailsWrapper.fileDetails?.data.parsedEpisodeInfo?.episodeNumbers ?? [1]; const seasonEpisodenumber = /\bS\d{1,3}E\d{1,4}\b/i.exec(fileName)?.at(0) ?? '';
return `${arrHost}/api/v3/rename?seriesId=${fileDetailsWrapper.id}&seasonNumber=` const episodeNumber = /\d{1,4}$/i.exec(seasonEpisodenumber)?.at(0) ?? '';
+ `${fileDetailsWrapper.fileDetails?.data.parsedEpisodeInfo?.seasonNumber ?? 1}`; fInfo.seasonNumber = Number(/\d{1,3}/i
.exec(seasonEpisodenumber.slice(0, -episodeNumber.length))
?.at(0) ?? '-1');
fInfo.episodeNumber = Number(episodeNumber !== '' ? episodeNumber : -1);
}
return fInfo;
}, },
getFileInfoFromParseResponse:
(parseResponse) => ({
id: String(parseResponse?.data?.series?.id ?? -1),
seasonNumber: parseResponse?.data?.parsedEpisodeInfo?.seasonNumber ?? 1,
episodeNumber: parseResponse?.data?.parsedEpisodeInfo?.episodeNumbers?.at(0) ?? 1,
}),
buildPreviewRenameResquestUrl:
(fInfo) => `${arrHost}/api/v3/rename?seriesId=${fInfo.id}&seasonNumber=${fInfo.seasonNumber}`,
getFileToRenameFromPreviewRenameResponse: getFileToRenameFromPreviewRenameResponse:
(previewRenameResponse) => previewRenameResponse.data (previewRenameResponse, fInfo) => previewRenameResponse.data
?.find((episodeFile) => episodeFile.episodeNumbers?.at(0) === episodeNumber), ?.find((episodeFile) => episodeFile.episodeNumbers?.at(0) === fInfo.episodeNumber),
}, },
}; };
args.jobLog('Going to apply new name'); args.jobLog('Going to apply new name');
args.jobLog(`Renaming ${renameType.appName}...`); args.jobLog(`Renaming ${renameType.appName}...`);
let fileDetailsWrapper = await getFileDetailsWrapper(args, arr, arrHost, headers, originalFileName, renameType); // Retrieving movie or serie id, plus season and episode number for serie
let fInfo = await getFileInfo(args, arr, arrHost, headers, originalFileName, renameType);
// Useful in some edge cases // Useful in some edge cases
if (fileDetailsWrapper.id === '-1' && currentFileName !== originalFileName) { if (fInfo.id === '-1' && currentFileName !== originalFileName) {
fileDetailsWrapper = await getFileDetailsWrapper(args, arr, arrHost, headers, currentFileName, renameType); fInfo = await getFileInfo(args, arr, arrHost, headers, currentFileName, renameType);
} }
// Checking that the file has been found // Checking that the file has been found
if (fileDetailsWrapper.id !== '-1') { if (fInfo.id !== '-1') {
// Using rename endpoint to get ids of all the files that need renaming // Using rename endpoint to get ids of all the files that need renaming
const previewRenameRequestResult = await args.deps.axios({ const previewRenameRequestResult = await args.deps.axios({
method: 'get', method: 'get',
url: renameType.delegates.buildPreviewRenameResquestUrl(fileDetailsWrapper), url: renameType.delegates.buildPreviewRenameResquestUrl(fInfo),
headers, headers,
}); });
const fileToRename = renameType.delegates const fileToRename = renameType.delegates
.getFileToRenameFromPreviewRenameResponse(previewRenameRequestResult); .getFileToRenameFromPreviewRenameResponse(previewRenameRequestResult, fInfo);
// Only if there is a rename to execute // Only if there is a rename to execute
if (fileToRename !== undefined) { if (fileToRename !== undefined) {
@ -241,8 +274,6 @@ const plugin = async (args: IpluginInputArgs): Promise<IpluginOutputArgs> => {
destinationPath: newPath, destinationPath: newPath,
args, args,
}); });
args.jobLog(`${renameType.content} ${fileDetailsWrapper.id} renamed : `
+ `'${originalFileName}' => '${newPath}'.`);
} else { } else {
isSuccessful = true; isSuccessful = true;
args.jobLog('✔ No rename necessary.'); args.jobLog('✔ No rename necessary.');

Loading…
Cancel
Save