mirror of
https://github.com/gabehf/Tdarr_Plugins.git
synced 2026-03-10 16:00:28 -07:00
Added imdbid detection from file name to get the movie or serie id
This commit is contained in:
parent
cf974234ea
commit
e5638dda65
2 changed files with 178 additions and 175 deletions
|
|
@ -1,4 +1,5 @@
|
|||
import { getFileName } from '../../../../FlowHelpers/1.0.0/fileUtils';
|
||||
import path from 'path';
|
||||
import { getFileAbosluteDir, getFileName } from '../../../../FlowHelpers/1.0.0/fileUtils';
|
||||
import {
|
||||
IpluginDetails,
|
||||
IpluginInputArgs,
|
||||
|
|
@ -67,127 +68,121 @@ const details = (): IpluginDetails => ({
|
|||
],
|
||||
});
|
||||
|
||||
interface IFileNames {
|
||||
originalFileName: string,
|
||||
currentFileName: string
|
||||
interface IHTTPHeaders {
|
||||
'Content-Type': string,
|
||||
'X-Api-Key': string,
|
||||
Accept: string,
|
||||
}
|
||||
interface IParsedRequestResult {
|
||||
data: {
|
||||
movie?: {
|
||||
movieFile: {
|
||||
movieId: number
|
||||
},
|
||||
},
|
||||
series?: {
|
||||
id: number
|
||||
},
|
||||
movie?: { id: number },
|
||||
series?: { id: number },
|
||||
},
|
||||
}
|
||||
interface IRefreshDelegates {
|
||||
getIdFromParseRequestResult: (parseRequestResult: IParsedRequestResult) => number,
|
||||
getIdFromParseResponse: (parseRequestResult: IParsedRequestResult) => number,
|
||||
buildRefreshResquestData: (id: number) => string
|
||||
}
|
||||
interface IRefreshType {
|
||||
appName: string,
|
||||
contentName: string,
|
||||
content: string,
|
||||
delegates: IRefreshDelegates
|
||||
}
|
||||
interface IRefreshTypes {
|
||||
radarr: IRefreshType,
|
||||
sonarr: IRefreshType
|
||||
}
|
||||
|
||||
const getId = async (
|
||||
args: IpluginInputArgs,
|
||||
arrHost: string, headers: IHTTPHeaders,
|
||||
fileName: string,
|
||||
refreshType: IRefreshType,
|
||||
)
|
||||
: Promise<number> => {
|
||||
const imdbId = /\b(tt|nm|co|ev|ch|ni)\d{7,10}\b/i.exec(fileName)?.at(0) ?? '';
|
||||
let id = (imdbId !== '')
|
||||
? Number(
|
||||
(await args.deps.axios({
|
||||
method: 'get',
|
||||
url: `${arrHost}/api/v3/movie/lookup?terms=imdb:${imdbId}`,
|
||||
headers,
|
||||
})).data?.id ?? -1,
|
||||
)
|
||||
: -1;
|
||||
if (id === -1) {
|
||||
id = refreshType.delegates.getIdFromParseResponse(
|
||||
(await args.deps.axios({
|
||||
method: 'get',
|
||||
url: `${arrHost}/api/v3/parse?title=${encodeURIComponent(getFileName(fileName))}`,
|
||||
headers,
|
||||
})),
|
||||
);
|
||||
}
|
||||
args.jobLog(`${refreshType.content} ${id !== -1 ? `${id} found` : 'not found'} for '${fileName}', imdb '${imdbId}'`);
|
||||
return id;
|
||||
};
|
||||
|
||||
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;
|
||||
// Variables initialization
|
||||
let refreshed = 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 fileNames: IFileNames = {
|
||||
originalFileName: getFileName(args.originalLibraryFile?._id ?? ''),
|
||||
currentFileName: getFileName(args.inputFileObj?._id ?? ''),
|
||||
const absoluteFileDir = getFileAbosluteDir(args.originalLibraryFile?._id ?? '');
|
||||
const fileNames = {
|
||||
originalFileName: path.join(absoluteFileDir, getFileName(args.originalLibraryFile?._id ?? '')),
|
||||
currentFileName: path.join(absoluteFileDir, getFileName(args.inputFileObj?._id ?? '')),
|
||||
};
|
||||
|
||||
const refresh = async (refreshType: IRefreshType)
|
||||
: Promise<boolean> => {
|
||||
args.jobLog('Going to force scan');
|
||||
args.jobLog(`Refreshing ${refreshType.appName}...`);
|
||||
|
||||
let refreshed = false;
|
||||
const headers = {
|
||||
'Content-Type': 'application/json',
|
||||
'X-Api-Key': arr_api_key,
|
||||
Accept: 'application/json',
|
||||
};
|
||||
|
||||
const getId = async (fileName: string)
|
||||
: Promise<number> => {
|
||||
// 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 = refreshType.delegates.getIdFromParseRequestResult(parseRequestResult);
|
||||
args.jobLog(id !== -1
|
||||
? `Found ${refreshType.contentName} ${id} with a file named '${fileName}'`
|
||||
: `Didn't find ${refreshType.contentName} with a file named '${fileName}' in ${arrHost}.`);
|
||||
return id;
|
||||
};
|
||||
|
||||
let fileName = fileNames.originalFileName;
|
||||
let id = await getId(fileName);
|
||||
// In case there has been a name change and the arr app already noticed it.
|
||||
if (id === -1 && fileNames.currentFileName !== fileNames.originalFileName) {
|
||||
fileName = fileNames.currentFileName;
|
||||
id = await getId(fileName);
|
||||
}
|
||||
|
||||
// Checking that the file has been found.
|
||||
if (id !== -1) {
|
||||
// Using refresh command endpoint to force rescan.
|
||||
const refreshResquestConfig = {
|
||||
method: 'post',
|
||||
url: `${arrHost}/api/v3/command`,
|
||||
headers,
|
||||
data: refreshType.delegates.buildRefreshResquestData(id),
|
||||
};
|
||||
await args.deps.axios(refreshResquestConfig);
|
||||
|
||||
refreshed = true;
|
||||
args.jobLog(`✔ Refreshed ${refreshType.contentName} ${id} in ${refreshType.appName}.`);
|
||||
}
|
||||
|
||||
return refreshed;
|
||||
const headers: IHTTPHeaders = {
|
||||
'Content-Type': 'application/json',
|
||||
'X-Api-Key': String(args.inputs.arr_api_key),
|
||||
Accept: 'application/json',
|
||||
};
|
||||
|
||||
const refreshTypes: IRefreshTypes = {
|
||||
radarr: {
|
||||
const refreshType: IRefreshType = arr === 'radarr'
|
||||
? {
|
||||
appName: 'Radarr',
|
||||
contentName: 'movie',
|
||||
content: 'Movie',
|
||||
delegates: {
|
||||
getIdFromParseRequestResult:
|
||||
(parseRequestResult) => Number(parseRequestResult.data?.movie?.movieFile?.movieId ?? -1),
|
||||
getIdFromParseResponse:
|
||||
(parseRequestResult: IParsedRequestResult) => Number(parseRequestResult.data?.movie?.id ?? -1),
|
||||
buildRefreshResquestData:
|
||||
(id) => JSON.stringify({ name: 'RefreshMovie', movieIds: [id] }),
|
||||
},
|
||||
},
|
||||
sonarr: {
|
||||
}
|
||||
: {
|
||||
appName: 'Sonarr',
|
||||
contentName: 'serie',
|
||||
content: 'Serie',
|
||||
delegates: {
|
||||
getIdFromParseRequestResult:
|
||||
(parseRequestResult) => Number(parseRequestResult.data?.series?.id ?? -1),
|
||||
getIdFromParseResponse:
|
||||
(parseRequestResult: IParsedRequestResult) => Number(parseRequestResult.data?.series?.id ?? -1),
|
||||
buildRefreshResquestData:
|
||||
(id) => JSON.stringify({ name: 'RefreshSeries', seriesId: id }),
|
||||
},
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
const refreshed = await refresh(arr === 'radarr' ? refreshTypes.radarr : refreshTypes.sonarr);
|
||||
args.jobLog('Going to force scan');
|
||||
args.jobLog(`Refreshing ${refreshType.appName}...`);
|
||||
|
||||
let id = await getId(args, arrHost, headers, fileNames.originalFileName, refreshType);
|
||||
// Useful in some edge cases
|
||||
if (id === -1 && fileNames.currentFileName !== fileNames.originalFileName) {
|
||||
id = await getId(args, arrHost, headers, fileNames.currentFileName, refreshType);
|
||||
}
|
||||
|
||||
// Checking that the file has been found
|
||||
if (id !== -1) {
|
||||
// Using command endpoint to queue a refresh task
|
||||
await args.deps.axios({
|
||||
method: 'post',
|
||||
url: `${arrHost}/api/v3/command`,
|
||||
headers,
|
||||
data: refreshType.delegates.buildRefreshResquestData(id),
|
||||
});
|
||||
|
||||
refreshed = true;
|
||||
args.jobLog(`✔ Refreshed ${refreshType.content} ${id} in ${refreshType.appName}.`);
|
||||
}
|
||||
|
||||
return {
|
||||
outputFileObj: args.inputFileObj,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue