mirror of
https://github.com/gabehf/Tdarr_Plugins.git
synced 2026-03-17 11:16:26 -07:00
Added the possibility to fin movieId from imdbId
This commit is contained in:
parent
5795a6df61
commit
d654454c5e
2 changed files with 279 additions and 266 deletions
|
|
@ -116,149 +116,163 @@ var details = function () { return ({
|
||||||
],
|
],
|
||||||
}); };
|
}); };
|
||||||
exports.details = details;
|
exports.details = details;
|
||||||
var plugin = function (args) { return __awaiter(void 0, void 0, void 0, function () {
|
var getMovieId = function (args, arrHost, headers, fileName, getNewPathType) { return __awaiter(void 0, void 0, void 0, function () {
|
||||||
var lib, _a, arr, arr_api_key, arr_host, arrHost, filePath, fileNames, getNewPath, episodeNumber, getNewPathTypes, newPathOutput;
|
var imdbId, id, _a, _b;
|
||||||
var _b, _c, _d, _e, _f, _g;
|
var _c, _d, _e, _f, _g;
|
||||||
return __generator(this, function (_h) {
|
return __generator(this, function (_h) {
|
||||||
switch (_h.label) {
|
switch (_h.label) {
|
||||||
|
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 : '';
|
||||||
|
if (!(imdbId !== '')) return [3 /*break*/, 2];
|
||||||
|
_b = String;
|
||||||
|
return [4 /*yield*/, args.deps.axios({
|
||||||
|
method: 'get',
|
||||||
|
url: "".concat(arrHost, "/api/v3/movie/lookup?term=imdb:").concat(imdbId),
|
||||||
|
headers: headers,
|
||||||
|
})];
|
||||||
|
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]);
|
||||||
|
return [3 /*break*/, 3];
|
||||||
|
case 2:
|
||||||
|
_a = '-1';
|
||||||
|
_h.label = 3;
|
||||||
|
case 3:
|
||||||
|
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 fdw, _a, _b;
|
||||||
|
var _c;
|
||||||
|
return __generator(this, function (_d) {
|
||||||
|
switch (_d.label) {
|
||||||
|
case 0:
|
||||||
|
_c = {};
|
||||||
|
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({
|
||||||
|
method: 'get',
|
||||||
|
url: "".concat(arrHost, "/api/v3/parse?title=").concat(encodeURIComponent((0, fileUtils_1.getFileName)(fileName))),
|
||||||
|
headers: headers,
|
||||||
|
})];
|
||||||
|
case 4:
|
||||||
|
_b.fileDetails = _d.sent();
|
||||||
|
fdw.id = renameType.delegates.getIdFromParseResponse(fdw);
|
||||||
|
args.jobLog("".concat(renameType.content, " ").concat(fdw.id !== '-1' ? "".concat(fdw.id, " found") : 'not found', " for '")
|
||||||
|
+ "".concat((0, fileUtils_1.getFileName)(fileName), "'"));
|
||||||
|
_d.label = 5;
|
||||||
|
case 5: return [2 /*return*/, fdw];
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}); };
|
||||||
|
var plugin = function (args) { return __awaiter(void 0, void 0, void 0, function () {
|
||||||
|
var lib, newPath, isSuccessful, arr, arr_host, arrHost, filePath, originalFileName, currentFileName, headers, episodeNumber, renameType, fileDetailsWrapper, previewRenameRequestResult, fileToRename;
|
||||||
|
var _a, _b, _c, _d, _e, _f;
|
||||||
|
return __generator(this, function (_g) {
|
||||||
|
switch (_g.label) {
|
||||||
case 0:
|
case 0:
|
||||||
lib = require('../../../../../methods/lib')();
|
lib = require('../../../../../methods/lib')();
|
||||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars,no-param-reassign
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars,no-param-reassign
|
||||||
args.inputs = lib.loadDefaultValues(args.inputs, details);
|
args.inputs = lib.loadDefaultValues(args.inputs, details);
|
||||||
_a = args.inputs, arr = _a.arr, arr_api_key = _a.arr_api_key;
|
newPath = '';
|
||||||
|
isSuccessful = false;
|
||||||
|
arr = String(args.inputs.arr);
|
||||||
arr_host = String(args.inputs.arr_host).trim();
|
arr_host = String(args.inputs.arr_host).trim();
|
||||||
arrHost = arr_host.endsWith('/') ? arr_host.slice(0, -1) : arr_host;
|
arrHost = arr_host.endsWith('/') ? arr_host.slice(0, -1) : arr_host;
|
||||||
filePath = (_c = (_b = args.originalLibraryFile) === null || _b === void 0 ? void 0 : _b._id) !== null && _c !== void 0 ? _c : '';
|
filePath = (_b = (_a = args.originalLibraryFile) === null || _a === void 0 ? void 0 : _a._id) !== null && _b !== void 0 ? _b : '';
|
||||||
fileNames = {
|
originalFileName = (_d = (_c = args.originalLibraryFile) === null || _c === void 0 ? void 0 : _c._id) !== null && _d !== void 0 ? _d : '';
|
||||||
originalFileName: (0, fileUtils_1.getFileName)((_e = (_d = args.originalLibraryFile) === null || _d === void 0 ? void 0 : _d._id) !== null && _e !== void 0 ? _e : ''),
|
currentFileName = (_f = (_e = args.inputFileObj) === null || _e === void 0 ? void 0 : _e._id) !== null && _f !== void 0 ? _f : '';
|
||||||
currentFileName: (0, fileUtils_1.getFileName)((_g = (_f = args.inputFileObj) === null || _f === void 0 ? void 0 : _f._id) !== null && _g !== void 0 ? _g : ''),
|
headers = {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
'X-Api-Key': String(args.inputs.arr_api_key),
|
||||||
|
Accept: 'application/json',
|
||||||
};
|
};
|
||||||
getNewPath = function (getNewPathType) { return __awaiter(void 0, void 0, void 0, function () {
|
|
||||||
var output, headers, getParseRequestResultWrapper, fileName, parseRequestResultWrapper, previewRenameRequestConfig, previewRenameRequestResult, fileToRename, _a;
|
|
||||||
return __generator(this, function (_b) {
|
|
||||||
switch (_b.label) {
|
|
||||||
case 0:
|
|
||||||
output = {
|
|
||||||
newPath: '',
|
|
||||||
isSuccessful: false,
|
|
||||||
};
|
|
||||||
args.jobLog('Going to apply new name');
|
|
||||||
args.jobLog("Renaming ".concat(getNewPathType.appName, "..."));
|
|
||||||
headers = {
|
|
||||||
'Content-Type': 'application/json',
|
|
||||||
'X-Api-Key': arr_api_key,
|
|
||||||
Accept: 'application/json',
|
|
||||||
};
|
|
||||||
getParseRequestResultWrapper = function (fileName) { return __awaiter(void 0, void 0, void 0, function () {
|
|
||||||
var parseRequestConfig, parseRequestResult, id;
|
|
||||||
return __generator(this, function (_a) {
|
|
||||||
switch (_a.label) {
|
|
||||||
case 0:
|
|
||||||
parseRequestConfig = {
|
|
||||||
method: 'get',
|
|
||||||
url: "".concat(arrHost, "/api/v3/parse?title=").concat(encodeURIComponent(fileName)),
|
|
||||||
headers: headers,
|
|
||||||
};
|
|
||||||
return [4 /*yield*/, args.deps.axios(parseRequestConfig)];
|
|
||||||
case 1:
|
|
||||||
parseRequestResult = _a.sent();
|
|
||||||
id = getNewPathType.delegates.getIdFromParseRequestResult(parseRequestResult);
|
|
||||||
args.jobLog(id !== '-1'
|
|
||||||
? "Found ".concat(getNewPathType.contentName, " ").concat(id, " with a file named '").concat(fileName, "'")
|
|
||||||
: "Didn't find ".concat(getNewPathType.contentName, " with a file named '").concat(fileName, "' in ").concat(arrHost, "."));
|
|
||||||
return [2 /*return*/, { parseRequestResult: parseRequestResult, id: id }];
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}); };
|
|
||||||
fileName = fileNames.originalFileName;
|
|
||||||
return [4 /*yield*/, getParseRequestResultWrapper(fileName)];
|
|
||||||
case 1:
|
|
||||||
parseRequestResultWrapper = _b.sent();
|
|
||||||
if (!(parseRequestResultWrapper.id === '-1' && fileNames.currentFileName !== fileNames.originalFileName)) return [3 /*break*/, 3];
|
|
||||||
fileName = fileNames.currentFileName;
|
|
||||||
return [4 /*yield*/, getParseRequestResultWrapper(fileName)];
|
|
||||||
case 2:
|
|
||||||
parseRequestResultWrapper = _b.sent();
|
|
||||||
_b.label = 3;
|
|
||||||
case 3:
|
|
||||||
if (!(parseRequestResultWrapper.id !== '-1')) return [3 /*break*/, 7];
|
|
||||||
previewRenameRequestConfig = {
|
|
||||||
method: 'get',
|
|
||||||
url: getNewPathType.delegates.buildPreviewRenameResquestUrl(parseRequestResultWrapper),
|
|
||||||
headers: headers,
|
|
||||||
};
|
|
||||||
return [4 /*yield*/, args.deps.axios(previewRenameRequestConfig)];
|
|
||||||
case 4:
|
|
||||||
previewRenameRequestResult = _b.sent();
|
|
||||||
fileToRename = getNewPathType.delegates
|
|
||||||
.getFileToRenameFromPreviewRenameRequestResult(previewRenameRequestResult);
|
|
||||||
if (!(fileToRename !== undefined)) return [3 /*break*/, 6];
|
|
||||||
output.newPath = "".concat((0, fileUtils_1.getFileAbosluteDir)(args.inputFileObj._id), "/").concat((0, fileUtils_1.getFileName)(fileToRename.newPath), ".").concat((0, fileUtils_1.getContainer)(fileToRename.newPath));
|
|
||||||
_a = output;
|
|
||||||
return [4 /*yield*/, (0, fileMoveOrCopy_1.default)({
|
|
||||||
operation: 'move',
|
|
||||||
sourcePath: args.inputFileObj._id,
|
|
||||||
destinationPath: output.newPath,
|
|
||||||
args: args,
|
|
||||||
})];
|
|
||||||
case 5:
|
|
||||||
_a.isSuccessful = _b.sent();
|
|
||||||
args.jobLog("\u2714 Renamed ".concat(getNewPathType.contentName, " ").concat(parseRequestResultWrapper.id, " : ")
|
|
||||||
+ "'".concat(filePath, "' => '").concat(output.newPath, "'."));
|
|
||||||
return [3 /*break*/, 7];
|
|
||||||
case 6:
|
|
||||||
output.isSuccessful = true;
|
|
||||||
args.jobLog('✔ No rename necessary.');
|
|
||||||
_b.label = 7;
|
|
||||||
case 7: return [2 /*return*/, output];
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}); };
|
|
||||||
episodeNumber = 0;
|
episodeNumber = 0;
|
||||||
getNewPathTypes = {
|
renameType = arr === 'radarr'
|
||||||
radarr: {
|
? {
|
||||||
appName: 'Radarr',
|
appName: 'Radarr',
|
||||||
contentName: 'movie',
|
content: 'Movie',
|
||||||
delegates: {
|
delegates: {
|
||||||
getIdFromParseRequestResult: function (parseRequestResult) { var _a, _b, _c, _d; return String((_d = (_c = (_b = (_a = parseRequestResult.data) === null || _a === void 0 ? void 0 : _a.movie) === null || _b === void 0 ? void 0 : _b.movieFile) === null || _c === void 0 ? void 0 : _c.movieId) !== null && _d !== void 0 ? _d : -1); },
|
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); },
|
||||||
buildPreviewRenameResquestUrl: function (parseRequestResultWrapper) { return "".concat(arrHost, "/api/v3/rename?movieId=").concat(parseRequestResultWrapper.id); },
|
buildPreviewRenameResquestUrl: function (fileDetailsWrapper) { return "".concat(arrHost, "/api/v3/rename?movieId=").concat(fileDetailsWrapper.id); },
|
||||||
getFileToRenameFromPreviewRenameRequestResult: function (previewRenameRequestResult) {
|
getFileToRenameFromPreviewRenameResponse: function (previewRenameResponse) { var _a; return (_a = previewRenameResponse.data) === null || _a === void 0 ? void 0 : _a.at(0); },
|
||||||
var _a, _b;
|
|
||||||
return ((((_b = (_a = previewRenameRequestResult.data) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0) > 0)
|
|
||||||
? previewRenameRequestResult.data[0]
|
|
||||||
: undefined);
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
},
|
}
|
||||||
sonarr: {
|
: {
|
||||||
appName: 'Sonarr',
|
appName: 'Sonarr',
|
||||||
contentName: 'serie',
|
content: 'Serie',
|
||||||
delegates: {
|
delegates: {
|
||||||
getIdFromParseRequestResult: function (parseRequestResult) { var _a, _b, _c; return String((_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 (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); },
|
||||||
buildPreviewRenameResquestUrl: function (parseRequestResultWrapper) {
|
buildPreviewRenameResquestUrl: function (fileDetailsWrapper) {
|
||||||
var _a, _b, _c, _d;
|
var _a, _b, _c, _d, _e, _f;
|
||||||
episodeNumber = ((_b = (_a = parseRequestResultWrapper.parseRequestResult.data.parsedEpisodeInfo) === null || _a === void 0 ? void 0 : _a.episodeNumbers) !== null && _b !== void 0 ? _b : [1])[0];
|
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];
|
||||||
return "".concat(arrHost, "/api/v3/rename?seriesId=").concat(parseRequestResultWrapper.id, "&seasonNumber=")
|
return "".concat(arrHost, "/api/v3/rename?seriesId=").concat(fileDetailsWrapper.id, "&seasonNumber=")
|
||||||
+ "".concat((_d = (_c = parseRequestResultWrapper.parseRequestResult.data.parsedEpisodeInfo) === null || _c === void 0 ? void 0 : _c.seasonNumber) !== null && _d !== void 0 ? _d : 1);
|
+ "".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);
|
||||||
},
|
},
|
||||||
getFileToRenameFromPreviewRenameRequestResult: function (previewRenameRequestResult) {
|
getFileToRenameFromPreviewRenameResponse: function (previewRenameResponse) {
|
||||||
var _a, _b;
|
var _a;
|
||||||
return ((((_b = (_a = previewRenameRequestResult.data) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0) > 0)
|
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; });
|
||||||
? previewRenameRequestResult.data.find(function (episodeFile) { var _a; return ((_a = episodeFile.episodeNumbers) === null || _a === void 0 ? void 0 : _a.at(0)) === episodeNumber; })
|
|
||||||
: undefined);
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
};
|
||||||
};
|
args.jobLog('Going to apply new name');
|
||||||
return [4 /*yield*/, getNewPath(arr === 'radarr' ? getNewPathTypes.radarr : getNewPathTypes.sonarr)];
|
args.jobLog("Renaming ".concat(renameType.appName, "..."));
|
||||||
|
return [4 /*yield*/, getFileDetailsWrapper(args, arr, arrHost, headers, originalFileName, renameType)];
|
||||||
case 1:
|
case 1:
|
||||||
newPathOutput = _h.sent();
|
fileDetailsWrapper = _g.sent();
|
||||||
return [2 /*return*/, {
|
if (!(fileDetailsWrapper.id === '-1' && currentFileName !== originalFileName)) return [3 /*break*/, 3];
|
||||||
outputFileObj: newPathOutput.isSuccessful && newPathOutput.newPath !== ''
|
return [4 /*yield*/, getFileDetailsWrapper(args, arr, arrHost, headers, currentFileName, renameType)];
|
||||||
? __assign(__assign({}, args.inputFileObj), { _id: newPathOutput.newPath }) : args.inputFileObj,
|
case 2:
|
||||||
outputNumber: newPathOutput.isSuccessful ? 1 : 2,
|
fileDetailsWrapper = _g.sent();
|
||||||
variables: args.variables,
|
_g.label = 3;
|
||||||
}];
|
case 3:
|
||||||
|
if (!(fileDetailsWrapper.id !== '-1')) return [3 /*break*/, 7];
|
||||||
|
return [4 /*yield*/, args.deps.axios({
|
||||||
|
method: 'get',
|
||||||
|
url: renameType.delegates.buildPreviewRenameResquestUrl(fileDetailsWrapper),
|
||||||
|
headers: headers,
|
||||||
|
})];
|
||||||
|
case 4:
|
||||||
|
previewRenameRequestResult = _g.sent();
|
||||||
|
fileToRename = renameType.delegates
|
||||||
|
.getFileToRenameFromPreviewRenameResponse(previewRenameRequestResult);
|
||||||
|
if (!(fileToRename !== undefined)) return [3 /*break*/, 6];
|
||||||
|
newPath = "".concat((0, fileUtils_1.getFileAbosluteDir)(args.inputFileObj._id), "/").concat((0, fileUtils_1.getFileName)(fileToRename.newPath), ".").concat((0, fileUtils_1.getContainer)(fileToRename.newPath));
|
||||||
|
return [4 /*yield*/, (0, fileMoveOrCopy_1.default)({
|
||||||
|
operation: 'move',
|
||||||
|
sourcePath: args.inputFileObj._id,
|
||||||
|
destinationPath: newPath,
|
||||||
|
args: args,
|
||||||
|
})];
|
||||||
|
case 5:
|
||||||
|
isSuccessful = _g.sent();
|
||||||
|
args.jobLog("\u2714 ".concat(renameType.content, " renamed ").concat(fileDetailsWrapper.id, " : ")
|
||||||
|
+ "'".concat(filePath, "' => '").concat(newPath, "'."));
|
||||||
|
return [3 /*break*/, 7];
|
||||||
|
case 6:
|
||||||
|
isSuccessful = true;
|
||||||
|
args.jobLog('✔ No rename necessary.');
|
||||||
|
_g.label = 7;
|
||||||
|
case 7: return [2 /*return*/, {
|
||||||
|
outputFileObj: isSuccessful && newPath !== ''
|
||||||
|
? __assign(__assign({}, args.inputFileObj), { _id: newPath }) : args.inputFileObj,
|
||||||
|
outputNumber: isSuccessful ? 1 : 2,
|
||||||
|
variables: args.variables,
|
||||||
|
}];
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}); };
|
}); };
|
||||||
|
|
|
||||||
|
|
@ -72,191 +72,190 @@ const details = (): IpluginDetails => ({
|
||||||
],
|
],
|
||||||
});
|
});
|
||||||
|
|
||||||
interface IFileNames {
|
interface IHTTPHeaders {
|
||||||
originalFileName: string,
|
'Content-Type': string,
|
||||||
currentFileName: string
|
'X-Api-Key': string,
|
||||||
|
Accept: string,
|
||||||
}
|
}
|
||||||
interface IParseRequestResult {
|
interface IFileDetails {
|
||||||
data: {
|
data: {
|
||||||
movie?: {
|
movie?: { id: number },
|
||||||
movieFile: {
|
series?: { id: number },
|
||||||
movieId: number
|
|
||||||
},
|
|
||||||
},
|
|
||||||
series?: {
|
|
||||||
id: number
|
|
||||||
},
|
|
||||||
parsedEpisodeInfo?: {
|
parsedEpisodeInfo?: {
|
||||||
episodeNumbers: number[],
|
episodeNumbers: number[],
|
||||||
seasonNumber: number
|
seasonNumber: number
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
interface IParseRequestResultWrapper {
|
interface IFileDetailsWrapper {
|
||||||
parseRequestResult: IParseRequestResult,
|
|
||||||
id: string
|
id: string
|
||||||
|
fileDetails?: IFileDetails
|
||||||
}
|
}
|
||||||
interface IFileToRename {
|
interface IFileToRename {
|
||||||
newPath: string
|
newPath: string
|
||||||
episodeNumbers?: number[]
|
episodeNumbers?: number[]
|
||||||
}
|
}
|
||||||
interface IPreviewRenameRequestResult {
|
interface IPreviewRenameResponse {
|
||||||
data: IFileToRename[]
|
data: IFileToRename[]
|
||||||
}
|
}
|
||||||
interface IGetNewPathDelegates {
|
interface IRenameType {
|
||||||
getIdFromParseRequestResult:
|
|
||||||
(parseRequestResult: IParseRequestResult) => string,
|
|
||||||
buildPreviewRenameResquestUrl:
|
|
||||||
(parseRequestResult: IParseRequestResultWrapper) => string,
|
|
||||||
getFileToRenameFromPreviewRenameRequestResult:
|
|
||||||
(previewRenameRequestResult: IPreviewRenameRequestResult) => IFileToRename | undefined
|
|
||||||
}
|
|
||||||
interface IGetNewPathType {
|
|
||||||
appName: string,
|
appName: string,
|
||||||
contentName: string,
|
content: string,
|
||||||
delegates: IGetNewPathDelegates
|
delegates: {
|
||||||
}
|
getIdFromParseResponse:
|
||||||
interface IGetNewPathTypes {
|
(fileDetailsWrapper: IFileDetailsWrapper) => string,
|
||||||
radarr: IGetNewPathType,
|
buildPreviewRenameResquestUrl:
|
||||||
sonarr: IGetNewPathType
|
(fileDetailsWrapper: IFileDetailsWrapper) => string,
|
||||||
}
|
getFileToRenameFromPreviewRenameResponse:
|
||||||
interface IGetNewPathOutput {
|
(previewRenameResponse: IPreviewRenameResponse) => IFileToRename | undefined
|
||||||
newPath: string,
|
}
|
||||||
isSuccessful: boolean
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const getMovieId = async (
|
||||||
|
args: IpluginInputArgs,
|
||||||
|
arrHost: string,
|
||||||
|
headers: IHTTPHeaders,
|
||||||
|
fileName: string,
|
||||||
|
getNewPathType: IRenameType,
|
||||||
|
)
|
||||||
|
: Promise<string> => {
|
||||||
|
const imdbId = /\b(tt|nm|co|ev|ch|ni)\d{7,10}\b/i.exec(fileName)?.at(0) ?? '';
|
||||||
|
const id = (imdbId !== '')
|
||||||
|
? String(
|
||||||
|
(await args.deps.axios({
|
||||||
|
method: 'get',
|
||||||
|
url: `${arrHost}/api/v3/movie/lookup?term=imdb:${imdbId}`,
|
||||||
|
headers,
|
||||||
|
})).data?.at(0)?.id ?? -1,
|
||||||
|
)
|
||||||
|
: '-1';
|
||||||
|
args.jobLog(`${getNewPathType.content} ${id !== '-1' ? `${id} found` : 'not found'} for imdb '${imdbId}'`);
|
||||||
|
return id;
|
||||||
|
};
|
||||||
|
|
||||||
|
const getFileDetailsWrapper = async (
|
||||||
|
args: IpluginInputArgs,
|
||||||
|
arr: string,
|
||||||
|
arrHost: string,
|
||||||
|
headers: IHTTPHeaders,
|
||||||
|
fileName: string,
|
||||||
|
renameType: IRenameType,
|
||||||
|
)
|
||||||
|
: Promise<IFileDetailsWrapper> => {
|
||||||
|
const fdw: IFileDetailsWrapper = {
|
||||||
|
id: arr === 'radarr' ? await getMovieId(args, arrHost, headers, fileName, renameType) : '-1',
|
||||||
|
fileDetails: undefined,
|
||||||
|
};
|
||||||
|
if (fdw.id === '-1') {
|
||||||
|
fdw.fileDetails = await args.deps.axios({
|
||||||
|
method: 'get',
|
||||||
|
url: `${arrHost}/api/v3/parse?title=${encodeURIComponent(getFileName(fileName))}`,
|
||||||
|
headers,
|
||||||
|
});
|
||||||
|
fdw.id = renameType.delegates.getIdFromParseResponse(fdw);
|
||||||
|
args.jobLog(`${renameType.content} ${fdw.id !== '-1' ? `${fdw.id} found` : 'not found'} for '`
|
||||||
|
+ `${getFileName(fileName)}'`);
|
||||||
|
}
|
||||||
|
return fdw;
|
||||||
|
};
|
||||||
|
|
||||||
const plugin = async (args: IpluginInputArgs): Promise<IpluginOutputArgs> => {
|
const plugin = async (args: IpluginInputArgs): Promise<IpluginOutputArgs> => {
|
||||||
const lib = require('../../../../../methods/lib')();
|
const lib = require('../../../../../methods/lib')();
|
||||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars,no-param-reassign
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars,no-param-reassign
|
||||||
args.inputs = lib.loadDefaultValues(args.inputs, details);
|
args.inputs = lib.loadDefaultValues(args.inputs, details);
|
||||||
|
|
||||||
const { arr, arr_api_key } = args.inputs;
|
let newPath = '';
|
||||||
|
let isSuccessful = false;
|
||||||
|
const arr = String(args.inputs.arr);
|
||||||
const arr_host = String(args.inputs.arr_host).trim();
|
const arr_host = String(args.inputs.arr_host).trim();
|
||||||
const arrHost = arr_host.endsWith('/') ? arr_host.slice(0, -1) : arr_host;
|
const arrHost = arr_host.endsWith('/') ? arr_host.slice(0, -1) : arr_host;
|
||||||
const filePath = args.originalLibraryFile?._id ?? '';
|
const filePath = args.originalLibraryFile?._id ?? '';
|
||||||
const fileNames: IFileNames = {
|
const originalFileName = args.originalLibraryFile?._id ?? '';
|
||||||
originalFileName: getFileName(args.originalLibraryFile?._id ?? ''),
|
const currentFileName = args.inputFileObj?._id ?? '';
|
||||||
currentFileName: getFileName(args.inputFileObj?._id ?? ''),
|
const headers = {
|
||||||
};
|
'Content-Type': 'application/json',
|
||||||
|
'X-Api-Key': String(args.inputs.arr_api_key),
|
||||||
const getNewPath = async (getNewPathType: IGetNewPathType)
|
Accept: 'application/json',
|
||||||
: Promise<IGetNewPathOutput> => {
|
|
||||||
const output: IGetNewPathOutput = {
|
|
||||||
newPath: '',
|
|
||||||
isSuccessful: false,
|
|
||||||
};
|
|
||||||
args.jobLog('Going to apply new name');
|
|
||||||
args.jobLog(`Renaming ${getNewPathType.appName}...`);
|
|
||||||
|
|
||||||
const headers = {
|
|
||||||
'Content-Type': 'application/json',
|
|
||||||
'X-Api-Key': arr_api_key,
|
|
||||||
Accept: 'application/json',
|
|
||||||
};
|
|
||||||
|
|
||||||
const getParseRequestResultWrapper = async (fileName: string)
|
|
||||||
: Promise<IParseRequestResultWrapper> => {
|
|
||||||
// Using parse endpoint to get the movie/serie's id.
|
|
||||||
const parseRequestConfig = {
|
|
||||||
method: 'get',
|
|
||||||
url: `${arrHost}/api/v3/parse?title=${encodeURIComponent(fileName)}`,
|
|
||||||
headers,
|
|
||||||
};
|
|
||||||
const parseRequestResult = await args.deps.axios(parseRequestConfig);
|
|
||||||
const id = getNewPathType.delegates.getIdFromParseRequestResult(parseRequestResult);
|
|
||||||
args.jobLog(id !== '-1'
|
|
||||||
? `Found ${getNewPathType.contentName} ${id} with a file named '${fileName}'`
|
|
||||||
: `Didn't find ${getNewPathType.contentName} with a file named '${fileName}' in ${arrHost}.`);
|
|
||||||
return { parseRequestResult, id };
|
|
||||||
};
|
|
||||||
|
|
||||||
let fileName = fileNames.originalFileName;
|
|
||||||
let parseRequestResultWrapper = await getParseRequestResultWrapper(fileName);
|
|
||||||
// In case there has been a name change and the arr app already noticed it.
|
|
||||||
if (parseRequestResultWrapper.id === '-1' && fileNames.currentFileName !== fileNames.originalFileName) {
|
|
||||||
fileName = fileNames.currentFileName;
|
|
||||||
parseRequestResultWrapper = await getParseRequestResultWrapper(fileName);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Checking that the file has been found.
|
|
||||||
if (parseRequestResultWrapper.id !== '-1') {
|
|
||||||
// Using rename endpoint to get ids of all the files that need renaming.
|
|
||||||
const previewRenameRequestConfig = {
|
|
||||||
method: 'get',
|
|
||||||
url: getNewPathType.delegates.buildPreviewRenameResquestUrl(parseRequestResultWrapper),
|
|
||||||
headers,
|
|
||||||
};
|
|
||||||
const previewRenameRequestResult = await args.deps.axios(previewRenameRequestConfig);
|
|
||||||
const fileToRename = getNewPathType.delegates
|
|
||||||
.getFileToRenameFromPreviewRenameRequestResult(previewRenameRequestResult);
|
|
||||||
|
|
||||||
// Only if there is a rename to execute
|
|
||||||
if (fileToRename !== undefined) {
|
|
||||||
output.newPath = `${getFileAbosluteDir(args.inputFileObj._id)
|
|
||||||
}/${getFileName(fileToRename.newPath)
|
|
||||||
}.${getContainer(fileToRename.newPath)}`;
|
|
||||||
|
|
||||||
output.isSuccessful = await fileMoveOrCopy({
|
|
||||||
operation: 'move',
|
|
||||||
sourcePath: args.inputFileObj._id,
|
|
||||||
destinationPath: output.newPath,
|
|
||||||
args,
|
|
||||||
});
|
|
||||||
args.jobLog(`✔ Renamed ${getNewPathType.contentName} ${parseRequestResultWrapper.id} : `
|
|
||||||
+ `'${filePath}' => '${output.newPath}'.`);
|
|
||||||
} else {
|
|
||||||
output.isSuccessful = true;
|
|
||||||
args.jobLog('✔ No rename necessary.');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return output;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
let episodeNumber = 0;
|
let episodeNumber = 0;
|
||||||
const getNewPathTypes: IGetNewPathTypes = {
|
const renameType: IRenameType = arr === 'radarr'
|
||||||
radarr: {
|
? {
|
||||||
appName: 'Radarr',
|
appName: 'Radarr',
|
||||||
contentName: 'movie',
|
content: 'Movie',
|
||||||
delegates: {
|
delegates: {
|
||||||
getIdFromParseRequestResult:
|
getIdFromParseResponse:
|
||||||
(parseRequestResult) => String(parseRequestResult.data?.movie?.movieFile?.movieId ?? -1),
|
(fileDetailsWrapper) => String(fileDetailsWrapper.fileDetails?.data?.movie?.id ?? -1),
|
||||||
buildPreviewRenameResquestUrl:
|
buildPreviewRenameResquestUrl:
|
||||||
(parseRequestResultWrapper) => `${arrHost}/api/v3/rename?movieId=${parseRequestResultWrapper.id}`,
|
(fileDetailsWrapper) => `${arrHost}/api/v3/rename?movieId=${fileDetailsWrapper.id}`,
|
||||||
getFileToRenameFromPreviewRenameRequestResult:
|
getFileToRenameFromPreviewRenameResponse:
|
||||||
(previewRenameRequestResult) => (((previewRenameRequestResult.data?.length ?? 0) > 0)
|
(previewRenameResponse) => previewRenameResponse.data?.at(0),
|
||||||
? previewRenameRequestResult.data[0]
|
|
||||||
: undefined),
|
|
||||||
},
|
},
|
||||||
},
|
}
|
||||||
sonarr: {
|
: {
|
||||||
appName: 'Sonarr',
|
appName: 'Sonarr',
|
||||||
contentName: 'serie',
|
content: 'Serie',
|
||||||
delegates: {
|
delegates: {
|
||||||
getIdFromParseRequestResult:
|
getIdFromParseResponse:
|
||||||
(parseRequestResult) => String(parseRequestResult.data?.series?.id ?? -1),
|
(fileDetailsWrapper) => String(fileDetailsWrapper.fileDetails?.data?.series?.id ?? -1),
|
||||||
buildPreviewRenameResquestUrl:
|
buildPreviewRenameResquestUrl:
|
||||||
(parseRequestResultWrapper) => {
|
(fileDetailsWrapper) => {
|
||||||
[episodeNumber] = parseRequestResultWrapper.parseRequestResult.data.parsedEpisodeInfo?.episodeNumbers
|
[episodeNumber] = fileDetailsWrapper.fileDetails?.data.parsedEpisodeInfo?.episodeNumbers ?? [1];
|
||||||
?? [1];
|
return `${arrHost}/api/v3/rename?seriesId=${fileDetailsWrapper.id}&seasonNumber=`
|
||||||
return `${arrHost}/api/v3/rename?seriesId=${parseRequestResultWrapper.id}&seasonNumber=`
|
+ `${fileDetailsWrapper.fileDetails?.data.parsedEpisodeInfo?.seasonNumber ?? 1}`;
|
||||||
+ `${parseRequestResultWrapper.parseRequestResult.data.parsedEpisodeInfo?.seasonNumber ?? 1}`;
|
|
||||||
},
|
},
|
||||||
getFileToRenameFromPreviewRenameRequestResult:
|
getFileToRenameFromPreviewRenameResponse:
|
||||||
(previewRenameRequestResult) => (((previewRenameRequestResult.data?.length ?? 0) > 0)
|
(previewRenameResponse) => previewRenameResponse.data
|
||||||
? previewRenameRequestResult.data.find((episodeFile) => episodeFile.episodeNumbers?.at(0) === episodeNumber)
|
?.find((episodeFile) => episodeFile.episodeNumbers?.at(0) === episodeNumber),
|
||||||
: undefined),
|
|
||||||
},
|
},
|
||||||
},
|
};
|
||||||
};
|
|
||||||
const newPathOutput = await getNewPath(arr === 'radarr' ? getNewPathTypes.radarr : getNewPathTypes.sonarr);
|
args.jobLog('Going to apply new name');
|
||||||
|
args.jobLog(`Renaming ${renameType.appName}...`);
|
||||||
|
|
||||||
|
let fileDetailsWrapper = await getFileDetailsWrapper(args, arr, arrHost, headers, originalFileName, renameType);
|
||||||
|
// Useful in some edge cases
|
||||||
|
if (fileDetailsWrapper.id === '-1' && currentFileName !== originalFileName) {
|
||||||
|
fileDetailsWrapper = await getFileDetailsWrapper(args, arr, arrHost, headers, currentFileName, renameType);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Checking that the file has been found
|
||||||
|
if (fileDetailsWrapper.id !== '-1') {
|
||||||
|
// 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(fileDetailsWrapper),
|
||||||
|
headers,
|
||||||
|
});
|
||||||
|
const fileToRename = renameType.delegates
|
||||||
|
.getFileToRenameFromPreviewRenameResponse(previewRenameRequestResult);
|
||||||
|
|
||||||
|
// Only if there is a rename to execute
|
||||||
|
if (fileToRename !== undefined) {
|
||||||
|
newPath = `${getFileAbosluteDir(args.inputFileObj._id)
|
||||||
|
}/${getFileName(fileToRename.newPath)
|
||||||
|
}.${getContainer(fileToRename.newPath)}`;
|
||||||
|
|
||||||
|
isSuccessful = await fileMoveOrCopy({
|
||||||
|
operation: 'move',
|
||||||
|
sourcePath: args.inputFileObj._id,
|
||||||
|
destinationPath: newPath,
|
||||||
|
args,
|
||||||
|
});
|
||||||
|
args.jobLog(`✔ ${renameType.content} renamed ${fileDetailsWrapper.id} : `
|
||||||
|
+ `'${filePath}' => '${newPath}'.`);
|
||||||
|
} else {
|
||||||
|
isSuccessful = true;
|
||||||
|
args.jobLog('✔ No rename necessary.');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
outputFileObj:
|
outputFileObj:
|
||||||
newPathOutput.isSuccessful && newPathOutput.newPath !== ''
|
isSuccessful && newPath !== ''
|
||||||
? { ...args.inputFileObj, _id: newPathOutput.newPath }
|
? { ...args.inputFileObj, _id: newPath }
|
||||||
: args.inputFileObj,
|
: args.inputFileObj,
|
||||||
outputNumber: newPathOutput.isSuccessful ? 1 : 2,
|
outputNumber: isSuccessful ? 1 : 2,
|
||||||
variables: args.variables,
|
variables: args.variables,
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue