diff --git a/.all-contributorsrc b/.all-contributorsrc index 371872759..b5166196f 100644 --- a/.all-contributorsrc +++ b/.all-contributorsrc @@ -872,6 +872,33 @@ "contributions": [ "code" ] + }, + { + "login": "scorp200", + "name": "Anton K. (ai Doge)", + "avatar_url": "https://avatars.githubusercontent.com/u/9427639?v=4", + "profile": "http://aidoge.xyz", + "contributions": [ + "code" + ] + }, + { + "login": "marcofaggian", + "name": "Marco Faggian", + "avatar_url": "https://avatars.githubusercontent.com/u/19221001?v=4", + "profile": "https://marcofaggian.com", + "contributions": [ + "code" + ] + }, + { + "login": "nemchik", + "name": "Eric Nemchik", + "avatar_url": "https://avatars.githubusercontent.com/u/725456?v=4", + "profile": "http://nemchik.com/", + "contributions": [ + "code" + ] } ], "badgeTemplate": "\"All-orange.svg\"/>", diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 77da7ea18..0d117c4ec 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -31,7 +31,7 @@ jobs: build_and_push: name: Build & Publish Docker Images if: github.ref == 'refs/heads/develop' && !contains(github.event.head_commit.message, '[skip ci]') - runs-on: self-hosted + runs-on: ubuntu-20.04 steps: - name: Checkout uses: actions/checkout@v3 @@ -39,13 +39,6 @@ jobs: uses: docker/setup-qemu-action@v2 - name: Set up Docker Buildx uses: docker/setup-buildx-action@v2 - - name: Cache Docker layers - uses: actions/cache@v3 - with: - path: /tmp/.buildx-cache - key: ${{ runner.os }}-buildx-${{ github.sha }} - restore-keys: | - ${{ runner.os }}-buildx- - name: Log in to Docker Hub uses: docker/login-action@v2 with: @@ -68,15 +61,6 @@ jobs: COMMIT_TAG=${{ github.sha }} tags: | fallenbagel/jellyseerr:develop - cache-from: type=local,src=/tmp/.buildx-cache - cache-to: type=local,dest=/tmp/.buildx-cache-new,mode=max - - # Temporary fix - # https://github.com/docker/build-push-action/issues/252 - # https://github.com/moby/buildkit/issues/1896 - name: Move cache - run: | - rm -rf /tmp/.buildx-cache - mv /tmp/.buildx-cache-new /tmp/.buildx-cache discord: name: Send Discord Notification diff --git a/README.md b/README.md index e32b51407..86f20ca02 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,9 @@ Discord Docker pulls GitHub + +All Contributors + **Jellyseerr** is a free and open source software application for managing requests for your media library. It is a a fork of Overseerr built to bring support for Jellyfin & Emby media servers! diff --git a/overseerr-api.yml b/overseerr-api.yml index 1f38f4338..b712d7c80 100644 --- a/overseerr-api.yml +++ b/overseerr-api.yml @@ -5657,6 +5657,63 @@ paths: audienceRating: type: string enum: ['Spilled', 'Upright'] + /movie/{movieId}/ratingscombined: + get: + summary: Get RT and IMDB movie ratings combined + description: Returns ratings from RottenTomatoes and IMDB based on the provided movieId in a JSON object. + tags: + - movies + parameters: + - in: path + name: movieId + required: true + schema: + type: number + example: 337401 + responses: + '200': + description: Ratings returned + content: + application/json: + schema: + type: object + properties: + rt: + type: object + properties: + title: + type: string + example: Mulan + year: + type: number + example: 2020 + url: + type: string + example: 'http://www.rottentomatoes.com/m/mulan_2020/' + criticsScore: + type: number + example: 85 + criticsRating: + type: string + enum: ['Rotten', 'Fresh', 'Certified Fresh'] + audienceScore: + type: number + example: 65 + audienceRating: + type: string + enum: ['Spilled', 'Upright'] + imdb: + type: object + properties: + title: + type: string + example: I am Legend + url: + type: string + example: 'https://www.imdb.com/title/tt0480249' + criticsScore: + type: number + example: 6.5 /tv/{tvId}: get: summary: Get TV details diff --git a/server/api/plextv.ts b/server/api/plextv.ts index 76ee66188..704926895 100644 --- a/server/api/plextv.ts +++ b/server/api/plextv.ts @@ -82,21 +82,6 @@ interface ServerResponse { }; } -interface FriendResponse { - MediaContainer: { - User: { - $: { - id: string; - title: string; - username: string; - email: string; - thumb: string; - }; - Server?: ServerResponse[]; - }[]; - }; -} - interface UsersResponse { MediaContainer: { User: { @@ -234,19 +219,6 @@ class PlexTvAPI extends ExternalAPI { } } - public async getFriends(): Promise { - const response = await this.axios.get('/pms/friends/all', { - transformResponse: [], - responseType: 'text', - }); - - const parsedXml = (await xml2js.parseStringPromise( - response.data - )) as FriendResponse; - - return parsedXml; - } - public async checkUserAccess(userId: number): Promise { const settings = getSettings(); @@ -255,9 +227,9 @@ class PlexTvAPI extends ExternalAPI { throw new Error('Plex is not configured!'); } - const friends = await this.getFriends(); + const usersResponse = await this.getUsers(); - const users = friends.MediaContainer.User; + const users = usersResponse.MediaContainer.User; const user = users.find((u) => parseInt(u.$.id) === userId); diff --git a/server/api/rating/imdbRadarrProxy.ts b/server/api/rating/imdbRadarrProxy.ts new file mode 100644 index 000000000..0d8ec79fb --- /dev/null +++ b/server/api/rating/imdbRadarrProxy.ts @@ -0,0 +1,195 @@ +import ExternalAPI from '@server/api/externalapi'; +import cacheManager from '@server/lib/cache'; + +type IMDBRadarrProxyResponse = IMDBMovie[]; + +interface IMDBMovie { + ImdbId: string; + Overview: string; + Title: string; + OriginalTitle: string; + TitleSlug: string; + Ratings: Rating[]; + MovieRatings: MovieRatings; + Runtime: number; + Images: Image[]; + Genres: string[]; + Popularity: number; + Premier: string; + InCinema: string; + PhysicalRelease: any; + DigitalRelease: string; + Year: number; + AlternativeTitles: AlternativeTitle[]; + Translations: Translation[]; + Recommendations: Recommendation[]; + Credits: Credits; + Studio: string; + YoutubeTrailerId: string; + Certifications: Certification[]; + Status: any; + Collection: Collection; + OriginalLanguage: string; + Homepage: string; + TmdbId: number; +} + +interface Rating { + Count: number; + Value: number; + Origin: string; + Type: string; +} + +interface MovieRatings { + Tmdb: Tmdb; + Imdb: Imdb; + Metacritic: Metacritic; + RottenTomatoes: RottenTomatoes; +} + +interface Tmdb { + Count: number; + Value: number; + Type: string; +} + +interface Imdb { + Count: number; + Value: number; + Type: string; +} + +interface Metacritic { + Count: number; + Value: number; + Type: string; +} + +interface RottenTomatoes { + Count: number; + Value: number; + Type: string; +} + +interface Image { + CoverType: string; + Url: string; +} + +interface AlternativeTitle { + Title: string; + Type: string; + Language: string; +} + +interface Translation { + Title: string; + Overview: string; + Language: string; +} + +interface Recommendation { + TmdbId: number; + Title: string; +} + +interface Credits { + Cast: Cast[]; + Crew: Crew[]; +} + +interface Cast { + Name: string; + Order: number; + Character: string; + TmdbId: number; + CreditId: string; + Images: Image2[]; +} + +interface Image2 { + CoverType: string; + Url: string; +} + +interface Crew { + Name: string; + Job: string; + Department: string; + TmdbId: number; + CreditId: string; + Images: Image3[]; +} + +interface Image3 { + CoverType: string; + Url: string; +} + +interface Certification { + Country: string; + Certification: string; +} + +interface Collection { + Name: string; + Images: any; + Overview: any; + Translations: any; + Parts: any; + TmdbId: number; +} + +export interface IMDBRating { + title: string; + url: string; + criticsScore: number; +} + +/** + * This is a best-effort API. The IMDB API is technically + * private and getting access costs money/requires approval. + * + * Radarr hosts a public proxy that's in use by all Radarr instances. + */ +class IMDBRadarrProxy extends ExternalAPI { + constructor() { + super('https://api.radarr.video/v1', { + headers: { + 'Content-Type': 'application/json', + Accept: 'application/json', + }, + nodeCache: cacheManager.getCache('imdb').data, + }); + } + + /** + * Ask the Radarr IMDB Proxy for the movie + * + * @param IMDBid Id of IMDB movie + */ + public async getMovieRatings(IMDBid: string): Promise { + try { + const data = await this.get( + `/movie/imdb/${IMDBid}` + ); + + if (!data?.length || data[0].ImdbId !== IMDBid) { + return null; + } + + return { + title: data[0].Title, + url: `https://www.imdb.com/title/${data[0].ImdbId}`, + criticsScore: data[0].MovieRatings.Imdb.Value, + }; + } catch (e) { + throw new Error( + `[IMDB RADARR PROXY API] Failed to retrieve movie ratings: ${e.message}` + ); + } + } +} + +export default IMDBRadarrProxy; diff --git a/server/api/rottentomatoes.ts b/server/api/rating/rottentomatoes.ts similarity index 93% rename from server/api/rottentomatoes.ts rename to server/api/rating/rottentomatoes.ts index 99a74eb1b..1cf9d6d8e 100644 --- a/server/api/rottentomatoes.ts +++ b/server/api/rating/rottentomatoes.ts @@ -1,6 +1,6 @@ +import ExternalAPI from '@server/api/externalapi'; import cacheManager from '@server/lib/cache'; import { getSettings } from '@server/lib/settings'; -import ExternalAPI from './externalapi'; interface RTAlgoliaSearchResponse { results: { @@ -144,6 +144,9 @@ class RottenTomatoes extends ExternalAPI { ? 'Fresh' : 'Rotten', criticsScore: movie.rottenTomatoes.criticsScore, + audienceRating: + movie.rottenTomatoes.audienceScore >= 60 ? 'Upright' : 'Spilled', + audienceScore: movie.rottenTomatoes.audienceScore, year: Number(movie.releaseYear), }; } catch (e) { @@ -192,6 +195,9 @@ class RottenTomatoes extends ExternalAPI { criticsRating: tvshow.rottenTomatoes.criticsScore >= 60 ? 'Fresh' : 'Rotten', criticsScore: tvshow.rottenTomatoes.criticsScore, + audienceRating: + tvshow.rottenTomatoes.audienceScore >= 60 ? 'Upright' : 'Spilled', + audienceScore: tvshow.rottenTomatoes.audienceScore, year: Number(tvshow.releaseYear), }; } catch (e) { diff --git a/server/api/ratings.ts b/server/api/ratings.ts new file mode 100644 index 000000000..1fe1354cf --- /dev/null +++ b/server/api/ratings.ts @@ -0,0 +1,7 @@ +import { type IMDBRating } from '@server/api/rating/imdbRadarrProxy'; +import { type RTRating } from '@server/api/rating/rottentomatoes'; + +export interface RatingResponse { + rt?: RTRating; + imdb?: IMDBRating; +} diff --git a/server/job/schedule.ts b/server/job/schedule.ts index bb56ea82e..ef54589f3 100644 --- a/server/job/schedule.ts +++ b/server/job/schedule.ts @@ -8,6 +8,7 @@ import type { JobId } from '@server/lib/settings'; import { getSettings } from '@server/lib/settings'; import watchlistSync from '@server/lib/watchlistsync'; import logger from '@server/logger'; +import random from 'lodash/random'; import schedule from 'node-schedule'; import { jobJellyfinFullSync, jobJellyfinRecentSync } from './jellyfinsync'; @@ -107,21 +108,31 @@ export const startJobs = (): void => { }); } - // Run watchlist sync every 5 minutes - scheduledJobs.push({ + // Watchlist Sync + const watchlistSyncJob: ScheduledJob = { id: 'plex-watchlist-sync', name: 'Plex Watchlist Sync', type: 'process', - interval: 'minutes', + interval: 'fixed', cronSchedule: jobs['plex-watchlist-sync'].schedule, - job: schedule.scheduleJob(jobs['plex-watchlist-sync'].schedule, () => { + job: schedule.scheduleJob(new Date(Date.now() + 1000 * 60 * 20), () => { logger.info('Starting scheduled job: Plex Watchlist Sync', { label: 'Jobs', }); watchlistSync.syncWatchlist(); }), + }; + + // To help alleviate load on Plex's servers, we will add some fuzziness to the next schedule + // after each run + watchlistSyncJob.job.on('run', () => { + watchlistSyncJob.job.schedule( + new Date(Math.floor(Date.now() + 1000 * 60 * random(14, 24, true))) + ); }); + scheduledJobs.push(watchlistSyncJob); + // Run full radarr scan every 24 hours scheduledJobs.push({ id: 'radarr-scan', diff --git a/server/lib/availabilitySync.ts b/server/lib/availabilitySync.ts index a9f61fff6..0a16302cc 100644 --- a/server/lib/availabilitySync.ts +++ b/server/lib/availabilitySync.ts @@ -1,14 +1,13 @@ import type { PlexMetadata } from '@server/api/plexapi'; import PlexAPI from '@server/api/plexapi'; -import type { RadarrMovie } from '@server/api/servarr/radarr'; -import RadarrAPI from '@server/api/servarr/radarr'; +import RadarrAPI, { type RadarrMovie } from '@server/api/servarr/radarr'; import type { SonarrSeason, SonarrSeries } from '@server/api/servarr/sonarr'; import SonarrAPI from '@server/api/servarr/sonarr'; -import { MediaStatus } from '@server/constants/media'; +import { MediaRequestStatus, MediaStatus } from '@server/constants/media'; import { getRepository } from '@server/datasource'; import Media from '@server/entity/Media'; import MediaRequest from '@server/entity/MediaRequest'; -import Season from '@server/entity/Season'; +import type Season from '@server/entity/Season'; import SeasonRequest from '@server/entity/SeasonRequest'; import { User } from '@server/entity/User'; import type { RadarrSettings, SonarrSettings } from '@server/lib/settings'; @@ -18,8 +17,8 @@ import logger from '@server/logger'; class AvailabilitySync { public running = false; private plexClient: PlexAPI; - private plexSeasonsCache: Record = {}; - private sonarrSeasonsCache: Record = {}; + private plexSeasonsCache: Record; + private sonarrSeasonsCache: Record; private radarrServers: RadarrSettings[]; private sonarrServers: SonarrSettings[]; @@ -30,167 +29,178 @@ class AvailabilitySync { this.sonarrSeasonsCache = {}; this.radarrServers = settings.radarr.filter((server) => server.syncEnabled); this.sonarrServers = settings.sonarr.filter((server) => server.syncEnabled); - await this.initPlexClient(); - - if (!this.plexClient) { - return; - } - - logger.info(`Starting availability sync...`, { - label: 'AvailabilitySync', - }); - const mediaRepository = getRepository(Media); - const requestRepository = getRepository(MediaRequest); - const seasonRepository = getRepository(Season); - const seasonRequestRepository = getRepository(SeasonRequest); - - const pageSize = 50; try { + logger.info(`Starting availability sync...`, { + label: 'AvailabilitySync', + }); + const pageSize = 50; + + const userRepository = getRepository(User); + const admin = await userRepository.findOne({ + select: { id: true, plexToken: true }, + where: { id: 1 }, + }); + + if (admin) { + this.plexClient = new PlexAPI({ plexToken: admin.plexToken }); + } else { + logger.error('An admin is not configured.'); + } + for await (const media of this.loadAvailableMediaPaginated(pageSize)) { if (!this.running) { throw new Error('Job aborted'); } - const mediaExists = await this.mediaExists(media); + // Check plex, radarr, and sonarr for that specific media and + // if unavailable, then we change the status accordingly. + // If a non-4k or 4k version exists in at least one of the instances, we will only update that specific version + if (media.mediaType === 'movie') { + let movieExists = false; + let movieExists4k = false; - // We can not delete media so if both versions do not exist, we will change both columns to unknown or null - if (!mediaExists) { - if ( - media.status !== MediaStatus.UNKNOWN || - media.status4k !== MediaStatus.UNKNOWN - ) { - const request = await requestRepository.find({ - relations: { - media: true, - }, - where: { media: { id: media.id } }, - }); + const { existsInPlex } = await this.mediaExistsInPlex(media, false); + const { existsInPlex: existsInPlex4k } = await this.mediaExistsInPlex( + media, + true + ); + const existsInRadarr = await this.mediaExistsInRadarr(media, false); + const existsInRadarr4k = await this.mediaExistsInRadarr(media, true); + + if (existsInPlex || existsInRadarr) { + movieExists = true; logger.info( - `Media ID ${media.id} does not exist in any of your media instances. Status will be changed to unknown.`, - { label: 'AvailabilitySync' } + `The non-4K movie [TMDB ID ${media.tmdbId}] still exists. Preventing removal.`, + { + label: 'AvailabilitySync', + } ); + } - await mediaRepository.update(media.id, { - status: MediaStatus.UNKNOWN, - status4k: MediaStatus.UNKNOWN, - serviceId: null, - serviceId4k: null, - externalServiceId: null, - externalServiceId4k: null, - externalServiceSlug: null, - externalServiceSlug4k: null, - ratingKey: null, - ratingKey4k: null, - }); + if (existsInPlex4k || existsInRadarr4k) { + movieExists4k = true; + logger.info( + `The 4K movie [TMDB ID ${media.tmdbId}] still exists. Preventing removal.`, + { + label: 'AvailabilitySync', + } + ); + } - await requestRepository.remove(request); + if (!movieExists && media.status === MediaStatus.AVAILABLE) { + await this.mediaUpdater(media, false); + } + + if (!movieExists4k && media.status4k === MediaStatus.AVAILABLE) { + await this.mediaUpdater(media, true); } } + // If both versions still exist in plex, we still need + // to check through sonarr to verify season availability if (media.mediaType === 'tv') { - // ok, the show itself exists, but do all it's seasons? - const seasons = await seasonRepository.find({ - where: [ - { status: MediaStatus.AVAILABLE, media: { id: media.id } }, + let showExists = false; + let showExists4k = false; + + const { existsInPlex, seasonsMap: plexSeasonsMap = new Map() } = + await this.mediaExistsInPlex(media, false); + const { + existsInPlex: existsInPlex4k, + seasonsMap: plexSeasonsMap4k = new Map(), + } = await this.mediaExistsInPlex(media, true); + + const { existsInSonarr, seasonsMap: sonarrSeasonsMap } = + await this.mediaExistsInSonarr(media, false); + const { + existsInSonarr: existsInSonarr4k, + seasonsMap: sonarrSeasonsMap4k, + } = await this.mediaExistsInSonarr(media, true); + + if (existsInPlex || existsInSonarr) { + showExists = true; + logger.info( + `The non-4K show [TMDB ID ${media.tmdbId}] still exists. Preventing removal.`, { - status: MediaStatus.PARTIALLY_AVAILABLE, - media: { id: media.id }, - }, - { status4k: MediaStatus.AVAILABLE, media: { id: media.id } }, + label: 'AvailabilitySync', + } + ); + } + + if (existsInPlex4k || existsInSonarr4k) { + showExists4k = true; + logger.info( + `The 4K show [TMDB ID ${media.tmdbId}] still exists. Preventing removal.`, { - status4k: MediaStatus.PARTIALLY_AVAILABLE, - media: { id: media.id }, - }, - ], - }); - - let didDeleteSeasons = false; - for (const season of seasons) { - if ( - !mediaExists && - (season.status !== MediaStatus.UNKNOWN || - season.status4k !== MediaStatus.UNKNOWN) - ) { - await seasonRepository.update( - { id: season.id }, - { - status: MediaStatus.UNKNOWN, - status4k: MediaStatus.UNKNOWN, - } - ); - } else { - const seasonExists = await this.seasonExists(media, season); - - if (!seasonExists) { - logger.info( - `Removing season ${season.seasonNumber}, media ID ${media.id} because it does not exist in any of your media instances.`, - { label: 'AvailabilitySync' } - ); - - if ( - season.status !== MediaStatus.UNKNOWN || - season.status4k !== MediaStatus.UNKNOWN - ) { - await seasonRepository.update( - { id: season.id }, - { - status: MediaStatus.UNKNOWN, - status4k: MediaStatus.UNKNOWN, - } - ); - } - - const seasonToBeDeleted = await seasonRequestRepository.findOne( - { - relations: { - request: { - media: true, - }, - }, - where: { - request: { - media: { - id: media.id, - }, - }, - seasonNumber: season.seasonNumber, - }, - } - ); - - if (seasonToBeDeleted) { - await seasonRequestRepository.remove(seasonToBeDeleted); - } - - didDeleteSeasons = true; + label: 'AvailabilitySync', } - } + ); + } - if (didDeleteSeasons) { - if ( - media.status === MediaStatus.AVAILABLE || - media.status4k === MediaStatus.AVAILABLE - ) { - logger.info( - `Marking media ID ${media.id} as PARTIALLY_AVAILABLE because season removal has occurred.`, - { label: 'AvailabilitySync' } - ); + // Here we will create a final map that will cross compare + // with plex and sonarr. Filtered seasons will go through + // each season and assume the season does not exist. If Plex or + // Sonarr finds that season, we will change the final seasons value + // to true. + const filteredSeasonsMap: Map = new Map(); - if (media.status === MediaStatus.AVAILABLE) { - await mediaRepository.update(media.id, { - status: MediaStatus.PARTIALLY_AVAILABLE, - }); - } + media.seasons + .filter( + (season) => + season.status === MediaStatus.AVAILABLE || + season.status === MediaStatus.PARTIALLY_AVAILABLE + ) + .forEach((season) => + filteredSeasonsMap.set(season.seasonNumber, false) + ); - if (media.status4k === MediaStatus.AVAILABLE) { - await mediaRepository.update(media.id, { - status4k: MediaStatus.PARTIALLY_AVAILABLE, - }); - } - } - } + const finalSeasons = new Map([ + ...filteredSeasonsMap, + ...plexSeasonsMap, + ...sonarrSeasonsMap, + ]); + + const filteredSeasonsMap4k: Map = new Map(); + + media.seasons + .filter( + (season) => + season.status4k === MediaStatus.AVAILABLE || + season.status4k === MediaStatus.PARTIALLY_AVAILABLE + ) + .forEach((season) => + filteredSeasonsMap4k.set(season.seasonNumber, false) + ); + + const finalSeasons4k = new Map([ + ...filteredSeasonsMap4k, + ...plexSeasonsMap4k, + ...sonarrSeasonsMap4k, + ]); + + if ([...finalSeasons.values()].includes(false)) { + await this.seasonUpdater(media, finalSeasons, false); + } + + if ([...finalSeasons4k.values()].includes(false)) { + await this.seasonUpdater(media, finalSeasons4k, true); + } + + if ( + !showExists && + (media.status === MediaStatus.AVAILABLE || + media.status === MediaStatus.PARTIALLY_AVAILABLE) + ) { + await this.mediaUpdater(media, false); + } + + if ( + !showExists4k && + (media.status4k === MediaStatus.AVAILABLE || + media.status4k === MediaStatus.PARTIALLY_AVAILABLE) + ) { + await this.mediaUpdater(media, true); } } } @@ -233,556 +243,481 @@ class AvailabilitySync { } while (mediaPage.length > 0); } + private findMediaStatus( + requests: MediaRequest[], + is4k: boolean + ): MediaStatus { + const filteredRequests = requests.filter( + (request) => request.is4k === is4k + ); + + let mediaStatus: MediaStatus; + + if ( + filteredRequests.some( + (request) => request.status === MediaRequestStatus.APPROVED + ) + ) { + mediaStatus = MediaStatus.PROCESSING; + } else if ( + filteredRequests.some( + (request) => request.status === MediaRequestStatus.PENDING + ) + ) { + mediaStatus = MediaStatus.PENDING; + } else { + mediaStatus = MediaStatus.UNKNOWN; + } + + return mediaStatus; + } + private async mediaUpdater(media: Media, is4k: boolean): Promise { const mediaRepository = getRepository(Media); const requestRepository = getRepository(MediaRequest); - const isTVType = media.mediaType === 'tv'; - - const request = await requestRepository.findOne({ - relations: { - media: true, - }, - where: { media: { id: media.id }, is4k: is4k ? true : false }, - }); - - logger.info( - `Media ID ${media.id} does not exist in your ${is4k ? '4k' : 'non-4k'} ${ - isTVType ? 'Sonarr' : 'Radarr' - } and Plex instance. Status will be changed to unknown.`, - { label: 'AvailabilitySync' } - ); - - await mediaRepository.update( - media.id, - is4k - ? { - status4k: MediaStatus.UNKNOWN, - serviceId4k: null, - externalServiceId4k: null, - externalServiceSlug4k: null, - ratingKey4k: null, + try { + // Find all related requests only if + // the related media has an available status + const requests = await requestRepository + .createQueryBuilder('request') + .leftJoinAndSelect('request.media', 'media') + .where('(media.id = :id)', { + id: media.id, + }) + .andWhere( + `(request.is4k = :is4k AND media.${ + is4k ? 'status4k' : 'status' + } IN (:...mediaStatus))`, + { + mediaStatus: [ + MediaStatus.AVAILABLE, + MediaStatus.PARTIALLY_AVAILABLE, + ], + is4k: is4k, } - : { - status: MediaStatus.UNKNOWN, - serviceId: null, - externalServiceId: null, - externalServiceSlug: null, - ratingKey: null, - } - ); + ) + .getMany(); - if (isTVType) { - const seasonRepository = getRepository(Season); + // Check if a season is processing or pending to + // make sure we set the media to the correct status + let mediaStatus = MediaStatus.UNKNOWN; - await seasonRepository?.update( - { media: { id: media.id } }, - is4k - ? { status4k: MediaStatus.UNKNOWN } - : { status: MediaStatus.UNKNOWN } + if (media.mediaType === 'tv') { + mediaStatus = this.findMediaStatus(requests, is4k); + } + + media[is4k ? 'status4k' : 'status'] = mediaStatus; + media[is4k ? 'serviceId4k' : 'serviceId'] = + mediaStatus === MediaStatus.PROCESSING + ? media[is4k ? 'serviceId4k' : 'serviceId'] + : null; + media[is4k ? 'externalServiceId4k' : 'externalServiceId'] = + mediaStatus === MediaStatus.PROCESSING + ? media[is4k ? 'externalServiceId4k' : 'externalServiceId'] + : null; + media[is4k ? 'externalServiceSlug4k' : 'externalServiceSlug'] = + mediaStatus === MediaStatus.PROCESSING + ? media[is4k ? 'externalServiceSlug4k' : 'externalServiceSlug'] + : null; + media[is4k ? 'ratingKey4k' : 'ratingKey'] = + mediaStatus === MediaStatus.PROCESSING + ? media[is4k ? 'ratingKey4k' : 'ratingKey'] + : null; + + logger.info( + `The ${is4k ? '4K' : 'non-4K'} ${ + media.mediaType === 'movie' ? 'movie' : 'show' + } [TMDB ID ${media.tmdbId}] was not found in any ${ + media.mediaType === 'movie' ? 'Radarr' : 'Sonarr' + } and Plex instance. Status will be changed to unknown.`, + { label: 'AvailabilitySync' } + ); + + await mediaRepository.save({ media, ...media }); + + // Only delete media request if type is movie. + // Type tv request deletion is handled + // in the season request entity + if (requests.length > 0 && media.mediaType === 'movie') { + await requestRepository.remove(requests); + } + } catch (ex) { + logger.debug( + `Failure updating the ${is4k ? '4K' : 'non-4K'} ${ + media.mediaType === 'tv' ? 'show' : 'movie' + } [TMDB ID ${media.tmdbId}].`, + { + errorMessage: ex.message, + label: 'AvailabilitySync', + } ); } + } - await requestRepository.delete({ id: request?.id }); + private async seasonUpdater( + media: Media, + seasons: Map, + is4k: boolean + ): Promise { + const mediaRepository = getRepository(Media); + const seasonRequestRepository = getRepository(SeasonRequest); + + const seasonsPendingRemoval = new Map( + // Disabled linter as only the value is needed from the filter + // eslint-disable-next-line @typescript-eslint/no-unused-vars + [...seasons].filter(([_, exists]) => !exists) + ); + const seasonKeys = [...seasonsPendingRemoval.keys()]; + + try { + // Need to check and see if there are any related season + // requests. If they are, we will need to delete them. + const seasonRequests = await seasonRequestRepository + .createQueryBuilder('seasonRequest') + .leftJoinAndSelect('seasonRequest.request', 'request') + .leftJoinAndSelect('request.media', 'media') + .where('(media.id = :id)', { id: media.id }) + .andWhere( + '(request.is4k = :is4k AND seasonRequest.seasonNumber IN (:...seasonNumbers))', + { + seasonNumbers: seasonKeys, + is4k: is4k, + } + ) + .getMany(); + + for (const mediaSeason of media.seasons) { + if (seasonsPendingRemoval.has(mediaSeason.seasonNumber)) { + mediaSeason[is4k ? 'status4k' : 'status'] = MediaStatus.UNKNOWN; + } + } + + if (media.status === MediaStatus.AVAILABLE) { + media.status = MediaStatus.PARTIALLY_AVAILABLE; + logger.info( + `Marking the non-4K show [TMDB ID ${media.tmdbId}] as PARTIALLY_AVAILABLE because season removal has occurred.`, + { label: 'AvailabilitySync' } + ); + } + + if (media.status4k === MediaStatus.AVAILABLE) { + media.status4k = MediaStatus.PARTIALLY_AVAILABLE; + logger.info( + `Marking the 4K show [TMDB ID ${media.tmdbId}] as PARTIALLY_AVAILABLE because season removal has occurred.`, + { label: 'AvailabilitySync' } + ); + } + + await mediaRepository.save({ media, ...media }); + + if (seasonRequests.length > 0) { + await seasonRequestRepository.remove(seasonRequests); + } + + logger.info( + `The ${is4k ? '4K' : 'non-4K'} season(s) [${seasonKeys}] [TMDB ID ${ + media.tmdbId + }] was not found in any ${ + media.mediaType === 'tv' ? 'Sonarr' : 'Radarr' + } and Plex instance. Status will be changed to unknown.`, + { label: 'AvailabilitySync' } + ); + } catch (ex) { + logger.debug( + `Failure updating the ${ + is4k ? '4K' : 'non-4K' + } season(s) [${seasonKeys}], TMDB ID ${media.tmdbId}.`, + { + errorMessage: ex.message, + label: 'AvailabilitySync', + } + ); + } } private async mediaExistsInRadarr( media: Media, - existsInPlex: boolean, - existsInPlex4k: boolean + is4k: boolean ): Promise { - let existsInRadarr = true; - let existsInRadarr4k = true; + let existsInRadarr = false; + // Check for availability in all of the available radarr servers + // If any find the media, we will assume the media exists for (const server of this.radarrServers) { - const api = new RadarrAPI({ + const radarrAPI = new RadarrAPI({ apiKey: server.apiKey, url: RadarrAPI.buildUrl(server, '/api/v3'), }); + try { - // Check if both exist or if a single non-4k or 4k exists - // If both do not exist we will return false + let radarr: RadarrMovie | undefined; - let meta: RadarrMovie | undefined; - - if (!server.is4k && media.externalServiceId) { - meta = await api.getMovie({ id: media.externalServiceId }); + if (!server.is4k && media.externalServiceId && !is4k) { + radarr = await radarrAPI.getMovie({ + id: media.externalServiceId, + }); } - if (server.is4k && media.externalServiceId4k) { - meta = await api.getMovie({ id: media.externalServiceId4k }); + if (server.is4k && media.externalServiceId4k && is4k) { + radarr = await radarrAPI.getMovie({ + id: media.externalServiceId4k, + }); } - if (!server.is4k && (!meta || !meta.hasFile)) { - existsInRadarr = false; - } - - if (server.is4k && (!meta || !meta.hasFile)) { - existsInRadarr4k = false; + if (radarr && radarr.hasFile) { + existsInRadarr = true; } } catch (ex) { - logger.debug( - `Failure retrieving media ID ${media.id} from your ${ - !server.is4k ? 'non-4K' : '4K' - } Radarr.`, - { - errorMessage: ex.message, - label: 'AvailabilitySync', - } - ); - if (!server.is4k) { - existsInRadarr = false; - } - - if (server.is4k) { - existsInRadarr4k = false; + if (!ex.message.includes('404')) { + existsInRadarr = true; + logger.debug( + `Failure retrieving the ${is4k ? '4K' : 'non-4K'} movie [TMDB ID ${ + media.tmdbId + }] from Radarr.`, + { + errorMessage: ex.message, + label: 'AvailabilitySync', + } + ); } } } - // If only a single non-4k or 4k exists, then change entity columns accordingly - // Related media request will then be deleted - if ( - !existsInRadarr && - (existsInRadarr4k || existsInPlex4k) && - !existsInPlex - ) { - if (media.status !== MediaStatus.UNKNOWN) { - this.mediaUpdater(media, false); - } - } - - if ( - (existsInRadarr || existsInPlex) && - !existsInRadarr4k && - !existsInPlex4k - ) { - if (media.status4k !== MediaStatus.UNKNOWN) { - this.mediaUpdater(media, true); - } - } - - if (existsInRadarr || existsInRadarr4k || existsInPlex || existsInPlex4k) { - return true; - } - - return false; + return existsInRadarr; } private async mediaExistsInSonarr( media: Media, - existsInPlex: boolean, - existsInPlex4k: boolean - ): Promise { - let existsInSonarr = true; - let existsInSonarr4k = true; + is4k: boolean + ): Promise<{ existsInSonarr: boolean; seasonsMap: Map }> { + let existsInSonarr = false; + let preventSeasonSearch = false; + // Check for availability in all of the available sonarr servers + // If any find the media, we will assume the media exists for (const server of this.sonarrServers) { - const api = new SonarrAPI({ + const sonarrAPI = new SonarrAPI({ apiKey: server.apiKey, url: SonarrAPI.buildUrl(server, '/api/v3'), }); + try { - // Check if both exist or if a single non-4k or 4k exists - // If both do not exist we will return false + let sonarr: SonarrSeries | undefined; - let meta: SonarrSeries | undefined; - - if (!server.is4k && media.externalServiceId) { - meta = await api.getSeriesById(media.externalServiceId); + if (!server.is4k && media.externalServiceId && !is4k) { + sonarr = await sonarrAPI.getSeriesById(media.externalServiceId); this.sonarrSeasonsCache[`${server.id}-${media.externalServiceId}`] = - meta.seasons; + sonarr.seasons; } - if (server.is4k && media.externalServiceId4k) { - meta = await api.getSeriesById(media.externalServiceId4k); + if (server.is4k && media.externalServiceId4k && is4k) { + sonarr = await sonarrAPI.getSeriesById(media.externalServiceId4k); this.sonarrSeasonsCache[`${server.id}-${media.externalServiceId4k}`] = - meta.seasons; + sonarr.seasons; } - if (!server.is4k && (!meta || meta.statistics.episodeFileCount === 0)) { - existsInSonarr = false; - } - - if (server.is4k && (!meta || meta.statistics.episodeFileCount === 0)) { - existsInSonarr4k = false; + if (sonarr && sonarr.statistics.episodeFileCount > 0) { + existsInSonarr = true; } } catch (ex) { - logger.debug( - `Failure retrieving media ID ${media.id} from your ${ - !server.is4k ? 'non-4K' : '4K' - } Sonarr.`, - { - errorMessage: ex.message, - label: 'AvailabilitySync', - } + if (!ex.message.includes('404')) { + existsInSonarr = true; + preventSeasonSearch = true; + logger.debug( + `Failure retrieving the ${is4k ? '4K' : 'non-4K'} show [TMDB ID ${ + media.tmdbId + }] from Sonarr.`, + { + errorMessage: ex.message, + label: 'AvailabilitySync', + } + ); + } + } + } + + // Here we check each season for availability + // If the API returns an error other than a 404, + // we will have to prevent the season check from happening + const seasonsMap: Map = new Map(); + + if (!preventSeasonSearch) { + const filteredSeasons = media.seasons.filter( + (season) => + season[is4k ? 'status4k' : 'status'] === MediaStatus.AVAILABLE || + season[is4k ? 'status4k' : 'status'] === + MediaStatus.PARTIALLY_AVAILABLE + ); + + for (const season of filteredSeasons) { + const seasonExists = await this.seasonExistsInSonarr( + media, + season, + is4k ); - if (!server.is4k) { - existsInSonarr = false; - } - - if (server.is4k) { - existsInSonarr4k = false; + if (seasonExists) { + seasonsMap.set(season.seasonNumber, true); } } } - // If only a single non-4k or 4k exists, then change entity columns accordingly - // Related media request will then be deleted - if ( - !existsInSonarr && - (existsInSonarr4k || existsInPlex4k) && - !existsInPlex - ) { - if (media.status !== MediaStatus.UNKNOWN) { - this.mediaUpdater(media, false); - } - } - - if ( - (existsInSonarr || existsInPlex) && - !existsInSonarr4k && - !existsInPlex4k - ) { - if (media.status4k !== MediaStatus.UNKNOWN) { - this.mediaUpdater(media, true); - } - } - - if (existsInSonarr || existsInSonarr4k || existsInPlex || existsInPlex4k) { - return true; - } - - return false; + return { existsInSonarr, seasonsMap }; } private async seasonExistsInSonarr( media: Media, season: Season, - seasonExistsInPlex: boolean, - seasonExistsInPlex4k: boolean + is4k: boolean ): Promise { - let seasonExistsInSonarr = true; - let seasonExistsInSonarr4k = true; - - const mediaRepository = getRepository(Media); - const seasonRepository = getRepository(Season); - const seasonRequestRepository = getRepository(SeasonRequest); + let seasonExists = false; + // Check each sonarr instance to see if the media still exists + // If found, we will assume the media exists and prevent removal + // We can use the cache we built when we fetched the series with mediaExistsInSonarr for (const server of this.sonarrServers) { - const api = new SonarrAPI({ - apiKey: server.apiKey, - url: SonarrAPI.buildUrl(server, '/api/v3'), - }); + let sonarrSeasons: SonarrSeason[] | undefined; - try { - // Here we can use the cache we built when we fetched the series with mediaExistsInSonarr - // If the cache does not have data, we will fetch with the api route + if (media.externalServiceId && !is4k) { + sonarrSeasons = + this.sonarrSeasonsCache[`${server.id}-${media.externalServiceId}`]; + } - let seasons: SonarrSeason[] = - this.sonarrSeasonsCache[ - `${server.id}-${ - !server.is4k ? media.externalServiceId : media.externalServiceId4k - }` - ]; + if (media.externalServiceId4k && is4k) { + sonarrSeasons = + this.sonarrSeasonsCache[`${server.id}-${media.externalServiceId4k}`]; + } - if (!server.is4k && media.externalServiceId) { - seasons = - this.sonarrSeasonsCache[ - `${server.id}-${media.externalServiceId}` - ] ?? (await api.getSeriesById(media.externalServiceId)).seasons; - this.sonarrSeasonsCache[`${server.id}-${media.externalServiceId}`] = - seasons; + const seasonIsAvailable = sonarrSeasons?.find( + ({ seasonNumber, statistics }) => + season.seasonNumber === seasonNumber && + statistics?.episodeFileCount && + statistics?.episodeFileCount > 0 + ); + + if (seasonIsAvailable && sonarrSeasons) { + seasonExists = true; + } + } + + return seasonExists; + } + + private async mediaExistsInPlex( + media: Media, + is4k: boolean + ): Promise<{ existsInPlex: boolean; seasonsMap?: Map }> { + const ratingKey = media.ratingKey; + const ratingKey4k = media.ratingKey4k; + let existsInPlex = false; + let preventSeasonSearch = false; + + // Check each plex instance to see if the media still exists + // If found, we will assume the media exists and prevent removal + // We can use the cache we built when we fetched the series with mediaExistsInPlex + try { + let plexMedia: PlexMetadata | undefined; + + if (ratingKey && !is4k) { + plexMedia = await this.plexClient?.getMetadata(ratingKey); + + if (media.mediaType === 'tv') { + this.plexSeasonsCache[ratingKey] = + await this.plexClient?.getChildrenMetadata(ratingKey); } + } - if (server.is4k && media.externalServiceId4k) { - seasons = - this.sonarrSeasonsCache[ - `${server.id}-${media.externalServiceId4k}` - ] ?? (await api.getSeriesById(media.externalServiceId4k)).seasons; - this.sonarrSeasonsCache[`${server.id}-${media.externalServiceId4k}`] = - seasons; + if (ratingKey4k && is4k) { + plexMedia = await this.plexClient?.getMetadata(ratingKey4k); + + if (media.mediaType === 'tv') { + this.plexSeasonsCache[ratingKey4k] = + await this.plexClient?.getChildrenMetadata(ratingKey4k); } + } - const seasonIsUnavailable = seasons?.find( - ({ seasonNumber, statistics }) => - season.seasonNumber === seasonNumber && - statistics?.episodeFileCount === 0 - ); - - if (!server.is4k && seasonIsUnavailable) { - seasonExistsInSonarr = false; - } - - if (server.is4k && seasonIsUnavailable) { - seasonExistsInSonarr4k = false; - } - } catch (ex) { + if (plexMedia) { + existsInPlex = true; + } + } catch (ex) { + if (!ex.message.includes('404')) { + existsInPlex = true; + preventSeasonSearch = true; logger.debug( - `Failure retrieving media ID ${media.id} from your ${ - !server.is4k ? 'non-4K' : '4K' - } Sonarr.`, + `Failure retrieving the ${is4k ? '4K' : 'non-4K'} ${ + media.mediaType === 'tv' ? 'show' : 'movie' + } [TMDB ID ${media.tmdbId}] from Plex.`, { errorMessage: ex.message, label: 'AvailabilitySync', } ); - - if (!server.is4k) { - seasonExistsInSonarr = false; - } - - if (server.is4k) { - seasonExistsInSonarr4k = false; - } - } - } - - const seasonToBeDeleted = await seasonRequestRepository.findOne({ - relations: { - request: { - media: true, - }, - }, - where: { - request: { - is4k: seasonExistsInSonarr ? true : false, - media: { - id: media.id, - }, - }, - seasonNumber: season.seasonNumber, - }, - }); - - // If season does not exist, we will change status to unknown and delete related season request - // If parent media request is empty(all related seasons have been removed), parent is automatically deleted - if ( - !seasonExistsInSonarr && - (seasonExistsInSonarr4k || seasonExistsInPlex4k) && - !seasonExistsInPlex - ) { - if (season.status !== MediaStatus.UNKNOWN) { - logger.info( - `Season ${season.seasonNumber}, media ID ${media.id} does not exist in your non-4k Sonarr and Plex instance. Status will be changed to unknown.`, - { label: 'AvailabilitySync' } - ); - await seasonRepository.update(season.id, { - status: MediaStatus.UNKNOWN, - }); - - if (seasonToBeDeleted) { - await seasonRequestRepository.remove(seasonToBeDeleted); - } - - if (media.status === MediaStatus.AVAILABLE) { - logger.info( - `Marking media ID ${media.id} as PARTIALLY_AVAILABLE because season removal has occurred.`, - { label: 'AvailabilitySync' } - ); - await mediaRepository.update(media.id, { - status: MediaStatus.PARTIALLY_AVAILABLE, - }); - } - } - } - - if ( - (seasonExistsInSonarr || seasonExistsInPlex) && - !seasonExistsInSonarr4k && - !seasonExistsInPlex4k - ) { - if (season.status4k !== MediaStatus.UNKNOWN) { - logger.info( - `Season ${season.seasonNumber}, media ID ${media.id} does not exist in your 4k Sonarr and Plex instance. Status will be changed to unknown.`, - { label: 'AvailabilitySync' } - ); - await seasonRepository.update(season.id, { - status4k: MediaStatus.UNKNOWN, - }); - - if (seasonToBeDeleted) { - await seasonRequestRepository.remove(seasonToBeDeleted); - } - - if (media.status4k === MediaStatus.AVAILABLE) { - logger.info( - `Marking media ID ${media.id} as PARTIALLY_AVAILABLE because season removal has occurred.`, - { label: 'AvailabilitySync' } - ); - await mediaRepository.update(media.id, { - status4k: MediaStatus.PARTIALLY_AVAILABLE, - }); - } - } - } - - if ( - seasonExistsInSonarr || - seasonExistsInSonarr4k || - seasonExistsInPlex || - seasonExistsInPlex4k - ) { - return true; - } - - return false; - } - - private async mediaExists(media: Media): Promise { - const ratingKey = media.ratingKey; - const ratingKey4k = media.ratingKey4k; - - let existsInPlex = false; - let existsInPlex4k = false; - - // Check each plex instance to see if media exists - try { - if (ratingKey) { - const meta = await this.plexClient?.getMetadata(ratingKey); - if (meta) { - existsInPlex = true; - } - } - - if (ratingKey4k) { - const meta4k = await this.plexClient?.getMetadata(ratingKey4k); - if (meta4k) { - existsInPlex4k = true; - } - } - } catch (ex) { - if (!ex.message.includes('response code: 404')) { - throw ex; - } - } - // Base case if both media versions exist in plex - if (existsInPlex && existsInPlex4k) { - return true; - } - - // We then check radarr or sonarr has that specific media. If not, then we will move to delete - // If a non-4k or 4k version exists in at least one of the instances, we will only update that specific version - if (media.mediaType === 'movie') { - const existsInRadarr = await this.mediaExistsInRadarr( - media, - existsInPlex, - existsInPlex4k - ); - - // If true, media exists in at least one radarr or plex instance. - if (existsInRadarr) { - logger.warn( - `${media.id} exists in at least one Radarr or Plex instance. Media will be updated if set to available.`, - { - label: 'AvailabilitySync', - } - ); - - return true; } } + // Here we check each season in plex for availability + // If the API returns an error other than a 404, + // we will have to prevent the season check from happening if (media.mediaType === 'tv') { - const existsInSonarr = await this.mediaExistsInSonarr( - media, - existsInPlex, - existsInPlex4k - ); + const seasonsMap: Map = new Map(); - // If true, media exists in at least one sonarr or plex instance. - if (existsInSonarr) { - logger.warn( - `${media.id} exists in at least one Sonarr or Plex instance. Media will be updated if set to available.`, - { - label: 'AvailabilitySync', - } + if (!preventSeasonSearch) { + const filteredSeasons = media.seasons.filter( + (season) => + season[is4k ? 'status4k' : 'status'] === MediaStatus.AVAILABLE || + season[is4k ? 'status4k' : 'status'] === + MediaStatus.PARTIALLY_AVAILABLE ); - return true; + for (const season of filteredSeasons) { + const seasonExists = await this.seasonExistsInPlex( + media, + season, + is4k + ); + + if (seasonExists) { + seasonsMap.set(season.seasonNumber, true); + } + } } + + return { existsInPlex, seasonsMap }; } - return false; + return { existsInPlex }; } - private async seasonExists(media: Media, season: Season) { + private async seasonExistsInPlex( + media: Media, + season: Season, + is4k: boolean + ): Promise { const ratingKey = media.ratingKey; const ratingKey4k = media.ratingKey4k; - let seasonExistsInPlex = false; - let seasonExistsInPlex4k = false; - if (ratingKey) { - const children = - this.plexSeasonsCache[ratingKey] ?? - (await this.plexClient?.getChildrenMetadata(ratingKey)) ?? - []; - this.plexSeasonsCache[ratingKey] = children; - const seasonMeta = children?.find( - (child) => child.index === season.seasonNumber - ); + // Check each plex instance to see if the season exists + let plexSeasons: PlexMetadata[] | undefined; - if (seasonMeta) { - seasonExistsInPlex = true; - } + if (ratingKey && !is4k) { + plexSeasons = this.plexSeasonsCache[ratingKey]; } - if (ratingKey4k) { - const children4k = - this.plexSeasonsCache[ratingKey4k] ?? - (await this.plexClient?.getChildrenMetadata(ratingKey4k)) ?? - []; - this.plexSeasonsCache[ratingKey4k] = children4k; - const seasonMeta4k = children4k?.find( - (child) => child.index === season.seasonNumber - ); - - if (seasonMeta4k) { - seasonExistsInPlex4k = true; - } + if (ratingKey4k && is4k) { + plexSeasons = this.plexSeasonsCache[ratingKey4k]; } - // Base case if both season versions exist in plex - if (seasonExistsInPlex && seasonExistsInPlex4k) { - return true; - } - - const existsInSonarr = await this.seasonExistsInSonarr( - media, - season, - seasonExistsInPlex, - seasonExistsInPlex4k + const seasonIsAvailable = plexSeasons?.find( + (plexSeason) => plexSeason.index === season.seasonNumber ); - if (existsInSonarr) { - logger.warn( - `Season ${season.seasonNumber}, media ID ${media.id} exists in at least one Sonarr or Plex instance. Media will be updated if set to available.`, - { - label: 'AvailabilitySync', - } - ); - - return true; + if (seasonIsAvailable) { + seasonExistsInPlex = true; } - return false; - } - - private async initPlexClient() { - const userRepository = getRepository(User); - const admin = await userRepository.findOne({ - select: { id: true, plexToken: true }, - where: { id: 1 }, - }); - - if (!admin) { - logger.warning('No admin configured. Availability sync skipped.'); - return; - } - - this.plexClient = new PlexAPI({ plexToken: admin.plexToken }); + return seasonExistsInPlex; } } diff --git a/server/lib/cache.ts b/server/lib/cache.ts index e81466629..011205e7f 100644 --- a/server/lib/cache.ts +++ b/server/lib/cache.ts @@ -5,6 +5,7 @@ export type AvailableCacheIds = | 'radarr' | 'sonarr' | 'rt' + | 'imdb' | 'github' | 'plexguid' | 'plextv'; @@ -51,6 +52,10 @@ class CacheManager { stdTtl: 43200, checkPeriod: 60 * 30, }), + imdb: new Cache('imdb', 'IMDB Radarr Proxy', { + stdTtl: 43200, + checkPeriod: 60 * 30, + }), github: new Cache('github', 'GitHub API', { stdTtl: 21600, checkPeriod: 60 * 30, diff --git a/server/lib/settings.ts b/server/lib/settings.ts index b1d105773..2b59a4fea 100644 --- a/server/lib/settings.ts +++ b/server/lib/settings.ts @@ -404,7 +404,7 @@ class Settings { options: { webhookUrl: '', jsonPayload: - 'IntcbiAgICBcIm5vdGlmaWNhdGlvbl90eXBlXCI6IFwie3tub3RpZmljYXRpb25fdHlwZX19XCIsXG4gICAgXCJldmVudFwiOiBcInt7ZXZlbnR9fVwiLFxuICAgIFwic3ViamVjdFwiOiBcInt7c3ViamVjdH19XCIsXG4gICAgXCJtZXNzYWdlXCI6IFwie3ttZXNzYWdlfX1cIixcbiAgICBcImltYWdlXCI6IFwie3tpbWFnZX19XCIsXG4gICAgXCJ7e21lZGlhfX1cIjoge1xuICAgICAgICBcIm1lZGlhX3R5cGVcIjogXCJ7e21lZGlhX3R5cGV9fVwiLFxuICAgICAgICBcInRtZGJJZFwiOiBcInt7bWVkaWFfdG1kYmlkfX1cIixcbiAgICAgICAgXCJ0dmRiSWRcIjogXCJ7e21lZGlhX3R2ZGJpZH19XCIsXG4gICAgICAgIFwic3RhdHVzXCI6IFwie3ttZWRpYV9zdGF0dXN9fVwiLFxuICAgICAgICBcInN0YXR1czRrXCI6IFwie3ttZWRpYV9zdGF0dXM0a319XCJcbiAgICB9LFxu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5X2F2YXRhclwiOiBcInt7Y29tbWVudGVkQnlfYXZhdGFyfX1cIixcbiAgICBcImNvbW1lbnRlZEJ5X3NldHRpbmdzX2Rpc2NvcmRJZFwiOiBcInt7Y29tbWVudGVkQnlfc2V0dGluZ3NfZGlzY29yZElkfX1cIixcbiAgICBcImNvbW1lbnRlZEJ5X3NldHRpbmdzX3RlbGVncmFtQ2hhdElkXCI6IFwie3tjb21tZW50ZWRCeV9zZXR0aW5nc190ZWxlZ3JhbUNoYXRJZH19XCJcbiAgfSxcbiAgXCJ7e2V4dHJhfX1cIjogW11cbn0i', }, }, webpush: { diff --git a/server/routes/movie.ts b/server/routes/movie.ts index 8d609262b..956bf60cd 100644 --- a/server/routes/movie.ts +++ b/server/routes/movie.ts @@ -1,4 +1,6 @@ -import RottenTomatoes from '@server/api/rottentomatoes'; +import IMDBRadarrProxy from '@server/api/rating/imdbRadarrProxy'; +import RottenTomatoes from '@server/api/rating/rottentomatoes'; +import { type RatingResponse } from '@server/api/ratings'; import TheMovieDb from '@server/api/themoviedb'; import { MediaType } from '@server/constants/media'; import Media from '@server/entity/Media'; @@ -118,6 +120,9 @@ movieRoutes.get('/:id/similar', async (req, res, next) => { } }); +/** + * Endpoint backed by RottenTomatoes + */ movieRoutes.get('/:id/ratings', async (req, res, next) => { const tmdb = new TheMovieDb(); const rtapi = new RottenTomatoes(); @@ -153,4 +158,53 @@ movieRoutes.get('/:id/ratings', async (req, res, next) => { } }); +/** + * Endpoint combining RottenTomatoes and IMDB + */ +movieRoutes.get('/:id/ratingscombined', async (req, res, next) => { + const tmdb = new TheMovieDb(); + const rtapi = new RottenTomatoes(); + const imdbApi = new IMDBRadarrProxy(); + + try { + const movie = await tmdb.getMovie({ + movieId: Number(req.params.id), + }); + + const rtratings = await rtapi.getMovieRatings( + movie.title, + Number(movie.release_date.slice(0, 4)) + ); + + let imdbRatings; + if (movie.imdb_id) { + imdbRatings = await imdbApi.getMovieRatings(movie.imdb_id); + } + + if (!rtratings && !imdbRatings) { + return next({ + status: 404, + message: 'No ratings found.', + }); + } + + const ratings: RatingResponse = { + ...(rtratings ? { rt: rtratings } : {}), + ...(imdbRatings ? { imdb: imdbRatings } : {}), + }; + + return res.status(200).json(ratings); + } catch (e) { + logger.debug('Something went wrong retrieving movie ratings', { + label: 'API', + errorMessage: e.message, + movieId: req.params.id, + }); + return next({ + status: 500, + message: 'Unable to retrieve movie ratings.', + }); + } +}); + export default movieRoutes; diff --git a/server/routes/settings/index.ts b/server/routes/settings/index.ts index d7c0f8398..dc3724207 100644 --- a/server/routes/settings/index.ts +++ b/server/routes/settings/index.ts @@ -367,25 +367,27 @@ settingsRoutes.post('/tautulli', async (req, res, next) => { Object.assign(settings.tautulli, req.body); - try { - const tautulliClient = new TautulliAPI(settings.tautulli); + if (settings.tautulli.hostname) { + try { + const tautulliClient = new TautulliAPI(settings.tautulli); - const result = await tautulliClient.getInfo(); + const result = await tautulliClient.getInfo(); - if (!semver.gte(semver.coerce(result?.tautulli_version) ?? '', '2.9.0')) { - throw new Error('Tautulli version not supported'); + if (!semver.gte(semver.coerce(result?.tautulli_version) ?? '', '2.9.0')) { + throw new Error('Tautulli version not supported'); + } + + settings.save(); + } catch (e) { + logger.error('Something went wrong testing Tautulli connection', { + label: 'API', + errorMessage: e.message, + }); + return next({ + status: 500, + message: 'Unable to connect to Tautulli.', + }); } - - settings.save(); - } catch (e) { - logger.error('Something went wrong testing Tautulli connection', { - label: 'API', - errorMessage: e.message, - }); - return next({ - status: 500, - message: 'Unable to connect to Tautulli.', - }); } return res.status(200).json(settings.tautulli); diff --git a/server/routes/tv.ts b/server/routes/tv.ts index 1d2b4deed..701bfd0ff 100644 --- a/server/routes/tv.ts +++ b/server/routes/tv.ts @@ -1,4 +1,4 @@ -import RottenTomatoes from '@server/api/rottentomatoes'; +import RottenTomatoes from '@server/api/rating/rottentomatoes'; import TheMovieDb from '@server/api/themoviedb'; import { MediaType } from '@server/constants/media'; import Media from '@server/entity/Media'; diff --git a/src/components/Layout/Sidebar/index.tsx b/src/components/Layout/Sidebar/index.tsx index bc9393621..c93db4a29 100644 --- a/src/components/Layout/Sidebar/index.tsx +++ b/src/components/Layout/Sidebar/index.tsx @@ -72,9 +72,7 @@ const SidebarLinks: SidebarLinkProps[] = [ { href: '/issues', messagesKey: 'issues', - svgIcon: ( - - ), + svgIcon: , activeRegExp: /^\/issues/, requiredPermission: [ Permission.MANAGE_ISSUES, diff --git a/src/components/MovieDetails/index.tsx b/src/components/MovieDetails/index.tsx index fa79c8d68..b7dc59172 100644 --- a/src/components/MovieDetails/index.tsx +++ b/src/components/MovieDetails/index.tsx @@ -2,6 +2,7 @@ import RTAudFresh from '@app/assets/rt_aud_fresh.svg'; import RTAudRotten from '@app/assets/rt_aud_rotten.svg'; import RTFresh from '@app/assets/rt_fresh.svg'; import RTRotten from '@app/assets/rt_rotten.svg'; +import ImdbLogo from '@app/assets/services/imdb.svg'; import TmdbLogo from '@app/assets/tmdb_logo.svg'; import Button from '@app/components/Common/Button'; import CachedImage from '@app/components/Common/CachedImage'; @@ -40,7 +41,7 @@ import { ChevronDoubleDownIcon, ChevronDoubleUpIcon, } from '@heroicons/react/24/solid'; -import type { RTRating } from '@server/api/rottentomatoes'; +import { type RatingResponse } from '@server/api/ratings'; import { IssueStatus } from '@server/constants/issue'; import { MediaStatus } from '@server/constants/media'; import { MediaServerType } from '@server/constants/server'; @@ -91,6 +92,7 @@ const messages = defineMessages({ rtcriticsscore: 'Rotten Tomatoes Tomatometer', rtaudiencescore: 'Rotten Tomatoes Audience Score', tmdbuserscore: 'TMDB User Score', + imdbuserscore: 'IMDB User Score', }); interface MovieDetailsProps { @@ -126,8 +128,8 @@ const MovieDetails = ({ movie }: MovieDetailsProps) => { ), }); - const { data: ratingData } = useSWR( - `/api/v1/movie/${router.query.movieId}/ratings` + const { data: ratingData } = useSWR( + `/api/v1/movie/${router.query.movieId}/ratingscombined` ); const sortedCrew = useMemo( @@ -541,44 +543,62 @@ const MovieDetails = ({ movie }: MovieDetailsProps) => { )}
{(!!data.voteCount || - (ratingData?.criticsRating && !!ratingData?.criticsScore) || - (ratingData?.audienceRating && !!ratingData?.audienceScore)) && ( + (ratingData?.rt?.criticsRating && + !!ratingData?.rt?.criticsScore) || + (ratingData?.rt?.audienceRating && + !!ratingData?.rt?.audienceScore) || + ratingData?.imdb?.criticsScore) && (
- {ratingData?.criticsRating && !!ratingData?.criticsScore && ( - + {ratingData?.rt?.criticsRating && + !!ratingData?.rt?.criticsScore && ( + + + {ratingData.rt.criticsRating === 'Rotten' ? ( + + ) : ( + + )} + {ratingData.rt.criticsScore}% + + + )} + {ratingData?.rt?.audienceRating && + !!ratingData?.rt?.audienceScore && ( + + + {ratingData.rt.audienceRating === 'Spilled' ? ( + + ) : ( + + )} + {ratingData.rt.audienceScore}% + + + )} + {ratingData?.imdb?.criticsScore && ( + - {ratingData.criticsRating === 'Rotten' ? ( - - ) : ( - - )} - {ratingData.criticsScore}% - - - )} - {ratingData?.audienceRating && !!ratingData?.audienceScore && ( - - - {ratingData.audienceRating === 'Spilled' ? ( - - ) : ( - - )} - {ratingData.audienceScore}% + + {ratingData.imdb.criticsScore} )} @@ -827,7 +847,7 @@ const MovieDetails = ({ movie }: MovieDetailsProps) => { tmdbId={data.id} tvdbId={data.externalIds.tvdbId} imdbId={data.externalIds.imdbId} - rtUrl={ratingData?.url} + rtUrl={ratingData?.rt?.url} mediaUrl={ data.mediaInfo?.mediaUrl ?? data.mediaInfo?.mediaUrl4k } diff --git a/src/components/Selector/index.tsx b/src/components/Selector/index.tsx index 78ae33ea1..7b2165872 100644 --- a/src/components/Selector/index.tsx +++ b/src/components/Selector/index.tsx @@ -437,6 +437,7 @@ export const WatchProviderSelector = ({ {otherProviders.length > 0 && ( @@ -165,6 +166,7 @@ const Slider = ({ }`} onClick={() => slide(Direction.RIGHT)} disabled={scrollPos.isEnd} + type="button" > diff --git a/src/components/StatusBadge/index.tsx b/src/components/StatusBadge/index.tsx index 53c8a0dca..58e722bdd 100644 --- a/src/components/StatusBadge/index.tsx +++ b/src/components/StatusBadge/index.tsx @@ -176,11 +176,11 @@ const StatusBadge = ({ {inProgress && ( <> - {mediaType === 'tv' && ( + {mediaType === 'tv' && downloadItem[0].episode && ( {intl.formatMessage(messages.seasonepisodenumber, { - seasonNumber: downloadItem[0].episode?.seasonNumber, - episodeNumber: downloadItem[0].episode?.episodeNumber, + seasonNumber: downloadItem[0].episode.seasonNumber, + episodeNumber: downloadItem[0].episode.episodeNumber, })} )} @@ -229,11 +229,11 @@ const StatusBadge = ({ {inProgress && ( <> - {mediaType === 'tv' && ( + {mediaType === 'tv' && downloadItem[0].episode && ( {intl.formatMessage(messages.seasonepisodenumber, { - seasonNumber: downloadItem[0].episode?.seasonNumber, - episodeNumber: downloadItem[0].episode?.episodeNumber, + seasonNumber: downloadItem[0].episode.seasonNumber, + episodeNumber: downloadItem[0].episode.episodeNumber, })} )} @@ -282,11 +282,11 @@ const StatusBadge = ({ {inProgress && ( <> - {mediaType === 'tv' && ( + {mediaType === 'tv' && downloadItem[0].episode && ( {intl.formatMessage(messages.seasonepisodenumber, { - seasonNumber: downloadItem[0].episode?.seasonNumber, - episodeNumber: downloadItem[0].episode?.episodeNumber, + seasonNumber: downloadItem[0].episode.seasonNumber, + episodeNumber: downloadItem[0].episode.episodeNumber, })} )} diff --git a/src/components/TvDetails/index.tsx b/src/components/TvDetails/index.tsx index 6c0064e45..daceb9c84 100644 --- a/src/components/TvDetails/index.tsx +++ b/src/components/TvDetails/index.tsx @@ -40,7 +40,7 @@ import { PlayIcon, } from '@heroicons/react/24/outline'; import { ChevronDownIcon } from '@heroicons/react/24/solid'; -import type { RTRating } from '@server/api/rottentomatoes'; +import type { RTRating } from '@server/api/rating/rottentomatoes'; import { ANIME_KEYWORD_ID } from '@server/api/themoviedb/constants'; import { IssueStatus } from '@server/constants/issue'; import { MediaRequestStatus, MediaStatus } from '@server/constants/media'; diff --git a/src/i18n/locale/en.json b/src/i18n/locale/en.json index 5dc30620e..ac450c78e 100644 --- a/src/i18n/locale/en.json +++ b/src/i18n/locale/en.json @@ -200,8 +200,6 @@ "components.LanguageSelector.originalLanguageDefault": "All Languages", "components.Layout.LanguagePicker.displaylanguage": "Display Language", "components.Layout.SearchInput.searchPlaceholder": "Search Movies & TV", - "components.Layout.Sidebar.browsemovies": "Movies", - "components.Layout.Sidebar.browsetv": "Series", "components.Layout.Sidebar.dashboard": "Discover", "components.Layout.Sidebar.browsemovies": "Movies", "components.Layout.Sidebar.browsetv": "Series", @@ -282,6 +280,7 @@ "components.MovieDetails.cast": "Cast", "components.MovieDetails.digitalrelease": "Digital Release", "components.MovieDetails.downloadstatus": "Download Status", + "components.MovieDetails.imdbuserscore": "IMDB User Score", "components.MovieDetails.managemovie": "Manage Movie", "components.MovieDetails.mark4kavailable": "Mark as Available in 4K", "components.MovieDetails.markavailable": "Mark as Available",