diff --git a/src/tmdb/tmdb.js b/src/tmdb/tmdb.js index 0d40b2a..e3473b8 100644 --- a/src/tmdb/tmdb.js +++ b/src/tmdb/tmdb.js @@ -1,25 +1,51 @@ const moviedb = require('moviedb'); const convertTmdbToMovie = require('src/tmdb/convertTmdbToMovie'); +var methodTypes = { 'movie': 'searchMovie', 'tv': 'searchTv', 'multi': 'searchMulti', 'movieInfo': 'movieInfo', + 'tvInfo': 'tvInfo' }; class TMDB { constructor(apiKey, tmdbLibrary) { this.tmdbLibrary = tmdbLibrary || moviedb(apiKey); } - search(text, page = 1, type = 'searchMulti') { + search(text, page = 1, type = 'multi') { const query = { query: text, page }; return Promise.resolve() .then(() => this.tmdb(type, query)) .catch(() => { throw new Error('Could not search for movies.'); }) .then((reponse) => { try { - return reponse.results.map(convertTmdbToMovie); + return reponse.results.filter(function(item) { + return (item.popularity >= 1.15) + }).map(convertTmdbToMovie); } catch (parseError) { throw new Error('Could not parse result.'); } }); } + + /** + * Retrieve a specific movie by id from TMDB. + * @param {Number} identifier of the movie you want to retrieve + * @returns {Promise} succeeds if movie was found + */ + lookup(identifier, type = 'movie') { + if (type === 'movie') { type = 'movieInfo'} + else if (type === 'tv') { type = 'tvInfo'} + const query = { id: identifier }; + return Promise.resolve() + .then(() => this.tmdb(type, query)) + .catch(() => { throw new Error('Could not find a movie with that id.'); }) + .then((response) => { + try { + return convertTmdbToMovie(response); + } catch (parseError) { + throw new Error('Could not parse movie.'); + } + }); + } + tmdb(method, argument) { return new Promise((resolve, reject) => { const callback = (error, reponse) => { @@ -30,9 +56,9 @@ class TMDB { }; if (!argument) { - this.tmdbLibrary[method](callback); + this.tmdbLibrary[methodTypes[method]](callback); } else { - this.tmdbLibrary[method](argument, callback); + this.tmdbLibrary[methodTypes[method]](argument, callback); } }) }