@@ -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;
|
||||
|
||||
@@ -29,108 +29,21 @@ class RequestRepository {
|
||||
}
|
||||
}
|
||||
|
||||
searchRequest(text, page, type) {
|
||||
// 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;
|
||||
}
|
||||
|
||||
search(query, type, page) {
|
||||
return Promise.resolve()
|
||||
.then(() => plexRepository.searchMedia(text))
|
||||
// Get the list of plexItems matching the query passed.
|
||||
.then((plexItem) => {
|
||||
let tmdbSearchResult = getTmdbResults();
|
||||
|
||||
// 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 };
|
||||
})
|
||||
.then(() => tmdb.search(query, type, page))
|
||||
// .then((tmdbResult) => plexRepository.multipleInPlex(tmdbResult))
|
||||
.then((result) => {
|
||||
return result
|
||||
})
|
||||
.catch((error) => {return 'error in the house' + error})
|
||||
}
|
||||
|
||||
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()
|
||||
.then(() => tmdb.lookup(identifier, tmdbType))
|
||||
.then(() => tmdb.lookup(identifier, type))
|
||||
.then((tmdbMovie) => this.checkID(tmdbMovie))
|
||||
.then((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;
|
||||
})
|
||||
})
|
||||
.then((tmdbMovie) => plexRepository.inPlex(tmdbMovie))
|
||||
.catch((error) => {
|
||||
console.log(error)
|
||||
})
|
||||
|
||||
@@ -43,29 +43,11 @@ class TMDB {
|
||||
.catch(() => this.tmdb(methodTypes[type], query))
|
||||
.catch(() => { throw new Error('Could not search for movies/shows at tmdb.'); })
|
||||
.then((response) => this.cache.set(cacheKey, response))
|
||||
.then((response) => {
|
||||
try {
|
||||
let filteredTmdbItems = response.results.filter(function(tmdbResultItem) {
|
||||
return ((tmdbResultItem.vote_count >= 10 || tmdbResultItem.popularity > 2) && (tmdbResultItem.release_date !== undefined || tmdbResultItem.first_air_date !== undefined))
|
||||
})
|
||||
|
||||
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.');
|
||||
}
|
||||
});
|
||||
.then((response) => this.mapResults(response))
|
||||
.catch((error) => { throw new Error(error); })
|
||||
.then(([mappedResults, pagenumber, totalpages, total_results]) => {
|
||||
return {'results': mappedResults, 'page': pagenumber, 'total_results': total_results, 'total_pages': totalpages}
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -140,7 +122,7 @@ class TMDB {
|
||||
const mappedResults = response.results.map((result) => {
|
||||
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)})
|
||||
|
||||
@@ -164,8 +146,8 @@ class TMDB {
|
||||
.then((response) => this.cache.set(cacheKey, response))
|
||||
.then((response) => this.mapResults(response, media_type))
|
||||
.catch((error) => { throw new Error(error); })
|
||||
.then(([mappedResults, pagenumber, totalpages]) => {
|
||||
return {'results': mappedResults, 'page': pagenumber, 'total_pages': totalpages}
|
||||
.then(([mappedResults, pagenumber, totalpages, total_results]) => {
|
||||
return {'results': mappedResults, 'page': pagenumber, 'total_pages': totalpages, 'total_results': total_results}
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -9,22 +9,12 @@ const searchHistory = new SearchHistory();
|
||||
function searchRequestController(req, res) {
|
||||
const user = req.headers.loggedinuser;
|
||||
const { query, page, type } = req.query;
|
||||
console.log('searchReq: ' + query, page, type);
|
||||
|
||||
Promise.resolve()
|
||||
.then(() => {
|
||||
if (user !== 'false') {
|
||||
searchHistory.create(user, query);
|
||||
}
|
||||
})
|
||||
.then(() => requestRepository.searchRequest(query, page, type))
|
||||
.then(() => searchHistory.create(user, query))
|
||||
.then(() => requestRepository.search(query, page, type))
|
||||
.then((searchResult) => {
|
||||
if (searchResult.results.length > 0) {
|
||||
res.send(searchResult);
|
||||
}
|
||||
else {
|
||||
res.status(404).send({success: false, error: 'Search query did not return any results.'})
|
||||
}
|
||||
res.send(searchResult);
|
||||
})
|
||||
.catch((error) => {
|
||||
res.status(500).send({success: false, error: error.message });
|
||||
|
||||
Reference in New Issue
Block a user