Implementing lists lookups for movie and shows. Add new cachetags for the lists & created a helper function for returning response with convertFunction as parameter.
This commit is contained in:
@@ -3,34 +3,25 @@ const convertTmdbToMovie = require('src/tmdb/convertTmdbToMovie');
|
|||||||
const convertTmdbToShow = require('src/tmdb/convertTmdbToShow');
|
const convertTmdbToShow = require('src/tmdb/convertTmdbToShow');
|
||||||
const convertTmdbToPerson = require('src/tmdb/convertTmdbToPerson');
|
const convertTmdbToPerson = require('src/tmdb/convertTmdbToPerson');
|
||||||
|
|
||||||
|
|
||||||
const TMDB_METHODS = {
|
|
||||||
upcoming: { movie: 'miscUpcomingMovies' },
|
|
||||||
discover: { movie: 'discoverMovie', show: 'discoverTv' },
|
|
||||||
popular: { movie: 'miscPopularMovies', show: 'miscPopularTvs' },
|
|
||||||
nowplaying: { movie: 'miscNowPlayingMovies', show: 'tvOnTheAir' },
|
|
||||||
similar: { movie: 'movieSimilar', show: 'tvSimilar' },
|
|
||||||
search: { movie: 'searchMovie', show: 'searchTv', multi: 'searchMulti' },
|
|
||||||
info: { movie: 'movieInfo', show: 'tvInfo' }
|
|
||||||
};
|
|
||||||
|
|
||||||
class TMDB {
|
class TMDB {
|
||||||
constructor(cache, apiKey, tmdbLibrary) {
|
constructor(cache, apiKey, tmdbLibrary) {
|
||||||
this.cache = cache;
|
this.cache = cache;
|
||||||
this.tmdbLibrary = tmdbLibrary || moviedb(apiKey);
|
this.tmdbLibrary = tmdbLibrary || moviedb(apiKey);
|
||||||
this.cacheTags = {
|
this.cacheTags = {
|
||||||
multiSearch: 'muse',
|
multiSearch: 'mus',
|
||||||
movieSearch: 'mose',
|
movieSearch: 'mos',
|
||||||
showSearch: 'sse',
|
showSearch: 'ss',
|
||||||
personSearch: 'pse',
|
personSearch: 'ps',
|
||||||
movieInfo: 'mi',
|
movieInfo: 'mi',
|
||||||
showInfo: 'si',
|
showInfo: 'si',
|
||||||
personInfo: 'pi',
|
personInfo: 'pi',
|
||||||
upcoming: 'u',
|
miscNowPlayingMovies: 'npm',
|
||||||
discover: 'd',
|
miscPopularMovies: 'pm',
|
||||||
popular: 'p',
|
miscTopRatedMovies: 'tpm',
|
||||||
nowplaying: 'n',
|
miscUpcomingMovies: 'um',
|
||||||
similar: 'sim',
|
tvOnTheAir: 'toa',
|
||||||
|
miscPopularTvs: 'pt',
|
||||||
|
miscTopRatedTvs: 'trt',
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -176,19 +167,8 @@ class TMDB {
|
|||||||
.catch(() => this.tmdb('searchMovie', tmdbquery))
|
.catch(() => this.tmdb('searchMovie', tmdbquery))
|
||||||
.catch(() => { throw new Error('Could not complete movie search to tmdb'); })
|
.catch(() => { throw new Error('Could not complete movie search to tmdb'); })
|
||||||
.then(response => this.cache.set(cacheKey, response))
|
.then(response => this.cache.set(cacheKey, response))
|
||||||
.then((response) => {
|
.then(response => this.mapAndCreateResponse(response, convertTmdbToMovie))
|
||||||
try {
|
.catch((error) => { console.log(error); throw new Error('Could not parse movie search result') })
|
||||||
return {
|
|
||||||
results: response.results.map(convertTmdbToMovie),
|
|
||||||
page: page,
|
|
||||||
total_results: response.total_results,
|
|
||||||
total_pages: response.total_pages
|
|
||||||
}
|
|
||||||
} catch (parseError) {
|
|
||||||
console.error(`ParseError: ${parseError}`);
|
|
||||||
throw new Error('Could not parse movie search results.')
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -205,19 +185,8 @@ class TMDB {
|
|||||||
.catch(() => this.tmdb('searchTv', tmdbquery))
|
.catch(() => this.tmdb('searchTv', tmdbquery))
|
||||||
.catch(() => { throw new Error('Could not complete show search to tmdb'); })
|
.catch(() => { throw new Error('Could not complete show search to tmdb'); })
|
||||||
.then(response => this.cache.set(cacheKey, response))
|
.then(response => this.cache.set(cacheKey, response))
|
||||||
.then((response) => {
|
.then(response => this.mapAndCreateResponse(response, convertTmdbToShow))
|
||||||
try {
|
.catch((error) => { console.log(error); throw new Error('Could not parse show search result') })
|
||||||
return {
|
|
||||||
results: response.results.map(convertTmdbToShow),
|
|
||||||
page: page,
|
|
||||||
total_results: response.total_results,
|
|
||||||
total_pages: response.total_pages
|
|
||||||
}
|
|
||||||
} catch (parseError) {
|
|
||||||
console.error(`ParseError: ${parseError}`);
|
|
||||||
throw new Error('Could not parse show search results.')
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -234,23 +203,42 @@ class TMDB {
|
|||||||
.catch(() => this.tmdb('searchPerson', tmdbquery))
|
.catch(() => this.tmdb('searchPerson', tmdbquery))
|
||||||
.catch(() => { throw new Error('Could not complete person search to tmdb'); })
|
.catch(() => { throw new Error('Could not complete person search to tmdb'); })
|
||||||
.then(response => this.cache.set(cacheKey, response))
|
.then(response => this.cache.set(cacheKey, response))
|
||||||
.then((response) => {
|
.then(response => this.mapAndCreateResponse(response, convertTmdbToPerson))
|
||||||
try {
|
.catch((error) => { console.log(error); throw new Error('Could not parse person search result') })
|
||||||
|
}
|
||||||
|
|
||||||
|
mapAndCreateResponse(response, resultConvertFunction) {
|
||||||
|
// console.log(response)
|
||||||
return {
|
return {
|
||||||
results: response.results.map(convertTmdbToPerson),
|
results: response.results.map(resultConvertFunction),
|
||||||
page: page,
|
page: response.page,
|
||||||
total_results: response.total_results,
|
total_results: response.total_results,
|
||||||
total_pages: response.total_pages
|
total_pages: response.total_pages
|
||||||
}
|
}
|
||||||
} catch (parseError) {
|
|
||||||
console.error(`ParseError: ${parseError}`);
|
|
||||||
throw new Error('Could not parse show search results.')
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
movieList(listname, page = 1) {
|
||||||
|
const query = { page: page };
|
||||||
|
const cacheKey = `${this.cacheTags[listname]}:${page}`;
|
||||||
|
return Promise.resolve()
|
||||||
|
.then(() => this.cache.get(cacheKey))
|
||||||
|
.catch(() => this.tmdb(listname, query))
|
||||||
|
.catch(() => { throw new Error('Unable to get movie list from tmdb')})
|
||||||
|
.then(response => this.cache.set(cacheKey, response))
|
||||||
|
.then(response => this.mapAndCreateResponse(response, convertTmdbToMovie));
|
||||||
|
}
|
||||||
|
|
||||||
|
showList(listname, page = 1) {
|
||||||
|
const query = { page: page };
|
||||||
|
const cacheKey = `${this.cacheTags[listname]}:${page}`;
|
||||||
|
return Promise.resolve()
|
||||||
|
.then(() => this.cache.get(cacheKey))
|
||||||
|
.catch(() => this.tmdb(listname, query))
|
||||||
|
.catch(() => { throw new Error('Unable to get show list from tmdb')})
|
||||||
|
.then(response => this.cache.set(cacheKey, response))
|
||||||
|
.then(response => this.mapAndCreateResponse(response, convertTmdbToShow));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Fetches a given list from tmdb.
|
* Fetches a given list from tmdb.
|
||||||
@@ -288,7 +276,7 @@ class TMDB {
|
|||||||
* @param {String} The type declared in listSearch.
|
* @param {String} The type declared in listSearch.
|
||||||
* @returns {Promise} dict with tmdb results, mapped as movie/show objects.
|
* @returns {Promise} dict with tmdb results, mapped as movie/show objects.
|
||||||
*/
|
*/
|
||||||
mapResults(response, type) {
|
mapResults(response, _) {
|
||||||
let results = response.results.map((result) => {
|
let results = response.results.map((result) => {
|
||||||
if (result.media_type === 'movie') {
|
if (result.media_type === 'movie') {
|
||||||
return convertTmdbToMovie(result);
|
return convertTmdbToMovie(result);
|
||||||
|
|||||||
Reference in New Issue
Block a user