Movie and show can also return credits for a item. Enabled by query parameter credits=true

This commit is contained in:
2019-06-04 23:32:38 +02:00
parent e6796aff8b
commit 99bab3fb73
2 changed files with 43 additions and 43 deletions

View File

@@ -39,7 +39,7 @@ class TMDB {
.catch(() => this.tmdb(TMDB_METHODS['info'][type], query)) .catch(() => this.tmdb(TMDB_METHODS['info'][type], query))
.catch(() => { throw new Error('Could not find a movie with that id.'); }) .catch(() => { throw new Error('Could not find a movie with that id.'); })
.then(response => this.cache.set(cacheKey, response)) .then(response => this.cache.set(cacheKey, response))
.then((response) => { .then(response => {
try { try {
return convertTmdbToSeasoned(response, type); return convertTmdbToSeasoned(response, type);
} catch (parseError) { } catch (parseError) {
@@ -73,22 +73,22 @@ class TMDB {
* @param {String} type filter results by type (default movie). * @param {String} type filter results by type (default movie).
* @returns {Promise} succeeds if movie was found * @returns {Promise} succeeds if movie was found
*/ */
movieInfo(identifier) { movieInfo(identifier, credits=false) {
const query = { id: identifier }; 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() return Promise.resolve()
.then(() => this.cache.get(cacheKey)) .then(() => this.cache.get(cacheKey))
.catch(() => this.tmdb('movieInfo', query)) .catch(() => Promise.all(requests))
.catch((error) => { console.log(error); throw new Error('Could not find a movie with that id.'); }) .catch((error) => { console.log(error); throw new Error('Could not find a movie with that id.'); })
.then(response => this.cache.set(cacheKey, response)) .then(([movies, credits]) => this.cache.set(cacheKey, [movies, credits]))
.then((response) => { .then(([movies, credits]) => convertTmdbToMovie(movies, credits))
try {
return convertTmdbToMovie(response);
} catch (parseError) {
console.error(parseError);
throw new Error('Could not parse movie.');
}
});
} }
/** /**
@@ -97,22 +97,22 @@ class TMDB {
* @param {String} type filter results by type (default show). * @param {String} type filter results by type (default show).
* @returns {Promise} succeeds if show was found * @returns {Promise} succeeds if show was found
*/ */
showInfo(identifier) { showInfo(identifier, credits=false) {
const query = { id: identifier }; 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() return Promise.resolve()
.then(() => this.cache.get(cacheKey)) .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.'); }) .catch(() => { throw new Error('Could not find a show with that id.'); })
.then(response => this.cache.set(cacheKey, response)) .then(([shows, credits]) => this.cache.set(cacheKey, [shows, credits]))
.then((response) => { .then(([shows, credits]) => convertTmdbToShow(shows, credits))
try {
return convertTmdbToShow(response);
} catch (parseError) {
console.error(parseError);
throw new Error('Could not parse show.');
}
});
} }
/** /**
@@ -124,19 +124,14 @@ class TMDB {
personInfo(identifier) { personInfo(identifier) {
const query = { id: identifier }; const query = { id: identifier };
const cacheKey = `${this.cacheTags.personInfo}:${identifier}`; const cacheKey = `${this.cacheTags.personInfo}:${identifier}`;
return Promise.resolve() return Promise.resolve()
.then(() => this.cache.get(cacheKey)) .then(() => this.cache.get(cacheKey))
.catch(() => this.tmdb('personInfo', query)) .catch(() => Promise.all([this.tmdb('personInfo', query), this.tmdb('personCombinedCredits', query)]))
.catch(() => { throw new Error('Could not find a person with that id.'); }) .catch(() => { throw new Error('Could not find a person with that id.'); })
.then(response => this.cache.set(cacheKey, response)) .then(([person, cast]) => this.cache.set(cacheKey, [person, cast]))
.then((response) => { .then(([person, cast]) => convertTmdbToPerson(person, cast))
try { .catch(err => new Error('Unable to convert result to person', err))
return convertTmdbToPerson(response);
} catch (parseError) {
console.error(parseError);
throw new Error('Could not parse person.');
}
});
} }
@@ -161,6 +156,7 @@ class TMDB {
movieSearch(query, page=1) { movieSearch(query, page=1) {
const tmdbquery = { query: query, page: page }; const tmdbquery = { query: query, page: page };
const cacheKey = `${this.cacheTags.movieSearch}:${page}:${query}`; const cacheKey = `${this.cacheTags.movieSearch}:${page}:${query}`;
return Promise.resolve() return Promise.resolve()
.then(() => this.cache.get(cacheKey)) .then(() => this.cache.get(cacheKey))
.catch(() => this.tmdb('searchMovie', tmdbquery)) .catch(() => this.tmdb('searchMovie', tmdbquery))

View File

@@ -13,11 +13,15 @@ const plex = new Plex(configuration.get('plex', 'ip'));
* @returns {Callback} * @returns {Callback}
*/ */
function showInfoController(req, res) { async function showInfoController(req, res) {
const showId = req.params.id; const showId = req.params.id;
tmdb.showInfo(showId) const { credits } = req.query;
.then((show) => plex.existsInPlex(show)) const show = await tmdb.showInfo(showId, credits);
.then((show) => {
plex.existsInPlex(show)
.catch((error) => { console.log('Error when searching plex'); })
.then(() => {
console.log('show', show)
res.send(show); res.send(show);
}).catch((error) => { }).catch((error) => {
res.status(404).send({ success: false, error: error.message }); res.status(404).send({ success: false, error: error.message });