Skip to content

Commit

Permalink
fix(plex-sync): store plex added date and sort recently added by it
Browse files Browse the repository at this point in the history
  • Loading branch information
sct committed Jan 13, 2021
1 parent a262727 commit d688a96
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 6 deletions.
2 changes: 1 addition & 1 deletion overseerr-api.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2976,7 +2976,7 @@ paths:
name: sort
schema:
type: string
enum: [added, modified]
enum: [added, modified, mediaAdded]
default: added
responses:
'200':
Expand Down
4 changes: 4 additions & 0 deletions server/api/plexapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ export interface PlexLibraryItem {
guid: string;
parentGuid?: string;
grandparentGuid?: string;
addedAt: number;
updatedAt: number;
type: 'movie' | 'show' | 'season' | 'episode';
}

Expand Down Expand Up @@ -48,6 +50,8 @@ export interface PlexMetadata {
parentIndex?: number;
leafCount: number;
viewedLeafCount: number;
addedAt: number;
updatedAt: number;
Media: Media[];
}

Expand Down
3 changes: 3 additions & 0 deletions server/entity/Media.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ class Media {
@Column({ type: 'datetime', default: () => 'CURRENT_TIMESTAMP' })
public lastSeasonChange: Date;

@Column({ type: 'datetime', nullable: true })
public mediaAddedAt: Date;

constructor(init?: Partial<Media>) {
Object.assign(this, init);
}
Expand Down
25 changes: 21 additions & 4 deletions server/job/plexsync/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ class JobPlexSync {
existing.status !== MediaStatus.AVAILABLE
) {
existing.status = MediaStatus.AVAILABLE;
existing.mediaAddedAt = new Date(plexitem.addedAt * 1000);
changedExisting = true;
}

Expand All @@ -123,6 +124,11 @@ class JobPlexSync {
changedExisting = true;
}

if (!existing.mediaAddedAt && !changedExisting) {
existing.mediaAddedAt = new Date(plexitem.addedAt * 1000);
changedExisting = true;
}

if (changedExisting) {
await mediaRepository.save(existing);
this.log(
Expand All @@ -144,6 +150,7 @@ class JobPlexSync {
? MediaStatus.AVAILABLE
: MediaStatus.UNKNOWN;
newMedia.mediaType = MediaType.MOVIE;
newMedia.mediaAddedAt = new Date(plexitem.addedAt * 1000);
await mediaRepository.save(newMedia);
this.log(`Saved ${plexitem.title}`);
}
Expand Down Expand Up @@ -208,6 +215,7 @@ class JobPlexSync {
existing.status !== MediaStatus.AVAILABLE
) {
existing.status = MediaStatus.AVAILABLE;
existing.mediaAddedAt = new Date(plexitem.addedAt * 1000);
changedExisting = true;
}

Expand All @@ -220,6 +228,11 @@ class JobPlexSync {
changedExisting = true;
}

if (!existing.mediaAddedAt && !changedExisting) {
existing.mediaAddedAt = new Date(plexitem.addedAt * 1000);
changedExisting = true;
}

if (changedExisting) {
await mediaRepository.save(existing);
this.log(
Expand All @@ -240,6 +253,7 @@ class JobPlexSync {
const newMedia = new Media();
newMedia.imdbId = tmdbMovie.external_ids.imdb_id;
newMedia.tmdbId = tmdbMovie.id;
newMedia.mediaAddedAt = new Date(plexitem.addedAt * 1000);
newMedia.status =
hasOtherResolution || (!this.enable4kMovie && has4k)
? MediaStatus.AVAILABLE
Expand All @@ -266,10 +280,7 @@ class JobPlexSync {
);
if (episodes) {
for (const episode of episodes) {
const special = await animeList.getSpecialEpisode(
tvdbId,
episode.index
);
const special = animeList.getSpecialEpisode(tvdbId, episode.index);
if (special) {
if (special.tmdbId) {
await this.processMovieWithId(episode, undefined, special.tmdbId);
Expand Down Expand Up @@ -519,6 +530,7 @@ class JobPlexSync {
'debug'
);
media.lastSeasonChange = new Date();
media.mediaAddedAt = new Date(plexitem.addedAt * 1000);
}

if (new4kSeasonAvailable > current4kSeasonAvailable) {
Expand All @@ -531,6 +543,10 @@ class JobPlexSync {
media.lastSeasonChange = new Date();
}

if (!media.mediaAddedAt) {
media.mediaAddedAt = new Date(plexitem.addedAt * 1000);
}

media.status = isAllStandardSeasons
? MediaStatus.AVAILABLE
: media.seasons.some(
Expand All @@ -553,6 +569,7 @@ class JobPlexSync {
seasons: newSeasons,
tmdbId: tvShow.id,
tvdbId: tvShow.external_ids.tvdb_id,
mediaAddedAt: new Date(plexitem.addedAt * 1000),
status: isAllStandardSeasons
? MediaStatus.AVAILABLE
: newSeasons.some(
Expand Down
52 changes: 52 additions & 0 deletions server/migration/1610522845513-AddMediaAddedFieldToMedia.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { MigrationInterface, QueryRunner } from 'typeorm';

export class AddMediaAddedFieldToMedia1610522845513
implements MigrationInterface {
name = 'AddMediaAddedFieldToMedia1610522845513';

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`DROP INDEX "IDX_7ff2d11f6a83cb52386eaebe74"`);
await queryRunner.query(`DROP INDEX "IDX_41a289eb1fa489c1bc6f38d9c3"`);
await queryRunner.query(`DROP INDEX "IDX_7157aad07c73f6a6ae3bbd5ef5"`);
await queryRunner.query(
`CREATE TABLE "temporary_media" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "mediaType" varchar NOT NULL, "tmdbId" integer NOT NULL, "tvdbId" integer, "imdbId" varchar, "status" integer NOT NULL DEFAULT (1), "createdAt" datetime NOT NULL DEFAULT (datetime('now')), "updatedAt" datetime NOT NULL DEFAULT (datetime('now')), "lastSeasonChange" datetime NOT NULL DEFAULT (CURRENT_TIMESTAMP), "status4k" integer NOT NULL DEFAULT (1), "mediaAddedAt" datetime, CONSTRAINT "UQ_41a289eb1fa489c1bc6f38d9c3c" UNIQUE ("tvdbId"))`
);
await queryRunner.query(
`INSERT INTO "temporary_media"("id", "mediaType", "tmdbId", "tvdbId", "imdbId", "status", "createdAt", "updatedAt", "lastSeasonChange", "status4k") SELECT "id", "mediaType", "tmdbId", "tvdbId", "imdbId", "status", "createdAt", "updatedAt", "lastSeasonChange", "status4k" FROM "media"`
);
await queryRunner.query(`DROP TABLE "media"`);
await queryRunner.query(`ALTER TABLE "temporary_media" RENAME TO "media"`);
await queryRunner.query(
`CREATE INDEX "IDX_7ff2d11f6a83cb52386eaebe74" ON "media" ("imdbId") `
);
await queryRunner.query(
`CREATE INDEX "IDX_41a289eb1fa489c1bc6f38d9c3" ON "media" ("tvdbId") `
);
await queryRunner.query(
`CREATE INDEX "IDX_7157aad07c73f6a6ae3bbd5ef5" ON "media" ("tmdbId") `
);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`DROP INDEX "IDX_7157aad07c73f6a6ae3bbd5ef5"`);
await queryRunner.query(`DROP INDEX "IDX_41a289eb1fa489c1bc6f38d9c3"`);
await queryRunner.query(`DROP INDEX "IDX_7ff2d11f6a83cb52386eaebe74"`);
await queryRunner.query(`ALTER TABLE "media" RENAME TO "temporary_media"`);
await queryRunner.query(
`CREATE TABLE "media" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "mediaType" varchar NOT NULL, "tmdbId" integer NOT NULL, "tvdbId" integer, "imdbId" varchar, "status" integer NOT NULL DEFAULT (1), "createdAt" datetime NOT NULL DEFAULT (datetime('now')), "updatedAt" datetime NOT NULL DEFAULT (datetime('now')), "lastSeasonChange" datetime NOT NULL DEFAULT (CURRENT_TIMESTAMP), "status4k" integer NOT NULL DEFAULT (1), CONSTRAINT "UQ_41a289eb1fa489c1bc6f38d9c3c" UNIQUE ("tvdbId"))`
);
await queryRunner.query(
`INSERT INTO "media"("id", "mediaType", "tmdbId", "tvdbId", "imdbId", "status", "createdAt", "updatedAt", "lastSeasonChange", "status4k") SELECT "id", "mediaType", "tmdbId", "tvdbId", "imdbId", "status", "createdAt", "updatedAt", "lastSeasonChange", "status4k" FROM "temporary_media"`
);
await queryRunner.query(`DROP TABLE "temporary_media"`);
await queryRunner.query(
`CREATE INDEX "IDX_7157aad07c73f6a6ae3bbd5ef5" ON "media" ("tmdbId") `
);
await queryRunner.query(
`CREATE INDEX "IDX_41a289eb1fa489c1bc6f38d9c3" ON "media" ("tvdbId") `
);
await queryRunner.query(
`CREATE INDEX "IDX_7ff2d11f6a83cb52386eaebe74" ON "media" ("imdbId") `
);
}
}
4 changes: 4 additions & 0 deletions server/routes/media.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ mediaRoutes.get('/', async (req, res, next) => {
updatedAt: 'DESC',
};
break;
case 'mediaAdded':
sortFilter = {
mediaAddedAt: 'DESC',
};
}

try {
Expand Down
2 changes: 1 addition & 1 deletion src/components/Discover/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ const Discover: React.FC = () => {
);

const { data: media, error: mediaError } = useSWR<MediaResultsResponse>(
'/api/v1/media?filter=available&take=20&sort=modified'
'/api/v1/media?filter=available&take=20&sort=mediaAdded'
);

const {
Expand Down

0 comments on commit d688a96

Please sign in to comment.