Rewrote the search function in tmdb to now return a JSON object with results: (all movie/show objects), number_of_items: (count of result objects), page and total page. Split up the filtering and converting of the result from tmdb api to finished seasoned objects for better readability.

This commit is contained in:
2017-09-20 10:21:57 +02:00
parent c187ce7515
commit 72d50209a5

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) {