Fixed issue with types not being upheld when converting from tmdb objects.

This commit is contained in:
2018-02-07 15:40:39 +01:00
parent 28a731efbf
commit ad4fa9d95a
2 changed files with 10 additions and 4 deletions

View File

@@ -9,10 +9,15 @@ function translateGenre(tmdbGenres) {
return tmdbGenres.map(genre => genre.name); return tmdbGenres.map(genre => genre.name);
} }
function convertType(tmdbType) {
if (tmdbType === 'tv') return 'show';
return undefined;
}
function convertTmdbToSeasoned(tmdb, manualType = undefined) { function convertTmdbToSeasoned(tmdb, manualType = undefined) {
const title = tmdb.title || tmdb.name; const title = tmdb.title || tmdb.name;
const year = translateYear(tmdb.release_date || tmdb.first_air_date); const year = translateYear(tmdb.release_date || tmdb.first_air_date);
const type = tmdb.media_type || manualType; const type = manualType || convertType(tmdb.media_type) || 'movie';
const id = tmdb.id; const id = tmdb.id;
const summary = tmdb.overview; const summary = tmdb.overview;

View File

@@ -12,7 +12,6 @@ const TMDB_METHODS = {
}; };
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);
@@ -63,7 +62,7 @@ class TMDB {
.catch(() => this.tmdb(this.tmdbMethod('search', type), query)) .catch(() => this.tmdb(this.tmdbMethod('search', type), query))
.catch(() => { throw new Error('Could not search for movies/shows at tmdb.'); }) .catch(() => { throw new Error('Could not search for movies/shows at tmdb.'); })
.then(response => this.cache.set(cacheKey, response)) .then(response => this.cache.set(cacheKey, response))
.then(response => this.mapResults(response, type)) .then(response => this.mapResults(response))
.catch((error) => { throw new Error(error); }) .catch((error) => { throw new Error(error); })
.then(([mappedResults, pagenumber, totalpages, total_results]) => ({ .then(([mappedResults, pagenumber, totalpages, total_results]) => ({
results: mappedResults, page: pagenumber, total_results, total_pages: totalpages, results: mappedResults, page: pagenumber, total_results, total_pages: totalpages,
@@ -107,7 +106,9 @@ class TMDB {
mapResults(response, type) { mapResults(response, type) {
return Promise.resolve() return Promise.resolve()
.then(() => { .then(() => {
const mappedResults = response.results.map(result => convertTmdbToSeasoned(result, type)); const mappedResults = response.results.filter((element) => {
return (element.type === 'movie' || element.type === 'tv');
}).map((element) => convertTmdbToSeasoned(element, type));
return [mappedResults, response.page, response.total_pages, response.total_results]; return [mappedResults, response.page, response.total_pages, response.total_results];
}) })
.catch((error) => { throw new Error(error); }); .catch((error) => { throw new Error(error); });