@@ -7,7 +7,7 @@
|
||||
},
|
||||
"main": "webserver/server.js",
|
||||
"scripts": {
|
||||
"start": "cross-env SEASONED_CONFIG=conf/development.json PROD=true NODE_PATH=. babel-node src/webserver/server.js",
|
||||
"start": "cross-env SEASONED_CONFIG=conf/development.json NODE_ENV=production NODE_PATH=. babel-node src/webserver/server.js",
|
||||
"test": "cross-env SEASONED_CONFIG=conf/test.json NODE_PATH=. mocha --require @babel/register --recursive test/unit test/system",
|
||||
"coverage": "cross-env SEASONED_CONFIG=conf/test.json NODE_PATH=. nyc mocha --require @babel/register --recursive test && nyc report --reporter=text-lcov | coveralls",
|
||||
"lint": "./node_modules/.bin/eslint src/",
|
||||
@@ -18,7 +18,7 @@
|
||||
},
|
||||
"dependencies": {
|
||||
"axios": "^0.18.0",
|
||||
"bcrypt": "^3.0.6",
|
||||
"bcrypt": "^5.0.1",
|
||||
"body-parser": "~1.18.2",
|
||||
"cookie-parser": "^1.4.6",
|
||||
"cross-env": "~5.1.4",
|
||||
@@ -33,7 +33,7 @@
|
||||
"redis": "^3.0.2",
|
||||
"request": "^2.87.0",
|
||||
"request-promise": "^4.2",
|
||||
"sqlite3": "^4.0.0"
|
||||
"sqlite3": "^5.0.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@babel/core": "^7.5.5",
|
||||
|
||||
35
seasoned_api/src/notifications/sms.js
Normal file
35
seasoned_api/src/notifications/sms.js
Normal file
@@ -0,0 +1,35 @@
|
||||
const request = require("request");
|
||||
const configuration = require('src/config/configuration').getInstance();
|
||||
|
||||
const sendSMS = (message) => {
|
||||
const apiKey = configuration.get('sms', 'apikey')
|
||||
|
||||
if (!apiKey) {
|
||||
console.warning("api key for sms not set, cannot send sms.")
|
||||
return null
|
||||
}
|
||||
|
||||
const sender = configuration.get('sms', 'sender')
|
||||
const recipient = configuration.get('sms', 'recipient')
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
request.post(
|
||||
{
|
||||
url: `https://gatewayapi.com/rest/mtsms?token=${apiKey}`,
|
||||
json: true,
|
||||
body: {
|
||||
sender,
|
||||
message,
|
||||
recipients: [{ msisdn: `47${recipient}` }]
|
||||
}
|
||||
},
|
||||
function(err, r, body) {
|
||||
console.log(err ? err : body);
|
||||
console.log("sms provider response:", body)
|
||||
resolve()
|
||||
}
|
||||
);
|
||||
})
|
||||
}
|
||||
|
||||
module.exports = { sendSMS }
|
||||
@@ -10,6 +10,12 @@ const redisCache = new RedisCache();
|
||||
|
||||
const sanitize = string => string.toLowerCase().replace(/[^\w]/gi, "");
|
||||
|
||||
function fixedEncodeURIComponent(str) {
|
||||
return encodeURIComponent(str).replace(/[!'()*]/g, function (c) {
|
||||
return "%" + c.charCodeAt(0).toString(16).toUpperCase();
|
||||
});
|
||||
}
|
||||
|
||||
const matchingTitleAndYear = (plex, tmdb) => {
|
||||
let matchingTitle, matchingYear;
|
||||
|
||||
@@ -121,15 +127,12 @@ class Plex {
|
||||
findPlexItemByTitleAndYear(title, year) {
|
||||
const query = { title, year };
|
||||
|
||||
return this.search(query.title).then(plexSearchResults => {
|
||||
const matchesInPlex = plexSearchResults.map(plex =>
|
||||
return this.search(title).then(plexResults => {
|
||||
const matchesInPlex = plexResults.map(plex =>
|
||||
this.matchTmdbAndPlexMedia(plex, query)
|
||||
);
|
||||
|
||||
if (matchesInPlex.includes(true) === false) return false;
|
||||
|
||||
const firstMatchIndex = matchesInPlex.indexOf(true);
|
||||
return plexSearchResults[firstMatchIndex][0];
|
||||
const matchesIndex = matchesInPlex.findIndex(el => el === true);
|
||||
return matchesInPlex != -1 ? plexResults[matchesIndex] : null;
|
||||
});
|
||||
}
|
||||
|
||||
@@ -152,7 +155,7 @@ class Plex {
|
||||
)
|
||||
return false;
|
||||
|
||||
const keyUriComponent = encodeURIComponent(matchingObjectInPlex.key);
|
||||
const keyUriComponent = fixedEncodeURIComponent(matchingObjectInPlex.key);
|
||||
return `https://app.plex.tv/desktop#!/server/${machineIdentifier}/details?key=${keyUriComponent}`;
|
||||
});
|
||||
}
|
||||
@@ -162,7 +165,7 @@ class Plex {
|
||||
|
||||
const url = `http://${this.plexIP}:${
|
||||
this.plexPort
|
||||
}/hubs/search?query=${encodeURIComponent(query)}`;
|
||||
}/hubs/search?query=${fixedEncodeURIComponent(query)}`;
|
||||
const options = {
|
||||
timeout: 20000,
|
||||
headers: { Accept: "application/json" }
|
||||
|
||||
@@ -1,29 +1,35 @@
|
||||
const moviedb = require('km-moviedb');
|
||||
const RedisCache = require('src/cache/redis')
|
||||
const redisCache = new RedisCache()
|
||||
const moviedb = require("km-moviedb");
|
||||
const RedisCache = require("src/cache/redis");
|
||||
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) {
|
||||
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`
|
||||
}
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
class TMDB {
|
||||
constructor(apiKey, cache, tmdbLibrary) {
|
||||
@@ -31,34 +37,38 @@ class TMDB {
|
||||
|
||||
this.cache = cache || redisCache;
|
||||
this.cacheTags = {
|
||||
multiSearch: 'mus',
|
||||
movieSearch: 'mos',
|
||||
showSearch: 'ss',
|
||||
personSearch: 'ps',
|
||||
movieInfo: 'mi',
|
||||
movieCredits: 'mc',
|
||||
movieReleaseDates: 'mrd',
|
||||
showInfo: 'si',
|
||||
showCredits: 'sc',
|
||||
personInfo: 'pi',
|
||||
miscNowPlayingMovies: 'npm',
|
||||
miscPopularMovies: 'pm',
|
||||
miscTopRatedMovies: 'tpm',
|
||||
miscUpcomingMovies: 'um',
|
||||
tvOnTheAir: 'toa',
|
||||
miscPopularTvs: 'pt',
|
||||
miscTopRatedTvs: 'trt',
|
||||
multiSearch: "mus",
|
||||
movieSearch: "mos",
|
||||
showSearch: "ss",
|
||||
personSearch: "ps",
|
||||
movieInfo: "mi",
|
||||
movieCredits: "mc",
|
||||
movieReleaseDates: "mrd",
|
||||
movieImages: "mimg",
|
||||
showInfo: "si",
|
||||
showCredits: "sc",
|
||||
personInfo: "pi",
|
||||
personCredits: "pc",
|
||||
miscNowPlayingMovies: "npm",
|
||||
miscPopularMovies: "pm",
|
||||
miscTopRatedMovies: "tpm",
|
||||
miscUpcomingMovies: "um",
|
||||
tvOnTheAir: "toa",
|
||||
miscPopularTvs: "pt",
|
||||
miscTopRatedTvs: "trt"
|
||||
};
|
||||
this.defaultTTL = 86400
|
||||
this.defaultTTL = 86400;
|
||||
}
|
||||
|
||||
getFromCacheOrFetchFromTmdb(cacheKey, tmdbMethod, query) {
|
||||
return new Promise((resolve, reject) => this.cache.get(cacheKey)
|
||||
.then(resolve)
|
||||
.catch(() => this.tmdb(tmdbMethod, query))
|
||||
.then(resolve)
|
||||
.catch(error => reject(tmdbErrorResponse(error, tmdbMethod)))
|
||||
)
|
||||
return new Promise((resolve, reject) =>
|
||||
this.cache
|
||||
.get(cacheKey)
|
||||
.then(resolve)
|
||||
.catch(() => this.tmdb(tmdbMethod, query))
|
||||
.then(resolve)
|
||||
.catch(error => reject(tmdbErrorResponse(error, tmdbMethod)))
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -72,9 +82,9 @@ class TMDB {
|
||||
const query = { id: 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 => Movie.convertFromTmdbResponse(movie))
|
||||
.then(movie => Movie.convertFromTmdbResponse(movie));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -83,12 +93,12 @@ class TMDB {
|
||||
* @returns {Promise} movie cast object
|
||||
*/
|
||||
movieCredits(identifier) {
|
||||
const query = { id: identifier }
|
||||
const cacheKey = `tmdb/${this.cacheTags.movieCredits}:${identifier}`
|
||||
const query = { id: 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 => Credits.convertFromTmdbResponse(credits))
|
||||
.then(credits => Credits.convertFromTmdbResponse(credits));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -115,18 +125,18 @@ class TMDB {
|
||||
const query = { id: 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 => Show.convertFromTmdbResponse(show))
|
||||
.then(show => Show.convertFromTmdbResponse(show));
|
||||
}
|
||||
|
||||
showCredits(identifier) {
|
||||
const query = { id: identifier }
|
||||
const cacheKey = `tmdb/${this.cacheTags.showCredits}:${identifier}`
|
||||
const query = { id: 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 => Credits.convertFromTmdbResponse(credits))
|
||||
.then(credits => Credits.convertFromTmdbResponse(credits));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -139,16 +149,29 @@ class TMDB {
|
||||
const query = { id: 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 => Person.convertFromTmdbResponse(person))
|
||||
.then(person => Person.convertFromTmdbResponse(person));
|
||||
}
|
||||
|
||||
multiSearch(search_query, page=1, adult=true) {
|
||||
const query = { query: search_query, page: page, include_adult: adult };
|
||||
const cacheKey = `tmdb/${this.cacheTags.multiSearch}:${page}:${search_query}:${adult}`;
|
||||
personCredits(identifier) {
|
||||
const query = { id: identifier };
|
||||
const cacheKey = `tmdb/${this.cacheTags.personCredits}:${identifier}`;
|
||||
|
||||
return this.getFromCacheOrFetchFromTmdb(cacheKey, 'searchMulti', query)
|
||||
return this.getFromCacheOrFetchFromTmdb(
|
||||
cacheKey,
|
||||
"personCombinedCredits",
|
||||
query
|
||||
)
|
||||
.then(credits => this.cache.set(cacheKey, credits, this.defaultTTL))
|
||||
.then(credits => Credits.convertFromTmdbResponse(credits));
|
||||
}
|
||||
|
||||
multiSearch(search_query, page = 1, include_adult = true) {
|
||||
const query = { query: search_query, page, include_adult };
|
||||
const cacheKey = `tmdb/${this.cacheTags.multiSearch}:${page}:${search_query}:${include_adult}`;
|
||||
|
||||
return this.getFromCacheOrFetchFromTmdb(cacheKey, "searchMulti", query)
|
||||
.then(response => this.cache.set(cacheKey, response, this.defaultTTL))
|
||||
.then(response => this.mapResults(response));
|
||||
}
|
||||
@@ -159,13 +182,13 @@ class TMDB {
|
||||
* @param {Number} page representing pagination of results
|
||||
* @returns {Promise} dict with query results, current page and total_pages
|
||||
*/
|
||||
movieSearch(query, page=1, adult=true) {
|
||||
const tmdbquery = { query: query, page: page, adult: adult };
|
||||
const cacheKey = `tmdb/${this.cacheTags.movieSearch}:${page}:${query}:${adult}`;
|
||||
movieSearch(search_query, page = 1, include_adult = true) {
|
||||
const tmdbquery = { query: search_query, page, include_adult };
|
||||
const cacheKey = `tmdb/${this.cacheTags.movieSearch}:${page}:${search_query}:${include_adult}`;
|
||||
|
||||
return this.getFromCacheOrFetchFromTmdb(cacheKey, 'searchMovie', query)
|
||||
return this.getFromCacheOrFetchFromTmdb(cacheKey, "searchMovie", tmdbquery)
|
||||
.then(response => this.cache.set(cacheKey, response, this.defaultTTL))
|
||||
.then(response => this.mapResults(response, 'movie'))
|
||||
.then(response => this.mapResults(response, "movie"));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -174,13 +197,13 @@ class TMDB {
|
||||
* @param {Number} page representing pagination of results
|
||||
* @returns {Promise} dict with query results, current page and total_pages
|
||||
*/
|
||||
showSearch(query, page=1) {
|
||||
const tmdbquery = { query: query, page: page };
|
||||
const cacheKey = `tmdb/${this.cacheTags.showSearch}:${page}:${query}`;
|
||||
showSearch(search_query, page = 1, include_adult = true) {
|
||||
const tmdbquery = { query: search_query, page, include_adult };
|
||||
const cacheKey = `tmdb/${this.cacheTags.showSearch}:${page}:${search_query}:${include_adult}`;
|
||||
|
||||
return this.getFromCacheOrFetchFromTmdb(cacheKey, 'searchTv', query)
|
||||
return this.getFromCacheOrFetchFromTmdb(cacheKey, "searchTv", tmdbquery)
|
||||
.then(response => this.cache.set(cacheKey, response, this.defaultTTL))
|
||||
.then(response => this.mapResults(response, 'show'))
|
||||
.then(response => this.mapResults(response, "show"));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -189,14 +212,13 @@ class TMDB {
|
||||
* @param {Number} page representing pagination of results
|
||||
* @returns {Promise} dict with query results, current page and total_pages
|
||||
*/
|
||||
personSearch(query, page=1) {
|
||||
personSearch(search_query, page = 1, include_adult = true) {
|
||||
const tmdbquery = { query: search_query, page, include_adult };
|
||||
const cacheKey = `tmdb/${this.cacheTags.personSearch}:${page}:${search_query}:${include_adult}`;
|
||||
|
||||
const tmdbquery = { query: query, page: page, include_adult: true };
|
||||
const cacheKey = `tmdb/${this.cacheTags.personSearch}:${page}:${query}:${include_adult}`;
|
||||
|
||||
return this.getFromCacheOrFetchFromTmdb(cacheKey, 'searchPerson', query)
|
||||
return this.getFromCacheOrFetchFromTmdb(cacheKey, "searchPerson", tmdbquery)
|
||||
.then(response => this.cache.set(cacheKey, response, this.defaultTTL))
|
||||
.then(response => this.mapResults(response, 'person'))
|
||||
.then(response => this.mapResults(response, "person"));
|
||||
}
|
||||
|
||||
movieList(listname, page = 1) {
|
||||
@@ -205,16 +227,16 @@ class TMDB {
|
||||
|
||||
return this.getFromCacheOrFetchFromTmdb(cacheKey, listname, query)
|
||||
.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) {
|
||||
const query = { page: page };
|
||||
const cacheKey = `tmdb/${this.cacheTags[listname]}:${page}`;
|
||||
|
||||
return this.getFromCacheOrFetchFromTmdb(cacheKey, listName, query)
|
||||
return this.getFromCacheOrFetchFromTmdb(cacheKey, listName, query)
|
||||
.then(response => this.cache.set(cacheKey, response, this.defaultTTL))
|
||||
.then(response => this.mapResults(response, 'show'))
|
||||
.then(response => this.mapResults(response, "show"));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -223,27 +245,26 @@ class TMDB {
|
||||
* @param {String} The type declared in listSearch.
|
||||
* @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 => {
|
||||
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()
|
||||
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();
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
return {
|
||||
results: results,
|
||||
page: response.page,
|
||||
total_results: response.total_results,
|
||||
total_pages: response.total_pages
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -252,25 +273,22 @@ class TMDB {
|
||||
* @param {Object} argument argument to function being called
|
||||
* @returns {Promise} succeeds if callback succeeds
|
||||
*/
|
||||
tmdb(method, argument) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const callback = (error, reponse) => {
|
||||
if (error) {
|
||||
return reject(error);
|
||||
}
|
||||
resolve(reponse);
|
||||
};
|
||||
|
||||
if (!argument) {
|
||||
this.tmdbLibrary[method](callback);
|
||||
} else {
|
||||
this.tmdbLibrary[method](argument, callback);
|
||||
}
|
||||
});
|
||||
}
|
||||
tmdb(method, argument) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const callback = (error, reponse) => {
|
||||
if (error) {
|
||||
return reject(error);
|
||||
}
|
||||
resolve(reponse);
|
||||
};
|
||||
|
||||
if (!argument) {
|
||||
this.tmdbLibrary[method](callback);
|
||||
} else {
|
||||
this.tmdbLibrary[method](argument, callback);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
module.exports = TMDB;
|
||||
|
||||
@@ -1,20 +1,55 @@
|
||||
class Credits {
|
||||
constructor(id, cast=[], crew=[]) {
|
||||
import Movie from "./movie";
|
||||
import Show from "./show";
|
||||
|
||||
class Credits {
|
||||
constructor(id, cast = [], crew = []) {
|
||||
this.id = id;
|
||||
this.cast = cast;
|
||||
this.crew = crew;
|
||||
this.type = 'credits';
|
||||
this.type = "credits";
|
||||
}
|
||||
|
||||
static convertFromTmdbResponse(response) {
|
||||
const { id, cast, crew } = response;
|
||||
|
||||
const allCast = cast.map(cast =>
|
||||
new CastMember(cast.character, cast.gender, cast.id, cast.name, cast.profile_path))
|
||||
const allCrew = crew.map(crew =>
|
||||
new CrewMember(crew.department, crew.gender, crew.id, crew.job, crew.name, crew.profile_path))
|
||||
const allCast = cast.map(cast => {
|
||||
if (cast["media_type"]) {
|
||||
if (cast.media_type === "movie") {
|
||||
return CreditedMovie.convertFromTmdbResponse(cast);
|
||||
} else if (cast.media_type === "tv") {
|
||||
return CreditedShow.convertFromTmdbResponse(cast);
|
||||
}
|
||||
}
|
||||
|
||||
return new Credits(id, allCast, allCrew)
|
||||
return new CastMember(
|
||||
cast.character,
|
||||
cast.gender,
|
||||
cast.id,
|
||||
cast.name,
|
||||
cast.profile_path
|
||||
);
|
||||
});
|
||||
|
||||
const allCrew = crew.map(crew => {
|
||||
if (cast["media_type"]) {
|
||||
if (cast.media_type === "movie") {
|
||||
return CreditedMovie.convertFromTmdbResponse(cast);
|
||||
} else if (cast.media_type === "tv") {
|
||||
return CreditedShow.convertFromTmdbResponse(cast);
|
||||
}
|
||||
}
|
||||
|
||||
return new CrewMember(
|
||||
crew.department,
|
||||
crew.gender,
|
||||
crew.id,
|
||||
crew.job,
|
||||
crew.name,
|
||||
crew.profile_path
|
||||
);
|
||||
});
|
||||
|
||||
return new Credits(id, allCast, allCrew);
|
||||
}
|
||||
|
||||
createJsonResponse() {
|
||||
@@ -22,7 +57,7 @@ class Credits {
|
||||
id: this.id,
|
||||
cast: this.cast.map(cast => cast.createJsonResponse()),
|
||||
crew: this.crew.map(crew => crew.createJsonResponse())
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,7 +68,7 @@ class CastMember {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.profile_path = profile_path;
|
||||
this.type = 'cast member';
|
||||
this.type = "person";
|
||||
}
|
||||
|
||||
createJsonResponse() {
|
||||
@@ -44,7 +79,7 @@ class CastMember {
|
||||
name: this.name,
|
||||
profile_path: this.profile_path,
|
||||
type: this.type
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -56,7 +91,7 @@ class CrewMember {
|
||||
this.job = job;
|
||||
this.name = name;
|
||||
this.profile_path = profile_path;
|
||||
this.type = 'crew member';
|
||||
this.type = "person";
|
||||
}
|
||||
|
||||
createJsonResponse() {
|
||||
@@ -68,8 +103,11 @@ class CrewMember {
|
||||
name: this.name,
|
||||
profile_path: this.profile_path,
|
||||
type: this.type
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
class CreditedMovie extends Movie {}
|
||||
class CreditedShow extends Show {}
|
||||
|
||||
module.exports = Credits;
|
||||
|
||||
@@ -1,23 +1,54 @@
|
||||
class Person {
|
||||
constructor(id, name, poster=undefined, birthday=undefined, deathday=undefined,
|
||||
adult=undefined, knownForDepartment=undefined) {
|
||||
class Person {
|
||||
constructor(
|
||||
id,
|
||||
name,
|
||||
poster = undefined,
|
||||
birthday = undefined,
|
||||
deathday = undefined,
|
||||
adult = undefined,
|
||||
placeOfBirth = undefined,
|
||||
biography = undefined,
|
||||
knownForDepartment = undefined
|
||||
) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.poster = poster;
|
||||
this.birthday = birthday;
|
||||
this.deathday = deathday;
|
||||
this.adult = adult;
|
||||
this.placeOfBirth = placeOfBirth;
|
||||
this.biography = biography;
|
||||
this.knownForDepartment = knownForDepartment;
|
||||
this.type = 'person';
|
||||
this.type = "person";
|
||||
}
|
||||
|
||||
static convertFromTmdbResponse(response) {
|
||||
const { id, name, profile_path, birthday, deathday, adult, known_for_department } = response;
|
||||
const {
|
||||
id,
|
||||
name,
|
||||
profile_path,
|
||||
birthday,
|
||||
deathday,
|
||||
adult,
|
||||
place_of_birth,
|
||||
biography,
|
||||
known_for_department
|
||||
} = response;
|
||||
|
||||
const birthDay = new Date(birthday)
|
||||
const deathDay = deathday ? new Date(deathday) : null
|
||||
const birthDay = new Date(birthday);
|
||||
const deathDay = deathday ? new Date(deathday) : null;
|
||||
|
||||
return new Person(id, name, profile_path, birthDay, deathDay, adult, known_for_department)
|
||||
return new Person(
|
||||
id,
|
||||
name,
|
||||
profile_path,
|
||||
birthDay,
|
||||
deathDay,
|
||||
adult,
|
||||
place_of_birth,
|
||||
biography,
|
||||
known_for_department
|
||||
);
|
||||
}
|
||||
|
||||
createJsonResponse() {
|
||||
@@ -27,10 +58,12 @@ class Person {
|
||||
poster: this.poster,
|
||||
birthday: this.birthday,
|
||||
deathday: this.deathday,
|
||||
place_of_birth: this.placeOfBirth,
|
||||
biography: this.biography,
|
||||
known_for_department: this.knownForDepartment,
|
||||
adult: this.adult,
|
||||
type: this.type
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -36,25 +36,28 @@ router.use(reqTokenToUser);
|
||||
// TODO: Should have a separate middleware/router for handling headers.
|
||||
router.use((req, res, next) => {
|
||||
// TODO add logging of all incoming
|
||||
const origin = req.headers.origin;
|
||||
if (allowedOrigins.indexOf(origin) > -1) {
|
||||
res.setHeader("Access-Control-Allow-Origin", origin);
|
||||
}
|
||||
// const origin = req.headers.origin;
|
||||
// if (allowedOrigins.indexOf(origin) > -1) {
|
||||
// res.setHeader("Access-Control-Allow-Origin", origin);
|
||||
// }
|
||||
|
||||
res.header(
|
||||
"Access-Control-Allow-Headers",
|
||||
"Content-Type, Authorization, loggedinuser"
|
||||
"Content-Type, Authorization, loggedinuser, set-cookie"
|
||||
);
|
||||
res.header("Access-Control-Allow-Methods", "POST, GET, PUT");
|
||||
|
||||
res.header("Access-Control-Allow-Credentials", "true");
|
||||
res.header("Access-Control-Allow-Methods", "POST, GET, PUT, OPTIONS");
|
||||
|
||||
next();
|
||||
});
|
||||
|
||||
router.get("/", function mainHandler(req, res) {
|
||||
throw new Error("Broke!");
|
||||
router.get("/", (req, res) => {
|
||||
res.send("welcome to seasoned api");
|
||||
});
|
||||
|
||||
app.use(Raven.errorHandler());
|
||||
app.use(function onError(err, req, res, next) {
|
||||
app.use((err, req, res, next) => {
|
||||
res.statusCode = 500;
|
||||
res.end(res.sentry + "\n");
|
||||
});
|
||||
@@ -142,20 +145,23 @@ router.get("/v2/movie/now_playing", listController.nowPlayingMovies);
|
||||
router.get("/v2/movie/popular", listController.popularMovies);
|
||||
router.get("/v2/movie/top_rated", listController.topRatedMovies);
|
||||
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/release_dates",
|
||||
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/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/person/:id/credits",
|
||||
require("./controllers/person/credits.js")
|
||||
);
|
||||
router.get("/v2/person/:id", require("./controllers/person/info.js"));
|
||||
|
||||
/**
|
||||
|
||||
26
seasoned_api/src/webserver/controllers/person/credits.js
Normal file
26
seasoned_api/src/webserver/controllers/person/credits.js
Normal file
@@ -0,0 +1,26 @@
|
||||
const configuration = require("src/config/configuration").getInstance();
|
||||
const TMDB = require("src/tmdb/tmdb");
|
||||
const tmdb = new TMDB(configuration.get("tmdb", "apiKey"));
|
||||
|
||||
const personCreditsController = (req, res) => {
|
||||
const personId = req.params.id;
|
||||
|
||||
return tmdb
|
||||
.personCredits(personId)
|
||||
.then(credits => res.send(credits))
|
||||
.catch(error => {
|
||||
const { status, message } = error;
|
||||
|
||||
if (status && message) {
|
||||
res.status(status).send({ success: false, message });
|
||||
} else {
|
||||
// TODO log unhandled errors
|
||||
console.log("caugth show credits controller error", error);
|
||||
res.status(500).send({
|
||||
message: "An unexpected error occured while requesting person credits"
|
||||
});
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
module.exports = personCreditsController;
|
||||
@@ -1,23 +1,49 @@
|
||||
const configuration = require('src/config/configuration').getInstance();
|
||||
const TMDB = require('src/tmdb/tmdb');
|
||||
const tmdb = new TMDB(configuration.get('tmdb', 'apiKey'));
|
||||
const configuration = require("src/config/configuration").getInstance();
|
||||
const TMDB = require("src/tmdb/tmdb");
|
||||
const tmdb = new TMDB(configuration.get("tmdb", "apiKey"));
|
||||
|
||||
function handleError(error, res) {
|
||||
const { status, message } = error;
|
||||
|
||||
if (status && message) {
|
||||
res.status(status).send({ success: false, message });
|
||||
} else {
|
||||
console.log("caught personinfo controller error", error);
|
||||
res.status(500).send({
|
||||
message: "An unexpected error occured while requesting person info."
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Controller: Retrieve information for a person
|
||||
* Controller: Retrieve information for a person
|
||||
* @param {Request} req http request variable
|
||||
* @param {Response} res
|
||||
* @returns {Callback}
|
||||
*/
|
||||
|
||||
function personInfoController(req, res) {
|
||||
async function personInfoController(req, res) {
|
||||
const personId = req.params.id;
|
||||
let { credits } = req.query;
|
||||
arguments;
|
||||
|
||||
credits && credits.toLowerCase() === "true"
|
||||
? (credits = true)
|
||||
: (credits = false);
|
||||
|
||||
tmdb.personInfo(personId)
|
||||
.then(person => res.send(person.createJsonResponse()))
|
||||
.catch(error => {
|
||||
res.status(404).send({ success: false, message: error.message });
|
||||
});
|
||||
let tmdbQueue = [tmdb.personInfo(personId)];
|
||||
if (credits) tmdbQueue.push(tmdb.personCredits(personId));
|
||||
|
||||
try {
|
||||
const [Person, Credits] = await Promise.all(tmdbQueue);
|
||||
|
||||
const person = Person.createJsonResponse();
|
||||
if (credits) person.credits = Credits.createJsonResponse();
|
||||
|
||||
return res.send(person);
|
||||
} catch (error) {
|
||||
handleError(error, res);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = personInfoController;
|
||||
|
||||
@@ -3,6 +3,7 @@ const TMDB = require("src/tmdb/tmdb");
|
||||
const RequestRepository = require("src/request/request");
|
||||
const tmdb = new TMDB(configuration.get("tmdb", "apiKey"));
|
||||
const request = new RequestRepository();
|
||||
// const { sendSMS } = require("src/notifications/sms");
|
||||
|
||||
const tmdbMovieInfo = id => {
|
||||
return tmdb.movieInfo(id);
|
||||
@@ -47,9 +48,14 @@ function requestTmdbIdController(req, res) {
|
||||
|
||||
mediaFunction(id)
|
||||
// .catch((error) => { console.error(error); res.status(404).send({ success: false, error: 'Id not found' }) })
|
||||
.then(tmdbMedia =>
|
||||
request.requestFromTmdb(tmdbMedia, ip, user_agent, username)
|
||||
)
|
||||
.then(tmdbMedia => {
|
||||
request.requestFromTmdb(tmdbMedia, ip, user_agent, username);
|
||||
|
||||
// TODO enable SMS
|
||||
// const url = `https://request.movie?${tmdbMedia.type}=${tmdbMedia.id}`;
|
||||
// const message = `${tmdbMedia.title} (${tmdbMedia.year}) requested!\n${url}`;
|
||||
// sendSMS(message);
|
||||
})
|
||||
.then(() =>
|
||||
res.send({ success: true, message: "Request has been submitted." })
|
||||
)
|
||||
|
||||
@@ -11,15 +11,16 @@ const searchHistory = new SearchHistory();
|
||||
* @returns {Callback}
|
||||
*/
|
||||
function movieSearchController(req, res) {
|
||||
const { query, page } = req.query;
|
||||
const { query, page, adult } = req.query;
|
||||
const username = req.loggedInUser ? req.loggedInUser.username : null;
|
||||
const includeAdult = adult == "true" ? true : false;
|
||||
|
||||
if (username) {
|
||||
return searchHistory.create(username, query);
|
||||
searchHistory.create(username, query);
|
||||
}
|
||||
|
||||
tmdb
|
||||
.movieSearch(query, page)
|
||||
return tmdb
|
||||
.movieSearch(query, page, includeAdult)
|
||||
.then(movieSearchResults => res.send(movieSearchResults))
|
||||
.catch(error => {
|
||||
const { status, message } = error;
|
||||
|
||||
@@ -11,18 +11,17 @@ const searchHistory = new SearchHistory();
|
||||
* @returns {Callback}
|
||||
*/
|
||||
function personSearchController(req, res) {
|
||||
const { query, page } = req.query;
|
||||
const { query, page, adult } = req.query;
|
||||
const username = req.loggedInUser ? req.loggedInUser.username : null;
|
||||
const includeAdult = adult == "true" ? true : false;
|
||||
|
||||
if (username) {
|
||||
return searchHistory.create(username, query);
|
||||
searchHistory.create(username, query);
|
||||
}
|
||||
|
||||
tmdb
|
||||
.personSearch(query, page)
|
||||
.then(person => {
|
||||
res.send(person);
|
||||
})
|
||||
return tmdb
|
||||
.personSearch(query, page, includeAdult)
|
||||
.then(persons => res.send(persons))
|
||||
.catch(error => {
|
||||
const { status, message } = error;
|
||||
|
||||
|
||||
@@ -11,17 +11,16 @@ const searchHistory = new SearchHistory();
|
||||
* @returns {Callback}
|
||||
*/
|
||||
function showSearchController(req, res) {
|
||||
const { query, page } = req.query;
|
||||
const { query, page, adult } = req.query;
|
||||
const username = req.loggedInUser ? req.loggedInUser.username : null;
|
||||
const includeAdult = adult == "true" ? true : false;
|
||||
|
||||
Promise.resolve()
|
||||
.then(() => {
|
||||
if (username) {
|
||||
return searchHistory.create(username, query);
|
||||
}
|
||||
return null;
|
||||
})
|
||||
.then(() => tmdb.showSearch(query, page))
|
||||
if (username) {
|
||||
searchHistory.create(username, query);
|
||||
}
|
||||
|
||||
return tmdb
|
||||
.showSearch(query, page, includeAdult)
|
||||
.then(shows => {
|
||||
res.send(shows);
|
||||
})
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user