From edbbccf3ae623430294f1a5c3fd2728dbd42e555 Mon Sep 17 00:00:00 2001 From: sct Date: Mon, 14 Dec 2020 08:01:33 +0000 Subject: [PATCH] fix(plex sync): catch errors that occur during processMovie this also removes the unique constraint on imdbId re #244 #246 #250 --- package.json | 1 + server/job/plexsync/index.ts | 103 ++++++++++-------- .../1607928251245-DropImdbIdConstraint.ts | 20 ++++ 3 files changed, 81 insertions(+), 43 deletions(-) create mode 100644 server/migration/1607928251245-DropImdbIdConstraint.ts diff --git a/package.json b/package.json index b053dcefc..f8926e9cf 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,7 @@ "start": "NODE_ENV=production node dist/index.js", "i18n:extract": "extract-messages -l=en -o src/i18n/locale -d en --flat true --overwriteDefault false './src/**/!(*.test).{ts,tsx}'", "migration:generate": "ts-node --project server/tsconfig.json ./node_modules/.bin/typeorm migration:generate", + "migration:create": "ts-node --project server/tsconfig.json ./node_modules/.bin/typeorm migration:create", "migration:run": "ts-node --project server/tsconfig.json ./node_modules/.bin/typeorm migration:run", "format": "prettier --write ." }, diff --git a/server/job/plexsync/index.ts b/server/job/plexsync/index.ts index c2c95face..cf1b3823b 100644 --- a/server/job/plexsync/index.ts +++ b/server/job/plexsync/index.ts @@ -51,64 +51,81 @@ class JobPlexSync { private async processMovie(plexitem: PlexLibraryItem) { const mediaRepository = getRepository(Media); - if (plexitem.guid.match(plexRegex)) { - const metadata = await this.plexClient.getMetadata(plexitem.ratingKey); - const newMedia = new Media(); - - metadata.Guid.forEach((ref) => { - if (ref.id.match(imdbRegex)) { - newMedia.imdbId = ref.id.match(imdbRegex)?.[1] ?? undefined; - } else if (ref.id.match(tmdbRegex)) { - const tmdbMatch = ref.id.match(tmdbRegex)?.[1]; - newMedia.tmdbId = Number(tmdbMatch); + try { + if (plexitem.guid.match(plexRegex)) { + const metadata = await this.plexClient.getMetadata(plexitem.ratingKey); + const newMedia = new Media(); + + if (!metadata.Guid) { + logger.debug('No Guid metadata for this title. Skipping', { + label: 'Plex Sync', + ratingKey: plexitem.ratingKey, + }); + return; } - }); - - const existing = await this.getExisting(newMedia.tmdbId); - - if (existing && existing.status === MediaStatus.AVAILABLE) { - this.log(`Title exists and is already available ${metadata.title}`); - } else if (existing && existing.status !== MediaStatus.AVAILABLE) { - existing.status = MediaStatus.AVAILABLE; - mediaRepository.save(existing); - this.log( - `Request for ${metadata.title} exists. Setting status AVAILABLE`, - 'info' - ); - } else { - newMedia.status = MediaStatus.AVAILABLE; - newMedia.mediaType = MediaType.MOVIE; - await mediaRepository.save(newMedia); - this.log(`Saved ${plexitem.title}`); - } - } else { - const matchedid = plexitem.guid.match(/imdb:\/\/(tt[0-9]+)/); - if (matchedid?.[1]) { - const tmdbMovie = await this.tmdb.getMovieByImdbId({ - imdbId: matchedid[1], + metadata.Guid.forEach((ref) => { + if (ref.id.match(imdbRegex)) { + newMedia.imdbId = ref.id.match(imdbRegex)?.[1] ?? undefined; + } else if (ref.id.match(tmdbRegex)) { + const tmdbMatch = ref.id.match(tmdbRegex)?.[1]; + newMedia.tmdbId = Number(tmdbMatch); + } }); - const existing = await this.getExisting(tmdbMovie.id); + const existing = await this.getExisting(newMedia.tmdbId); + if (existing && existing.status === MediaStatus.AVAILABLE) { - this.log(`Title exists and is already available ${plexitem.title}`); + this.log(`Title exists and is already available ${metadata.title}`); } else if (existing && existing.status !== MediaStatus.AVAILABLE) { existing.status = MediaStatus.AVAILABLE; - await mediaRepository.save(existing); + mediaRepository.save(existing); this.log( - `Request for ${plexitem.title} exists. Setting status AVAILABLE`, + `Request for ${metadata.title} exists. Setting status AVAILABLE`, 'info' ); - } else if (tmdbMovie) { - const newMedia = new Media(); - newMedia.imdbId = tmdbMovie.external_ids.imdb_id; - newMedia.tmdbId = tmdbMovie.id; + } else { newMedia.status = MediaStatus.AVAILABLE; newMedia.mediaType = MediaType.MOVIE; await mediaRepository.save(newMedia); - this.log(`Saved ${tmdbMovie.title}`); + this.log(`Saved ${plexitem.title}`); + } + } else { + const matchedid = plexitem.guid.match(/imdb:\/\/(tt[0-9]+)/); + + if (matchedid?.[1]) { + const tmdbMovie = await this.tmdb.getMovieByImdbId({ + imdbId: matchedid[1], + }); + + const existing = await this.getExisting(tmdbMovie.id); + if (existing && existing.status === MediaStatus.AVAILABLE) { + this.log(`Title exists and is already available ${plexitem.title}`); + } else if (existing && existing.status !== MediaStatus.AVAILABLE) { + existing.status = MediaStatus.AVAILABLE; + await mediaRepository.save(existing); + this.log( + `Request for ${plexitem.title} exists. Setting status AVAILABLE`, + 'info' + ); + } else if (tmdbMovie) { + const newMedia = new Media(); + newMedia.imdbId = tmdbMovie.external_ids.imdb_id; + newMedia.tmdbId = tmdbMovie.id; + newMedia.status = MediaStatus.AVAILABLE; + newMedia.mediaType = MediaType.MOVIE; + await mediaRepository.save(newMedia); + this.log(`Saved ${tmdbMovie.title}`); + } } } + } catch (e) { + this.log( + `Failed to process plex item. ratingKey: ${ + plexitem.parentRatingKey ?? plexitem.ratingKey + }`, + 'error' + ); } } diff --git a/server/migration/1607928251245-DropImdbIdConstraint.ts b/server/migration/1607928251245-DropImdbIdConstraint.ts new file mode 100644 index 000000000..97baa861a --- /dev/null +++ b/server/migration/1607928251245-DropImdbIdConstraint.ts @@ -0,0 +1,20 @@ +import { MigrationInterface, QueryRunner, TableUnique } from 'typeorm'; + +export class DropImdbIdConstraint1607928251245 implements MigrationInterface { + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.dropUniqueConstraint( + 'media', + 'UQ_7ff2d11f6a83cb52386eaebe74b' + ); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.createUniqueConstraint( + 'media', + new TableUnique({ + name: 'UQ_7ff2d11f6a83cb52386eaebe74b', + columnNames: ['imdbId'], + }) + ); + } +}