Skip to content

Commit

Permalink
feat(api): plex tv sync and recently added sync
Browse files Browse the repository at this point in the history
  • Loading branch information
sct committed Nov 11, 2020
1 parent 16221a4 commit 1390cc1
Show file tree
Hide file tree
Showing 19 changed files with 554 additions and 76 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,6 @@ config/logs/*.log

# dist files
dist

# sqlite journal
config/db/db.sqlite3-journal
23 changes: 23 additions & 0 deletions overseerr-api.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1155,6 +1155,29 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/PublicSettings'
/settings/jobs:
get:
summary: Returns list of scheduled jobs
description: Returns list of all scheduled jobs and details about their next execution time
tags:
- settings
responses:
'200':
description: Scheduled jobs returned
content:
application/json:
schema:
type: array
items:
type: object
properties:
name:
type: string
example: A Job Name
nextExecutionTime:
type: string
example: '2020-09-02T05:02:23.000Z'

/auth/me:
get:
summary: Returns the currently logged in user
Expand Down
35 changes: 28 additions & 7 deletions server/api/plexapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -63,6 +74,9 @@ class PlexAPI {
cb(undefined, plexToken);
},
},
// requestOptions: {
// includeChildren: 1,
// },
options: {
identifier: settings.clientId,
product: 'Overseerr',
Expand Down Expand Up @@ -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;
}
}

Expand Down
32 changes: 32 additions & 0 deletions server/api/themoviedb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
7 changes: 7 additions & 0 deletions server/entity/Media.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
import { MediaRequest } from './MediaRequest';
import { MediaStatus, MediaType } from '../constants/media';
import logger from '../logger';
import Season from './Season';

@Entity()
class Media {
Expand Down Expand Up @@ -79,6 +80,12 @@ class Media {
@OneToMany(() => MediaRequest, (request) => request.media, { cascade: true })
public requests: MediaRequest[];

@OneToMany(() => Season, (season) => season.media, {
cascade: true,
eager: true,
})
public seasons: Season[];

@CreateDateColumn()
public createdAt: Date;

Expand Down
37 changes: 37 additions & 0 deletions server/entity/Season.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import {
Entity,
PrimaryGeneratedColumn,
Column,
ManyToOne,
CreateDateColumn,
UpdateDateColumn,
} from 'typeorm';
import { MediaStatus } from '../constants/media';
import Media from './Media';

@Entity()
class Season {
@PrimaryGeneratedColumn()
public id: number;

@Column()
public seasonNumber: number;

@Column({ type: 'int', default: MediaStatus.UNKNOWN })
public status: MediaStatus;

@ManyToOne(() => Media, (media) => media.seasons)
public media: Media;

@CreateDateColumn()
public createdAt: Date;

@UpdateDateColumn()
public updatedAt: Date;

constructor(init?: Partial<Season>) {
Object.assign(this, init);
}
}

export default Season;

0 comments on commit 1390cc1

Please sign in to comment.