tmdb-client
Advanced tools
| // dependency modules | ||
| var request = require('request'); | ||
| var cdnaUtils = require('cdna-utils'); | ||
| // shortcut for api in modules | ||
| var MapObject = cdnaUtils.MapObject; | ||
| // error message | ||
| var errorMessage = { | ||
| parseBody: 'unable to parse body to json' | ||
| }; | ||
| // default url to tmdb api | ||
| var tmdbApi = { | ||
| key: '5850a992cce5c609f88466d906e77e88', | ||
| host: 'http://api.themoviedb.org/3', | ||
| path: { | ||
| findMovie: '/movie/popular', | ||
| findMovieById: '/movie', | ||
| findTv: '/tv/popular', | ||
| findTvById: '/tv', | ||
| findTvSeason: '/tv/:id/season/:number', | ||
| findTvEpisode: '/tv/:id/season/:seNumber/episode/:epNumber' | ||
| } | ||
| }; | ||
| // default find params | ||
| var findParams = { | ||
| pageIndex: 1 | ||
| }; | ||
| // the movie database client | ||
| // using for create new instance of the movie database client | ||
| // params | ||
| // - api: object contain url to tmdb api | ||
| function TmdbClient(api) { | ||
| var self = this; | ||
| self.api = api || tmdbApi; | ||
| } | ||
| // shortcut for the movie database prototype | ||
| var prototype = TmdbClient.prototype; | ||
| // return url to api | ||
| prototype.apiUrl = function(path) { | ||
| var self = this; | ||
| return self.api.host + path; | ||
| } | ||
| // find movie | ||
| // prams | ||
| // - selector: object contain information to match | ||
| // - pageIndex: index of page, [1, 1000] | ||
| // - callback: function will call | ||
| // prams | ||
| // - error: error occur during process | ||
| // - items: items match with selector | ||
| prototype.findMovie = function(selector, callback) { | ||
| var self = this; | ||
| var params = self.queryParams(); | ||
| params.page = selector.pageIndex || findParams.pageIndex; | ||
| var options = {url: self.apiUrl(self.api.path.findMovie), qs: params}; | ||
| request(options, function(err, res, body) { | ||
| // check error | ||
| if(err) { callback(err); return; } | ||
| if(res.statusCode != 200) { callback(res); return; } | ||
| // parse body to javascript object | ||
| var jsonBody = JSON.parse(body); | ||
| if(!jsonBody) { | ||
| callback({ message: errorMessage.parseBody, response: response }); | ||
| return; | ||
| } | ||
| // valid data | ||
| if(!jsonBody.results) { | ||
| callback({ message: 'results not in body', response: response }); | ||
| return; | ||
| } | ||
| // callback with items | ||
| callback(null, jsonBody.results); | ||
| }); | ||
| } | ||
| // find movie by identity | ||
| // params | ||
| // - id: identity of movie | ||
| // - callback: function will call | ||
| // params | ||
| // - error: error occur during process | ||
| // - item: movie correct with identity | ||
| prototype.findMovieById = function(id, callback) { | ||
| var self = this; | ||
| var params = self.queryParams(); | ||
| var options = {url: self.apiUrl(self.api.path.findMovieById) + '/' + id, | ||
| qs: params | ||
| }; | ||
| request(options, function(err, res, body) { | ||
| // check error | ||
| if(err) { callback(err); return; } | ||
| if(res.statusCode != 200) { callback(res); return; } | ||
| // parse body to javascript object | ||
| var jsonBody = JSON.parse(body); | ||
| if(!jsonBody) { | ||
| callback({ message: errorMessage.parseBody, response: response }); | ||
| return; | ||
| } | ||
| // callback with items | ||
| callback(null, jsonBody); | ||
| }); | ||
| }; | ||
| // count number of movie match with selector | ||
| // params | ||
| // - selector: same as findMovie | ||
| // - callback: function will call | ||
| // params | ||
| // - error: error occur during process | ||
| // - size: number of item match with selector | ||
| prototype.movieSize = function(selector, callback) { | ||
| var self = this; | ||
| var params = self.queryParams(); | ||
| var options = {url: self.apiUrl(self.api.path.findMovie), qs: params }; | ||
| request(options, function(err, res, body) { | ||
| // check error | ||
| if(err) { callback(err); return; } | ||
| if(res.statusCode != 200) { callback(res); return; } | ||
| // parse body to javascript object | ||
| var jsonBody = JSON.parse(body); | ||
| if(!jsonBody) { | ||
| callback({ message: errorMessage.parseBody, response: response }); | ||
| return; | ||
| } | ||
| // valid data | ||
| if(!jsonBody.total_results) { | ||
| callback({ message: 'total_results not in body', | ||
| response: response | ||
| }); | ||
| return; | ||
| } | ||
| // callback with items | ||
| callback(null, jsonBody.total_results); | ||
| }); | ||
| }; | ||
| // movie size | ||
| // params | ||
| // - selector: same as findMovie | ||
| // find tv show | ||
| // prams | ||
| // - selector: object contain information to match | ||
| // - pageIndex: index of page, [1, 1000] | ||
| // - callback: function will call | ||
| // prams | ||
| // - error: error occur during process | ||
| // - items: items match with selector | ||
| prototype.findTv = function(selector, callback) { | ||
| var self = this; | ||
| var params = self.queryParams(); | ||
| params.page = selector.pageIndex || findParams.pageIndex; | ||
| var options = {url: self.apiUrl(self.api.path.findTv), qs: params}; | ||
| request(options, function(err, res, body) { | ||
| // check error | ||
| if(err) { callback(err); return; } | ||
| if(res.statusCode != 200) { callback(res); return; } | ||
| // parse body to javascript object | ||
| var jsonBody = JSON.parse(body); | ||
| if(!jsonBody) { | ||
| callback({ message: errorMessage.parseBody, response: response }); | ||
| return; | ||
| } | ||
| // valid data | ||
| if(!jsonBody.results) { | ||
| callback({ message: 'results not in body', response: response }); | ||
| return; | ||
| } | ||
| // callback with items | ||
| callback(null, jsonBody.results); | ||
| }); | ||
| } | ||
| // find tv by identity | ||
| // params | ||
| // - id: identity of movie | ||
| // - callback: function will call | ||
| // params | ||
| // - error: error occur during process | ||
| // - item: movie correct with identity | ||
| prototype.findTvById = function(id, callback) { | ||
| var self = this; | ||
| var params = self.queryParams(); | ||
| var options = {url: self.apiUrl(self.api.path.findTvById) + '/' + id, | ||
| qs: params | ||
| }; | ||
| request(options, function(err, res, body) { | ||
| // check error | ||
| if(err) { callback(err); return; } | ||
| if(res.statusCode != 200) { callback(res); return; } | ||
| // parse body to javascript object | ||
| var jsonBody = JSON.parse(body); | ||
| if(!jsonBody) { | ||
| callback({ message: errorMessage.parseBody, response: response }); | ||
| return; | ||
| } | ||
| // callback with items | ||
| callback(null, jsonBody); | ||
| }); | ||
| }; | ||
| // count number of tv match with selector | ||
| // params | ||
| // - selector: same as findTv | ||
| // - callback: function will call | ||
| // params | ||
| // - error: error occur during process | ||
| // - size: number of item match with selector | ||
| prototype.tvSize = function(selector, callback) { | ||
| var self = this; | ||
| var params = self.queryParams(); | ||
| var options = {url: self.apiUrl(self.api.path.findTv), qs: params }; | ||
| request(options, function(err, res, body) { | ||
| // check error | ||
| if(err) { callback(err); return; } | ||
| if(res.statusCode != 200) { callback(res); return; } | ||
| // parse body to javascript object | ||
| var jsonBody = JSON.parse(body); | ||
| if(!jsonBody) { | ||
| callback({ message: errorMessage.parseBody, response: response }); | ||
| return; | ||
| } | ||
| // valid data | ||
| if(!jsonBody.total_results) { | ||
| callback({ message: 'total_results not in body', | ||
| response: response | ||
| }); | ||
| return; | ||
| } | ||
| // callback with items | ||
| callback(null, jsonBody.total_results); | ||
| }); | ||
| }; | ||
| // using for find season of tv show | ||
| // params | ||
| // - tvId: identity of tv show | ||
| // - seasonNumber: order of season | ||
| // - callback: function will call when process done | ||
| // params | ||
| // - error: error during process | ||
| // - season: season in tv show | ||
| prototype.findTvSeason = function(tvId, seasonNumber, callback) { | ||
| var self = this; | ||
| var params = self.queryParams(); | ||
| var url = self.apiUrl(self.api.path.findTvSeason).replace(':id', tvId). | ||
| replace(':number', seasonNumber); | ||
| var options = {url: url, qs: params }; | ||
| request(options, function(err, res, body) { | ||
| // check error | ||
| if(err) { callback(err); return; } | ||
| if(res.statusCode != 200) { callback(res); return; } | ||
| // parse body to javascript object | ||
| var jsonBody = JSON.parse(body); | ||
| if(!jsonBody) { | ||
| callback({ message: errorMessage.parseBody, response: response }); | ||
| return; | ||
| } | ||
| // callback with items | ||
| callback(null, jsonBody); | ||
| }); | ||
| }; | ||
| // using to find episode in season | ||
| // params | ||
| // - tvId: identity of tv show | ||
| // - seasonNumber: order of season | ||
| // - epNumber: order of episode in season | ||
| // - callback: function will call when process done | ||
| // params | ||
| // - error: error during process | ||
| // - episode: episode in season | ||
| prototype.findTvEpisode = function(tvId, seasonNumber, epNumber, callback) { | ||
| var self = this; | ||
| var params = self.queryParams(); | ||
| var url = self.apiUrl(self.api.path.findTvEpisode).replace(':id', tvId). | ||
| replace(':seNumber', seasonNumber).replace(':epNumber', epNumber); | ||
| var options = {url: url, qs: params }; | ||
| request(options, function(err, res, body) { | ||
| // check error | ||
| if(err) { callback(err); return; } | ||
| if(res.statusCode != 200) { callback(res); return; } | ||
| // parse body to javascript object | ||
| var jsonBody = JSON.parse(body); | ||
| if(!jsonBody) { | ||
| callback({ message: errorMessage.parseBody, response: response }); | ||
| return; | ||
| } | ||
| // callback with items | ||
| callback(null, jsonBody); | ||
| }); | ||
| } | ||
| // query params | ||
| // using for generate query params | ||
| // return object contains configuration properties for tmdb api | ||
| prototype.queryParams = function() { | ||
| var self = this; | ||
| return { | ||
| api_key: self.api.key | ||
| }; | ||
| } | ||
| module.exports = TmdbClient; |
+1
-1
@@ -1,2 +0,2 @@ | ||
| var TmdbClient = require('./lib/tmbn-client'); | ||
| var TmdbClient = require('./lib/tmdb-client'); | ||
| module.exports = TmdbClient; |
+1
-1
| { | ||
| "name": "tmdb-client", | ||
| "version": "0.1.5", | ||
| "version": "0.1.6", | ||
| "description": "the movie database api for client", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
+13
-4
@@ -92,10 +92,19 @@ # tmdb-client | ||
| ## next api | ||
| ## find season of tv | ||
| ```javascript | ||
| var tvId = <number>; | ||
| var seasonNumber = <number>; | ||
| tmdb.findTvSeason(tvId, seasonNumber, function(error, season) {}); | ||
| ``` | ||
| ## find episode of season | ||
| ```javascript | ||
| // next day comming with | ||
| Tmdb.findTvSeason(tvId, function(error, items) {}); | ||
| Tmdb.findTvEpisode(seasonId, function(error, items) {}); | ||
| var tvId = <number>; | ||
| var seasonNumber = <number>; | ||
| var episodeNumber = <number>; | ||
| tmdb.findTvSeason(tvId, seasonNumber, epNumber, function(error, episode) {}); | ||
| ``` | ||
| ## next api (pending) | ||
| ## development | ||
@@ -102,0 +111,0 @@ |
+24
-0
@@ -65,2 +65,26 @@ var chai = require('chai'); | ||
| }); | ||
| it('find tv season', function(done) { | ||
| tmdbClient.findTvSeason(39351, 1, function(err, season) { | ||
| expect(err).equal(null); | ||
| var keys = ['air_date', 'episodes', 'name', 'overview', 'id', | ||
| 'poster_path', 'season_number' | ||
| ]; | ||
| expect(season).include.keys(keys); | ||
| done(); | ||
| }); | ||
| }); | ||
| it('find tv episode', function(done) { | ||
| tmdbClient.findTvEpisode(39351, 1, 1, function(err, season) { | ||
| expect(err).equal(null); | ||
| var keys = ['air_date', 'crew', 'episode_number', 'name', | ||
| 'overview', 'id', 'production_code', 'still_path', 'season_number' | ||
| ]; | ||
| expect(season).include.keys(keys); | ||
| done(); | ||
| }); | ||
| }); | ||
| }); |
| // dependency modules | ||
| var request = require('request'); | ||
| var cdnaUtils = require('cdna-utils'); | ||
| // shortcut for api in modules | ||
| var MapObject = cdnaUtils.MapObject; | ||
| // error message | ||
| var errorMessage = { | ||
| parseBody: 'unable to parse body to json' | ||
| }; | ||
| // default url to tmdb api | ||
| var tmdbApi = { | ||
| key: '5850a992cce5c609f88466d906e77e88', | ||
| host: 'http://api.themoviedb.org/3', | ||
| path: { | ||
| findMovie: '/movie/popular', | ||
| findMovieById: '/movie', | ||
| findTv: '/tv/popular', | ||
| findTvById: '/tv' | ||
| } | ||
| }; | ||
| // default find params | ||
| var findParams = { | ||
| pageIndex: 1 | ||
| }; | ||
| // the movie database client | ||
| // using for create new instance of the movie database client | ||
| // params | ||
| // - api: object contain url to tmdb api | ||
| function TmdbClient(api) { | ||
| var self = this; | ||
| self.api = api || tmdbApi; | ||
| } | ||
| // shortcut for the movie database prototype | ||
| var prototype = TmdbClient.prototype; | ||
| // return url to api | ||
| prototype.apiUrl = function(path) { | ||
| var self = this; | ||
| return self.api.host + path; | ||
| } | ||
| // find movie | ||
| // prams | ||
| // - selector: object contain information to match | ||
| // - pageIndex: index of page, [1, 1000] | ||
| // - callback: function will call | ||
| // prams | ||
| // - error: error occur during process | ||
| // - items: items match with selector | ||
| prototype.findMovie = function(selector, callback) { | ||
| var self = this; | ||
| var params = self.queryParams(); | ||
| params.page = selector.pageIndex || findParams.pageIndex; | ||
| var options = {url: self.apiUrl(self.api.path.findMovie), qs: params}; | ||
| request(options, function(err, res, body) { | ||
| // check error | ||
| if(err) { callback(err); return; } | ||
| if(res.statusCode != 200) { callback(res); return; } | ||
| // parse body to javascript object | ||
| var jsonBody = JSON.parse(body); | ||
| if(!jsonBody) { | ||
| callback({ message: errorMessage.parseBody, response: response }); | ||
| return; | ||
| } | ||
| // valid data | ||
| if(!jsonBody.results) { | ||
| callback({ message: 'results not in body', response: response }); | ||
| return; | ||
| } | ||
| // callback with items | ||
| callback(null, jsonBody.results); | ||
| }); | ||
| } | ||
| // find movie by identity | ||
| // params | ||
| // - id: identity of movie | ||
| // - callback: function will call | ||
| // params | ||
| // - error: error occur during process | ||
| // - item: movie correct with identity | ||
| prototype.findMovieById = function(id, callback) { | ||
| var self = this; | ||
| var params = self.queryParams(); | ||
| var options = {url: self.apiUrl(self.api.path.findMovieById) + '/' + id, | ||
| qs: params | ||
| }; | ||
| request(options, function(err, res, body) { | ||
| // check error | ||
| if(err) { callback(err); return; } | ||
| if(res.statusCode != 200) { callback(res); return; } | ||
| // parse body to javascript object | ||
| var jsonBody = JSON.parse(body); | ||
| if(!jsonBody) { | ||
| callback({ message: errorMessage.parseBody, response: response }); | ||
| return; | ||
| } | ||
| // callback with items | ||
| callback(null, jsonBody); | ||
| }); | ||
| }; | ||
| // count number of movie match with selector | ||
| // params | ||
| // - selector: same as findMovie | ||
| // - callback: function will call | ||
| // params | ||
| // - error: error occur during process | ||
| // - size: number of item match with selector | ||
| prototype.movieSize = function(selector, callback) { | ||
| var self = this; | ||
| var params = self.queryParams(); | ||
| var options = {url: self.apiUrl(self.api.path.findMovie), qs: params }; | ||
| request(options, function(err, res, body) { | ||
| // check error | ||
| if(err) { callback(err); return; } | ||
| if(res.statusCode != 200) { callback(res); return; } | ||
| // parse body to javascript object | ||
| var jsonBody = JSON.parse(body); | ||
| if(!jsonBody) { | ||
| callback({ message: errorMessage.parseBody, response: response }); | ||
| return; | ||
| } | ||
| // valid data | ||
| if(!jsonBody.total_results) { | ||
| callback({ message: 'total_results not in body', | ||
| response: response | ||
| }); | ||
| return; | ||
| } | ||
| // callback with items | ||
| callback(null, jsonBody.total_results); | ||
| }); | ||
| }; | ||
| // movie size | ||
| // params | ||
| // - selector: same as findMovie | ||
| // find tv show | ||
| // prams | ||
| // - selector: object contain information to match | ||
| // - pageIndex: index of page, [1, 1000] | ||
| // - callback: function will call | ||
| // prams | ||
| // - error: error occur during process | ||
| // - items: items match with selector | ||
| prototype.findTv = function(selector, callback) { | ||
| var self = this; | ||
| var params = self.queryParams(); | ||
| params.page = selector.pageIndex || findParams.pageIndex; | ||
| var options = {url: self.apiUrl(self.api.path.findTv), qs: params}; | ||
| request(options, function(err, res, body) { | ||
| // check error | ||
| if(err) { callback(err); return; } | ||
| if(res.statusCode != 200) { callback(res); return; } | ||
| // parse body to javascript object | ||
| var jsonBody = JSON.parse(body); | ||
| if(!jsonBody) { | ||
| callback({ message: errorMessage.parseBody, response: response }); | ||
| return; | ||
| } | ||
| // valid data | ||
| if(!jsonBody.results) { | ||
| callback({ message: 'results not in body', response: response }); | ||
| return; | ||
| } | ||
| // callback with items | ||
| callback(null, jsonBody.results); | ||
| }); | ||
| } | ||
| // find tv by identity | ||
| // params | ||
| // - id: identity of movie | ||
| // - callback: function will call | ||
| // params | ||
| // - error: error occur during process | ||
| // - item: movie correct with identity | ||
| prototype.findTvById = function(id, callback) { | ||
| var self = this; | ||
| var params = self.queryParams(); | ||
| var options = {url: self.apiUrl(self.api.path.findTvById) + '/' + id, | ||
| qs: params | ||
| }; | ||
| request(options, function(err, res, body) { | ||
| // check error | ||
| if(err) { callback(err); return; } | ||
| if(res.statusCode != 200) { callback(res); return; } | ||
| // parse body to javascript object | ||
| var jsonBody = JSON.parse(body); | ||
| if(!jsonBody) { | ||
| callback({ message: errorMessage.parseBody, response: response }); | ||
| return; | ||
| } | ||
| // callback with items | ||
| callback(null, jsonBody); | ||
| }); | ||
| }; | ||
| // count number of tv match with selector | ||
| // params | ||
| // - selector: same as findTv | ||
| // - callback: function will call | ||
| // params | ||
| // - error: error occur during process | ||
| // - size: number of item match with selector | ||
| prototype.tvSize = function(selector, callback) { | ||
| var self = this; | ||
| var params = self.queryParams(); | ||
| var options = {url: self.apiUrl(self.api.path.findTv), qs: params }; | ||
| request(options, function(err, res, body) { | ||
| // check error | ||
| if(err) { callback(err); return; } | ||
| if(res.statusCode != 200) { callback(res); return; } | ||
| // parse body to javascript object | ||
| var jsonBody = JSON.parse(body); | ||
| if(!jsonBody) { | ||
| callback({ message: errorMessage.parseBody, response: response }); | ||
| return; | ||
| } | ||
| // valid data | ||
| if(!jsonBody.total_results) { | ||
| callback({ message: 'total_results not in body', | ||
| response: response | ||
| }); | ||
| return; | ||
| } | ||
| // callback with items | ||
| callback(null, jsonBody.total_results); | ||
| }); | ||
| }; | ||
| // query params | ||
| // using for generate query params | ||
| // return object contains configuration properties for tmdb api | ||
| prototype.queryParams = function() { | ||
| var self = this; | ||
| return { | ||
| api_key: self.api.key | ||
| }; | ||
| } | ||
| module.exports = TmdbClient; |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
15753
25.67%368
27.34%131
7.38%1
Infinity%