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' 'discover', 'popular', 'nowplaying', 'upcoming'
] ]
// this.baseUrl = 'https://apollo.kevinmidboe.com/api/v1/'; this.baseUrl = 'https://apollo.kevinmidboe.com/api/v1/tmdb';
this.baseUrl = 'http://localhost:31459/api/v1/tmdb/'; // this.baseUrl = 'http://localhost:31459/api/v1/tmdb/';
this.URLs = { this.URLs = {
// request: 'https://apollo.kevinmidboe.com/api/v1/plex/request?page='+this.state.page+'&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=', // request: 'http://localhost:31459/api/v1/plex/request?page='+this.state.page+'&query=',
// upcoming: 'https://apollo.kevinmidboe.com/api/v1/tmdb/upcoming', upcoming: 'https://apollo.kevinmidboe.com/api/v1/tmdb/upcoming',
upcoming: 'http://localhost:31459/api/v1/tmdb/upcoming', // upcoming: 'http://localhost:31459/api/v1/tmdb/upcoming',
// sendRequest: 'https://apollo.kevinmidboe.com/api/v1/plex/request?query=' sendRequest: 'https://apollo.kevinmidboe.com/api/v1/plex/request?query='
sendRequest: 'http://localhost:31459/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 .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 // 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 // movie objects that where returned by the search request
console.log(data) if (data.results.length > 0) {
if (data.length > 0) {
this.setState({ 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 { class RequestRepository {
searchRequest(query, page, type) { searchRequest(query, page, type) {
// TODO get from cache
// STRIP METADATA THAT IS NOT ALLOWED // 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() return Promise.resolve()
.then(() => tmdb.search(query, page, type)) .then(() => plexRepository.searchMedia(query)
.then((tmdbMovies) => { // Get the list of plexItems matching the query passed.
return Promise.resolve() .then((plexItem) => {
.then(() => plexRepository.searchMedia(query)) let tmdbSearchResult = getTmdbResults();
.then((plexMedia) => {
return Promise.each(tmdbMovies, function(tmdbMovie) { // When we get the result from tmdbSearchResult we pass it along and iterate over each
return Promise.each(plexMedia, function(plexMovie) { // element, and updates the matchedInPlex status of a item.
if (tmdbMovie.title == plexMovie.title && tmdbMovie.year == plexMovie.year) { return tmdbSearchResult.then((tmdbResult) => {
tmdbMovie.matchedInPlex = true; for (var i = 0; i < tmdbResult.length; i++) {
// console.log('Matched: ' + tmdbMovie.title + ' : ' + tmdbMovie.year); let foundMatchInPlex = checkIfMatchesPlexObjects(tmdbResult[i].title, tmdbResult[i].year, plexItem);
} tmdbResult[i].matchedInPlex = foundMatchInPlex;
return tmdbMovie; }
}) 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 .catch(() => {
// the tmdbMovies list is just returned without checking plexStatus. let tmdbSearchResult = getTmdbResults();
.catch((error) => {
return tmdbMovies; // Catch if empty, then 404
return tmdbSearchResult.then((tmdbResult) => {
return {'results': tmdbResult, 'page': 1 };
})
}) })
}) )
.catch((error) => {
return error;
});
} }
lookup(identifier, type = 'movie') { lookup(identifier, type = 'movie') {

View File

@@ -11,16 +11,33 @@ class TMDB {
this.tmdbLibrary = tmdbLibrary || moviedb(apiKey); 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() return Promise.resolve()
.then(() => this.tmdb(type, query)) .then(() => this.tmdb(type, query)) // Search the tmdb api
.catch(() => { throw new Error('Could not search for movies.'); }) .catch(() => { throw new Error('Could not search for movies.'); }) // If any error at all when fetching
.then((reponse) => { .then((reponse) => {
try { try {
return reponse.results.filter(function(item) { // We want to filter because there are movies really low rated that are not interesting to us.
return ((item.vote_count >= 80 || item.popularity > 18) && (item.release_date !== undefined || item.first_air_date !== undefined)) let filteredTmdbItems = reponse.results.filter(function(tmdbResultItem) {
}).map(convertTmdbToSeasoned); 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) { } catch (parseError) {
console.log(parseError) console.log(parseError)
throw new Error('Could not parse result.'); throw new Error('Could not parse result.');
@@ -33,7 +50,7 @@ class TMDB {
* Retrive list of discover section of movies from TMDB. * Retrive list of discover section of movies from TMDB.
* @param {Page, type} the page number to specify in the request for discover, * @param {Page, type} the page number to specify in the request for discover,
* and type for movie or show * 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') { discover(page, type='movie') {
// Sets the tmdb function type to the corresponding type from query // 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. * Retrive list of popular section of movies or shows from TMDB.
* @param {Page, type} the page number to specify in the request for popular, * @param {Page, type} the page number to specify in the request for popular,
* and type for movie or show * 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 // TODO add filter for language
popular(page, type='movie') { popular(page, type='movie') {
@@ -121,7 +138,7 @@ class TMDB {
* Retrive list of now playing/airing section of movies or shows from 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, * @param {Page, type} the page number to specify in the request for now playing/airing,
* and type for movie or show * 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 // TODO add filter for language
nowplaying(page, type='movie') { 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 * @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 // TODO add filter for language
upcoming(page) { upcoming(page) {
@@ -190,7 +207,7 @@ class TMDB {
/** /**
* Retrive list of upcmoing movies from TMDB. * Retrive list of upcmoing movies from TMDB.
* @param {Page} the page number to specify in the request for upcoming movies * @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 // TODO add filter for language
similar(identifier, type) { similar(identifier, type) {

View File

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