Moved, renamed, re-did and added a lot of stuff. Getting ready for the v2 upgrade
This commit is contained in:
@@ -18,12 +18,12 @@ class Cache {
|
||||
* @returns {Object}
|
||||
*/
|
||||
get(key) {
|
||||
return Promise.resolve()
|
||||
.then(() => this.database.get(this.queries.read, [key]))
|
||||
.then((row) => {
|
||||
assert(row, 'Could not find cache enrty with that key.');
|
||||
return JSON.parse(row.value);
|
||||
});
|
||||
return Promise.resolve()
|
||||
.then(() => this.database.get(this.queries.read, [key]))
|
||||
.then(row => {
|
||||
assert(row, 'Could not find cache entry with that key.');
|
||||
return JSON.parse(row.value);
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,71 +0,0 @@
|
||||
import { Movie } from './types'
|
||||
|
||||
const tmdbSwitcher = (tmdbMovie, property) => tmdbMovie[property]
|
||||
|
||||
const releaseTypeEnum = {
|
||||
1: 'Premier',
|
||||
2: 'Limited theatrical',
|
||||
3: 'Theatrical',
|
||||
4: 'Digital',
|
||||
5: 'Physical',
|
||||
6: 'TV'
|
||||
}
|
||||
|
||||
function convertTmdbToMovie(tmdbMovie, credits=undefined, releaseDates=undefined) {
|
||||
const movie = new Movie(tmdbMovie.id, tmdbMovie.title)
|
||||
movie.overview = tmdbMovie.overview;
|
||||
movie.rank = tmdbMovie.vote_average;
|
||||
|
||||
if (credits) {
|
||||
movie.credits = { cast: credits.cast, crew: credits.crew };
|
||||
}
|
||||
|
||||
if (releaseDates) {
|
||||
movie.release_dates = releaseDates.results.map((releasePlace) => {
|
||||
const newestRelease = releasePlace.release_dates.sort((a,b) => a.type < b.type ? 1 : -1)[0]
|
||||
const type = releaseTypeEnum[newestRelease.type]
|
||||
|
||||
return {
|
||||
country: releasePlace.iso_3166_1,
|
||||
type: type,
|
||||
date: newestRelease.release_date
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
if (tmdbMovie.release_date !== undefined && tmdbMovie.release_date) {
|
||||
movie.release_date = new Date(tmdbMovie.release_date);
|
||||
movie.year = movie.release_date.getFullYear();
|
||||
}
|
||||
|
||||
if (tmdbMovie.poster_path !== undefined && tmdbMovie.poster_path) {
|
||||
movie.poster = tmdbMovie.poster_path;
|
||||
}
|
||||
if (tmdbMovie.backdrop_path !== undefined && tmdbMovie.backdrop_path) {
|
||||
movie.backdrop = tmdbMovie.backdrop_path;
|
||||
}
|
||||
|
||||
if (tmdbMovie.status !== undefined && tmdbMovie.status) {
|
||||
movie.status = tmdbMovie.status;
|
||||
}
|
||||
|
||||
if (tmdbMovie.genres !== undefined && tmdbMovie.genres) {
|
||||
movie.genres = tmdbMovie.genres.map(genre => genre.name);
|
||||
}
|
||||
|
||||
if (tmdbMovie.tagline !== undefined && tmdbMovie.tagline) {
|
||||
movie.tagline = tmdbMovie.tagline;
|
||||
}
|
||||
|
||||
if (tmdbMovie.runtime !== undefined && tmdbMovie.runtime) {
|
||||
movie.runtime = tmdbMovie.runtime;
|
||||
}
|
||||
|
||||
if (tmdbMovie.imdb_id !== undefined && tmdbMovie.imdb_id) {
|
||||
movie.imdb_id = tmdbMovie.imdb_id;
|
||||
}
|
||||
|
||||
return movie;
|
||||
}
|
||||
|
||||
module.exports = convertTmdbToMovie;
|
||||
@@ -1,30 +0,0 @@
|
||||
import { Person } from './types'
|
||||
const convertTmdbToMovie = require('src/tmdb/convertTmdbToMovie');
|
||||
|
||||
function convertTmdbToPerson(tmdbPerson, cast=undefined) {
|
||||
const person = new Person(tmdbPerson.id, tmdbPerson.name);
|
||||
|
||||
if (tmdbPerson.profile_path !== undefined) {
|
||||
person.poster = tmdbPerson.profile_path;
|
||||
}
|
||||
|
||||
if (tmdbPerson.birthday !== undefined) {
|
||||
person.birthday = new Date(tmdbPerson.birthday);
|
||||
}
|
||||
|
||||
if (tmdbPerson.deathday !== undefined) {
|
||||
person.deathday = tmdbPerson.deathday;
|
||||
}
|
||||
|
||||
if (tmdbPerson.known_for !== undefined) {
|
||||
person.known_for = tmdbPerson.known_for.map(convertTmdbToMovie);
|
||||
}
|
||||
|
||||
if (cast) {
|
||||
person.cast = cast.cast;
|
||||
}
|
||||
|
||||
return person;
|
||||
}
|
||||
|
||||
module.exports = convertTmdbToPerson;
|
||||
@@ -1,43 +0,0 @@
|
||||
const TMDB = require('src/media_classes/tmdb');
|
||||
|
||||
function translateYear(tmdbReleaseDate) {
|
||||
return new Date(tmdbReleaseDate).getFullYear();
|
||||
}
|
||||
|
||||
function translateGenre(tmdbGenres) {
|
||||
return tmdbGenres.map(genre => genre.name);
|
||||
}
|
||||
|
||||
function convertType(tmdbType) {
|
||||
if (tmdbType === 'tv') return 'show';
|
||||
return undefined;
|
||||
}
|
||||
|
||||
function convertTmdbToMovie(tmdb) {
|
||||
const title = tmdb.title || tmdb.name;
|
||||
const year = translateYear(tmdb.release_date || tmdb.first_air_date);
|
||||
const type = manualType || convertType(tmdb.type) || 'movie';
|
||||
|
||||
const id = tmdb.id;
|
||||
const summary = tmdb.overview;
|
||||
const poster_path = tmdb.poster_path;
|
||||
const background_path = tmdb.backdrop_path;
|
||||
const popularity = tmdb.popularity;
|
||||
const score = tmdb.vote_average;
|
||||
// const genres = translateGenre(tmdb.genres);
|
||||
const release_status = tmdb.status;
|
||||
const tagline = tmdb.tagline;
|
||||
|
||||
const seasons = tmdb.number_of_seasons;
|
||||
const episodes = tmdb.episodes;
|
||||
|
||||
const seasoned = new TMDB(
|
||||
title, year, type, id, summary, poster_path, background_path,
|
||||
popularity, score, release_status, tagline, seasons, episodes
|
||||
);
|
||||
|
||||
// seasoned.print()
|
||||
return seasoned;
|
||||
}
|
||||
|
||||
module.exports = convertTmdbToSeasoned;
|
||||
@@ -1,41 +0,0 @@
|
||||
import { Show } from './types'
|
||||
|
||||
function convertTmdbToShow(tmdbShow, credits=undefined) {
|
||||
const show = new Show(tmdbShow.id, tmdbShow.name)
|
||||
show.seasons = tmdbShow.number_of_seasons;
|
||||
show.episodes = tmdbShow.number_of_episodes;
|
||||
show.overview = tmdbShow.overview;
|
||||
show.rank = tmdbShow.vote_average;
|
||||
|
||||
if (credits) {
|
||||
show.credits = credits
|
||||
}
|
||||
|
||||
if (tmdbShow.genres !== undefined) {
|
||||
show.genres = tmdbShow.genres.map(genre => genre.name);
|
||||
}
|
||||
|
||||
if (tmdbShow.first_air_date !== undefined) {
|
||||
show.first_air_date = new Date(tmdbShow.first_air_date);
|
||||
show.year = show.first_air_date.getFullYear();
|
||||
}
|
||||
|
||||
if (tmdbShow.poster_path !== undefined) {
|
||||
show.poster = tmdbShow.poster_path;
|
||||
}
|
||||
if (tmdbShow.backdrop_path !== undefined) {
|
||||
show.backdrop = tmdbShow.backdrop_path;
|
||||
}
|
||||
|
||||
if (tmdbShow.status !== undefined) {
|
||||
show.status = tmdbShow.status;
|
||||
}
|
||||
|
||||
if (tmdbShow.episode_run_time !== undefined) {
|
||||
show.runtime = tmdbShow.runtime;
|
||||
}
|
||||
|
||||
return show;
|
||||
}
|
||||
|
||||
module.exports = convertTmdbToShow;
|
||||
@@ -1,9 +1,6 @@
|
||||
const moviedb = require('km-moviedb');
|
||||
const convertTmdbToMovie = require('src/tmdb/convertTmdbToMovie');
|
||||
const convertTmdbToShow = require('src/tmdb/convertTmdbToShow');
|
||||
const convertTmdbToPerson = require('src/tmdb/convertTmdbToPerson');
|
||||
|
||||
const { Credits, ReleaseDates } = require('src/tmdb/types');
|
||||
const { Movie, Show, Person, Credits, ReleaseDates } = require('src/tmdb/types');
|
||||
// const { tmdbInfo } = require('src/tmdb/types')
|
||||
|
||||
class TMDB {
|
||||
@@ -18,7 +15,8 @@ class TMDB {
|
||||
movieInfo: 'mi',
|
||||
movieCredits: 'mc',
|
||||
movieReleaseDates: 'mrd',
|
||||
showInfo: 'si',
|
||||
showInfo: 'si',
|
||||
showCredits: 'sc',
|
||||
personInfo: 'pi',
|
||||
miscNowPlayingMovies: 'npm',
|
||||
miscPopularMovies: 'pm',
|
||||
@@ -30,123 +28,22 @@ class TMDB {
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve a specific movie by id from TMDB.
|
||||
* @param {Number} identifier of the movie you want to retrieve
|
||||
* @param {String} type filter results by type (default movie).
|
||||
* @returns {Promise} succeeds if movie was found
|
||||
*/
|
||||
lookup(identifier, type = 'movie') {
|
||||
const query = { id: identifier };
|
||||
const cacheKey = `${this.cacheTags.info}:${type}:${identifier}`;
|
||||
return Promise.resolve()
|
||||
.then(() => this.cache.get(cacheKey))
|
||||
.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 => {
|
||||
try {
|
||||
return convertTmdbToSeasoned(response, type);
|
||||
} catch (parseError) {
|
||||
console.error(parseError);
|
||||
throw new Error('Could not parse movie.');
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrive search results from TMDB.
|
||||
* @param {String} text query you want to search for
|
||||
* @param {Number} page representing pagination of results
|
||||
* @param {String} type filter results by type (default multi)
|
||||
* @returns {Promise} dict with query results, current page and total_pages
|
||||
*/
|
||||
search(text, page = 1, type = 'multi') {
|
||||
const query = { query: text, page: page };
|
||||
const cacheKey = `${this.cacheTags.search}:${page}:${type}:${text}`;
|
||||
return Promise.resolve()
|
||||
.then(() => this.cache.get(cacheKey))
|
||||
.catch(() => this.tmdb(TMDB_METHODS['search'][type], query))
|
||||
.catch(() => { throw new Error('Could not search for movies/shows at tmdb.'); })
|
||||
.then(response => this.cache.set(cacheKey, response))
|
||||
.then(response => this.mapResults(response));
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve a specific movie by id from TMDB.
|
||||
* @param {Number} identifier of the movie you want to retrieve
|
||||
* @param {String} type filter results by type (default movie).
|
||||
* @param {Boolean} add credits (cast & crew) for movie
|
||||
* @param {Boolean} add release dates for every country
|
||||
* @returns {Promise} succeeds if movie was found
|
||||
*/
|
||||
movieInfo(identifier, credits=false, releaseDates=false) {
|
||||
movieInfo(identifier) {
|
||||
const query = { id: identifier };
|
||||
const cacheKey = `${this.cacheTags.movieInfo}:${identifier}:${credits}`;
|
||||
const cacheKey = `${this.cacheTags.movieInfo}:${identifier}`;
|
||||
|
||||
const requests = [this.tmdb('movieInfo', query)]
|
||||
|
||||
if (credits) {
|
||||
requests.push(this.tmdb('movieCredits', query))
|
||||
} else {
|
||||
// This is because we expect ordered parameters below
|
||||
requests.push(Promise.resolve([]))
|
||||
}
|
||||
|
||||
if (releaseDates) {
|
||||
requests.push(this.tmdb('movieReleaseDates', query))
|
||||
}
|
||||
|
||||
return Promise.resolve()
|
||||
.then(() => this.cache.get(cacheKey))
|
||||
.catch(() => Promise.all(requests))
|
||||
.catch(error => {
|
||||
if (error.status === 401) {
|
||||
throw new Error('Unathorized tmdb request, please check api key.')
|
||||
} else if (error.status === 404) {
|
||||
throw new Error(`Could not find a movie with id: ${identifier}`)
|
||||
}
|
||||
|
||||
throw new Error('Unexpected error has occured:', error.message)
|
||||
})
|
||||
.then(([movies, credits, releaseDates]) => this.cache.set(cacheKey, [movies, credits, releaseDates]))
|
||||
.then(([movies, credits, releaseDates]) => convertTmdbToMovie(movies, credits, releaseDates))
|
||||
}
|
||||
|
||||
tmdbCreditsError(error) {
|
||||
if (error.status === 404) {
|
||||
throw {
|
||||
status: 404,
|
||||
message: error.response.body.status_message
|
||||
}
|
||||
} else if (error.status === 401) {
|
||||
throw {
|
||||
status: 401,
|
||||
message: error.response.body.status_message
|
||||
}
|
||||
}
|
||||
|
||||
throw {
|
||||
status: 500,
|
||||
message: 'An unexpected error occured while fetching credits from tmdb'
|
||||
}
|
||||
}
|
||||
|
||||
tmdbRelaseDatesError(error) {
|
||||
if (error.status === 404) {
|
||||
throw {
|
||||
status: 404,
|
||||
message: error.response.body.status_message
|
||||
}
|
||||
} else if (error.status === 401) {
|
||||
throw {
|
||||
status: 401,
|
||||
message: error.response.body.status_message
|
||||
}
|
||||
}
|
||||
|
||||
throw {
|
||||
status: 500,
|
||||
message: 'An unexpected error occured while fetching release dates from tmdb'
|
||||
}
|
||||
return this.cache.get(cacheKey)
|
||||
.catch(() => this.tmdb('movieInfo', query))
|
||||
.catch(tmdbError => tmdbErrorResponse(tmdbError, 'movie info'))
|
||||
.then(movie => this.cache.set(cacheKey, movie, 1))
|
||||
.then(movie => Movie.convertFromTmdbResponse(movie))
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -160,8 +57,8 @@ class TMDB {
|
||||
|
||||
return this.cache.get(cacheKey)
|
||||
.catch(() => this.tmdb('movieCredits', query))
|
||||
.catch(tmdbError => this.tmdbCreditsError(tmdbError))
|
||||
.then(credits => this.cache.set(cacheKey, credits, 86400))
|
||||
.catch(tmdbError => tmdbErrorResponse(tmdbError, 'movie credits'))
|
||||
.then(credits => this.cache.set(cacheKey, credits, 1))
|
||||
.then(credits => Credits.convertFromTmdbResponse(credits))
|
||||
}
|
||||
|
||||
@@ -176,8 +73,8 @@ class TMDB {
|
||||
|
||||
return this.cache.get(cacheKey)
|
||||
.catch(() => this.tmdb('movieReleaseDates', query))
|
||||
.catch(tmdbError => this.tmdbRelaseDatesError(tmdbError))
|
||||
.then(releaseDates => this.cache.set(cacheKey, releaseDates, 86400))
|
||||
.catch(tmdbError => tmdbErrorResponse(tmdbError, 'movie release dates'))
|
||||
.then(releaseDates => this.cache.set(cacheKey, releaseDates, 1))
|
||||
.then(releaseDates => ReleaseDates.convertFromTmdbResponse(releaseDates))
|
||||
}
|
||||
|
||||
@@ -187,30 +84,26 @@ class TMDB {
|
||||
* @param {String} type filter results by type (default show).
|
||||
* @returns {Promise} succeeds if show was found
|
||||
*/
|
||||
showInfo(identifier, credits=false) {
|
||||
showInfo(identifier) {
|
||||
const query = { id: identifier };
|
||||
const cacheKey = `${this.cacheTags.showInfo}:${identifier}:${credits}`;
|
||||
const cacheKey = `${this.cacheTags.showInfo}:${identifier}`;
|
||||
|
||||
const requests = [this.tmdb('tvInfo', query)]
|
||||
return this.cache.get(cacheKey)
|
||||
.catch(() => this.tmdb('tvInfo', query))
|
||||
.catch(tmdbError => tmdbErrorResponse(tmdbError, 'tv info'))
|
||||
.then(show => this.cache.set(cacheKey, show, 1))
|
||||
.then(show => Show.convertFromTmdbResponse(show))
|
||||
}
|
||||
|
||||
if (credits) {
|
||||
requests.push(this.tmdb('tvCredits', query))
|
||||
}
|
||||
showCredits(identifier) {
|
||||
const query = { id: identifier }
|
||||
const cacheKey = `${this.cacheTags.showCredits}:${identifier}`
|
||||
|
||||
return Promise.resolve()
|
||||
.then(() => this.cache.get(cacheKey))
|
||||
.catch(() => Promise.all(requests))
|
||||
.catch(error => {
|
||||
if (error.status === 401) {
|
||||
throw new Error('Unathorized tmdb request, please check api key.')
|
||||
} else if (error.status === 404) {
|
||||
throw new Error(`Could not find a show with id: ${identifier}`)
|
||||
}
|
||||
|
||||
throw new Error('Unexpected error has occured:', error.message)
|
||||
})
|
||||
.then(([shows, credits]) => this.cache.set(cacheKey, [shows, credits]))
|
||||
.then(([shows, credits]) => convertTmdbToShow(shows, credits))
|
||||
return this.cache.get(cacheKey)
|
||||
.catch(() => this.tmdb('tvCredits', query))
|
||||
.catch(tmdbError => tmdbErrorResponse(tmdbError, 'show credits'))
|
||||
.then(credits => this.cache.set(cacheKey, credits, 1))
|
||||
.then(credits => Credits.convertFromTmdbResponse(credits))
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -223,25 +116,20 @@ class TMDB {
|
||||
const query = { id: identifier };
|
||||
const cacheKey = `${this.cacheTags.personInfo}:${identifier}`;
|
||||
|
||||
return Promise.resolve()
|
||||
.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))
|
||||
return this.cache.get(cacheKey)
|
||||
.catch(() => this.tmdb('personInfo', query))
|
||||
.catch(tmdbError => tmdbErrorResponse(tmdbError, 'person info'))
|
||||
.then(person => this.cache.set(cacheKey, person, 1))
|
||||
.then(person => Person.convertFromTmdbResponse(person))
|
||||
}
|
||||
|
||||
|
||||
|
||||
multiSearch(search_query, page=1) {
|
||||
const query = { query: search_query, page: page };
|
||||
const cacheKey = `${this.cacheTags.multiSearch}:${page}:${search_query}`;
|
||||
return Promise.resolve()
|
||||
.then(() => this.cache.get(cacheKey))
|
||||
return this.cache.get(cacheKey)
|
||||
.catch(() => this.tmdb('searchMulti', query))
|
||||
.catch(() => { throw new Error('Could not complete search to tmdb'); })
|
||||
.then(response => this.cache.set(cacheKey, response))
|
||||
.catch(tmdbError => tmdbErrorResponse(tmdbError, 'search results'))
|
||||
.then(response => this.cache.set(cacheKey, response, 1))
|
||||
.then(response => this.mapResults(response));
|
||||
}
|
||||
|
||||
@@ -255,13 +143,11 @@ class TMDB {
|
||||
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))
|
||||
.catch(() => { throw new Error('Could not complete movie search to tmdb'); })
|
||||
.then(response => this.cache.set(cacheKey, response))
|
||||
.then(response => this.mapAndCreateResponse(response, convertTmdbToMovie))
|
||||
.catch((error) => { console.log(error); throw new Error('Could not parse movie search result') })
|
||||
return this.cache.get(cacheKey)
|
||||
.catch(() => this.tmdb('searchMovie', tmdbquery))
|
||||
.catch(tmdbError => tmdbErrorResponse(tmdbError, 'movie search results'))
|
||||
.then(response => this.cache.set(cacheKey, response, 1))
|
||||
.then(response => this.mapResults(response, 'movie'))
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -273,13 +159,12 @@ class TMDB {
|
||||
showSearch(query, page=1) {
|
||||
const tmdbquery = { query: query, page: page };
|
||||
const cacheKey = `${this.cacheTags.showSearch}:${page}:${query}`;
|
||||
return Promise.resolve()
|
||||
.then(() => this.cache.get(cacheKey))
|
||||
.catch(() => this.tmdb('searchTv', tmdbquery))
|
||||
.catch(() => { throw new Error('Could not complete show search to tmdb'); })
|
||||
.then(response => this.cache.set(cacheKey, response))
|
||||
.then(response => this.mapAndCreateResponse(response, convertTmdbToShow))
|
||||
.catch((error) => { console.log(error); throw new Error('Could not parse show search result') })
|
||||
|
||||
return this.cache.get(cacheKey)
|
||||
.catch(() => this.tmdb('searchTv', tmdbquery))
|
||||
.catch(tmdbError => tmdbErrorResponse(tmdbError, 'tv search results'))
|
||||
.then(response => this.cache.set(cacheKey, response, 1))
|
||||
.then(response => this.mapResults(response, 'show'))
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -289,78 +174,36 @@ class TMDB {
|
||||
* @returns {Promise} dict with query results, current page and total_pages
|
||||
*/
|
||||
personSearch(query, page=1) {
|
||||
const tmdbquery = { query: query, page: page };
|
||||
|
||||
const tmdbquery = { query: query, page: page, include_adult: true };
|
||||
const cacheKey = `${this.cacheTags.personSearch}:${page}:${query}`;
|
||||
return Promise.resolve()
|
||||
.then(() => this.cache.get(cacheKey))
|
||||
.catch(() => this.tmdb('searchPerson', tmdbquery))
|
||||
.catch(() => { throw new Error('Could not complete person search to tmdb'); })
|
||||
.then(response => this.cache.set(cacheKey, response))
|
||||
.then(response => this.mapAndCreateResponse(response, convertTmdbToPerson))
|
||||
.catch((error) => { console.log(error); throw new Error('Could not parse person search result') })
|
||||
}
|
||||
|
||||
mapAndCreateResponse(response, resultConvertFunction) {
|
||||
// console.log(response)
|
||||
return {
|
||||
results: response.results.map(resultConvertFunction),
|
||||
page: response.page,
|
||||
total_results: response.total_results,
|
||||
total_pages: response.total_pages
|
||||
}
|
||||
return this.cache.get(cacheKey)
|
||||
.catch(() => this.tmdb('searchPerson', tmdbquery))
|
||||
.catch(tmdbError => tmdbErrorResponse(tmdbError, 'person search results'))
|
||||
.then(response => this.cache.set(cacheKey, response, 1))
|
||||
.then(response => this.mapResults(response, 'person'))
|
||||
}
|
||||
|
||||
|
||||
movieList(listname, page = 1) {
|
||||
const query = { page: page };
|
||||
const cacheKey = `${this.cacheTags[listname]}:${page}`;
|
||||
return Promise.resolve()
|
||||
.then(() => this.cache.get(cacheKey))
|
||||
return this.cache.get(cacheKey)
|
||||
.catch(() => this.tmdb(listname, query))
|
||||
.catch(() => { throw new Error('Unable to get movie list from tmdb')})
|
||||
.then(response => this.cache.set(cacheKey, response))
|
||||
.then(response => this.mapAndCreateResponse(response, convertTmdbToMovie));
|
||||
.catch(tmdbError => this.tmdbErrorResponse(tmdbError, 'movie list ' + listname))
|
||||
.then(response => this.cache.set(cacheKey, response, 1))
|
||||
.then(response => this.mapResults(response, 'movie'))
|
||||
}
|
||||
|
||||
showList(listname, page = 1) {
|
||||
const query = { page: page };
|
||||
const cacheKey = `${this.cacheTags[listname]}:${page}`;
|
||||
return Promise.resolve()
|
||||
.then(() => this.cache.get(cacheKey))
|
||||
|
||||
return this.cache.get(cacheKey)
|
||||
.catch(() => this.tmdb(listname, query))
|
||||
.catch(() => { throw new Error('Unable to get show list from tmdb')})
|
||||
.then(response => this.cache.set(cacheKey, response))
|
||||
.then(response => this.mapAndCreateResponse(response, convertTmdbToShow));
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches a given list from tmdb.
|
||||
* @param {String} listName Name of list
|
||||
* @param {String} type filter results by type (default movie)
|
||||
* @param {Number} page representing pagination of results
|
||||
* @returns {Promise} dict with query results, current page and total_pages
|
||||
*/
|
||||
listSearch(listName, type = 'movie', page = '1') {
|
||||
const query = { page: page };
|
||||
console.log(query);
|
||||
const cacheKey = `${this.cacheTags[listName]}:${type}:${page}`;
|
||||
return Promise.resolve()
|
||||
.then(() => this.cache.get(cacheKey))
|
||||
.catch(() => this.tmdb(TMDB_METHODS[listName][type], query))
|
||||
.catch(() => { throw new Error('Error fetching list from tmdb.'); })
|
||||
.then(response => this.cache.set(cacheKey, response))
|
||||
.then(response => this.mapResults(response, type));
|
||||
}
|
||||
|
||||
popular(type='movie', page=1) {
|
||||
const query = { type: type, page: page };
|
||||
const cacheKey = `${this.cacheTags.popular}:${type}:${page}`;
|
||||
return Promise.resolve()
|
||||
.then(() => this.cache.get(cacheKey))
|
||||
.catch(() => this.tmdb('miscPopularMovies', query))
|
||||
.catch((e) => { throw new Error(`Error fetching popular list of type ${type} : ${e}`) })
|
||||
.then(response => this.cache.set(cacheKey, response))
|
||||
.then(response => this.mapResults(response, type));
|
||||
.catch(tmdbError => this.tmdbErrorResponse(tmdbError, 'show list ' + listname))
|
||||
.then(response => this.cache.set(cacheKey, response, 1))
|
||||
.then(response => this.mapResults(response, 'show'))
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -369,14 +212,20 @@ class TMDB {
|
||||
* @param {String} The type declared in listSearch.
|
||||
* @returns {Promise} dict with tmdb results, mapped as movie/show objects.
|
||||
*/
|
||||
mapResults(response, _) {
|
||||
let results = response.results.map((result) => {
|
||||
if (result.media_type === 'movie') {
|
||||
return convertTmdbToMovie(result);
|
||||
} else if (result.media_type === 'tv') {
|
||||
return convertTmdbToShow(result);
|
||||
} else if (result.media_type === 'person') {
|
||||
return convertTmdbToPerson(result);
|
||||
mapResults(response, type=undefined) {
|
||||
// console.log(response.results)
|
||||
// response.results.map(te => console.table(te))
|
||||
|
||||
let results = response.results.map(result => {
|
||||
if (type === 'movie' || result.media_type === 'movie') {
|
||||
const movie = Movie.convertFromTmdbResponse(result)
|
||||
return movie.createJsonResponse()
|
||||
} else if (type === 'show' || result.media_type === 'tv') {
|
||||
const show = Show.convertFromTmdbResponse(result)
|
||||
return show.createJsonResponse()
|
||||
} else if (type === 'person' || result.media_type === 'person') {
|
||||
const person = Person.convertFromTmdbResponse(result)
|
||||
return person.createJsonResponse()
|
||||
}
|
||||
})
|
||||
|
||||
@@ -410,6 +259,28 @@ class TMDB {
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function tmdbErrorResponse(error, typeString=undefined) {
|
||||
if (error.status === 404) {
|
||||
let message = error.response.body.status_message;
|
||||
|
||||
throw {
|
||||
status: 404,
|
||||
message: message.slice(0, -1) + " in tmdb."
|
||||
}
|
||||
} else if (error.status === 401) {
|
||||
throw {
|
||||
status: 401,
|
||||
message: error.response.body.status_message
|
||||
}
|
||||
}
|
||||
|
||||
throw {
|
||||
status: 500,
|
||||
message: `An unexpected error occured while fetching ${typeString} from tmdb`
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = TMDB;
|
||||
|
||||
7
seasoned_api/src/tmdb/tmdb.ts
Normal file
7
seasoned_api/src/tmdb/tmdb.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
import { Movie } from './types'
|
||||
|
||||
Movie('str', 123)
|
||||
|
||||
|
||||
|
||||
module.exports = TMDB;
|
||||
64
seasoned_api/src/tmdb/types.ts
Normal file
64
seasoned_api/src/tmdb/types.ts
Normal file
@@ -0,0 +1,64 @@
|
||||
interface Movie {
|
||||
adult: boolean;
|
||||
backdrop: string;
|
||||
genres: Genre[];
|
||||
id: number;
|
||||
imdb_id: number;
|
||||
overview: string;
|
||||
popularity: number;
|
||||
poster: string;
|
||||
release_date: Date;
|
||||
rank: number;
|
||||
runtime: number;
|
||||
status: string;
|
||||
tagline: string;
|
||||
title: string;
|
||||
vote_count: number;
|
||||
}
|
||||
|
||||
interface Show {
|
||||
adult: boolean;
|
||||
backdrop: string;
|
||||
episodes: number;
|
||||
genres: Genre[];
|
||||
id: number;
|
||||
imdb_id: number;
|
||||
overview: string;
|
||||
popularity: number;
|
||||
poster: string;
|
||||
rank: number;
|
||||
runtime: number;
|
||||
seasons: number;
|
||||
status: string;
|
||||
tagline: string;
|
||||
title: string;
|
||||
vote_count: number;
|
||||
}
|
||||
|
||||
interface Person {
|
||||
birthday: Date;
|
||||
deathday: Date;
|
||||
id: number;
|
||||
known_for: string;
|
||||
name: string;
|
||||
poster: string;
|
||||
}
|
||||
|
||||
interface SearchResult {
|
||||
adult: boolean;
|
||||
backdrop_path: string;
|
||||
id: number;
|
||||
original_title: string;
|
||||
release_date: Date;
|
||||
poster_path: string;
|
||||
popularity: number;
|
||||
vote_average: number;
|
||||
vote_counte: number;
|
||||
}
|
||||
|
||||
interface Genre {
|
||||
id: number;
|
||||
name: string;
|
||||
}
|
||||
|
||||
export { Movie, Show, Person, Genre }
|
||||
@@ -1,7 +1,7 @@
|
||||
class Movie {
|
||||
constructor(id, title, year=undefined, overview=undefined, poster=undefined,
|
||||
backdrop=undefined, rank=undefined, genres=undefined, status=undefined,
|
||||
tagline=undefined, runtime=undefined, imdb_id=undefined) {
|
||||
backdrop=undefined, rank=undefined, genres=undefined, productionStatus=undefined,
|
||||
tagline=undefined, runtime=undefined, imdb_id=undefined, popularity) {
|
||||
this.id = id;
|
||||
this.title = title;
|
||||
this.year = year;
|
||||
@@ -10,13 +10,33 @@ class Movie {
|
||||
this.backdrop = backdrop;
|
||||
this.rank = rank;
|
||||
this.genres = genres;
|
||||
this.status = status;
|
||||
this.productionStatus = productionStatus;
|
||||
this.tagline = tagline;
|
||||
this.runtime = runtime;
|
||||
this.imdb_id = imdb_id;
|
||||
this.popularity = popularity;
|
||||
this.type = 'movie';
|
||||
}
|
||||
|
||||
static convertFromTmdbResponse(response) {
|
||||
const { id, title, release_date, overview, poster_path, backdrop_path, rank, genres, status,
|
||||
tagline, runtime, imdb_id, popularity } = response;
|
||||
|
||||
const year = new Date(release_date).getFullYear()
|
||||
const genreNames = genres ? genres.map(g => g.name) : undefined
|
||||
|
||||
return new Movie(id, title, year, overview, poster_path, backdrop_path, rank, genreNames, status,
|
||||
tagline, runtime, imdb_id, popularity)
|
||||
}
|
||||
|
||||
static convertFromPlexResponse(response) {
|
||||
// console.log('response', response)
|
||||
const { title, year, rating, tagline, summary } = response;
|
||||
const _ = undefined
|
||||
|
||||
return new Movie(null, title, year, summary, _, _, rating, _, _, tagline)
|
||||
}
|
||||
|
||||
createJsonResponse() {
|
||||
return {
|
||||
id: this.id,
|
||||
@@ -27,7 +47,7 @@ class Movie {
|
||||
backdrop: this.backdrop,
|
||||
rank: this.rank,
|
||||
genres: this.genres,
|
||||
status: this.status,
|
||||
production_status: this.productionStatus,
|
||||
tagline: this.tagline,
|
||||
runtime: this.runtime,
|
||||
imdb_id: this.imdb_id,
|
||||
|
||||
@@ -1,14 +1,25 @@
|
||||
class Person {
|
||||
constructor(id, name, poster=null, birthday=null, deathday=null, known_for=null) {
|
||||
constructor(id, name, poster=undefined, birthday=undefined, deathday=undefined,
|
||||
adult=undefined, knownForDepartment=undefined) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.poster = poster;
|
||||
this.birthday = birthday;
|
||||
this.deathday = deathday;
|
||||
this.known_for = known_for;
|
||||
this.adult = adult;
|
||||
this.knownForDepartment = knownForDepartment;
|
||||
this.type = 'person';
|
||||
}
|
||||
|
||||
static convertFromTmdbResponse(response) {
|
||||
const { id, name, poster, birthday, deathday, adult, known_for_department } = response;
|
||||
|
||||
const birthDay = new Date(birthday)
|
||||
const deathDay = deathday ? new Date(deathday) : null
|
||||
|
||||
return new Person(id, name, poster, birthDay, deathDay, adult, known_for_department)
|
||||
}
|
||||
|
||||
createJsonResponse() {
|
||||
return {
|
||||
id: this.id,
|
||||
@@ -16,7 +27,8 @@ class Person {
|
||||
poster: this.poster,
|
||||
birthday: this.birthday,
|
||||
deathday: this.deathday,
|
||||
known_for: this.known_for,
|
||||
known_for_department: this.knownForDepartment,
|
||||
adult: this.adult,
|
||||
type: this.type
|
||||
}
|
||||
}
|
||||
|
||||
@@ -68,7 +68,7 @@ class ReleaseDate {
|
||||
return {
|
||||
certification: this.certification,
|
||||
language: this.language,
|
||||
releaseDate: this.releaseDate,
|
||||
release_date: this.releaseDate,
|
||||
type: this.type,
|
||||
note: this.note
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
class Show {
|
||||
constructor(id, title, year=null, overview=null, poster=null, backdrop=null,
|
||||
seasons=null, episodes=null, rank=null, genres=null, status=null,
|
||||
runtime=null) {
|
||||
constructor(id, title, year=undefined, overview=undefined, poster=undefined, backdrop=undefined,
|
||||
seasons=undefined, episodes=undefined, rank=undefined, genres=undefined, status=undefined,
|
||||
runtime=undefined) {
|
||||
this.id = id;
|
||||
this.title = title;
|
||||
this.year = year;
|
||||
@@ -12,11 +12,22 @@ class Show {
|
||||
this.episodes = episodes;
|
||||
this.rank = rank;
|
||||
this.genres = genres;
|
||||
this.status = status;
|
||||
this.productionStatus = status;
|
||||
this.runtime = runtime;
|
||||
this.type = 'show';
|
||||
}
|
||||
|
||||
static convertFromTmdbResponse(response) {
|
||||
const { id, name, first_air_date, overview, poster_path, backdrop_path, number_of_seasons, number_of_episodes,
|
||||
rank, genres, status, episode_run_time, popularity } = response;
|
||||
|
||||
const year = new Date(first_air_date).getFullYear()
|
||||
const genreNames = genres ? genres.map(g => g.name) : undefined
|
||||
|
||||
return new Show(id, name, year, overview, poster_path, backdrop_path, number_of_seasons, number_of_episodes,
|
||||
rank, genreNames, status, episode_run_time, popularity)
|
||||
}
|
||||
|
||||
createJsonResponse() {
|
||||
return {
|
||||
id: this.id,
|
||||
@@ -29,9 +40,8 @@ class Show {
|
||||
episodes: this.episodes,
|
||||
rank: this.rank,
|
||||
genres: this.genres,
|
||||
status: this.status,
|
||||
production_status: this.productionStatus,
|
||||
runtime: this.runtime,
|
||||
// imdb_id: this.imdb_id,
|
||||
type: this.type
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user