mirror of
https://github.com/aleksilassila/reiverr.git
synced 2026-04-26 02:35:20 +02:00
feat: Request utils
This commit is contained in:
@@ -190,7 +190,7 @@ export class JellyfinApi implements Api<paths> {
|
||||
}
|
||||
|
||||
export const jellyfinApi = new JellyfinApi();
|
||||
export const getJellyfinApiClient = jellyfinApi.getClient;
|
||||
export const jellyfinApiClient = jellyfinApi.getClient;
|
||||
|
||||
/*
|
||||
function getJellyfinApi() {
|
||||
|
||||
264
src/lib/apis/radarr/radarr-api.ts
Normal file
264
src/lib/apis/radarr/radarr-api.ts
Normal file
@@ -0,0 +1,264 @@
|
||||
import axios from 'axios';
|
||||
import createClient from 'openapi-fetch';
|
||||
import { get } from 'svelte/store';
|
||||
import { settings } from '../../stores/settings.store';
|
||||
import type { components, paths } from './radarr.generated';
|
||||
import { getTmdbMovie } from '../tmdb/tmdb-api';
|
||||
import { log } from '../../utils';
|
||||
import { appState } from '../../stores/app-state.store';
|
||||
import type { Api } from '../api.interface';
|
||||
|
||||
export type RadarrMovie = components['schemas']['MovieResource'];
|
||||
export type MovieFileResource = components['schemas']['MovieFileResource'];
|
||||
export type ReleaseResource = components['schemas']['ReleaseResource'];
|
||||
export type RadarrDownload = components['schemas']['QueueResource'] & { movie: RadarrMovie };
|
||||
export type DiskSpaceInfo = components['schemas']['DiskSpaceResource'];
|
||||
|
||||
export interface RadarrMovieOptions {
|
||||
title: string;
|
||||
qualityProfileId: number;
|
||||
minimumAvailability: 'announced' | 'inCinemas' | 'released';
|
||||
tags: number[];
|
||||
profileId: number;
|
||||
year: number;
|
||||
rootFolderPath: string;
|
||||
tmdbId: number;
|
||||
monitored?: boolean;
|
||||
searchNow?: boolean;
|
||||
}
|
||||
|
||||
export class RadarrApi implements Api<paths> {
|
||||
getClient() {
|
||||
const radarrSettings = get(appState).user?.settings.radarr;
|
||||
const baseUrl = radarrSettings?.baseUrl;
|
||||
const apiKey = radarrSettings?.apiKey;
|
||||
|
||||
return createClient<paths>({
|
||||
baseUrl,
|
||||
headers: {
|
||||
'X-Api-Key': apiKey
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
getBaseUrl() {
|
||||
return get(appState)?.user?.settings?.radarr.baseUrl || '';
|
||||
}
|
||||
|
||||
getSettings() {
|
||||
return get(appState).user?.settings.radarr;
|
||||
}
|
||||
|
||||
getMovieByTmdbId = (tmdbId: number): Promise<RadarrMovie | undefined> =>
|
||||
this.getClient()
|
||||
?.GET('/api/v3/movie', {
|
||||
params: {
|
||||
query: {
|
||||
tmdbId: Number(tmdbId)
|
||||
}
|
||||
}
|
||||
})
|
||||
.then((r) => r.data?.find((m) => m.tmdbId == tmdbId)) || Promise.resolve(undefined);
|
||||
|
||||
getRadarrMovies = (): Promise<RadarrMovie[]> =>
|
||||
this.getClient()
|
||||
?.GET('/api/v3/movie', {
|
||||
params: {}
|
||||
})
|
||||
.then((r) => r.data || []) || Promise.resolve([]);
|
||||
|
||||
addMovieToRadarr = async (tmdbId: number) => {
|
||||
const tmdbMovie = await getTmdbMovie(tmdbId);
|
||||
const radarrMovie = await this.lookupRadarrMovieByTmdbId(tmdbId);
|
||||
|
||||
if (radarrMovie?.id) throw new Error('Movie already exists');
|
||||
|
||||
if (!tmdbMovie) throw new Error('Movie not found');
|
||||
|
||||
const options: RadarrMovieOptions = {
|
||||
qualityProfileId: get(appState).user?.settings.radarr.qualityProfileId || 0,
|
||||
profileId: get(appState).user?.settings.radarr?.qualityProfileId || 0,
|
||||
rootFolderPath: get(appState).user?.settings.radarr.rootFolderPath || '',
|
||||
minimumAvailability: 'announced',
|
||||
title: tmdbMovie.title || tmdbMovie.original_title || '',
|
||||
tmdbId: tmdbMovie.id || 0,
|
||||
year: Number(tmdbMovie.release_date?.slice(0, 4)),
|
||||
monitored: false,
|
||||
tags: [],
|
||||
searchNow: false
|
||||
};
|
||||
|
||||
return (
|
||||
this.getClient()
|
||||
?.POST('/api/v3/movie', {
|
||||
params: {},
|
||||
body: options
|
||||
})
|
||||
.then((r) => r.data) || Promise.resolve(undefined)
|
||||
);
|
||||
};
|
||||
|
||||
cancelDownloadRadarrMovie = async (downloadId: number) => {
|
||||
const deleteResponse = await this.getClient()
|
||||
?.DELETE('/api/v3/queue/{id}', {
|
||||
params: {
|
||||
path: {
|
||||
id: downloadId
|
||||
},
|
||||
query: {
|
||||
blocklist: false,
|
||||
removeFromClient: true
|
||||
}
|
||||
}
|
||||
})
|
||||
.then((r) => log(r));
|
||||
|
||||
return !!deleteResponse?.response.ok;
|
||||
};
|
||||
|
||||
fetchRadarrReleases = (movieId: number) =>
|
||||
this.getClient()
|
||||
?.GET('/api/v3/release', { params: { query: { movieId: movieId } } })
|
||||
.then((r) => r.data || []) || Promise.resolve([]);
|
||||
|
||||
downloadRadarrMovie = (guid: string, indexerId: number) =>
|
||||
this.getClient()
|
||||
?.POST('/api/v3/release', {
|
||||
params: {},
|
||||
body: {
|
||||
indexerId,
|
||||
guid
|
||||
}
|
||||
})
|
||||
.then((res) => res.response.ok) || Promise.resolve(false);
|
||||
|
||||
deleteRadarrMovieFile = (id: number) =>
|
||||
this.getClient()
|
||||
?.DELETE('/api/v3/moviefile/{id}', {
|
||||
params: {
|
||||
path: {
|
||||
id
|
||||
}
|
||||
}
|
||||
})
|
||||
.then((res) => res.response.ok) || Promise.resolve(false);
|
||||
|
||||
getRadarrDownloads = (): Promise<RadarrDownload[]> =>
|
||||
this.getClient()
|
||||
?.GET('/api/v3/queue', {
|
||||
params: {
|
||||
query: {
|
||||
includeMovie: true
|
||||
}
|
||||
}
|
||||
})
|
||||
.then((r) => (r.data?.records?.filter((record) => record.movie) as RadarrDownload[]) || []) ||
|
||||
Promise.resolve([]);
|
||||
|
||||
getRadarrDownloadsById = (radarrId: number) =>
|
||||
this.getRadarrDownloads().then((downloads) => downloads.filter((d) => d.movie.id === radarrId));
|
||||
|
||||
getRadarrDownloadsByTmdbId = (tmdbId: number) =>
|
||||
this.getRadarrDownloads().then((downloads) =>
|
||||
downloads.filter((d) => d.movie.tmdbId === tmdbId)
|
||||
);
|
||||
|
||||
private lookupRadarrMovieByTmdbId = (tmdbId: number) =>
|
||||
this.getClient()
|
||||
?.GET('/api/v3/movie/lookup/tmdb', {
|
||||
params: {
|
||||
query: {
|
||||
tmdbId
|
||||
}
|
||||
}
|
||||
})
|
||||
.then((r) => r.data as unknown as RadarrMovie) || Promise.resolve(undefined);
|
||||
|
||||
getDiskSpace = (): Promise<DiskSpaceInfo[]> =>
|
||||
this.getClient()
|
||||
?.GET('/api/v3/diskspace', {})
|
||||
.then((d) => d.data || []) || Promise.resolve([]);
|
||||
|
||||
removeFromRadarr = (id: number) =>
|
||||
this.getClient()
|
||||
?.DELETE('/api/v3/movie/{id}', {
|
||||
params: {
|
||||
path: {
|
||||
id
|
||||
}
|
||||
}
|
||||
})
|
||||
.then((res) => res.response.ok) || Promise.resolve(false);
|
||||
|
||||
getRadarrHealth = async (
|
||||
baseUrl: string | undefined = undefined,
|
||||
apiKey: string | undefined = undefined
|
||||
) =>
|
||||
axios
|
||||
.get((baseUrl || this.getBaseUrl()) + '/api/v3/health', {
|
||||
headers: {
|
||||
'X-Api-Key': apiKey || this.getSettings()?.apiKey
|
||||
}
|
||||
})
|
||||
.then((res) => res.status === 200)
|
||||
.catch(() => false);
|
||||
|
||||
getRootFolders = async (
|
||||
baseUrl: string | undefined = undefined,
|
||||
apiKey: string | undefined = undefined
|
||||
) =>
|
||||
axios
|
||||
.get<components['schemas']['RootFolderResource'][]>(
|
||||
(baseUrl || this.getBaseUrl()) + '/api/v3/rootFolder',
|
||||
{
|
||||
headers: {
|
||||
'X-Api-Key': apiKey || this.getSettings()?.apiKey
|
||||
}
|
||||
}
|
||||
)
|
||||
.then((res) => res.data || []);
|
||||
|
||||
getQualityProfiles = async (
|
||||
baseUrl: string | undefined = undefined,
|
||||
apiKey: string | undefined = undefined
|
||||
) =>
|
||||
axios
|
||||
.get<components['schemas']['QualityProfileResource'][]>(
|
||||
(baseUrl || get(appState)?.user?.settings.radarr.baseUrl) + '/api/v3/qualityprofile',
|
||||
{
|
||||
headers: {
|
||||
'X-Api-Key': apiKey || get(appState)?.user?.settings.radarr.apiKey
|
||||
}
|
||||
}
|
||||
)
|
||||
.then((res) => res.data || []);
|
||||
|
||||
getRadarrPosterUrl(item: RadarrMovie, original = false) {
|
||||
const url =
|
||||
get(settings).radarr.baseUrl +
|
||||
(item.images?.find((i) => i.coverType === 'poster')?.url || '');
|
||||
|
||||
if (!original) return url.replace('poster.jpg', `poster-${500}.jpg`);
|
||||
|
||||
return url;
|
||||
}
|
||||
}
|
||||
|
||||
export const radarrApi = new RadarrApi();
|
||||
export const radarrApiClient = radarrApi.getClient;
|
||||
|
||||
// function getRadarrApi() {
|
||||
// const baseUrl = get(settings)?.radarr.baseUrl;
|
||||
// const apiKey = get(settings)?.radarr.apiKey;
|
||||
// const rootFolder = get(settings)?.radarr.rootFolderPath;
|
||||
// const qualityProfileId = get(settings)?.radarr.qualityProfileId;
|
||||
//
|
||||
// if (!baseUrl || !apiKey || !rootFolder || !qualityProfileId) return undefined;
|
||||
//
|
||||
// return createClient<paths>({
|
||||
// baseUrl,
|
||||
// headers: {
|
||||
// 'X-Api-Key': apiKey
|
||||
// }
|
||||
// });
|
||||
// }
|
||||
@@ -1,233 +0,0 @@
|
||||
import axios from 'axios';
|
||||
import createClient from 'openapi-fetch';
|
||||
import { get } from 'svelte/store';
|
||||
import { settings } from '../../stores/settings.store';
|
||||
import type { components, paths } from './radarr.generated';
|
||||
import { getTmdbMovie } from '../tmdb/tmdb-api';
|
||||
import { log } from '../../utils';
|
||||
|
||||
export type RadarrMovie = components['schemas']['MovieResource'];
|
||||
export type MovieFileResource = components['schemas']['MovieFileResource'];
|
||||
export type ReleaseResource = components['schemas']['ReleaseResource'];
|
||||
export type RadarrDownload = components['schemas']['QueueResource'] & { movie: RadarrMovie };
|
||||
export type DiskSpaceInfo = components['schemas']['DiskSpaceResource'];
|
||||
|
||||
export interface RadarrMovieOptions {
|
||||
title: string;
|
||||
qualityProfileId: number;
|
||||
minimumAvailability: 'announced' | 'inCinemas' | 'released';
|
||||
tags: number[];
|
||||
profileId: number;
|
||||
year: number;
|
||||
rootFolderPath: string;
|
||||
tmdbId: number;
|
||||
monitored?: boolean;
|
||||
searchNow?: boolean;
|
||||
}
|
||||
|
||||
function getRadarrApi() {
|
||||
const baseUrl = get(settings)?.radarr.baseUrl;
|
||||
const apiKey = get(settings)?.radarr.apiKey;
|
||||
const rootFolder = get(settings)?.radarr.rootFolderPath;
|
||||
const qualityProfileId = get(settings)?.radarr.qualityProfileId;
|
||||
|
||||
if (!baseUrl || !apiKey || !rootFolder || !qualityProfileId) return undefined;
|
||||
|
||||
return createClient<paths>({
|
||||
baseUrl,
|
||||
headers: {
|
||||
'X-Api-Key': apiKey
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
export const getRadarrMovies = (): Promise<RadarrMovie[]> =>
|
||||
getRadarrApi()
|
||||
?.GET('/api/v3/movie', {
|
||||
params: {}
|
||||
})
|
||||
.then((r) => r.data || []) || Promise.resolve([]);
|
||||
|
||||
export const getRadarrMovieByTmdbId = (tmdbId: string): Promise<RadarrMovie | undefined> =>
|
||||
getRadarrApi()
|
||||
?.GET('/api/v3/movie', {
|
||||
params: {
|
||||
query: {
|
||||
tmdbId: Number(tmdbId)
|
||||
}
|
||||
}
|
||||
})
|
||||
.then((r) => r.data?.find((m) => (m.tmdbId as any) == tmdbId)) || Promise.resolve(undefined);
|
||||
|
||||
export const addMovieToRadarr = async (tmdbId: number) => {
|
||||
const tmdbMovie = await getTmdbMovie(tmdbId);
|
||||
const radarrMovie = await lookupRadarrMovieByTmdbId(tmdbId);
|
||||
|
||||
if (radarrMovie?.id) throw new Error('Movie already exists');
|
||||
|
||||
if (!tmdbMovie) throw new Error('Movie not found');
|
||||
|
||||
const options: RadarrMovieOptions = {
|
||||
qualityProfileId: get(settings)?.radarr.qualityProfileId || 0,
|
||||
profileId: get(settings)?.radarr.profileId || 0,
|
||||
rootFolderPath: get(settings)?.radarr.rootFolderPath || '',
|
||||
minimumAvailability: 'announced',
|
||||
title: tmdbMovie.title || tmdbMovie.original_title || '',
|
||||
tmdbId: tmdbMovie.id || 0,
|
||||
year: Number(tmdbMovie.release_date?.slice(0, 4)),
|
||||
monitored: false,
|
||||
tags: [],
|
||||
searchNow: false
|
||||
};
|
||||
|
||||
return (
|
||||
getRadarrApi()
|
||||
?.POST('/api/v3/movie', {
|
||||
params: {},
|
||||
body: options
|
||||
})
|
||||
.then((r) => r.data) || Promise.resolve(undefined)
|
||||
);
|
||||
};
|
||||
|
||||
export const cancelDownloadRadarrMovie = async (downloadId: number) => {
|
||||
const deleteResponse = await getRadarrApi()
|
||||
?.DELETE('/api/v3/queue/{id}', {
|
||||
params: {
|
||||
path: {
|
||||
id: downloadId
|
||||
},
|
||||
query: {
|
||||
blocklist: false,
|
||||
removeFromClient: true
|
||||
}
|
||||
}
|
||||
})
|
||||
.then((r) => log(r));
|
||||
|
||||
return !!deleteResponse?.response.ok;
|
||||
};
|
||||
|
||||
export const fetchRadarrReleases = (movieId: number) =>
|
||||
getRadarrApi()
|
||||
?.GET('/api/v3/release', { params: { query: { movieId: movieId } } })
|
||||
.then((r) => r.data || []) || Promise.resolve([]);
|
||||
|
||||
export const downloadRadarrMovie = (guid: string, indexerId: number) =>
|
||||
getRadarrApi()
|
||||
?.POST('/api/v3/release', {
|
||||
params: {},
|
||||
body: {
|
||||
indexerId,
|
||||
guid
|
||||
}
|
||||
})
|
||||
.then((res) => res.response.ok) || Promise.resolve(false);
|
||||
|
||||
export const deleteRadarrMovie = (id: number) =>
|
||||
getRadarrApi()
|
||||
?.DELETE('/api/v3/moviefile/{id}', {
|
||||
params: {
|
||||
path: {
|
||||
id
|
||||
}
|
||||
}
|
||||
})
|
||||
.then((res) => res.response.ok) || Promise.resolve(false);
|
||||
|
||||
export const getRadarrDownloads = (): Promise<RadarrDownload[]> =>
|
||||
getRadarrApi()
|
||||
?.GET('/api/v3/queue', {
|
||||
params: {
|
||||
query: {
|
||||
includeMovie: true
|
||||
}
|
||||
}
|
||||
})
|
||||
.then((r) => (r.data?.records?.filter((record) => record.movie) as RadarrDownload[]) || []) ||
|
||||
Promise.resolve([]);
|
||||
|
||||
export const getRadarrDownloadsById = (radarrId: number) =>
|
||||
getRadarrDownloads().then((downloads) => downloads.filter((d) => d.movie.id === radarrId));
|
||||
|
||||
export const getRadarrDownloadsByTmdbId = (tmdbId: number) =>
|
||||
getRadarrDownloads().then((downloads) => downloads.filter((d) => d.movie.tmdbId === tmdbId));
|
||||
|
||||
const lookupRadarrMovieByTmdbId = (tmdbId: number) =>
|
||||
getRadarrApi()
|
||||
?.GET('/api/v3/movie/lookup/tmdb', {
|
||||
params: {
|
||||
query: {
|
||||
tmdbId
|
||||
}
|
||||
}
|
||||
})
|
||||
.then((r) => r.data as any as RadarrMovie) || Promise.resolve(undefined);
|
||||
|
||||
export const getDiskSpace = (): Promise<DiskSpaceInfo[]> =>
|
||||
getRadarrApi()
|
||||
?.GET('/api/v3/diskspace', {})
|
||||
.then((d) => d.data || []) || Promise.resolve([]);
|
||||
|
||||
export const removeFromRadarr = (id: number) =>
|
||||
getRadarrApi()
|
||||
?.DELETE('/api/v3/movie/{id}', {
|
||||
params: {
|
||||
path: {
|
||||
id
|
||||
}
|
||||
}
|
||||
})
|
||||
.then((res) => res.response.ok) || Promise.resolve(false);
|
||||
|
||||
export const getRadarrHealth = async (
|
||||
baseUrl: string | undefined = undefined,
|
||||
apiKey: string | undefined = undefined
|
||||
) =>
|
||||
axios
|
||||
.get((baseUrl || get(settings)?.radarr.baseUrl) + '/api/v3/health', {
|
||||
headers: {
|
||||
'X-Api-Key': apiKey || get(settings)?.radarr.apiKey
|
||||
}
|
||||
})
|
||||
.then((res) => res.status === 200)
|
||||
.catch(() => false);
|
||||
|
||||
export const getRadarrRootFolders = async (
|
||||
baseUrl: string | undefined = undefined,
|
||||
apiKey: string | undefined = undefined
|
||||
) =>
|
||||
axios
|
||||
.get<components['schemas']['RootFolderResource'][]>(
|
||||
(baseUrl || get(settings)?.sonarr.baseUrl) + '/api/v3/rootFolder',
|
||||
{
|
||||
headers: {
|
||||
'X-Api-Key': apiKey || get(settings)?.sonarr.apiKey
|
||||
}
|
||||
}
|
||||
)
|
||||
.then((res) => res.data || []);
|
||||
|
||||
export const getRadarrQualityProfiles = async (
|
||||
baseUrl: string | undefined = undefined,
|
||||
apiKey: string | undefined = undefined
|
||||
) =>
|
||||
axios
|
||||
.get<components['schemas']['QualityProfileResource'][]>(
|
||||
(baseUrl || get(settings)?.sonarr.baseUrl) + '/api/v3/qualityprofile',
|
||||
{
|
||||
headers: {
|
||||
'X-Api-Key': apiKey || get(settings)?.sonarr.apiKey
|
||||
}
|
||||
}
|
||||
)
|
||||
.then((res) => res.data || []);
|
||||
|
||||
export function getRadarrPosterUrl(item: RadarrMovie, original = false) {
|
||||
const url =
|
||||
get(settings).radarr.baseUrl + (item.images?.find((i) => i.coverType === 'poster')?.url || '');
|
||||
|
||||
if (!original) return url.replace('poster.jpg', `poster-${500}.jpg`);
|
||||
|
||||
return url;
|
||||
}
|
||||
Reference in New Issue
Block a user