|
|
@ -59,38 +59,6 @@ export default class PlexLibrary extends InternalService<Application> |
|
|
|
* Update the movie catalog |
|
|
|
*/ |
|
|
|
public async updateMovies() { |
|
|
|
if (this.isUpdating) { |
|
|
|
return; |
|
|
|
} |
|
|
|
this.isUpdating = true; |
|
|
|
let start = Date.now(); |
|
|
|
this.log("Updating movies..."); |
|
|
|
|
|
|
|
// Fetch the current and new sets of IMDb IDs
|
|
|
|
let [currentIds, newIds] = await Promise.all([ |
|
|
|
PlexMovie.imdbSet(), |
|
|
|
this.plex.movies(this.KEY_MOVIES), |
|
|
|
]); |
|
|
|
|
|
|
|
// Calculate the updates
|
|
|
|
let toInsert = new Set(Object.keys(newIds).filter(id => !currentIds.has(id))); |
|
|
|
let toRemove = new Set([...currentIds].filter(id => !(id in newIds))); |
|
|
|
|
|
|
|
// Remove ald Ids and insert new ones
|
|
|
|
await PlexMovie.removeImdbSet(toRemove); |
|
|
|
await PlexMovie.insertMovies([...toInsert].map(imdbId => ({imdbId, plexKey: newIds[imdbId]}))); |
|
|
|
|
|
|
|
// Update TMDb IDs
|
|
|
|
for (let movie of await PlexMovie.find({ where: { tmdbId: null } })) { |
|
|
|
let result = await this.app.service<MovieSearch>("Movie Search").findImdb(movie.imdbId); |
|
|
|
if (result !== null) { |
|
|
|
movie.tmdbId = result.id; |
|
|
|
await movie.save(); |
|
|
|
} |
|
|
|
await sleep(API_DATABASE_THROTTLE); |
|
|
|
} |
|
|
|
|
|
|
|
this.log("Movies updated. Took", (Date.now() - start)/1000, "seconds"); |
|
|
|
this.isUpdating = false; |
|
|
|
} |
|
|
|
} |