From 99bab3fb73b2938b378fa61cdc6dfda77ec7c86a Mon Sep 17 00:00:00 2001 From: KevinMidboe Date: Tue, 4 Jun 2019 23:32:38 +0200 Subject: [PATCH] Movie and show can also return credits for a item. Enabled by query parameter credits=true --- seasoned_api/src/tmdb/tmdb.js | 74 +++++++++---------- .../webserver/controllers/info/showInfo.js | 12 ++- 2 files changed, 43 insertions(+), 43 deletions(-) diff --git a/seasoned_api/src/tmdb/tmdb.js b/seasoned_api/src/tmdb/tmdb.js index e1016f8..37011a8 100644 --- a/seasoned_api/src/tmdb/tmdb.js +++ b/seasoned_api/src/tmdb/tmdb.js @@ -39,7 +39,7 @@ class TMDB { .catch(() => this.tmdb(TMDB_METHODS['info'][type], query)) .catch(() => { throw new Error('Could not find a movie with that id.'); }) .then(response => this.cache.set(cacheKey, response)) - .then((response) => { + .then(response => { try { return convertTmdbToSeasoned(response, type); } catch (parseError) { @@ -73,22 +73,22 @@ class TMDB { * @param {String} type filter results by type (default movie). * @returns {Promise} succeeds if movie was found */ - movieInfo(identifier) { + movieInfo(identifier, credits=false) { const query = { id: identifier }; - const cacheKey = `${this.cacheTags.movieInfo}:${identifier}`; + const cacheKey = `${this.cacheTags.movieInfo}:${identifier}:${credits}`; + + const requests = [this.tmdb('movieInfo', query)] + + if (credits) { + requests.push(this.tmdb('movieCredits', query)) + } + return Promise.resolve() - .then(() => this.cache.get(cacheKey)) - .catch(() => this.tmdb('movieInfo', query)) - .catch((error) => { console.log(error); throw new Error('Could not find a movie with that id.'); }) - .then(response => this.cache.set(cacheKey, response)) - .then((response) => { - try { - return convertTmdbToMovie(response); - } catch (parseError) { - console.error(parseError); - throw new Error('Could not parse movie.'); - } - }); + .then(() => this.cache.get(cacheKey)) + .catch(() => Promise.all(requests)) + .catch((error) => { console.log(error); throw new Error('Could not find a movie with that id.'); }) + .then(([movies, credits]) => this.cache.set(cacheKey, [movies, credits])) + .then(([movies, credits]) => convertTmdbToMovie(movies, credits)) } /** @@ -97,22 +97,22 @@ class TMDB { * @param {String} type filter results by type (default show). * @returns {Promise} succeeds if show was found */ - showInfo(identifier) { + showInfo(identifier, credits=false) { const query = { id: identifier }; - const cacheKey = `${this.cacheTags.showInfo}:${identifier}`; + const cacheKey = `${this.cacheTags.showInfo}:${identifier}:${credits}`; + + const requests = [this.tmdb('tvInfo', query)] + + if (credits) { + requests.push(this.tmdb('tvCredits', query)) + } + return Promise.resolve() .then(() => this.cache.get(cacheKey)) - .catch(() => this.tmdb('tvInfo', query)) + .catch(() => Promise.all(requests)) .catch(() => { throw new Error('Could not find a show with that id.'); }) - .then(response => this.cache.set(cacheKey, response)) - .then((response) => { - try { - return convertTmdbToShow(response); - } catch (parseError) { - console.error(parseError); - throw new Error('Could not parse show.'); - } - }); + .then(([shows, credits]) => this.cache.set(cacheKey, [shows, credits])) + .then(([shows, credits]) => convertTmdbToShow(shows, credits)) } /** @@ -124,19 +124,14 @@ class TMDB { personInfo(identifier) { const query = { id: identifier }; const cacheKey = `${this.cacheTags.personInfo}:${identifier}`; + return Promise.resolve() - .then(() => this.cache.get(cacheKey)) - .catch(() => this.tmdb('personInfo', query)) - .catch(() => { throw new Error('Could not find a person with that id.'); }) - .then(response => this.cache.set(cacheKey, response)) - .then((response) => { - try { - return convertTmdbToPerson(response); - } catch (parseError) { - console.error(parseError); - throw new Error('Could not parse person.'); - } - }); + .then(() => this.cache.get(cacheKey)) + .catch(() => Promise.all([this.tmdb('personInfo', query), this.tmdb('personCombinedCredits', query)])) + .catch(() => { throw new Error('Could not find a person with that id.'); }) + .then(([person, cast]) => this.cache.set(cacheKey, [person, cast])) + .then(([person, cast]) => convertTmdbToPerson(person, cast)) + .catch(err => new Error('Unable to convert result to person', err)) } @@ -161,6 +156,7 @@ class TMDB { movieSearch(query, page=1) { const tmdbquery = { query: query, page: page }; const cacheKey = `${this.cacheTags.movieSearch}:${page}:${query}`; + return Promise.resolve() .then(() => this.cache.get(cacheKey)) .catch(() => this.tmdb('searchMovie', tmdbquery)) diff --git a/seasoned_api/src/webserver/controllers/info/showInfo.js b/seasoned_api/src/webserver/controllers/info/showInfo.js index f345855..4cbc5a9 100644 --- a/seasoned_api/src/webserver/controllers/info/showInfo.js +++ b/seasoned_api/src/webserver/controllers/info/showInfo.js @@ -13,11 +13,15 @@ const plex = new Plex(configuration.get('plex', 'ip')); * @returns {Callback} */ -function showInfoController(req, res) { +async function showInfoController(req, res) { const showId = req.params.id; - tmdb.showInfo(showId) - .then((show) => plex.existsInPlex(show)) - .then((show) => { + const { credits } = req.query; + const show = await tmdb.showInfo(showId, credits); + + plex.existsInPlex(show) + .catch((error) => { console.log('Error when searching plex'); }) + .then(() => { + console.log('show', show) res.send(show); }).catch((error) => { res.status(404).send({ success: false, error: error.message });