Now tmdb searches also check cache for a matching query request, if so returns it.
This commit is contained in:
@@ -7,8 +7,19 @@ var methodTypes = { 'movie': 'searchMovie', 'show': 'searchTv', 'multi': 'search
|
|||||||
'showSimilar': 'tvSimilar' };
|
'showSimilar': 'tvSimilar' };
|
||||||
|
|
||||||
class TMDB {
|
class TMDB {
|
||||||
constructor(apiKey, tmdbLibrary) {
|
constructor(cache, apiKey, tmdbLibrary) {
|
||||||
|
this.cache = cache
|
||||||
this.tmdbLibrary = tmdbLibrary || moviedb(apiKey);
|
this.tmdbLibrary = tmdbLibrary || moviedb(apiKey);
|
||||||
|
this.cacheTags = {
|
||||||
|
'search': 'se',
|
||||||
|
'discover': 'd',
|
||||||
|
'popular': 'p',
|
||||||
|
'nowplaying': 'n',
|
||||||
|
'upcoming': 'u',
|
||||||
|
'similar': 'si',
|
||||||
|
'lookup': 'l'
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -16,22 +27,21 @@ class TMDB {
|
|||||||
* @param {queryText, page, type} the page number to specify in the request for discover,
|
* @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
|
* @returns {Promise} dict with query results, current page and total_pages
|
||||||
*/
|
*/
|
||||||
search(queryText, page = 1, type = 'multi') {
|
search(text, page = 1, type = 'multi') {
|
||||||
// Setup query object for tmdb api search
|
const query = { 'query': text, 'page': page };
|
||||||
const query = { 'query': queryText, 'page': page };
|
const cacheKey = `${this.cacheTags.search}${page}:${type}:${text}`;
|
||||||
return Promise.resolve()
|
return Promise.resolve()
|
||||||
.then(() => this.tmdb(type, query)) // Search the tmdb api
|
.then(() => this.cache.get(cacheKey))
|
||||||
.catch(() => { throw new Error('Could not search for movies.'); }) // If any error at all when fetching
|
.catch(() => this.tmdb(type, query))
|
||||||
|
.catch(() => { throw new Error('Could not search for movies/shows at tmdb.'); })
|
||||||
|
.then((response) => this.cache.set(cacheKey, response))
|
||||||
.then((response) => {
|
.then((response) => {
|
||||||
try {
|
try {
|
||||||
// We want to filter because there are movies really low rated that are not interesting to us.
|
|
||||||
let filteredTmdbItems = response.results.filter(function(tmdbResultItem) {
|
let filteredTmdbItems = response.results.filter(function(tmdbResultItem) {
|
||||||
return ((tmdbResultItem.vote_count >= 40 || tmdbResultItem.popularity > 8) && (tmdbResultItem.release_date !== undefined || tmdbResultItem.first_air_date !== undefined))
|
return ((tmdbResultItem.vote_count >= 40 || tmdbResultItem.popularity > 8) && (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) => {
|
let seasonedItems = filteredTmdbItems.map((tmdbItem) => {
|
||||||
|
|
||||||
if (type === 'movie')
|
if (type === 'movie')
|
||||||
return convertTmdbToSeasoned(tmdbItem, 'movie');
|
return convertTmdbToSeasoned(tmdbItem, 'movie');
|
||||||
else if (type === 'show')
|
else if (type === 'show')
|
||||||
@@ -45,7 +55,6 @@ class TMDB {
|
|||||||
'page': 1, 'total_pages': 1 };
|
'page': 1, 'total_pages': 1 };
|
||||||
|
|
||||||
} catch (parseError) {
|
} catch (parseError) {
|
||||||
console.log(parseError)
|
|
||||||
throw new Error('Could not parse result.');
|
throw new Error('Could not parse result.');
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user