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:
2018-10-29 21:01:16 +01:00
parent 8f5bd44e4d
commit 161a466ab7
8 changed files with 171 additions and 0 deletions

View 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;

View 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;

View 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;

View 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;

View 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;

View 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;

View 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;

View 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;