mirror of
https://github.com/KevinMidboe/moviedb.git
synced 2025-10-29 17:50:25 +00:00
initial commit
This commit is contained in:
11
Makefile
Normal file
11
Makefile
Normal file
@@ -0,0 +1,11 @@
|
||||
ALL_TESTS = $(shell find test/ -name '*.test.js')
|
||||
|
||||
run-tests:
|
||||
@./node_modules/.bin/mocha \
|
||||
$(TESTFLAGS) \
|
||||
$(TESTS)
|
||||
|
||||
test:
|
||||
@$(MAKE) NODE_PATH=lib TESTS="$(ALL_TESTS)" run-tests
|
||||
|
||||
.PHONY: test
|
||||
7
example.js
Normal file
7
example.js
Normal file
@@ -0,0 +1,7 @@
|
||||
var mdb = require('./lib/moviedb');
|
||||
|
||||
var m = new mdb('yout api key');
|
||||
|
||||
m.searchMovie({query: 'alien'}, function(err, res){
|
||||
console.log(res);
|
||||
});
|
||||
25
lib/auth.js
Normal file
25
lib/auth.js
Normal file
@@ -0,0 +1,25 @@
|
||||
|
||||
/**
|
||||
* Module dependencies
|
||||
*/
|
||||
|
||||
var request = require('superagent')
|
||||
, endpoints = require('./endpoints.json')
|
||||
, MovieDB = module.parent.exports;
|
||||
|
||||
|
||||
MovieDB.prototype.requestToken = function(fn){
|
||||
var that = this;
|
||||
|
||||
request
|
||||
.get(endpoints.base_url + endpoints.authentication.requestToken)
|
||||
.send({api_key: that.api_key})
|
||||
.set('Accept', 'application/json')
|
||||
.end(function(res){
|
||||
if(res.ok) that.token = res.body;
|
||||
else throw new Error('Invalid authentication');
|
||||
|
||||
fn();
|
||||
});
|
||||
};
|
||||
|
||||
78
lib/endpoints.json
Normal file
78
lib/endpoints.json
Normal file
@@ -0,0 +1,78 @@
|
||||
{
|
||||
"base_url": "http://api.themoviedb.org/3/"
|
||||
, "configuration" : "configuration"
|
||||
, "authentication" : {
|
||||
"requestToken" : "authentication/token/new"
|
||||
, "sessionId" : "authentication/session/new"
|
||||
}
|
||||
, "methods" : {
|
||||
"search" : {
|
||||
"get" : {
|
||||
"Movie" : "search/movie"
|
||||
, "Person" : "search/person"
|
||||
}
|
||||
, "post" : {
|
||||
}
|
||||
}
|
||||
, "collection" : {
|
||||
"get" : {
|
||||
"Info" : "collection/:id"
|
||||
}
|
||||
, "post" : {
|
||||
}
|
||||
}
|
||||
, "movie" : {
|
||||
"get" : {
|
||||
"Info" : "movie/:id"
|
||||
, "AlternativeTitles" : "movie/:id/alternative_titles"
|
||||
, "Casts" : "movie/:id/casts"
|
||||
, "Images" : "movie/:id/images"
|
||||
, "Keywords" : "movie/:id/keywords"
|
||||
, "Releases" : "movie/:id/releases"
|
||||
, "Trailers" : "movie/:id/trailers"
|
||||
, "Translations" : "movie/:id/translations"
|
||||
, "Similar" : "movie/:id/similar_movies"
|
||||
}
|
||||
, "post" : {
|
||||
}
|
||||
}
|
||||
, "person" : {
|
||||
"get" : {
|
||||
"Info" : "person/:id"
|
||||
, "Credits" : "person/:id/credits"
|
||||
, "Images" : "person/:id/images"
|
||||
}
|
||||
, "post" : {
|
||||
}
|
||||
}
|
||||
, "misc" : {
|
||||
"get" : {
|
||||
"LatestMovie" : "latest/movie"
|
||||
, "NowPlaying" : "movie/now-playing"
|
||||
, "PopularMovies" : "movie/popular"
|
||||
, "TopRatedMovies" : "movie/top-rated"
|
||||
}
|
||||
, "post" : {
|
||||
"AddMovieRating" : "movie/:id/rating"
|
||||
}
|
||||
}
|
||||
, "company" : {
|
||||
"get" : {
|
||||
"Info" : "company/:id"
|
||||
, "Movies" : "company/:id/movies"
|
||||
}
|
||||
, "post" : {
|
||||
}
|
||||
}
|
||||
, "account" : {
|
||||
"get" : {
|
||||
"Info" : "account"
|
||||
, "FavoriteMovies" : "account/:id/favorite_movies"
|
||||
, "RatedMovies" : "account/:id/rated_movies"
|
||||
}
|
||||
, "post" : {
|
||||
"AddFavorite" : "account/:id/favorite"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
21
lib/moviedb.js
Normal file
21
lib/moviedb.js
Normal file
@@ -0,0 +1,21 @@
|
||||
|
||||
/*
|
||||
* Exports the constructor
|
||||
*/
|
||||
|
||||
module.exports = function MovieDB(api_key){
|
||||
if(api_key) this.api_key = api_key;
|
||||
else throw new Error('Bad api key');
|
||||
};
|
||||
|
||||
/*
|
||||
* API auth
|
||||
*/
|
||||
|
||||
require('./auth');
|
||||
|
||||
/*
|
||||
* API request
|
||||
*/
|
||||
|
||||
require('./request');
|
||||
116
lib/request.js
Normal file
116
lib/request.js
Normal file
@@ -0,0 +1,116 @@
|
||||
|
||||
/**
|
||||
* Module dependencies
|
||||
*/
|
||||
|
||||
var request = require('superagent')
|
||||
var endpoints = require('./endpoints.json')
|
||||
, methods = endpoints.methods
|
||||
, base_url = endpoints.base_url
|
||||
, MovieDB = module.exports = module.parent.exports;
|
||||
|
||||
var get, post;
|
||||
|
||||
Object.keys(methods).forEach(function(method){
|
||||
get = methods[method].get;
|
||||
post = methods[method].post;
|
||||
|
||||
// GET requests
|
||||
|
||||
Object.keys(methods[method].get).forEach(function(g){
|
||||
|
||||
MovieDB.prototype[method + g] = function(params, fn){
|
||||
|
||||
var that = this;
|
||||
|
||||
if(!this.token || Date.now() > +new Date(this.token.expires_at)) {
|
||||
|
||||
this.requestToken(function(){
|
||||
execMethod.call(that, 'get', params, methods[method].get[g], fn);
|
||||
});
|
||||
|
||||
} else {
|
||||
|
||||
execMethod.call(this, 'get', methods[method].get[g], fn);
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
});
|
||||
|
||||
// POST requests
|
||||
Object.keys(methods[method].post).forEach(function(p){
|
||||
|
||||
MovieDB.prototype[method + p] = function(params, fn){
|
||||
|
||||
var that = this;
|
||||
|
||||
if(!this.token || Date.now() > +new Date(this.token.expires_at)) {
|
||||
|
||||
this.requestToken(function(){
|
||||
execMethod.call(that, 'post', params, methods[method].post[p], fn);
|
||||
});
|
||||
|
||||
} else {
|
||||
|
||||
execMethod.call(this, 'post', params, methods[method].post[p], fn);
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
var execMethod = function(type, params, endpoint, fn){
|
||||
|
||||
params = params || {};
|
||||
endpoint = endpoint.replace(':id', params.id);
|
||||
delete params.id;
|
||||
|
||||
if(type === 'post'){
|
||||
|
||||
request
|
||||
.post(base_url + endpoint)
|
||||
.query({api_key : this.api_key})
|
||||
.send(params)
|
||||
.set('Accept', 'application/json')
|
||||
.end(function(res){
|
||||
if(res.ok){
|
||||
try{
|
||||
var body = JSON.parse(res.text);
|
||||
fn(null, body);
|
||||
} catch(e){
|
||||
fn(e, null);
|
||||
}
|
||||
|
||||
} else {
|
||||
fn(res.error, null);
|
||||
}
|
||||
});
|
||||
|
||||
} else {
|
||||
|
||||
request
|
||||
.get(base_url + endpoint)
|
||||
.send(params)
|
||||
.set('Accept', 'application/json')
|
||||
.end(function(res){
|
||||
if(res.ok){
|
||||
try{
|
||||
var body = JSON.parse(res.text);
|
||||
fn(null, body);
|
||||
} catch(e){
|
||||
fn(e, null);
|
||||
}
|
||||
|
||||
} else {
|
||||
fn(res.error, null);
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
10
package.json
Normal file
10
package.json
Normal file
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"name" : "moviedb"
|
||||
, "version" : "0.0.1"
|
||||
, "dependencies" : {
|
||||
"superagent" : "0.4.x"
|
||||
}
|
||||
, "devDependencies" : {
|
||||
"mocha" : "1.0.x"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user