Merge pull request #29 from KevinMidboe/searchRequest

Search request
This commit is contained in:
2017-09-20 10:49:27 +02:00
committed by GitHub
4 changed files with 91 additions and 48 deletions

View File

@@ -25,16 +25,16 @@ class SearchRequest extends React.Component {
'discover', 'popular', 'nowplaying', 'upcoming'
]
// this.baseUrl = 'https://apollo.kevinmidboe.com/api/v1/';
this.baseUrl = 'http://localhost:31459/api/v1/tmdb/';
this.baseUrl = 'https://apollo.kevinmidboe.com/api/v1/tmdb';
// this.baseUrl = 'http://localhost:31459/api/v1/tmdb/';
this.URLs = {
// request: 'https://apollo.kevinmidboe.com/api/v1/plex/request?page='+this.state.page+'&query=',
request: 'http://localhost:31459/api/v1/plex/request?page='+this.state.page+'&query=',
// upcoming: 'https://apollo.kevinmidboe.com/api/v1/tmdb/upcoming',
upcoming: 'http://localhost:31459/api/v1/tmdb/upcoming',
// sendRequest: 'https://apollo.kevinmidboe.com/api/v1/plex/request?query='
sendRequest: 'http://localhost:31459/api/v1/plex/request?query='
request: 'https://apollo.kevinmidboe.com/api/v1/plex/request?page='+this.state.page+'&query=',
// request: 'http://localhost:31459/api/v1/plex/request?page='+this.state.page+'&query=',
upcoming: 'https://apollo.kevinmidboe.com/api/v1/tmdb/upcoming',
// upcoming: 'http://localhost:31459/api/v1/tmdb/upcoming',
sendRequest: 'https://apollo.kevinmidboe.com/api/v1/plex/request?query='
// sendRequest: 'http://localhost:31459/api/v1/plex/request?query='
}
}
@@ -108,10 +108,9 @@ class SearchRequest extends React.Component {
.then(data => { // Parse the data of the JSON response
// If it is something here it updates the state variable with the HTML list of all
// movie objects that where returned by the search request
console.log(data)
if (data.length > 0) {
if (data.results.length > 0) {
this.setState({
responseMovieList: data.map(item => this.createMovieObjects(item))
responseMovieList: data.results.map(item => this.createMovieObjects(item))
})
}
})

View File

@@ -16,33 +16,60 @@ const nodemailer = require('nodemailer');
class RequestRepository {
searchRequest(query, page, type) {
// TODO get from cache
// 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(query, 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
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()
.then(() => tmdb.search(query, page, type))
.then((tmdbMovies) => {
return Promise.resolve()
.then(() => plexRepository.searchMedia(query))
.then((plexMedia) => {
return Promise.each(tmdbMovies, function(tmdbMovie) {
return Promise.each(plexMedia, function(plexMovie) {
if (tmdbMovie.title == plexMovie.title && tmdbMovie.year == plexMovie.year) {
tmdbMovie.matchedInPlex = true;
// console.log('Matched: ' + tmdbMovie.title + ' : ' + tmdbMovie.year);
}
return tmdbMovie;
})
.then(() => plexRepository.searchMedia(query)
// 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.');
})
})
// This is pretty janky, but if there is a error because plex does not not get any results
// the tmdbMovies list is just returned without checking plexStatus.
.catch((error) => {
return tmdbMovies;
.catch(() => {
let tmdbSearchResult = getTmdbResults();
// Catch if empty, then 404
return tmdbSearchResult.then((tmdbResult) => {
return {'results': tmdbResult, 'page': 1 };
})
})
})
.catch((error) => {
return error;
});
)
}
lookup(identifier, type = 'movie') {

View File

@@ -11,16 +11,33 @@ class TMDB {
this.tmdbLibrary = tmdbLibrary || moviedb(apiKey);
}
search(text, page = 1, type = 'multi') {
const query = { 'query': text, 'page': page };
/**
* Retrive list of of items from TMDB matching the query and/or type given.
* @param {queryText, page, type} the page number to specify in the request for discover,
* @returns {Promise} dict with query results, current page and total_pages
*/
search(queryText, page = 1, type = 'multi') {
// Setup query object for tmdb api search
const query = { 'query': queryText, 'page': page };
return Promise.resolve()
.then(() => this.tmdb(type, query))
.catch(() => { throw new Error('Could not search for movies.'); })
.then(() => this.tmdb(type, query)) // Search the tmdb api
.catch(() => { throw new Error('Could not search for movies.'); }) // If any error at all when fetching
.then((reponse) => {
try {
return reponse.results.filter(function(item) {
return ((item.vote_count >= 80 || item.popularity > 18) && (item.release_date !== undefined || item.first_air_date !== undefined))
}).map(convertTmdbToSeasoned);
// We want to filter because there are movies really low rated that are not interesting to us.
let filteredTmdbItems = reponse.results.filter(function(tmdbResultItem) {
return ((tmdbResultItem.vote_count >= 80 || tmdbResultItem.popularity > 18) && (tmdbResultItem.release_date !== undefined || tmdbResultItem.first_air_date !== undefined))
})
// Here we convert the filtered result from the tmdb api to seaonsed objects
let seasonedItems = filteredTmdbItems.map((tmdbItem) => {
return convertTmdbToSeasoned(tmdbItem);
});
// TODO add page number if results are larger than 20
return { 'results': seasonedItems, 'number_of_items_on_page': seasonedItems,
'page': 1, 'total_pages': 1 };
} catch (parseError) {
console.log(parseError)
throw new Error('Could not parse result.');
@@ -33,7 +50,7 @@ class TMDB {
* Retrive list of discover section of movies from TMDB.
* @param {Page, type} the page number to specify in the request for discover,
* and type for movie or show
* @returns {Promise} dict with query results, current page and total_pages
* @returns {Promise} dict with discover results, current page and total_pages
*/
discover(page, type='movie') {
// Sets the tmdb function type to the corresponding type from query
@@ -76,7 +93,7 @@ class TMDB {
* Retrive list of popular section of movies or shows from TMDB.
* @param {Page, type} the page number to specify in the request for popular,
* and type for movie or show
* @returns {Promise} dict with query results, current page and total_pages
* @returns {Promise} dict with popular results, current page and total_pages
*/
// TODO add filter for language
popular(page, type='movie') {
@@ -121,7 +138,7 @@ class TMDB {
* Retrive list of now playing/airing section of movies or shows from TMDB.
* @param {Page, type} the page number to specify in the request for now playing/airing,
* and type for movie or show
* @returns {Promise} dict with query results, current page and total_pages
* @returns {Promise} dict with nowplaying results, current page and total_pages
*/
// TODO add filter for language
nowplaying(page, type='movie') {
@@ -161,9 +178,9 @@ class TMDB {
}
/**
* Retrive list of upcmoing movies from TMDB.
* Retrive list of upcoming movies from TMDB.
* @param {Page} the page number to specify in the request for upcoming movies
* @returns {Promise} dict with query results, current page and total_pages
* @returns {Promise} dict with upcoming results, current page and total_pages
*/
// TODO add filter for language
upcoming(page) {
@@ -190,7 +207,7 @@ class TMDB {
/**
* Retrive list of upcmoing movies from TMDB.
* @param {Page} the page number to specify in the request for upcoming movies
* @returns {Promise} dict with query results, current page and total_pages
* @returns {Promise} dict with similar results, current page and total_pages
*/
// TODO add filter for language
similar(identifier, type) {

View File

@@ -6,10 +6,10 @@ function searchRequestController(req, res) {
console.log('searchReq: ' + query, page, type);
requestRepository.searchRequest(query, page, type)
.then((movies) => {
.then((searchResult) => {
// Verify that respond has content, if so send the content back
if (movies.length > 0 && movies != null) {
res.send(movies);
if (searchResult.results.length > 0) {
res.send(searchResult);
}
// If no content was found, send 404 status and error message
else {