Skip to content

Commit

Permalink
fix(api): only run recently added sync on enabled libraries
Browse files Browse the repository at this point in the history
fixes #259
  • Loading branch information
sct committed Dec 15, 2020
1 parent d96d65b commit e08fa35
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 19 deletions.
1 change: 0 additions & 1 deletion overseerr-api.yml
Original file line number Diff line number Diff line change
Expand Up @@ -643,7 +643,6 @@ components:
readOnly: true
requestedBy:
$ref: '#/components/schemas/User'
readOnly: true
modifiedBy:
anyOf:
- $ref: '#/components/schemas/User'
Expand Down
9 changes: 6 additions & 3 deletions server/api/plexapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import { getSettings } from '../lib/settings';
export interface PlexLibraryItem {
ratingKey: string;
parentRatingKey?: string;
grandparentRatingKey?: string;
title: string;
guid: string;
parentGuid?: string;
type: 'movie' | 'show' | 'season';
grandparentGuid?: string;
type: 'movie' | 'show' | 'season' | 'episode';
}

interface PlexLibraryResponse {
Expand All @@ -20,6 +22,7 @@ export interface PlexLibrary {
type: 'show' | 'movie';
key: string;
title: string;
agent: string;
}

interface PlexLibrariesResponse {
Expand Down Expand Up @@ -120,9 +123,9 @@ class PlexAPI {
return response.MediaContainer.Metadata[0];
}

public async getRecentlyAdded(): Promise<PlexLibraryItem[]> {
public async getRecentlyAdded(id: string): Promise<PlexLibraryItem[]> {
const response = await this.plexClient.query<PlexLibraryResponse>(
'/library/recentlyAdded'
`/library/sections/${id}/recentlyAdded`
);

return response.MediaContainer.Metadata;
Expand Down
40 changes: 25 additions & 15 deletions server/job/plexsync/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,9 @@ class JobPlexSync {

try {
const metadata = await this.plexClient.getMetadata(
plexitem.parentRatingKey ?? plexitem.ratingKey,
plexitem.grandparentRatingKey ??
plexitem.parentRatingKey ??
plexitem.ratingKey,
{ includeChildren: true }
);
if (metadata.guid.match(tvdbRegex)) {
Expand Down Expand Up @@ -240,7 +242,9 @@ class JobPlexSync {
} catch (e) {
this.log(
`Failed to process plex item. ratingKey: ${
plexitem.parentRatingKey ?? plexitem.ratingKey
plexitem.grandparentRatingKey ??
plexitem.parentRatingKey ??
plexitem.ratingKey
}`,
'error'
);
Expand All @@ -252,7 +256,11 @@ class JobPlexSync {
slicedItems.map(async (plexitem) => {
if (plexitem.type === 'movie') {
await this.processMovie(plexitem);
} else if (plexitem.type === 'show') {
} else if (
plexitem.type === 'show' ||
plexitem.type === 'episode' ||
plexitem.type === 'season'
) {
await this.processShow(plexitem);
}
})
Expand Down Expand Up @@ -301,20 +309,22 @@ class JobPlexSync {
});

this.plexClient = new PlexAPI({ plexToken: admin.plexToken });

this.libraries = settings.plex.libraries.filter(
(library) => library.enabled
);

if (this.isRecentOnly) {
this.currentLibrary = {
id: '0',
name: 'Recently Added',
enabled: true,
};
this.log(`Beginning to process recently added`, 'info');
this.items = await this.plexClient.getRecentlyAdded();
await this.loop();
for (const library of this.libraries) {
this.currentLibrary = library;
this.log(
`Beginning to process recently added for library: ${library.name}`,
'info'
);
this.items = await this.plexClient.getRecentlyAdded(library.id);
await this.loop();
}
} else {
this.libraries = settings.plex.libraries.filter(
(library) => library.enabled
);

for (const library of this.libraries) {
this.currentLibrary = library;
this.log(`Beginning to process library: ${library.name}`, 'info');
Expand Down

0 comments on commit e08fa35

Please sign in to comment.