mirror of
https://github.com/gabehf/Tdarr_Plugins.git
synced 2026-03-14 17:55:55 -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
|
|
@ -72,191 +72,190 @@ const details = (): IpluginDetails => ({
|
|||
],
|
||||
});
|
||||
|
||||
interface IFileNames {
|
||||
originalFileName: string,
|
||||
currentFileName: string
|
||||
interface IHTTPHeaders {
|
||||
'Content-Type': string,
|
||||
'X-Api-Key': string,
|
||||
Accept: string,
|
||||
}
|
||||
interface IParseRequestResult {
|
||||
interface IFileDetails {
|
||||
data: {
|
||||
movie?: {
|
||||
movieFile: {
|
||||
movieId: number
|
||||
},
|
||||
},
|
||||
series?: {
|
||||
id: number
|
||||
},
|
||||
movie?: { id: number },
|
||||
series?: { id: number },
|
||||
parsedEpisodeInfo?: {
|
||||
episodeNumbers: number[],
|
||||
seasonNumber: number
|
||||
},
|
||||
},
|
||||
}
|
||||
interface IParseRequestResultWrapper {
|
||||
parseRequestResult: IParseRequestResult,
|
||||
interface IFileDetailsWrapper {
|
||||
id: string
|
||||
fileDetails?: IFileDetails
|
||||
}
|
||||
interface IFileToRename {
|
||||
newPath: string
|
||||
episodeNumbers?: number[]
|
||||
}
|
||||
interface IPreviewRenameRequestResult {
|
||||
interface IPreviewRenameResponse {
|
||||
data: IFileToRename[]
|
||||
}
|
||||
interface IGetNewPathDelegates {
|
||||
getIdFromParseRequestResult:
|
||||
(parseRequestResult: IParseRequestResult) => string,
|
||||
buildPreviewRenameResquestUrl:
|
||||
(parseRequestResult: IParseRequestResultWrapper) => string,
|
||||
getFileToRenameFromPreviewRenameRequestResult:
|
||||
(previewRenameRequestResult: IPreviewRenameRequestResult) => IFileToRename | undefined
|
||||
}
|
||||
interface IGetNewPathType {
|
||||
interface IRenameType {
|
||||
appName: string,
|
||||
contentName: string,
|
||||
delegates: IGetNewPathDelegates
|
||||
}
|
||||
interface IGetNewPathTypes {
|
||||
radarr: IGetNewPathType,
|
||||
sonarr: IGetNewPathType
|
||||
}
|
||||
interface IGetNewPathOutput {
|
||||
newPath: string,
|
||||
isSuccessful: boolean
|
||||
content: string,
|
||||
delegates: {
|
||||
getIdFromParseResponse:
|
||||
(fileDetailsWrapper: IFileDetailsWrapper) => string,
|
||||
buildPreviewRenameResquestUrl:
|
||||
(fileDetailsWrapper: IFileDetailsWrapper) => string,
|
||||
getFileToRenameFromPreviewRenameResponse:
|
||||
(previewRenameResponse: IPreviewRenameResponse) => IFileToRename | undefined
|
||||
}
|
||||
}
|
||||
|
||||
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 lib = require('../../../../../methods/lib')();
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars,no-param-reassign
|
||||
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 arrHost = arr_host.endsWith('/') ? arr_host.slice(0, -1) : arr_host;
|
||||
const filePath = args.originalLibraryFile?._id ?? '';
|
||||
const fileNames: IFileNames = {
|
||||
originalFileName: getFileName(args.originalLibraryFile?._id ?? ''),
|
||||
currentFileName: getFileName(args.inputFileObj?._id ?? ''),
|
||||
};
|
||||
|
||||
const getNewPath = async (getNewPathType: IGetNewPathType)
|
||||
: 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;
|
||||
const originalFileName = args.originalLibraryFile?._id ?? '';
|
||||
const currentFileName = args.inputFileObj?._id ?? '';
|
||||
const headers = {
|
||||
'Content-Type': 'application/json',
|
||||
'X-Api-Key': String(args.inputs.arr_api_key),
|
||||
Accept: 'application/json',
|
||||
};
|
||||
|
||||
let episodeNumber = 0;
|
||||
const getNewPathTypes: IGetNewPathTypes = {
|
||||
radarr: {
|
||||
const renameType: IRenameType = arr === 'radarr'
|
||||
? {
|
||||
appName: 'Radarr',
|
||||
contentName: 'movie',
|
||||
content: 'Movie',
|
||||
delegates: {
|
||||
getIdFromParseRequestResult:
|
||||
(parseRequestResult) => String(parseRequestResult.data?.movie?.movieFile?.movieId ?? -1),
|
||||
getIdFromParseResponse:
|
||||
(fileDetailsWrapper) => String(fileDetailsWrapper.fileDetails?.data?.movie?.id ?? -1),
|
||||
buildPreviewRenameResquestUrl:
|
||||
(parseRequestResultWrapper) => `${arrHost}/api/v3/rename?movieId=${parseRequestResultWrapper.id}`,
|
||||
getFileToRenameFromPreviewRenameRequestResult:
|
||||
(previewRenameRequestResult) => (((previewRenameRequestResult.data?.length ?? 0) > 0)
|
||||
? previewRenameRequestResult.data[0]
|
||||
: undefined),
|
||||
(fileDetailsWrapper) => `${arrHost}/api/v3/rename?movieId=${fileDetailsWrapper.id}`,
|
||||
getFileToRenameFromPreviewRenameResponse:
|
||||
(previewRenameResponse) => previewRenameResponse.data?.at(0),
|
||||
},
|
||||
},
|
||||
sonarr: {
|
||||
}
|
||||
: {
|
||||
appName: 'Sonarr',
|
||||
contentName: 'serie',
|
||||
content: 'Serie',
|
||||
delegates: {
|
||||
getIdFromParseRequestResult:
|
||||
(parseRequestResult) => String(parseRequestResult.data?.series?.id ?? -1),
|
||||
getIdFromParseResponse:
|
||||
(fileDetailsWrapper) => String(fileDetailsWrapper.fileDetails?.data?.series?.id ?? -1),
|
||||
buildPreviewRenameResquestUrl:
|
||||
(parseRequestResultWrapper) => {
|
||||
[episodeNumber] = parseRequestResultWrapper.parseRequestResult.data.parsedEpisodeInfo?.episodeNumbers
|
||||
?? [1];
|
||||
return `${arrHost}/api/v3/rename?seriesId=${parseRequestResultWrapper.id}&seasonNumber=`
|
||||
+ `${parseRequestResultWrapper.parseRequestResult.data.parsedEpisodeInfo?.seasonNumber ?? 1}`;
|
||||
(fileDetailsWrapper) => {
|
||||
[episodeNumber] = fileDetailsWrapper.fileDetails?.data.parsedEpisodeInfo?.episodeNumbers ?? [1];
|
||||
return `${arrHost}/api/v3/rename?seriesId=${fileDetailsWrapper.id}&seasonNumber=`
|
||||
+ `${fileDetailsWrapper.fileDetails?.data.parsedEpisodeInfo?.seasonNumber ?? 1}`;
|
||||
},
|
||||
getFileToRenameFromPreviewRenameRequestResult:
|
||||
(previewRenameRequestResult) => (((previewRenameRequestResult.data?.length ?? 0) > 0)
|
||||
? previewRenameRequestResult.data.find((episodeFile) => episodeFile.episodeNumbers?.at(0) === episodeNumber)
|
||||
: undefined),
|
||||
getFileToRenameFromPreviewRenameResponse:
|
||||
(previewRenameResponse) => previewRenameResponse.data
|
||||
?.find((episodeFile) => episodeFile.episodeNumbers?.at(0) === episodeNumber),
|
||||
},
|
||||
},
|
||||
};
|
||||
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 {
|
||||
outputFileObj:
|
||||
newPathOutput.isSuccessful && newPathOutput.newPath !== ''
|
||||
? { ...args.inputFileObj, _id: newPathOutput.newPath }
|
||||
isSuccessful && newPath !== ''
|
||||
? { ...args.inputFileObj, _id: newPath }
|
||||
: args.inputFileObj,
|
||||
outputNumber: newPathOutput.isSuccessful ? 1 : 2,
|
||||
outputNumber: isSuccessful ? 1 : 2,
|
||||
variables: args.variables,
|
||||
};
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue