feat(api): plex tv sync and recently added sync

This commit is contained in:
sct
2020-11-11 09:02:28 +00:00
parent 16221a46a7
commit 1390cc1f13
19 changed files with 554 additions and 76 deletions

View File

@@ -3,9 +3,11 @@ import { getSettings } from '../lib/settings';
export interface PlexLibraryItem {
ratingKey: string;
parentRatingKey?: string;
title: string;
guid: string;
type: 'movie' | 'show';
parentGuid?: string;
type: 'movie' | 'show' | 'season';
}
interface PlexLibraryResponse {
@@ -28,12 +30,21 @@ interface PlexLibrariesResponse {
export interface PlexMetadata {
ratingKey: string;
parentRatingKey?: string;
guid: string;
type: 'movie' | 'show';
type: 'movie' | 'show' | 'season';
title: string;
Guid: {
id: string;
}[];
Children?: {
size: 12;
Metadata: PlexMetadata[];
};
index: number;
parentIndex?: number;
leafCount: number;
viewedLeafCount: number;
}
interface PlexMetadataResponse {
@@ -63,6 +74,9 @@ class PlexAPI {
cb(undefined, plexToken);
},
},
// requestOptions: {
// includeChildren: 1,
// },
options: {
identifier: settings.clientId,
product: 'Overseerr',
@@ -92,18 +106,25 @@ class PlexAPI {
return response.MediaContainer.Metadata;
}
public async getMetadata(key: string): Promise<PlexMetadata> {
public async getMetadata(
key: string,
options: { includeChildren?: boolean } = {}
): Promise<PlexMetadata> {
const response = await this.plexClient.query<PlexMetadataResponse>(
`/library/metadata/${key}`
`/library/metadata/${key}${
options.includeChildren ? '?includeChildren=1' : ''
}`
);
return response.MediaContainer.Metadata[0];
}
public async getRecentlyAdded() {
const response = await this.plexClient.query('/library/recentlyAdded');
public async getRecentlyAdded(): Promise<PlexLibraryItem[]> {
const response = await this.plexClient.query<PlexLibraryResponse>(
'/library/recentlyAdded'
);
return response;
return response.MediaContainer.Metadata;
}
}

View File

@@ -649,6 +649,38 @@ class TheMovieDb {
);
}
}
public async getShowByTvdbId({
tvdbId,
language = 'en-US',
}: {
tvdbId: number;
language?: string;
}): Promise<TmdbTvDetails> {
try {
const extResponse = await this.getByExternalId({
externalId: tvdbId,
type: 'tvdb',
});
if (extResponse.tv_results[0]) {
const tvshow = await this.getTvShow({
tvId: extResponse.tv_results[0].id,
language,
});
return tvshow;
}
throw new Error(
`[TMDB] Failed to find a tv show with the provided TVDB id: ${tvdbId}`
);
} catch (e) {
throw new Error(
`[TMDB] Failed to get tv show by external tvdb ID: ${e.message}`
);
}
}
}
export default TheMovieDb;