Skip to content

Commit

Permalink
fix: handle failure in syncLibraries() instead
Browse files Browse the repository at this point in the history
  • Loading branch information
TheCatLady committed Dec 4, 2021
1 parent 7185dbe commit 10d63ce
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 36 deletions.
53 changes: 33 additions & 20 deletions server/api/plexapi.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import NodePlexAPI from 'plex-api';
import { getSettings, Library, PlexSettings } from '../lib/settings';
import logger from '../logger';

export interface PlexLibraryItem {
ratingKey: string;
Expand Down Expand Up @@ -145,28 +146,40 @@ class PlexAPI {
public async syncLibraries(): Promise<void> {
const settings = getSettings();

const libraries = await this.getLibraries();

const newLibraries: Library[] = libraries
// Remove libraries that are not movie or show
.filter((library) => library.type === 'movie' || library.type === 'show')
// Remove libraries that do not have a metadata agent set (usually personal video libraries)
.filter((library) => library.agent !== 'com.plexapp.agents.none')
.map((library) => {
const existing = settings.plex.libraries.find(
(l) => l.id === library.key && l.name === library.title
);

return {
id: library.key,
name: library.title,
enabled: existing?.enabled ?? false,
type: library.type,
lastScan: existing?.lastScan,
};
try {
const libraries = await this.getLibraries();

const newLibraries: Library[] = libraries
// Remove libraries that are not movie or show
.filter(
(library) => library.type === 'movie' || library.type === 'show'
)
// Remove libraries that do not have a metadata agent set (usually personal video libraries)
.filter((library) => library.agent !== 'com.plexapp.agents.none')
.map((library) => {
const existing = settings.plex.libraries.find(
(l) => l.id === library.key && l.name === library.title
);

return {
id: library.key,
name: library.title,
enabled: existing?.enabled ?? false,
type: library.type,
lastScan: existing?.lastScan,
};
});

settings.plex.libraries = newLibraries;
} catch (e) {
logger.error('Failed to fetch Plex libraries', {
label: 'Plex API',
message: e.message,
});

settings.plex.libraries = newLibraries;
settings.plex.libraries = [];
}

settings.save();
}

Expand Down
18 changes: 2 additions & 16 deletions server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,22 +67,8 @@ app
label: 'Settings',
});

try {
const plexapi = new PlexAPI({ plexToken: admin.plexToken });
await plexapi.syncLibraries();

logger.info('Plex library migration completed successfully', {
label: 'Settings',
});
} catch (e) {
logger.error('Failed to fetch Plex libraries', {
label: 'Settings',
message: e.message,
});

settings.plex.libraries = [];
settings.save();
}
const plexapi = new PlexAPI({ plexToken: admin.plexToken });
await plexapi.syncLibraries();
}
}

Expand Down

0 comments on commit 10d63ce

Please sign in to comment.