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:
20
seasoned_api/src/plex/convertPlexToEpisode.js
Normal file
20
seasoned_api/src/plex/convertPlexToEpisode.js
Normal file
@@ -0,0 +1,20 @@
|
||||
const Episode = require('src/plex/types/episode');
|
||||
|
||||
function convertPlexToEpisode(plexEpisode) {
|
||||
const episode = new Episode(plexEpisode.title, plexEpisode.grandparentTitle, plexEpisode.year);
|
||||
episode.season = plexEpisode.parentIndex;
|
||||
episode.episode = plexEpisode.index;
|
||||
episode.summary = plexEpisode.summary;
|
||||
episode.rating = plexEpisode.rating;
|
||||
|
||||
if (plexEpisode.viewCount !== undefined) {
|
||||
episode.views = plexEpisode.viewCount;
|
||||
}
|
||||
|
||||
if (plexEpisode.originallyAvailableAt !== undefined) {
|
||||
episode.airdate = new Date(plexEpisode.originallyAvailableAt)
|
||||
}
|
||||
|
||||
return episode;
|
||||
}
|
||||
module.exports = convertPlexToEpisode;
|
||||
15
seasoned_api/src/plex/convertPlexToMovie.js
Normal file
15
seasoned_api/src/plex/convertPlexToMovie.js
Normal file
@@ -0,0 +1,15 @@
|
||||
const Movie = require('src/plex/types/movie');
|
||||
|
||||
function convertPlexToMovie(plexMovie) {
|
||||
const movie = new Movie(plexMovie.title, plexMovie.year);
|
||||
movie.rating = plexMovie.rating;
|
||||
movie.tagline = plexMovie.tagline;
|
||||
|
||||
if (plexMovie.summary !== undefined) {
|
||||
movie.summary = plexMovie.summary;
|
||||
}
|
||||
|
||||
return movie;
|
||||
}
|
||||
|
||||
module.exports = convertPlexToMovie;
|
||||
13
seasoned_api/src/plex/convertPlexToShow.js
Normal file
13
seasoned_api/src/plex/convertPlexToShow.js
Normal file
@@ -0,0 +1,13 @@
|
||||
const Show = require('src/plex/types/show');
|
||||
|
||||
function convertPlexToShow(plexShow) {
|
||||
const show = new Show(plexShow.title, plexShow.year);
|
||||
show.summary = plexShow.summary;
|
||||
show.rating = plexShow.rating;
|
||||
show.seasons = plexShow.childCount;
|
||||
show.episodes = plexShow.leafCount;
|
||||
|
||||
return show;
|
||||
}
|
||||
|
||||
module.exports = convertPlexToShow;
|
||||
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;
|
||||
16
seasoned_api/src/plex/types/episode.js
Normal file
16
seasoned_api/src/plex/types/episode.js
Normal file
@@ -0,0 +1,16 @@
|
||||
class Episode {
|
||||
constructor(title, show, year) {
|
||||
this.title = title;
|
||||
this.show = show;
|
||||
this.year = year;
|
||||
this.season = null;
|
||||
this.episode = null;
|
||||
this.summary = null;
|
||||
this.rating = null;
|
||||
this.views = null;
|
||||
this.aired = null;
|
||||
this.type = 'episode';
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Episode;
|
||||
12
seasoned_api/src/plex/types/movie.js
Normal file
12
seasoned_api/src/plex/types/movie.js
Normal file
@@ -0,0 +1,12 @@
|
||||
class Movie {
|
||||
constructor(title, year) {
|
||||
this.title = title;
|
||||
this.year = year;
|
||||
this.summary = null;
|
||||
this.rating = null;
|
||||
this.tagline = null;
|
||||
this.type = 'movie';
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Movie;
|
||||
13
seasoned_api/src/plex/types/show.js
Normal file
13
seasoned_api/src/plex/types/show.js
Normal file
@@ -0,0 +1,13 @@
|
||||
class Show {
|
||||
constructor(title, year) {
|
||||
this.title = title;
|
||||
this.year = year;
|
||||
this.summary = null;
|
||||
this.rating = null;
|
||||
this.seasons = null;
|
||||
this.episodes = null;
|
||||
this.type = 'show';
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Show;
|
||||
25
seasoned_api/src/webserver/controllers/plex/search.js
Normal file
25
seasoned_api/src/webserver/controllers/plex/search.js
Normal file
@@ -0,0 +1,25 @@
|
||||
const configuration = require('src/config/configuration').getInstance();
|
||||
const Plex = require('src/plex/plex');
|
||||
const plex = new Plex(configuration.get('plex', 'ip'));
|
||||
|
||||
/**
|
||||
* Controller: Search plex for movies, shows and episodes by query
|
||||
* @param {Request} req http request variable
|
||||
* @param {Response} res
|
||||
* @returns {Callback}
|
||||
*/
|
||||
function searchPlexController(req, res) {
|
||||
const { query, type } = req.query;
|
||||
plex.search(query, type)
|
||||
.then((movies) => {
|
||||
if (movies.length > 0) {
|
||||
res.send(movies);
|
||||
} else {
|
||||
res.status(404).send({ success: false, error: 'Search query did not give any results from plex.'})
|
||||
}
|
||||
}).catch((error) => {
|
||||
res.status(500).send({ success: false, error: error.message });
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = searchPlexController;
|
||||
Reference in New Issue
Block a user