You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
71 lines
2.2 KiB
71 lines
2.2 KiB
import LidarrAPI from '@server/api/servarr/lidarr';
|
|
import { getSettings } from '@server/lib/settings';
|
|
import type { mbArtist, mbRelease, mbReleaseGroup } from './interfaces';
|
|
|
|
async function getPosterFromMB(
|
|
element: mbRelease | mbReleaseGroup | mbArtist
|
|
): Promise<string | undefined> {
|
|
if (element.media_type === 'artist') {
|
|
const settings = getSettings();
|
|
const lidarrSettings = settings.lidarr.find((lidarr) => lidarr.isDefault);
|
|
if (!lidarrSettings) {
|
|
throw new Error('No default Lidarr instance found');
|
|
}
|
|
const lidarr: LidarrAPI = new LidarrAPI({
|
|
apiKey: lidarrSettings.apiKey,
|
|
url: LidarrAPI.buildUrl(lidarrSettings, '/api/v1'),
|
|
});
|
|
try {
|
|
const artist = await (lidarr as LidarrAPI).getArtist(element.id);
|
|
if (artist.images.find((i) => i.coverType === 'poster')?.url) {
|
|
return LidarrAPI.buildUrl(
|
|
lidarrSettings,
|
|
artist.images.find((i) => i.coverType === 'poster')?.url
|
|
);
|
|
} else {
|
|
return undefined;
|
|
}
|
|
} catch (e) {
|
|
return undefined;
|
|
}
|
|
}
|
|
return `https://coverartarchive.org/${element.media_type}/${element.id}/front-250.jpg`;
|
|
}
|
|
|
|
async function getFanartFromMB(element: mbArtist): Promise<string | undefined> {
|
|
const settings = getSettings();
|
|
const lidarrSettings = settings.lidarr.find((lidarr) => lidarr.isDefault);
|
|
if (!lidarrSettings) {
|
|
throw new Error('No default Lidarr instance found');
|
|
}
|
|
const lidarr = new LidarrAPI({
|
|
apiKey: lidarrSettings.apiKey,
|
|
url: LidarrAPI.buildUrl(lidarrSettings, '/api/v1'),
|
|
});
|
|
try {
|
|
const artist = await lidarr.getArtist(element.id);
|
|
return (
|
|
artist.images ?? [{ coverType: 'fanart', remoteUrl: undefined }]
|
|
).filter((i) => i.coverType === 'fanart')[0].remoteUrl;
|
|
} catch (e) {
|
|
return undefined;
|
|
}
|
|
}
|
|
|
|
const memoize = <T = unknown>(fn: (...val: T[]) => unknown) => {
|
|
const cache = new Map();
|
|
const cached = function (this: unknown, val: T) {
|
|
return cache.has(val)
|
|
? cache.get(val)
|
|
: cache.set(val, fn.call(this, val) as ReturnType<typeof fn>) &&
|
|
cache.get(val);
|
|
};
|
|
cached.cache = cache;
|
|
return cached;
|
|
};
|
|
|
|
const cachedFanartFromMB = memoize(getFanartFromMB);
|
|
|
|
export default memoize(getPosterFromMB);
|
|
export { cachedFanartFromMB };
|