Rewrote most of this class. Now we have helper functions for getting info about items in plex. Used function is inPlex where we can input a tmdb item and it append if the item exsists in plex.

This commit is contained in:
2018-02-05 22:44:57 +01:00
parent cfb878fa64
commit 6f626d410d

View File

@@ -3,9 +3,11 @@ const convertPlexToSeasoned = require('src/plex/convertPlexToSeasoned');
const convertPlexToStream = require('src/plex/convertPlexToStream');
var rp = require('request-promise');
const PLEX_METHODS = ['lookup', 'playing']
class PlexRepository {
searchMedia(query) {
search(query, callback) {
var options = {
uri: 'http://10.0.0.44:32400/search?query=' + query,
headers: {
@@ -15,18 +17,40 @@ class PlexRepository {
}
return rp(options)
.then((result) => {
var seasonedMediaObjects = result.MediaContainer.Metadata.reduce(function(match, media_item) {
if (media_item.type === 'movie' || media_item.type === 'show') {
match.push(convertPlexToSeasoned(media_item));
}
return match;
}, []);
return seasonedMediaObjects;
})
.catch((err) => {
throw new Error(err);
})
.then((result) => this.mapResults(result))
.then(([mappedResults, resultCount]) => {
return { 'results': mappedResults, 'total_results': resultCount }
})
}
compareTmdbToPlex(tmdb, plexResult) {
return Promise.resolve()
.then(() => {
plexResult.results.map((plexItem) => {
if (tmdb.title === plexItem.title && tmdb.year === plexItem.year)
tmdb.matchedInPlex = true;
})
return tmdb
})
}
inPlex(tmdbResult) {
return Promise.resolve()
.then(() => this.search(tmdbResult.title))
.then((plexResult) => this.compareTmdbToPlex(tmdbResult, plexResult))
}
mapResults(response) {
return Promise.resolve()
.then(() => {
if (! response.MediaContainer.hasOwnProperty('Metadata')) return [[], 0]
const mappedResults = response.MediaContainer.Metadata.filter((element) => {
return (element.type === 'movie' || element.type === 'show')
}).map((element) => convertPlexToSeasoned(element))
return [mappedResults, mappedResults.length]
})
.catch((error) => {throw new Error(error)})
}
nowPlaying() {
@@ -51,6 +75,13 @@ class PlexRepository {
throw new Error('Error handling plex playing. Error: ' + err);
})
}
// multipleInPlex(tmdbResults) {
// const results = tmdbResults.results.map(async (tmdb) => {
// return this.inPlex(tmdb)
// })
// return Promise.all(results)
// }
}
module.exports = PlexRepository;