Merge pull request #83 from KevinMidboe/api

Api
This commit is contained in:
2018-02-07 15:56:40 +01:00
committed by GitHub
3 changed files with 13 additions and 7 deletions

View File

@@ -23,7 +23,7 @@ class PlexRepository {
.then(([mappedResults, resultCount]) => ({ results: mappedResults, total_results: resultCount })); .then(([mappedResults, resultCount]) => ({ results: mappedResults, total_results: resultCount }));
} }
static compareTmdbToPlex(tmdb, plexResult) { compareTmdbToPlex(tmdb, plexResult) {
return Promise.resolve() return Promise.resolve()
.then(() => { .then(() => {
plexResult.results.map((plexItem) => { plexResult.results.map((plexItem) => {
@@ -34,7 +34,7 @@ class PlexRepository {
}); });
} }
static mapResults(response) { mapResults(response) {
return Promise.resolve() return Promise.resolve()
.then(() => { .then(() => {
if (!response.MediaContainer.hasOwnProperty('Metadata')) return [[], 0]; if (!response.MediaContainer.hasOwnProperty('Metadata')) return [[], 0];

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;
@@ -29,7 +34,7 @@ function convertTmdbToSeasoned(tmdb, manualType = undefined) {
const seasoned = new TMDB( const seasoned = new TMDB(
title, year, type, id, summary, poster_path, background_path, title, year, type, id, summary, poster_path, background_path,
popularity, score, release_status, tagline, seasons, episodes, popularity, score, release_status, tagline, seasons, episodes
); );
// seasoned.print() // seasoned.print()

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.media_type === 'movie' || element.media_type === 'tv' || element.media_type === undefined);
}).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); });