Merge pull request #76 from KevinMidboe/api
Api
This commit was merged in pull request #76.
This commit is contained in:
@@ -3,9 +3,11 @@ const convertPlexToSeasoned = require('src/plex/convertPlexToSeasoned');
|
|||||||
const convertPlexToStream = require('src/plex/convertPlexToStream');
|
const convertPlexToStream = require('src/plex/convertPlexToStream');
|
||||||
var rp = require('request-promise');
|
var rp = require('request-promise');
|
||||||
|
|
||||||
|
const PLEX_METHODS = ['lookup', 'playing']
|
||||||
|
|
||||||
class PlexRepository {
|
class PlexRepository {
|
||||||
|
|
||||||
searchMedia(query) {
|
search(query, callback) {
|
||||||
var options = {
|
var options = {
|
||||||
uri: 'http://10.0.0.44:32400/search?query=' + query,
|
uri: 'http://10.0.0.44:32400/search?query=' + query,
|
||||||
headers: {
|
headers: {
|
||||||
@@ -15,18 +17,40 @@ class PlexRepository {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return rp(options)
|
return rp(options)
|
||||||
.then((result) => {
|
.then((result) => this.mapResults(result))
|
||||||
var seasonedMediaObjects = result.MediaContainer.Metadata.reduce(function(match, media_item) {
|
.then(([mappedResults, resultCount]) => {
|
||||||
if (media_item.type === 'movie' || media_item.type === 'show') {
|
return { 'results': mappedResults, 'total_results': resultCount }
|
||||||
match.push(convertPlexToSeasoned(media_item));
|
})
|
||||||
}
|
}
|
||||||
return match;
|
|
||||||
}, []);
|
compareTmdbToPlex(tmdb, plexResult) {
|
||||||
return seasonedMediaObjects;
|
return Promise.resolve()
|
||||||
})
|
.then(() => {
|
||||||
.catch((err) => {
|
plexResult.results.map((plexItem) => {
|
||||||
throw new Error(err);
|
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() {
|
nowPlaying() {
|
||||||
@@ -51,6 +75,13 @@ class PlexRepository {
|
|||||||
throw new Error('Error handling plex playing. Error: ' + err);
|
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;
|
module.exports = PlexRepository;
|
||||||
|
|||||||
@@ -29,108 +29,21 @@ class RequestRepository {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
searchRequest(text, page, type) {
|
search(query, type, page) {
|
||||||
// STRIP METADATA THAT IS NOT ALLOWED
|
|
||||||
|
|
||||||
// Do a search in the tmdb api and return the results of the object
|
|
||||||
let getTmdbResults = function() {
|
|
||||||
return tmdb.search(text, page, type)
|
|
||||||
.then((tmdbSearch) => {
|
|
||||||
return tmdbSearch.results;
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// Take inputs and verify them with a list. Now we are for every item in tmdb result
|
|
||||||
// runnning through the entire plex loop. Many loops, but safe.
|
|
||||||
let checkIfMatchesPlexObjects = function(title, year, plexarray) {
|
|
||||||
// Iterate all elements in plexarray
|
|
||||||
console.log(plexArray)
|
|
||||||
for (let plexItem of plexarray) {
|
|
||||||
// If matches with our title and year return true
|
|
||||||
if (plexItem.title === title && plexItem.year === year)
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
// If no matches were found, return false
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return Promise.resolve()
|
return Promise.resolve()
|
||||||
.then(() => plexRepository.searchMedia(text))
|
.then(() => tmdb.search(query, type, page))
|
||||||
// Get the list of plexItems matching the query passed.
|
// .then((tmdbResult) => plexRepository.multipleInPlex(tmdbResult))
|
||||||
.then((plexItem) => {
|
.then((result) => {
|
||||||
let tmdbSearchResult = getTmdbResults();
|
return result
|
||||||
|
|
||||||
// When we get the result from tmdbSearchResult we pass it along and iterate over each
|
|
||||||
// element, and updates the matchedInPlex status of a item.
|
|
||||||
return tmdbSearchResult.then((tmdbResult) => {
|
|
||||||
for (var i = 0; i < tmdbResult.length; i++) {
|
|
||||||
let foundMatchInPlex = checkIfMatchesPlexObjects(tmdbResult[i].title, tmdbResult[i].year, plexItem);
|
|
||||||
tmdbResult[i].matchedInPlex = foundMatchInPlex;
|
|
||||||
}
|
|
||||||
return { 'results': tmdbResult, 'page': 1 };
|
|
||||||
})
|
|
||||||
// TODO log error
|
|
||||||
.catch((error) => {
|
|
||||||
console.log(error);
|
|
||||||
throw new Error('Search query did not give any results.');
|
|
||||||
})
|
|
||||||
})
|
|
||||||
.catch(() => {
|
|
||||||
let tmdbSearchResult = getTmdbResults();
|
|
||||||
|
|
||||||
// Catch if empty, then 404
|
|
||||||
return tmdbSearchResult.then((tmdbResult) => {
|
|
||||||
return {'results': tmdbResult, 'page': 1 };
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
|
.catch((error) => {return 'error in the house' + error})
|
||||||
}
|
}
|
||||||
|
|
||||||
lookup(identifier, type = 'movie') {
|
lookup(identifier, type = 'movie') {
|
||||||
// console.log('Lookup: ', identifier + ' : ' + type)
|
|
||||||
// if (type === 'movie') { type = 'movieInfo'}
|
|
||||||
// else if (type === 'tv') { type = 'tvInfo'}
|
|
||||||
// return Promise.resolve()
|
|
||||||
// .then(() => tmdb.lookup(identifier, type))
|
|
||||||
// .then((tmdbMovie) => {
|
|
||||||
// return Promise.resolve(plexRepository.searchMedia(tmdbMovie.title))
|
|
||||||
// .then((plexMovies) => {
|
|
||||||
// for (var i = 0; i < plexMovies.length; i++) {
|
|
||||||
// if (tmdbMovie.title === plexMovies[i].title && tmdbMovie.year === plexMovies[i].year) {
|
|
||||||
// tmdbMovie.matchedInPlex = true;
|
|
||||||
// return tmdbMovie;
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// })
|
|
||||||
// .catch((error) => {
|
|
||||||
// return error;
|
|
||||||
// });
|
|
||||||
// return tmdbMovie;
|
|
||||||
// });
|
|
||||||
let tmdbType = undefined;
|
|
||||||
if (type === 'movie') { tmdbType = 'movieInfo'}
|
|
||||||
else if (type === 'tv') { tmdbType = 'tvInfo'}
|
|
||||||
return Promise.resolve()
|
return Promise.resolve()
|
||||||
.then(() => tmdb.lookup(identifier, tmdbType))
|
.then(() => tmdb.lookup(identifier, type))
|
||||||
.then((tmdbMovie) => this.checkID(tmdbMovie))
|
.then((tmdbMovie) => this.checkID(tmdbMovie))
|
||||||
.then((tmdbMovie) => {
|
.then((tmdbMovie) => plexRepository.inPlex(tmdbMovie))
|
||||||
return Promise.resolve(plexRepository.searchMedia(tmdbMovie.title))
|
|
||||||
.then((plexMovies) => {
|
|
||||||
console.log('plexMovies lookup: ', plexMovies)
|
|
||||||
for (var i = 0; i < plexMovies.length; i++) {
|
|
||||||
if (tmdbMovie.title === plexMovies[i].title && tmdbMovie.year === plexMovies[i].year) {
|
|
||||||
console.log('matched')
|
|
||||||
tmdbMovie.matchedInPlex = true;
|
|
||||||
return tmdbMovie;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return tmdbMovie;
|
|
||||||
})
|
|
||||||
// Add it here
|
|
||||||
.catch((error) => {
|
|
||||||
console.log('there was a error:', error)
|
|
||||||
return tmdbMovie;
|
|
||||||
})
|
|
||||||
})
|
|
||||||
.catch((error) => {
|
.catch((error) => {
|
||||||
console.log(error)
|
console.log(error)
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -43,29 +43,11 @@ class TMDB {
|
|||||||
.catch(() => this.tmdb(methodTypes[type], query))
|
.catch(() => this.tmdb(methodTypes[type], query))
|
||||||
.catch(() => { throw new Error('Could not search for movies/shows at tmdb.'); })
|
.catch(() => { throw new Error('Could not search for movies/shows at tmdb.'); })
|
||||||
.then((response) => this.cache.set(cacheKey, response))
|
.then((response) => this.cache.set(cacheKey, response))
|
||||||
.then((response) => {
|
.then((response) => this.mapResults(response))
|
||||||
try {
|
.catch((error) => { throw new Error(error); })
|
||||||
let filteredTmdbItems = response.results.filter(function(tmdbResultItem) {
|
.then(([mappedResults, pagenumber, totalpages, total_results]) => {
|
||||||
return ((tmdbResultItem.vote_count >= 10 || tmdbResultItem.popularity > 2) && (tmdbResultItem.release_date !== undefined || tmdbResultItem.first_air_date !== undefined))
|
return {'results': mappedResults, 'page': pagenumber, 'total_results': total_results, 'total_pages': totalpages}
|
||||||
})
|
})
|
||||||
|
|
||||||
let seasonedItems = filteredTmdbItems.map((tmdbItem) => {
|
|
||||||
if (type === 'movie')
|
|
||||||
return convertTmdbToSeasoned(tmdbItem, 'movie');
|
|
||||||
else if (type === 'show')
|
|
||||||
return convertTmdbToSeasoned(tmdbItem, 'show');
|
|
||||||
else
|
|
||||||
return convertTmdbToSeasoned(tmdbItem);
|
|
||||||
});
|
|
||||||
|
|
||||||
// TODO add page number if results are larger than 20
|
|
||||||
return { 'results': seasonedItems, 'number_of_items_on_page': seasonedItems.length,
|
|
||||||
'page': 1, 'total_pages': 1 };
|
|
||||||
|
|
||||||
} catch (parseError) {
|
|
||||||
throw new Error('Could not parse result.');
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -140,7 +122,7 @@ class TMDB {
|
|||||||
const mappedResults = response.results.map((result) => {
|
const mappedResults = response.results.map((result) => {
|
||||||
return convertTmdbToSeasoned(result, type)
|
return convertTmdbToSeasoned(result, type)
|
||||||
})
|
})
|
||||||
return [mappedResults, response.page, response.total_pages]
|
return [mappedResults, response.page, response.total_pages, response.total_results]
|
||||||
})
|
})
|
||||||
.catch((error) => { throw new Error(error)})
|
.catch((error) => { throw new Error(error)})
|
||||||
|
|
||||||
@@ -164,8 +146,8 @@ class TMDB {
|
|||||||
.then((response) => this.cache.set(cacheKey, response))
|
.then((response) => this.cache.set(cacheKey, response))
|
||||||
.then((response) => this.mapResults(response, media_type))
|
.then((response) => this.mapResults(response, media_type))
|
||||||
.catch((error) => { throw new Error(error); })
|
.catch((error) => { throw new Error(error); })
|
||||||
.then(([mappedResults, pagenumber, totalpages]) => {
|
.then(([mappedResults, pagenumber, totalpages, total_results]) => {
|
||||||
return {'results': mappedResults, 'page': pagenumber, 'total_pages': totalpages}
|
return {'results': mappedResults, 'page': pagenumber, 'total_pages': totalpages, 'total_results': total_results}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -9,22 +9,12 @@ const searchHistory = new SearchHistory();
|
|||||||
function searchRequestController(req, res) {
|
function searchRequestController(req, res) {
|
||||||
const user = req.headers.loggedinuser;
|
const user = req.headers.loggedinuser;
|
||||||
const { query, page, type } = req.query;
|
const { query, page, type } = req.query;
|
||||||
console.log('searchReq: ' + query, page, type);
|
|
||||||
|
|
||||||
Promise.resolve()
|
Promise.resolve()
|
||||||
.then(() => {
|
.then(() => searchHistory.create(user, query))
|
||||||
if (user !== 'false') {
|
.then(() => requestRepository.search(query, page, type))
|
||||||
searchHistory.create(user, query);
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.then(() => requestRepository.searchRequest(query, page, type))
|
|
||||||
.then((searchResult) => {
|
.then((searchResult) => {
|
||||||
if (searchResult.results.length > 0) {
|
res.send(searchResult);
|
||||||
res.send(searchResult);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
res.status(404).send({success: false, error: 'Search query did not return any results.'})
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
.catch((error) => {
|
.catch((error) => {
|
||||||
res.status(500).send({success: false, error: error.message });
|
res.status(500).send({success: false, error: error.message });
|
||||||
|
|||||||
Reference in New Issue
Block a user