mirror of
https://github.com/gabehf/Tdarr_Plugins.git
synced 2026-03-09 15:38:19 -07:00
After thorough testing, some renaming and refactoring
This commit is contained in:
parent
ecb519576a
commit
2903603a5d
2 changed files with 57 additions and 56 deletions
|
|
@ -116,7 +116,7 @@ var details = function () { return ({
|
|||
],
|
||||
}); };
|
||||
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 _a, _b;
|
||||
return __generator(this, function (_c) {
|
||||
|
|
@ -127,20 +127,20 @@ var getFileInfoFromLookup = function (args, arr, arrHost, headers, fileName, ren
|
|||
if (!(imdbId !== '')) return [3 /*break*/, 2];
|
||||
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:
|
||||
lookupResponse = _c.sent();
|
||||
fInfo = renameType.delegates.getFileInfoFromLookupResponse(lookupResponse, fileName);
|
||||
args.jobLog("".concat(renameType.content, " ").concat(fInfo.id !== '-1' ? "'".concat(fInfo.id, "' found") : 'not found')
|
||||
fInfo = arrApp.delegates.getFileInfoFromLookupResponse(lookupResponse, fileName);
|
||||
args.jobLog("".concat(arrApp.content, " ").concat(fInfo.id !== '-1' ? "'".concat(fInfo.id, "' found") : 'not found')
|
||||
+ " for imdb '".concat(imdbId, "'"));
|
||||
_c.label = 2;
|
||||
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;
|
||||
return __generator(this, function (_a) {
|
||||
switch (_a.label) {
|
||||
|
|
@ -148,33 +148,33 @@ var getFileInfoFromParse = function (args, arr, arrHost, headers, fileName, rena
|
|||
fInfo = { id: '-1' };
|
||||
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 1:
|
||||
parseResponse = _a.sent();
|
||||
fInfo = renameType.delegates.getFileInfoFromParseResponse(parseResponse);
|
||||
args.jobLog("".concat(renameType.content, " ").concat(fInfo.id !== '-1' ? "'".concat(fInfo.id, "' found") : 'not found')
|
||||
fInfo = arrApp.delegates.getFileInfoFromParseResponse(parseResponse);
|
||||
args.jobLog("".concat(arrApp.content, " ").concat(fInfo.id !== '-1' ? "'".concat(fInfo.id, "' found") : 'not found')
|
||||
+ " for '".concat((0, fileUtils_1.getFileName)(fileName), "'"));
|
||||
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;
|
||||
return __generator(this, function (_a) {
|
||||
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:
|
||||
fInfo = _a.sent();
|
||||
return [2 /*return*/, (fInfo.id === '-1' || (arr === 'sonarr' && (fInfo.seasonNumber === -1 || fInfo.episodeNumber === -1)))
|
||||
? getFileInfoFromParse(args, arr, arrHost, headers, fileName, renameType)
|
||||
return [2 /*return*/, (fInfo.id === '-1' || (arrApp.name === 'sonarr' && (fInfo.seasonNumber === -1 || fInfo.episodeNumber === -1)))
|
||||
? getFileInfoFromParse(args, arrApp, fileName)
|
||||
: fInfo];
|
||||
}
|
||||
});
|
||||
}); };
|
||||
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;
|
||||
return __generator(this, function (_e) {
|
||||
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),
|
||||
Accept: 'application/json',
|
||||
};
|
||||
renameType = arr === 'radarr'
|
||||
arrApp = arr === 'radarr'
|
||||
? {
|
||||
appName: 'Radarr',
|
||||
name: arr,
|
||||
host: arrHost,
|
||||
headers: headers,
|
||||
content: 'Movie',
|
||||
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) }); },
|
||||
|
|
@ -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',
|
||||
delegates: {
|
||||
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("Renaming ".concat(renameType.appName, "..."));
|
||||
return [4 /*yield*/, getFileInfo(args, arr, arrHost, headers, originalFileName, renameType)];
|
||||
args.jobLog("Renaming ".concat(arrApp.name, "..."));
|
||||
return [4 /*yield*/, getFileInfo(args, arrApp, originalFileName)];
|
||||
case 1:
|
||||
fInfo = _e.sent();
|
||||
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:
|
||||
fInfo = _e.sent();
|
||||
_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];
|
||||
return [4 /*yield*/, args.deps.axios({
|
||||
method: 'get',
|
||||
url: renameType.delegates.buildPreviewRenameResquestUrl(fInfo),
|
||||
url: arrApp.delegates.buildPreviewRenameResquestUrl(fInfo),
|
||||
headers: headers,
|
||||
})];
|
||||
case 4:
|
||||
previewRenameRequestResult = _e.sent();
|
||||
fileToRename = renameType.delegates
|
||||
fileToRename = arrApp.delegates
|
||||
.getFileToRenameFromPreviewRenameResponse(previewRenameRequestResult, fInfo);
|
||||
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));
|
||||
|
|
|
|||
|
|
@ -102,8 +102,10 @@ interface IFileToRename {
|
|||
interface IPreviewRenameResponse {
|
||||
data: IFileToRename[]
|
||||
}
|
||||
interface IRenameType {
|
||||
appName: string,
|
||||
interface IArrApp {
|
||||
name: string,
|
||||
host: string,
|
||||
headers: IHTTPHeaders,
|
||||
content: string,
|
||||
delegates: {
|
||||
getFileInfoFromLookupResponse:
|
||||
|
|
@ -119,11 +121,8 @@ interface IRenameType {
|
|||
|
||||
const getFileInfoFromLookup = async (
|
||||
args: IpluginInputArgs,
|
||||
arr: string,
|
||||
arrHost: string,
|
||||
headers: IHTTPHeaders,
|
||||
arrApp: IArrApp,
|
||||
fileName: string,
|
||||
renameType: IRenameType,
|
||||
)
|
||||
: Promise<IFileInfo> => {
|
||||
let fInfo: IFileInfo = { id: '-1' };
|
||||
|
|
@ -131,11 +130,11 @@ const getFileInfoFromLookup = async (
|
|||
if (imdbId !== '') {
|
||||
const lookupResponse: ILookupResponse = await args.deps.axios({
|
||||
method: 'get',
|
||||
url: `${arrHost}/api/v3/${arr === 'radarr' ? 'movie' : 'series'}/lookup?term=imdb:${imdbId}`,
|
||||
headers,
|
||||
url: `${arrApp.host}/api/v3/${arrApp.name === 'radarr' ? 'movie' : 'series'}/lookup?term=imdb:${imdbId}`,
|
||||
headers: arrApp.headers,
|
||||
});
|
||||
fInfo = renameType.delegates.getFileInfoFromLookupResponse(lookupResponse, fileName);
|
||||
args.jobLog(`${renameType.content} ${fInfo.id !== '-1' ? `'${fInfo.id}' found` : 'not found'}`
|
||||
fInfo = arrApp.delegates.getFileInfoFromLookupResponse(lookupResponse, fileName);
|
||||
args.jobLog(`${arrApp.content} ${fInfo.id !== '-1' ? `'${fInfo.id}' found` : 'not found'}`
|
||||
+ ` for imdb '${imdbId}'`);
|
||||
}
|
||||
return fInfo;
|
||||
|
|
@ -143,37 +142,31 @@ const getFileInfoFromLookup = async (
|
|||
|
||||
const getFileInfoFromParse = async (
|
||||
args: IpluginInputArgs,
|
||||
arr: string,
|
||||
arrHost: string,
|
||||
headers: IHTTPHeaders,
|
||||
arrApp: IArrApp,
|
||||
fileName: string,
|
||||
renameType: IRenameType,
|
||||
)
|
||||
: Promise<IFileInfo> => {
|
||||
let fInfo: IFileInfo = { id: '-1' };
|
||||
const parseResponse: IParseResponse = 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,
|
||||
});
|
||||
fInfo = renameType.delegates.getFileInfoFromParseResponse(parseResponse);
|
||||
args.jobLog(`${renameType.content} ${fInfo.id !== '-1' ? `'${fInfo.id}' found` : 'not found'}`
|
||||
fInfo = arrApp.delegates.getFileInfoFromParseResponse(parseResponse);
|
||||
args.jobLog(`${arrApp.content} ${fInfo.id !== '-1' ? `'${fInfo.id}' found` : 'not found'}`
|
||||
+ ` for '${getFileName(fileName)}'`);
|
||||
return fInfo;
|
||||
};
|
||||
|
||||
const getFileInfo = async (
|
||||
args: IpluginInputArgs,
|
||||
arr: string,
|
||||
arrHost: string,
|
||||
headers: IHTTPHeaders,
|
||||
arrApp: IArrApp,
|
||||
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)
|
||||
const fInfo = await getFileInfoFromLookup(args, arrApp, fileName);
|
||||
return (fInfo.id === '-1' || (arrApp.name === 'sonarr' && (fInfo.seasonNumber === -1 || fInfo.episodeNumber === -1)))
|
||||
? getFileInfoFromParse(args, arrApp, fileName)
|
||||
: fInfo;
|
||||
};
|
||||
|
||||
|
|
@ -195,9 +188,11 @@ const plugin = async (args: IpluginInputArgs): Promise<IpluginOutputArgs> => {
|
|||
Accept: 'application/json',
|
||||
};
|
||||
|
||||
const renameType: IRenameType = arr === 'radarr'
|
||||
const arrApp: IArrApp = arr === 'radarr'
|
||||
? {
|
||||
appName: 'Radarr',
|
||||
name: arr,
|
||||
host: arrHost,
|
||||
headers,
|
||||
content: 'Movie',
|
||||
delegates: {
|
||||
getFileInfoFromLookupResponse:
|
||||
|
|
@ -211,7 +206,9 @@ const plugin = async (args: IpluginInputArgs): Promise<IpluginOutputArgs> => {
|
|||
},
|
||||
}
|
||||
: {
|
||||
appName: 'Sonarr',
|
||||
name: arr,
|
||||
host: arrHost,
|
||||
headers,
|
||||
content: 'Serie',
|
||||
delegates: {
|
||||
getFileInfoFromLookupResponse:
|
||||
|
|
@ -242,13 +239,13 @@ const plugin = async (args: IpluginInputArgs): Promise<IpluginOutputArgs> => {
|
|||
};
|
||||
|
||||
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
|
||||
let fInfo = await getFileInfo(args, arr, arrHost, headers, originalFileName, renameType);
|
||||
let fInfo = await getFileInfo(args, arrApp, originalFileName);
|
||||
// Useful in some edge cases
|
||||
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
|
||||
|
|
@ -256,10 +253,10 @@ const plugin = async (args: IpluginInputArgs): Promise<IpluginOutputArgs> => {
|
|||
// Using rename endpoint to get ids of all the files that need renaming
|
||||
const previewRenameRequestResult = await args.deps.axios({
|
||||
method: 'get',
|
||||
url: renameType.delegates.buildPreviewRenameResquestUrl(fInfo),
|
||||
url: arrApp.delegates.buildPreviewRenameResquestUrl(fInfo),
|
||||
headers,
|
||||
});
|
||||
const fileToRename = renameType.delegates
|
||||
const fileToRename = arrApp.delegates
|
||||
.getFileToRenameFromPreviewRenameResponse(previewRenameRequestResult, fInfo);
|
||||
|
||||
// Only if there is a rename to execute
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue