Rewrote how local plex library is indexed and what it returns. After searching plex the response is separated into three classes by types (movie, show & episode). Plex also has a function for inputing a (tmdb)movie object and searching for matches of name & type in plex. If a match the object matchedInPlex variable is set to true.
This commit is contained in:
57
seasoned_api/src/plex/plex.js
Normal file
57
seasoned_api/src/plex/plex.js
Normal file
@@ -0,0 +1,57 @@
|
||||
const axios = require('axios');
|
||||
const convertPlexToMovie = require('src/plex/convertPlexToMovie');
|
||||
const convertPlexToShow = require('src/plex/convertPlexToShow');
|
||||
const convertPlexToEpisode = require('src/plex/convertPlexToEpisode');
|
||||
|
||||
class Plex {
|
||||
constructor(ip) {
|
||||
this.plexIP = ip;
|
||||
this.plexPort = 32400;
|
||||
}
|
||||
|
||||
existsInPlex(tmdbMovie) {
|
||||
return Promise.resolve()
|
||||
.then(() => this.search(tmdbMovie.title))
|
||||
.then((plexMovies) => {
|
||||
const matches = plexMovies.some((plexMovie) => {
|
||||
return tmdbMovie.title === plexMovie.title && tmdbMovie.type === plexMovie.type;
|
||||
})
|
||||
|
||||
tmdbMovie.existsInPlex = matches;
|
||||
return tmdbMovie;
|
||||
})
|
||||
}
|
||||
|
||||
search(query) {
|
||||
const options = {
|
||||
baseURL: `http://${this.plexIP}:${this.plexPort}`,
|
||||
url: '/hubs/search',
|
||||
params: { query: query },
|
||||
responseType: 'json'
|
||||
}
|
||||
|
||||
return Promise.resolve()
|
||||
.then(() => axios.request(options))
|
||||
.catch((error) => { throw new Error(`Unable to search plex library`); })
|
||||
.then(response => this.mapResults(response));
|
||||
}
|
||||
|
||||
|
||||
mapResults(response) {
|
||||
return response.data.MediaContainer.Hub.reduce((result, hub) => {
|
||||
if (hub.type === 'movie' && hub.Metadata !== undefined) {
|
||||
return [...result, ...hub.Metadata.map(convertPlexToMovie)];
|
||||
}
|
||||
else if (hub.type === 'show' && hub.Metadata !== undefined) {
|
||||
return [...result, ...hub.Metadata.map(convertPlexToShow)];
|
||||
}
|
||||
else if (hub.type === 'episode' && hub.Metadata !== undefined) {
|
||||
return [...result, ...hub.Metadata.map(convertPlexToEpisode)];
|
||||
}
|
||||
|
||||
return result
|
||||
}, [])
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Plex;
|
||||
Reference in New Issue
Block a user