This commit is contained in:
2022-03-06 11:59:32 +01:00
parent 5036f4ca36
commit bfd31ebd23
2 changed files with 64 additions and 64 deletions

View File

@@ -1,29 +1,35 @@
const moviedb = require('km-moviedb'); const moviedb = require("km-moviedb");
const RedisCache = require('src/cache/redis') const RedisCache = require("src/cache/redis");
const redisCache = new RedisCache() const redisCache = new RedisCache();
const { Movie, Show, Person, Credits, ReleaseDates } = require('src/tmdb/types'); const {
Movie,
Show,
Person,
Credits,
ReleaseDates
} = require("src/tmdb/types");
const tmdbErrorResponse = (error, typeString=undefined) => { const tmdbErrorResponse = (error, typeString = undefined) => {
if (error.status === 404) { if (error.status === 404) {
let message = error.response.body.status_message; let message = error.response.body.status_message;
throw { throw {
status: 404, status: 404,
message: message.slice(0, -1) + " in tmdb." message: message.slice(0, -1) + " in tmdb."
} };
} else if (error.status === 401) { } else if (error.status === 401) {
throw { throw {
status: 401, status: 401,
message: error.response.body.status_message message: error.response.body.status_message
} };
} }
throw { throw {
status: 500, status: 500,
message: `An unexpected error occured while fetching ${typeString} from tmdb` message: `An unexpected error occured while fetching ${typeString} from tmdb`
} };
} };
class TMDB { class TMDB {
constructor(apiKey, cache, tmdbLibrary) { constructor(apiKey, cache, tmdbLibrary) {
@@ -76,9 +82,9 @@ class TMDB {
const query = { id: identifier }; const query = { id: identifier };
const cacheKey = `tmdb/${this.cacheTags.movieInfo}:${identifier}`; const cacheKey = `tmdb/${this.cacheTags.movieInfo}:${identifier}`;
return this.getFromCacheOrFetchFromTmdb(cacheKey, 'movieInfo', query) return this.getFromCacheOrFetchFromTmdb(cacheKey, "movieInfo", query)
.then(movie => this.cache.set(cacheKey, movie, this.defaultTTL)) .then(movie => this.cache.set(cacheKey, movie, this.defaultTTL))
.then(movie => Movie.convertFromTmdbResponse(movie)) .then(movie => Movie.convertFromTmdbResponse(movie));
} }
/** /**
@@ -87,12 +93,12 @@ class TMDB {
* @returns {Promise} movie cast object * @returns {Promise} movie cast object
*/ */
movieCredits(identifier) { movieCredits(identifier) {
const query = { id: identifier } const query = { id: identifier };
const cacheKey = `tmdb/${this.cacheTags.movieCredits}:${identifier}` const cacheKey = `tmdb/${this.cacheTags.movieCredits}:${identifier}`;
return this.getFromCacheOrFetchFromTmdb(cacheKey, 'movieCredits', query) return this.getFromCacheOrFetchFromTmdb(cacheKey, "movieCredits", query)
.then(credits => this.cache.set(cacheKey, credits, this.defaultTTL)) .then(credits => this.cache.set(cacheKey, credits, this.defaultTTL))
.then(credits => Credits.convertFromTmdbResponse(credits)) .then(credits => Credits.convertFromTmdbResponse(credits));
} }
/** /**
@@ -119,18 +125,18 @@ class TMDB {
const query = { id: identifier }; const query = { id: identifier };
const cacheKey = `tmdb/${this.cacheTags.showInfo}:${identifier}`; const cacheKey = `tmdb/${this.cacheTags.showInfo}:${identifier}`;
return this.getFromCacheOrFetchFromTmdb(cacheKey, 'tvInfo', query) return this.getFromCacheOrFetchFromTmdb(cacheKey, "tvInfo", query)
.then(show => this.cache.set(cacheKey, show, this.defaultTTL)) .then(show => this.cache.set(cacheKey, show, this.defaultTTL))
.then(show => Show.convertFromTmdbResponse(show)) .then(show => Show.convertFromTmdbResponse(show));
} }
showCredits(identifier) { showCredits(identifier) {
const query = { id: identifier } const query = { id: identifier };
const cacheKey = `tmdb/${this.cacheTags.showCredits}:${identifier}` const cacheKey = `tmdb/${this.cacheTags.showCredits}:${identifier}`;
return this.getFromCacheOrFetchFromTmdb(cacheKey, 'tvCredits', query) return this.getFromCacheOrFetchFromTmdb(cacheKey, "tvCredits", query)
.then(credits => this.cache.set(cacheKey, credits, this.defaultTTL)) .then(credits => this.cache.set(cacheKey, credits, this.defaultTTL))
.then(credits => Credits.convertFromTmdbResponse(credits)) .then(credits => Credits.convertFromTmdbResponse(credits));
} }
/** /**
@@ -143,9 +149,9 @@ class TMDB {
const query = { id: identifier }; const query = { id: identifier };
const cacheKey = `tmdb/${this.cacheTags.personInfo}:${identifier}`; const cacheKey = `tmdb/${this.cacheTags.personInfo}:${identifier}`;
return this.getFromCacheOrFetchFromTmdb(cacheKey, 'personInfo', query) return this.getFromCacheOrFetchFromTmdb(cacheKey, "personInfo", query)
.then(person => this.cache.set(cacheKey, person, this.defaultTTL)) .then(person => this.cache.set(cacheKey, person, this.defaultTTL))
.then(person => Person.convertFromTmdbResponse(person)) .then(person => Person.convertFromTmdbResponse(person));
} }
personCredits(identifier) { personCredits(identifier) {
@@ -221,7 +227,7 @@ class TMDB {
return this.getFromCacheOrFetchFromTmdb(cacheKey, listname, query) return this.getFromCacheOrFetchFromTmdb(cacheKey, listname, query)
.then(response => this.cache.set(cacheKey, response, this.defaultTTL)) .then(response => this.cache.set(cacheKey, response, this.defaultTTL))
.then(response => this.mapResults(response, 'movie')) .then(response => this.mapResults(response, "movie"));
} }
showList(listname, page = 1) { showList(listname, page = 1) {
@@ -230,7 +236,7 @@ class TMDB {
return this.getFromCacheOrFetchFromTmdb(cacheKey, listName, query) return this.getFromCacheOrFetchFromTmdb(cacheKey, listName, query)
.then(response => this.cache.set(cacheKey, response, this.defaultTTL)) .then(response => this.cache.set(cacheKey, response, this.defaultTTL))
.then(response => this.mapResults(response, 'show')) .then(response => this.mapResults(response, "show"));
} }
/** /**
@@ -239,27 +245,26 @@ 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=undefined) { mapResults(response, type = undefined) {
let results = response.results.map(result => { let results = response.results.map(result => {
if (type === 'movie' || result.media_type === 'movie') { if (type === "movie" || result.media_type === "movie") {
const movie = Movie.convertFromTmdbResponse(result) const movie = Movie.convertFromTmdbResponse(result);
return movie.createJsonResponse() return movie.createJsonResponse();
} else if (type === 'show' || result.media_type === 'tv') { } else if (type === "show" || result.media_type === "tv") {
const show = Show.convertFromTmdbResponse(result) const show = Show.convertFromTmdbResponse(result);
return show.createJsonResponse() return show.createJsonResponse();
} else if (type === 'person' || result.media_type === 'person') { } else if (type === "person" || result.media_type === "person") {
const person = Person.convertFromTmdbResponse(result) const person = Person.convertFromTmdbResponse(result);
return person.createJsonResponse() return person.createJsonResponse();
} }
}) });
return { return {
results: results, results: results,
page: response.page, page: response.page,
total_results: response.total_results, total_results: response.total_results,
total_pages: response.total_pages total_pages: response.total_pages
} };
} }
/** /**
@@ -284,9 +289,6 @@ class TMDB {
} }
}); });
} }
} }
module.exports = TMDB; module.exports = TMDB;

View File

@@ -142,19 +142,17 @@ router.get("/v2/movie/now_playing", listController.nowPlayingMovies);
router.get("/v2/movie/popular", listController.popularMovies); router.get("/v2/movie/popular", listController.popularMovies);
router.get("/v2/movie/top_rated", listController.topRatedMovies); router.get("/v2/movie/top_rated", listController.topRatedMovies);
router.get("/v2/movie/upcoming", listController.upcomingMovies); router.get("/v2/movie/upcoming", listController.upcomingMovies);
router.get("/v2/show/now_playing", listController.nowPlayingShows);
router.get("/v2/show/popular", listController.popularShows);
router.get("/v2/show/top_rated", listController.topRatedShows);
router.get("/v2/movie/:id/credits", require("./controllers/movie/credits.js")); router.get("/v2/movie/:id/credits", require("./controllers/movie/credits.js"));
router.get( router.get(
"/v2/movie/:id/release_dates", "/v2/movie/:id/release_dates",
require("./controllers/movie/releaseDates.js") require("./controllers/movie/releaseDates.js")
); );
router.get("/v2/show/:id/credits", require("./controllers/show/credits.js"));
router.get("/v2/movie/:id", require("./controllers/movie/info.js")); router.get("/v2/movie/:id", require("./controllers/movie/info.js"));
router.get("/v2/show/now_playing", listController.nowPlayingShows);
router.get("/v2/show/popular", listController.popularShows);
router.get("/v2/show/top_rated", listController.topRatedShows);
router.get("/v2/show/:id/credits", require("./controllers/show/credits.js"));
router.get("/v2/show/:id", require("./controllers/show/info.js")); router.get("/v2/show/:id", require("./controllers/show/info.js"));
router.get( router.get(