New release_dates endpoint for movie
This commit is contained in:
@@ -3,7 +3,7 @@ const convertTmdbToMovie = require('src/tmdb/convertTmdbToMovie');
|
||||
const convertTmdbToShow = require('src/tmdb/convertTmdbToShow');
|
||||
const convertTmdbToPerson = require('src/tmdb/convertTmdbToPerson');
|
||||
|
||||
const { Credits } = require('src/tmdb/types');
|
||||
const { Credits, ReleaseDates} = require('src/tmdb/types');
|
||||
// const { tmdbInfo } = require('src/tmdb/types')
|
||||
|
||||
class TMDB {
|
||||
@@ -17,6 +17,7 @@ class TMDB {
|
||||
personSearch: 'ps',
|
||||
movieInfo: 'mi',
|
||||
movieCredits: 'mc',
|
||||
movieReleaseDates: 'mrd',
|
||||
showInfo: 'si',
|
||||
personInfo: 'pi',
|
||||
miscNowPlayingMovies: 'npm',
|
||||
@@ -129,6 +130,25 @@ class 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'
|
||||
}
|
||||
}
|
||||
|
||||
movieCredits(identifier) {
|
||||
const query = { id: identifier }
|
||||
const cacheKey = `${this.cacheTags.movieCredits}:${identifier}`
|
||||
@@ -139,6 +159,17 @@ class TMDB {
|
||||
.then(credits => this.cache.set(cacheKey, credits, 86400))
|
||||
.then(credits => Credits.convertFromTmdbResponse(credits))
|
||||
}
|
||||
|
||||
movieReleaseDates(identifier) {
|
||||
const query = { id: identifier }
|
||||
const cacheKey = `${this.cacheTags.movieReleaseDates}:${identifier}`
|
||||
|
||||
return this.cache.get(cacheKey)
|
||||
.catch(() => this.tmdb('movieReleaseDates', query))
|
||||
.catch(tmdbError => this.tmdbRelaseDatesError(tmdbError))
|
||||
.then(releaseDates => this.cache.set(cacheKey, releaseDates, 1))
|
||||
.then(releaseDates => ReleaseDates.convertFromTmdbResponse(releaseDates))
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve a specific show by id from TMDB.
|
||||
|
||||
@@ -2,5 +2,6 @@ import Movie from './types/movie.js'
|
||||
import Show from './types/show.js'
|
||||
import Person from './types/person.js'
|
||||
import Credits from './types/credits.js'
|
||||
import ReleaseDates from './types/releaseDates.js'
|
||||
|
||||
module.exports = { Movie, Show, Person, Credits }
|
||||
module.exports = { Movie, Show, Person, ReleaseDates }
|
||||
|
||||
62
seasoned_api/src/tmdb/types/releaseDates.js
Normal file
62
seasoned_api/src/tmdb/types/releaseDates.js
Normal file
@@ -0,0 +1,62 @@
|
||||
class ReleaseDate {
|
||||
constructor(certification, language, releaseDate, type, note) {
|
||||
this.certification = certification;
|
||||
this.language = language;
|
||||
this.releaseDate = releaseDate;
|
||||
this.type = this.releaseTypeLookup(type);
|
||||
this.note = note;
|
||||
}
|
||||
|
||||
releaseTypeLookup(releaseTypeKey) {
|
||||
const releaseTypeEnum = {
|
||||
1: 'Premier',
|
||||
2: 'Limited theatrical',
|
||||
3: 'Theatrical',
|
||||
4: 'Digital',
|
||||
5: 'Physical',
|
||||
6: 'TV'
|
||||
}
|
||||
if (releaseTypeKey <= Object.keys(releaseTypeEnum).length) {
|
||||
return releaseTypeEnum[releaseTypeKey]
|
||||
} else {
|
||||
// TODO log | Release type not defined, does this need updating?
|
||||
return null
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class Release {
|
||||
constructor(country, releaseDates) {
|
||||
this.country = country;
|
||||
this.releaseDates = releaseDates;
|
||||
}
|
||||
}
|
||||
|
||||
class ReleaseDates {
|
||||
constructor(id, releases) {
|
||||
this.id = id;
|
||||
this.releases = releases;
|
||||
}
|
||||
|
||||
static convertFromTmdbResponse(response) {
|
||||
console.log('this is relese dates response')
|
||||
const { id, results } = response;
|
||||
|
||||
const releases = results.map(countryRelease =>
|
||||
new Release(
|
||||
countryRelease.iso_3166_1,
|
||||
countryRelease.release_dates.map(rd => new ReleaseDate(rd.certification, rd.iso_639_1, rd.release_date, rd.type, rd.note))
|
||||
))
|
||||
|
||||
return new ReleaseDates(id, releases)
|
||||
}
|
||||
|
||||
createJsonResponse() {
|
||||
return JSON.stringify({
|
||||
id: this.id,
|
||||
release_dates: this.releases
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = ReleaseDates;
|
||||
Reference in New Issue
Block a user