mirror of
https://github.com/gabehf/Tdarr_Plugins.git
synced 2026-03-16 02:35:54 -07:00
Force rename Radarr/Sonarr logic simplified to just apply the naming policy
This commit is contained in:
parent
d6ac4ba9fa
commit
713923cc1e
2 changed files with 45 additions and 110 deletions
|
|
@ -43,8 +43,8 @@ exports.plugin = exports.details = void 0;
|
||||||
var fileMoveOrCopy_1 = __importDefault(require("../../../../FlowHelpers/1.0.0/fileMoveOrCopy"));
|
var fileMoveOrCopy_1 = __importDefault(require("../../../../FlowHelpers/1.0.0/fileMoveOrCopy"));
|
||||||
var fileUtils_1 = require("../../../../FlowHelpers/1.0.0/fileUtils");
|
var fileUtils_1 = require("../../../../FlowHelpers/1.0.0/fileUtils");
|
||||||
var details = function () { return ({
|
var details = function () { return ({
|
||||||
name: 'Force File Rename Radarr or Sonarr',
|
name: 'Apply Radarr or Sonarr naming policy',
|
||||||
description: 'Force Radarr or Sonarr to rename a file according to the naming policy',
|
description: 'Apply Radarr or Sonarr naming policy to a file. This plugin should be called after the original file has been replaced and Radarr or Sonarr has been notified. Radarr or Sonarr should also be notified after this plugin.',
|
||||||
style: {
|
style: {
|
||||||
borderColor: 'green',
|
borderColor: 'green',
|
||||||
},
|
},
|
||||||
|
|
@ -101,10 +101,9 @@ var details = function () { return ({
|
||||||
}); };
|
}); };
|
||||||
exports.details = details;
|
exports.details = details;
|
||||||
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, _a, arr, arr_api_key, arr_host, arrHost, fileName, rename, existingPath, newPath, episodeNumber_1, outputFileObj, destinationPath;
|
var lib, _a, arr, arr_api_key, arr_host, arrHost, fileName, rename, destinationPath, episodeNumber_1;
|
||||||
var _b, _c;
|
return __generator(this, function (_b) {
|
||||||
return __generator(this, function (_d) {
|
switch (_b.label) {
|
||||||
switch (_d.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
|
||||||
|
|
@ -114,13 +113,13 @@ var plugin = function (args) { return __awaiter(void 0, void 0, void 0, function
|
||||||
arrHost = arr_host.endsWith('/') ? arr_host.slice(0, -1) : arr_host;
|
arrHost = arr_host.endsWith('/') ? arr_host.slice(0, -1) : arr_host;
|
||||||
fileName = (0, fileUtils_1.getFileName)(args.inputFileObj._id);
|
fileName = (0, fileUtils_1.getFileName)(args.inputFileObj._id);
|
||||||
rename = function (delegates) { return __awaiter(void 0, void 0, void 0, function () {
|
rename = function (delegates) { return __awaiter(void 0, void 0, void 0, function () {
|
||||||
var existingPath, newPath, headers, parseRequestConfig, parseRequestResult, id, previewRenameRequestConfig, previewRenameRequestResult, fileToRename, renameRequestConfig;
|
var destinationPath, headers, parseRequestConfig, parseRequestResult, id, previewRenameRequestConfig, previewRenameRequestResult, fileToRename;
|
||||||
return __generator(this, function (_a) {
|
return __generator(this, function (_a) {
|
||||||
switch (_a.label) {
|
switch (_a.label) {
|
||||||
case 0:
|
case 0:
|
||||||
|
destinationPath = '';
|
||||||
args.jobLog('Going to force rename');
|
args.jobLog('Going to force rename');
|
||||||
args.jobLog("Renaming ".concat(arr === 'radarr' ? 'Radarr' : 'Sonarr', "..."));
|
args.jobLog("Renaming ".concat(arr === 'radarr' ? 'Radarr' : 'Sonarr', "..."));
|
||||||
existingPath = '', newPath = '';
|
|
||||||
headers = {
|
headers = {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
'X-Api-Key': arr_api_key,
|
'X-Api-Key': arr_api_key,
|
||||||
|
|
@ -145,26 +144,25 @@ var plugin = function (args) { return __awaiter(void 0, void 0, void 0, function
|
||||||
previewRenameRequestResult = _a.sent();
|
previewRenameRequestResult = _a.sent();
|
||||||
fileToRename = delegates.getFileToRename(previewRenameRequestResult);
|
fileToRename = delegates.getFileToRename(previewRenameRequestResult);
|
||||||
if (!(fileToRename !== undefined)) return [3 /*break*/, 4];
|
if (!(fileToRename !== undefined)) return [3 /*break*/, 4];
|
||||||
(existingPath = fileToRename.existingPath, newPath = fileToRename.newPath);
|
destinationPath = "".concat((0, fileUtils_1.getFileAbosluteDir)(args.inputFileObj._id), "/").concat((0, fileUtils_1.getFileName)(fileToRename.newPath), ".").concat((0, fileUtils_1.getContainer)(fileToRename.newPath));
|
||||||
renameRequestConfig = {
|
return [4 /*yield*/, (0, fileMoveOrCopy_1.default)({
|
||||||
method: 'post',
|
operation: 'move',
|
||||||
url: "".concat(arrHost, "/api/v3/command"),
|
sourcePath: args.inputFileObj._id,
|
||||||
headers: headers,
|
destinationPath: destinationPath,
|
||||||
data: JSON.stringify(delegates.getRenameResquestConfigData(id, fileToRename))
|
args: args,
|
||||||
};
|
})];
|
||||||
return [4 /*yield*/, args.deps.axios(renameRequestConfig)];
|
|
||||||
case 3:
|
case 3:
|
||||||
_a.sent();
|
_a.sent();
|
||||||
args.jobLog("\u2714 Renamed ".concat(arr === 'radarr' ? 'movie' : 'serie', " ").concat(id, " in ").concat(arr === 'radarr' ? 'Radarr' : 'Sonarr', " : '").concat(existingPath, "' => '").concat(newPath, "'."));
|
args.jobLog("\u2714 Renamed ".concat(arr === 'radarr' ? 'movie' : 'serie', " ").concat(id, " : '").concat(args.inputFileObj._id, "' => '").concat(destinationPath, "'."));
|
||||||
return [3 /*break*/, 5];
|
return [3 /*break*/, 5];
|
||||||
case 4:
|
case 4:
|
||||||
args.jobLog('✔ No rename necessary.');
|
args.jobLog('✔ No rename necessary.');
|
||||||
_a.label = 5;
|
_a.label = 5;
|
||||||
case 5: return [2 /*return*/, { existingPath: existingPath, newPath: newPath }];
|
case 5: return [2 /*return*/, destinationPath];
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}); };
|
}); };
|
||||||
newPath = '';
|
destinationPath = '';
|
||||||
if (!(arr === 'radarr')) return [3 /*break*/, 2];
|
if (!(arr === 'radarr')) return [3 /*break*/, 2];
|
||||||
return [4 /*yield*/, rename({
|
return [4 /*yield*/, rename({
|
||||||
getId: function (parseRequestResult) { return parseRequestResult.data.movie.movieFile.movieId; },
|
getId: function (parseRequestResult) { return parseRequestResult.data.movie.movieFile.movieId; },
|
||||||
|
|
@ -174,17 +172,10 @@ var plugin = function (args) { return __awaiter(void 0, void 0, void 0, function
|
||||||
return (((_b = (_a = previewRenameRequestResult.data) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0) > 0) ?
|
return (((_b = (_a = previewRenameRequestResult.data) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0) > 0) ?
|
||||||
previewRenameRequestResult.data[0]
|
previewRenameRequestResult.data[0]
|
||||||
: undefined;
|
: undefined;
|
||||||
},
|
|
||||||
getRenameResquestConfigData: function (id, fileToRename) {
|
|
||||||
return {
|
|
||||||
name: 'RenameFiles',
|
|
||||||
movieId: id,
|
|
||||||
files: [fileToRename.movieFileId]
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
})];
|
})];
|
||||||
case 1:
|
case 1:
|
||||||
(_b = _d.sent(), existingPath = _b.existingPath, newPath = _b.newPath);
|
destinationPath = _b.sent();
|
||||||
return [3 /*break*/, 5];
|
return [3 /*break*/, 5];
|
||||||
case 2:
|
case 2:
|
||||||
if (!(arr === 'sonarr')) return [3 /*break*/, 4];
|
if (!(arr === 'sonarr')) return [3 /*break*/, 4];
|
||||||
|
|
@ -200,38 +191,16 @@ var plugin = function (args) { return __awaiter(void 0, void 0, void 0, function
|
||||||
return (((_b = (_a = previewRenameRequestResult.data) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0) > 0) ?
|
return (((_b = (_a = previewRenameRequestResult.data) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0) > 0) ?
|
||||||
previewRenameRequestResult.data.find(function (episFile) { var _a, _b; return (((_b = (_a = episFile.episodeNumbers) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0) > 0) ? episFile.episodeNumbers[0] === episodeNumber_1 : false; })
|
previewRenameRequestResult.data.find(function (episFile) { var _a, _b; return (((_b = (_a = episFile.episodeNumbers) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0) > 0) ? episFile.episodeNumbers[0] === episodeNumber_1 : false; })
|
||||||
: undefined;
|
: undefined;
|
||||||
},
|
|
||||||
getRenameResquestConfigData: function (id, fileToRename) {
|
|
||||||
return {
|
|
||||||
name: 'RenameFiles',
|
|
||||||
seriesId: id,
|
|
||||||
files: [fileToRename.episodeFileId]
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
})];
|
})];
|
||||||
case 3:
|
case 3:
|
||||||
(_c = _d.sent(), existingPath = _c.existingPath, newPath = _c.newPath);
|
destinationPath = _b.sent();
|
||||||
return [3 /*break*/, 5];
|
return [3 /*break*/, 5];
|
||||||
case 4:
|
case 4:
|
||||||
args.jobLog('No arr specified in plugin inputs.');
|
args.jobLog('No arr specified in plugin inputs.');
|
||||||
_d.label = 5;
|
_b.label = 5;
|
||||||
case 5:
|
case 5: return [2 /*return*/, {
|
||||||
outputFileObj = args.inputFileObj;
|
outputFileObj: destinationPath !== '' ? { _id: destinationPath } : args.inputFileObj,
|
||||||
if (!(existingPath !== newPath)) return [3 /*break*/, 7];
|
|
||||||
destinationPath = "".concat((0, fileUtils_1.getFileAbosluteDir)(args.inputFileObj._id), "/").concat((0, fileUtils_1.getFileName)(newPath));
|
|
||||||
return [4 /*yield*/, (0, fileMoveOrCopy_1.default)({
|
|
||||||
operation: 'move',
|
|
||||||
sourcePath: args.inputFileObj._id,
|
|
||||||
destinationPath: destinationPath,
|
|
||||||
args: args,
|
|
||||||
})];
|
|
||||||
case 6:
|
|
||||||
_d.sent();
|
|
||||||
args.jobLog("\u2714 File moved : '".concat(args.inputFileObj._id, "' => '").concat(destinationPath, "'."));
|
|
||||||
outputFileObj = { _id: destinationPath };
|
|
||||||
_d.label = 7;
|
|
||||||
case 7: return [2 /*return*/, {
|
|
||||||
outputFileObj: outputFileObj,
|
|
||||||
outputNumber: 1,
|
outputNumber: 1,
|
||||||
variables: args.variables,
|
variables: args.variables,
|
||||||
}];
|
}];
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
import fileMoveOrCopy from '../../../../FlowHelpers/1.0.0/fileMoveOrCopy';
|
import fileMoveOrCopy from '../../../../FlowHelpers/1.0.0/fileMoveOrCopy';
|
||||||
import {
|
import {
|
||||||
getFileAbosluteDir, getFileName,
|
getContainer, getFileAbosluteDir, getFileName,
|
||||||
} from '../../../../FlowHelpers/1.0.0/fileUtils';
|
} from '../../../../FlowHelpers/1.0.0/fileUtils';
|
||||||
import {
|
import {
|
||||||
IpluginDetails,
|
IpluginDetails,
|
||||||
|
|
@ -9,8 +9,8 @@ import {
|
||||||
} from '../../../../FlowHelpers/1.0.0/interfaces/interfaces';
|
} from '../../../../FlowHelpers/1.0.0/interfaces/interfaces';
|
||||||
|
|
||||||
const details = (): IpluginDetails => ({
|
const details = (): IpluginDetails => ({
|
||||||
name: 'Force File Rename Radarr or Sonarr',
|
name: 'Apply Radarr or Sonarr naming policy',
|
||||||
description: 'Force Radarr or Sonarr to rename a file according to the naming policy',
|
description: 'Apply Radarr or Sonarr naming policy to a file. This plugin should be called after the original file has been replaced and Radarr or Sonarr has been notified. Radarr or Sonarr should also be notified after this plugin.',
|
||||||
style: {
|
style: {
|
||||||
borderColor: 'green',
|
borderColor: 'green',
|
||||||
},
|
},
|
||||||
|
|
@ -79,17 +79,16 @@ const plugin = async (args: IpluginInputArgs): Promise<IpluginOutputArgs> => {
|
||||||
interface IRenameDelegates {
|
interface IRenameDelegates {
|
||||||
getId: (parseRequestResult: any) => any,
|
getId: (parseRequestResult: any) => any,
|
||||||
getPreviewRenameResquestUrl: (id: any, parseRequestResult: any) => any,
|
getPreviewRenameResquestUrl: (id: any, parseRequestResult: any) => any,
|
||||||
getFileToRename: (previewRenameRequestResult: any) => any,
|
getFileToRename: (previewRenameRequestResult: any) => any
|
||||||
getRenameResquestConfigData: (id: any, fileToRename: any) => any
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const rename = async (delegates: IRenameDelegates)
|
const rename = async (delegates: IRenameDelegates)
|
||||||
: Promise<{ existingPath: any, newPath: any }> => {
|
: Promise<string> => {
|
||||||
|
let destinationPath = '';
|
||||||
|
|
||||||
args.jobLog('Going to force rename');
|
args.jobLog('Going to force rename');
|
||||||
args.jobLog(`Renaming ${arr === 'radarr' ? 'Radarr' : 'Sonarr'}...`);
|
args.jobLog(`Renaming ${arr === 'radarr' ? 'Radarr' : 'Sonarr'}...`);
|
||||||
|
|
||||||
let existingPath = '', newPath = '';
|
|
||||||
|
|
||||||
const headers = {
|
const headers = {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
'X-Api-Key': arr_api_key,
|
'X-Api-Key': arr_api_key,
|
||||||
|
|
@ -116,44 +115,34 @@ const plugin = async (args: IpluginInputArgs): Promise<IpluginOutputArgs> => {
|
||||||
|
|
||||||
// Only if there is a rename to execute
|
// Only if there is a rename to execute
|
||||||
if (fileToRename !== undefined) {
|
if (fileToRename !== undefined) {
|
||||||
({ existingPath, newPath } = fileToRename);
|
destinationPath = `${getFileAbosluteDir(args.inputFileObj._id)}/${getFileName(fileToRename.newPath)}.${getContainer(fileToRename.newPath)}`;
|
||||||
|
|
||||||
// Using command endpoint to schedule the renames.
|
await fileMoveOrCopy({
|
||||||
const renameRequestConfig = {
|
operation: 'move',
|
||||||
method: 'post',
|
sourcePath: args.inputFileObj._id,
|
||||||
url: `${arrHost}/api/v3/command`,
|
destinationPath: destinationPath,
|
||||||
headers,
|
args,
|
||||||
data: JSON.stringify(delegates.getRenameResquestConfigData(id, fileToRename))
|
});
|
||||||
};
|
args.jobLog(`✔ Renamed ${arr === 'radarr' ? 'movie' : 'serie'} ${id} : '${args.inputFileObj._id}' => '${destinationPath}'.`);
|
||||||
await args.deps.axios(renameRequestConfig);
|
|
||||||
|
|
||||||
args.jobLog(`✔ Renamed ${arr === 'radarr' ? 'movie' : 'serie'} ${id} in ${arr === 'radarr' ? 'Radarr' : 'Sonarr'} : '${existingPath}' => '${newPath}'.`);
|
|
||||||
} else
|
} else
|
||||||
args.jobLog('✔ No rename necessary.');
|
args.jobLog('✔ No rename necessary.');
|
||||||
|
|
||||||
return { existingPath, newPath };
|
return destinationPath;
|
||||||
};
|
};
|
||||||
|
|
||||||
let existingPath, newPath = '';
|
let destinationPath = '';
|
||||||
if (arr === 'radarr') {
|
if (arr === 'radarr') {
|
||||||
({ existingPath, newPath } = await rename({
|
destinationPath = await rename({
|
||||||
getId: (parseRequestResult) => parseRequestResult.data.movie.movieFile.movieId,
|
getId: (parseRequestResult) => parseRequestResult.data.movie.movieFile.movieId,
|
||||||
getPreviewRenameResquestUrl: (id, parseRequestResult) => `${arrHost}/api/v3/rename?movieId=${id}`,
|
getPreviewRenameResquestUrl: (id, parseRequestResult) => `${arrHost}/api/v3/rename?movieId=${id}`,
|
||||||
getFileToRename: (previewRenameRequestResult) =>
|
getFileToRename: (previewRenameRequestResult) =>
|
||||||
((previewRenameRequestResult.data?.length ?? 0) > 0) ?
|
((previewRenameRequestResult.data?.length ?? 0) > 0) ?
|
||||||
previewRenameRequestResult.data[0]
|
previewRenameRequestResult.data[0]
|
||||||
: undefined,
|
: undefined
|
||||||
getRenameResquestConfigData: (id, fileToRename) => {
|
});
|
||||||
return {
|
|
||||||
name: 'RenameFiles',
|
|
||||||
movieId: id,
|
|
||||||
files: [fileToRename.movieFileId]
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}));
|
|
||||||
} else if (arr === 'sonarr') {
|
} else if (arr === 'sonarr') {
|
||||||
let episodeNumber = 0;
|
let episodeNumber = 0;
|
||||||
({ existingPath, newPath } = await rename({
|
destinationPath = await rename({
|
||||||
getId: (parseRequestResult) => parseRequestResult.data.series.id,
|
getId: (parseRequestResult) => parseRequestResult.data.series.id,
|
||||||
getPreviewRenameResquestUrl: (id, parseRequestResult) => {
|
getPreviewRenameResquestUrl: (id, parseRequestResult) => {
|
||||||
episodeNumber = parseRequestResult.data.parsedEpisodeInfo.episodeNumbers[0];
|
episodeNumber = parseRequestResult.data.parsedEpisodeInfo.episodeNumbers[0];
|
||||||
|
|
@ -163,37 +152,14 @@ const plugin = async (args: IpluginInputArgs): Promise<IpluginOutputArgs> => {
|
||||||
return ((previewRenameRequestResult.data?.length ?? 0) > 0) ?
|
return ((previewRenameRequestResult.data?.length ?? 0) > 0) ?
|
||||||
previewRenameRequestResult.data.find((episFile: { episodeNumbers: number[]; }) => ((episFile.episodeNumbers?.length ?? 0) > 0) ? episFile.episodeNumbers[0] === episodeNumber : false)
|
previewRenameRequestResult.data.find((episFile: { episodeNumbers: number[]; }) => ((episFile.episodeNumbers?.length ?? 0) > 0) ? episFile.episodeNumbers[0] === episodeNumber : false)
|
||||||
: undefined
|
: undefined
|
||||||
},
|
|
||||||
getRenameResquestConfigData: (id, fileToRename) => {
|
|
||||||
return {
|
|
||||||
name: 'RenameFiles',
|
|
||||||
seriesId: id,
|
|
||||||
files: [fileToRename.episodeFileId]
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
}));
|
});
|
||||||
} else {
|
} else {
|
||||||
args.jobLog('No arr specified in plugin inputs.');
|
args.jobLog('No arr specified in plugin inputs.');
|
||||||
}
|
}
|
||||||
|
|
||||||
// If file has been renamed, move the transcoded file accordingly
|
|
||||||
let outputFileObj: {_id : string} = args.inputFileObj;
|
|
||||||
if (existingPath !== newPath) {
|
|
||||||
const destinationPath = `${getFileAbosluteDir(args.inputFileObj._id)}/${getFileName(newPath)}`;
|
|
||||||
|
|
||||||
await fileMoveOrCopy({
|
|
||||||
operation: 'move',
|
|
||||||
sourcePath: args.inputFileObj._id,
|
|
||||||
destinationPath: destinationPath,
|
|
||||||
args,
|
|
||||||
});
|
|
||||||
args.jobLog(`✔ File moved : '${args.inputFileObj._id}' => '${destinationPath}'.`);
|
|
||||||
|
|
||||||
outputFileObj = { _id: destinationPath };
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
outputFileObj: outputFileObj,
|
outputFileObj: destinationPath !== '' ? { _id: destinationPath } : args.inputFileObj,
|
||||||
outputNumber: 1,
|
outputNumber: 1,
|
||||||
variables: args.variables,
|
variables: args.variables,
|
||||||
};
|
};
|
||||||
Loading…
Add table
Add a link
Reference in a new issue