After thorough testing, some renaming and refactoring

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

@ -116,7 +116,7 @@ var details = function () { return ({
], ],
}); }; }); };
exports.details = details; exports.details = details;
var getFileInfoFromLookup = function (args, arr, arrHost, headers, fileName, renameType) { return __awaiter(void 0, void 0, void 0, function () { var getFileInfoFromLookup = function (args, arrApp, fileName) { return __awaiter(void 0, void 0, void 0, function () {
var fInfo, imdbId, lookupResponse; var fInfo, imdbId, lookupResponse;
var _a, _b; var _a, _b;
return __generator(this, function (_c) { return __generator(this, function (_c) {
@ -127,20 +127,20 @@ var getFileInfoFromLookup = function (args, arr, arrHost, headers, fileName, ren
if (!(imdbId !== '')) return [3 /*break*/, 2]; if (!(imdbId !== '')) return [3 /*break*/, 2];
return [4 /*yield*/, args.deps.axios({ return [4 /*yield*/, args.deps.axios({
method: 'get', method: 'get',
url: "".concat(arrHost, "/api/v3/").concat(arr === 'radarr' ? 'movie' : 'series', "/lookup?term=imdb:").concat(imdbId), url: "".concat(arrApp.host, "/api/v3/").concat(arrApp.name === 'radarr' ? 'movie' : 'series', "/lookup?term=imdb:").concat(imdbId),
headers: headers, headers: arrApp.headers,
})]; })];
case 1: case 1:
lookupResponse = _c.sent(); lookupResponse = _c.sent();
fInfo = renameType.delegates.getFileInfoFromLookupResponse(lookupResponse, fileName); fInfo = arrApp.delegates.getFileInfoFromLookupResponse(lookupResponse, fileName);
args.jobLog("".concat(renameType.content, " ").concat(fInfo.id !== '-1' ? "'".concat(fInfo.id, "' found") : 'not found') args.jobLog("".concat(arrApp.content, " ").concat(fInfo.id !== '-1' ? "'".concat(fInfo.id, "' found") : 'not found')
+ " for imdb '".concat(imdbId, "'")); + " for imdb '".concat(imdbId, "'"));
_c.label = 2; _c.label = 2;
case 2: return [2 /*return*/, fInfo]; case 2: return [2 /*return*/, fInfo];
} }
}); });
}); }; }); };
var getFileInfoFromParse = function (args, arr, arrHost, headers, fileName, renameType) { return __awaiter(void 0, void 0, void 0, function () { var getFileInfoFromParse = function (args, arrApp, fileName) { return __awaiter(void 0, void 0, void 0, function () {
var fInfo, parseResponse; var fInfo, parseResponse;
return __generator(this, function (_a) { return __generator(this, function (_a) {
switch (_a.label) { switch (_a.label) {
@ -148,33 +148,33 @@ var getFileInfoFromParse = function (args, arr, arrHost, headers, fileName, rena
fInfo = { id: '-1' }; fInfo = { id: '-1' };
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(arrApp.host, "/api/v3/parse?title=").concat(encodeURIComponent((0, fileUtils_1.getFileName)(fileName))),
headers: headers, headers: arrApp.headers,
})]; })];
case 1: case 1:
parseResponse = _a.sent(); parseResponse = _a.sent();
fInfo = renameType.delegates.getFileInfoFromParseResponse(parseResponse); fInfo = arrApp.delegates.getFileInfoFromParseResponse(parseResponse);
args.jobLog("".concat(renameType.content, " ").concat(fInfo.id !== '-1' ? "'".concat(fInfo.id, "' found") : 'not found') args.jobLog("".concat(arrApp.content, " ").concat(fInfo.id !== '-1' ? "'".concat(fInfo.id, "' found") : 'not found')
+ " for '".concat((0, fileUtils_1.getFileName)(fileName), "'")); + " for '".concat((0, fileUtils_1.getFileName)(fileName), "'"));
return [2 /*return*/, fInfo]; return [2 /*return*/, fInfo];
} }
}); });
}); }; }); };
var getFileInfo = function (args, arr, arrHost, headers, fileName, renameType) { return __awaiter(void 0, void 0, void 0, function () { var getFileInfo = function (args, arrApp, fileName) { return __awaiter(void 0, void 0, void 0, function () {
var fInfo; var fInfo;
return __generator(this, function (_a) { return __generator(this, function (_a) {
switch (_a.label) { switch (_a.label) {
case 0: return [4 /*yield*/, getFileInfoFromLookup(args, arr, arrHost, headers, fileName, renameType)]; case 0: return [4 /*yield*/, getFileInfoFromLookup(args, arrApp, fileName)];
case 1: case 1:
fInfo = _a.sent(); fInfo = _a.sent();
return [2 /*return*/, (fInfo.id === '-1' || (arr === 'sonarr' && (fInfo.seasonNumber === -1 || fInfo.episodeNumber === -1))) return [2 /*return*/, (fInfo.id === '-1' || (arrApp.name === 'sonarr' && (fInfo.seasonNumber === -1 || fInfo.episodeNumber === -1)))
? getFileInfoFromParse(args, arr, arrHost, headers, fileName, renameType) ? getFileInfoFromParse(args, arrApp, fileName)
: fInfo]; : 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, renameType, fInfo, previewRenameRequestResult, fileToRename; var lib, newPath, isSuccessful, arr, arr_host, arrHost, originalFileName, currentFileName, headers, arrApp, 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) {
@ -194,9 +194,11 @@ 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',
}; };
renameType = arr === 'radarr' arrApp = arr === 'radarr'
? { ? {
appName: 'Radarr', name: arr,
host: arrHost,
headers: headers,
content: 'Movie', content: 'Movie',
delegates: { delegates: {
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) }); }, 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) }); },
@ -206,7 +208,9 @@ var plugin = function (args) { return __awaiter(void 0, void 0, void 0, function
}, },
} }
: { : {
appName: 'Sonarr', name: arr,
host: arrHost,
headers: headers,
content: 'Serie', content: 'Serie',
delegates: { delegates: {
getFileInfoFromLookupResponse: function (lookupResponse, fileName) { getFileInfoFromLookupResponse: function (lookupResponse, fileName) {
@ -237,12 +241,12 @@ var plugin = function (args) { return __awaiter(void 0, void 0, void 0, function
}, },
}; };
args.jobLog('Going to apply new name'); args.jobLog('Going to apply new name');
args.jobLog("Renaming ".concat(renameType.appName, "...")); args.jobLog("Renaming ".concat(arrApp.name, "..."));
return [4 /*yield*/, getFileInfo(args, arr, arrHost, headers, originalFileName, renameType)]; return [4 /*yield*/, getFileInfo(args, arrApp, originalFileName)];
case 1: case 1:
fInfo = _e.sent(); fInfo = _e.sent();
if (!(fInfo.id === '-1' && currentFileName !== originalFileName)) return [3 /*break*/, 3]; if (!(fInfo.id === '-1' && currentFileName !== originalFileName)) return [3 /*break*/, 3];
return [4 /*yield*/, getFileInfo(args, arr, arrHost, headers, currentFileName, renameType)]; return [4 /*yield*/, getFileInfo(args, arrApp, currentFileName)];
case 2: case 2:
fInfo = _e.sent(); fInfo = _e.sent();
_e.label = 3; _e.label = 3;
@ -250,12 +254,12 @@ var plugin = function (args) { return __awaiter(void 0, void 0, void 0, function
if (!(fInfo.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(fInfo), url: arrApp.delegates.buildPreviewRenameResquestUrl(fInfo),
headers: headers, headers: headers,
})]; })];
case 4: case 4:
previewRenameRequestResult = _e.sent(); previewRenameRequestResult = _e.sent();
fileToRename = renameType.delegates fileToRename = arrApp.delegates
.getFileToRenameFromPreviewRenameResponse(previewRenameRequestResult, fInfo); .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));

@ -102,8 +102,10 @@ interface IFileToRename {
interface IPreviewRenameResponse { interface IPreviewRenameResponse {
data: IFileToRename[] data: IFileToRename[]
} }
interface IRenameType { interface IArrApp {
appName: string, name: string,
host: string,
headers: IHTTPHeaders,
content: string, content: string,
delegates: { delegates: {
getFileInfoFromLookupResponse: getFileInfoFromLookupResponse:
@ -119,11 +121,8 @@ interface IRenameType {
const getFileInfoFromLookup = async ( const getFileInfoFromLookup = async (
args: IpluginInputArgs, args: IpluginInputArgs,
arr: string, arrApp: IArrApp,
arrHost: string,
headers: IHTTPHeaders,
fileName: string, fileName: string,
renameType: IRenameType,
) )
: Promise<IFileInfo> => { : Promise<IFileInfo> => {
let fInfo: IFileInfo = { id: '-1' }; let fInfo: IFileInfo = { id: '-1' };
@ -131,11 +130,11 @@ const getFileInfoFromLookup = async (
if (imdbId !== '') { if (imdbId !== '') {
const lookupResponse: ILookupResponse = await args.deps.axios({ const lookupResponse: ILookupResponse = await args.deps.axios({
method: 'get', method: 'get',
url: `${arrHost}/api/v3/${arr === 'radarr' ? 'movie' : 'series'}/lookup?term=imdb:${imdbId}`, url: `${arrApp.host}/api/v3/${arrApp.name === 'radarr' ? 'movie' : 'series'}/lookup?term=imdb:${imdbId}`,
headers, headers: arrApp.headers,
}); });
fInfo = renameType.delegates.getFileInfoFromLookupResponse(lookupResponse, fileName); fInfo = arrApp.delegates.getFileInfoFromLookupResponse(lookupResponse, fileName);
args.jobLog(`${renameType.content} ${fInfo.id !== '-1' ? `'${fInfo.id}' found` : 'not found'}` args.jobLog(`${arrApp.content} ${fInfo.id !== '-1' ? `'${fInfo.id}' found` : 'not found'}`
+ ` for imdb '${imdbId}'`); + ` for imdb '${imdbId}'`);
} }
return fInfo; return fInfo;
@ -143,37 +142,31 @@ const getFileInfoFromLookup = async (
const getFileInfoFromParse = async ( const getFileInfoFromParse = async (
args: IpluginInputArgs, args: IpluginInputArgs,
arr: string, arrApp: IArrApp,
arrHost: string,
headers: IHTTPHeaders,
fileName: string, fileName: string,
renameType: IRenameType,
) )
: Promise<IFileInfo> => { : Promise<IFileInfo> => {
let fInfo: IFileInfo = { id: '-1' }; let fInfo: IFileInfo = { id: '-1' };
const parseResponse: IParseResponse = await args.deps.axios({ const parseResponse: IParseResponse = await args.deps.axios({
method: 'get', method: 'get',
url: `${arrHost}/api/v3/parse?title=${encodeURIComponent(getFileName(fileName))}`, url: `${arrApp.host}/api/v3/parse?title=${encodeURIComponent(getFileName(fileName))}`,
headers, headers: arrApp.headers,
}); });
fInfo = renameType.delegates.getFileInfoFromParseResponse(parseResponse); fInfo = arrApp.delegates.getFileInfoFromParseResponse(parseResponse);
args.jobLog(`${renameType.content} ${fInfo.id !== '-1' ? `'${fInfo.id}' found` : 'not found'}` args.jobLog(`${arrApp.content} ${fInfo.id !== '-1' ? `'${fInfo.id}' found` : 'not found'}`
+ ` for '${getFileName(fileName)}'`); + ` for '${getFileName(fileName)}'`);
return fInfo; return fInfo;
}; };
const getFileInfo = async ( const getFileInfo = async (
args: IpluginInputArgs, args: IpluginInputArgs,
arr: string, arrApp: IArrApp,
arrHost: string,
headers: IHTTPHeaders,
fileName: string, fileName: string,
renameType: IRenameType,
) )
: Promise<IFileInfo> => { : Promise<IFileInfo> => {
const fInfo = await getFileInfoFromLookup(args, arr, arrHost, headers, fileName, renameType); const fInfo = await getFileInfoFromLookup(args, arrApp, fileName);
return (fInfo.id === '-1' || (arr === 'sonarr' && (fInfo.seasonNumber === -1 || fInfo.episodeNumber === -1))) return (fInfo.id === '-1' || (arrApp.name === 'sonarr' && (fInfo.seasonNumber === -1 || fInfo.episodeNumber === -1)))
? getFileInfoFromParse(args, arr, arrHost, headers, fileName, renameType) ? getFileInfoFromParse(args, arrApp, fileName)
: fInfo; : fInfo;
}; };
@ -195,9 +188,11 @@ const plugin = async (args: IpluginInputArgs): Promise<IpluginOutputArgs> => {
Accept: 'application/json', Accept: 'application/json',
}; };
const renameType: IRenameType = arr === 'radarr' const arrApp: IArrApp = arr === 'radarr'
? { ? {
appName: 'Radarr', name: arr,
host: arrHost,
headers,
content: 'Movie', content: 'Movie',
delegates: { delegates: {
getFileInfoFromLookupResponse: getFileInfoFromLookupResponse:
@ -211,7 +206,9 @@ const plugin = async (args: IpluginInputArgs): Promise<IpluginOutputArgs> => {
}, },
} }
: { : {
appName: 'Sonarr', name: arr,
host: arrHost,
headers,
content: 'Serie', content: 'Serie',
delegates: { delegates: {
getFileInfoFromLookupResponse: getFileInfoFromLookupResponse:
@ -242,13 +239,13 @@ const plugin = async (args: IpluginInputArgs): Promise<IpluginOutputArgs> => {
}; };
args.jobLog('Going to apply new name'); args.jobLog('Going to apply new name');
args.jobLog(`Renaming ${renameType.appName}...`); args.jobLog(`Renaming ${arrApp.name}...`);
// Retrieving movie or serie id, plus season and episode number for serie // Retrieving movie or serie id, plus season and episode number for serie
let fInfo = await getFileInfo(args, arr, arrHost, headers, originalFileName, renameType); let fInfo = await getFileInfo(args, arrApp, originalFileName);
// Useful in some edge cases // Useful in some edge cases
if (fInfo.id === '-1' && currentFileName !== originalFileName) { if (fInfo.id === '-1' && currentFileName !== originalFileName) {
fInfo = await getFileInfo(args, arr, arrHost, headers, currentFileName, renameType); fInfo = await getFileInfo(args, arrApp, currentFileName);
} }
// Checking that the file has been found // Checking that the file has been found
@ -256,10 +253,10 @@ const plugin = async (args: IpluginInputArgs): Promise<IpluginOutputArgs> => {
// 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(fInfo), url: arrApp.delegates.buildPreviewRenameResquestUrl(fInfo),
headers, headers,
}); });
const fileToRename = renameType.delegates const fileToRename = arrApp.delegates
.getFileToRenameFromPreviewRenameResponse(previewRenameRequestResult, fInfo); .getFileToRenameFromPreviewRenameResponse(previewRenameRequestResult, fInfo);
// Only if there is a rename to execute // Only if there is a rename to execute

Loading…
Cancel
Save