├── .gitignore ├── example ├── movie.js └── search.js ├── package.json ├── README.md └── lib └── allocine-api.js /.gitignore: -------------------------------------------------------------------------------- 1 | *.sublime* 2 | .DS_Store -------------------------------------------------------------------------------- /example/movie.js: -------------------------------------------------------------------------------- 1 | var allocine = require('../lib/allocine-api'); 2 | 3 | allocine.api('movie', {code: '143067'}, function(error, result) { 4 | if(error) { console.log('Error : '+ error); return; } 5 | 6 | console.log('Success !'); 7 | console.log(result.movie.title); 8 | }); -------------------------------------------------------------------------------- /example/search.js: -------------------------------------------------------------------------------- 1 | var allocine = require('../lib/allocine-api'); 2 | 3 | allocine.api('search', {q: 'spiderman', filter: 'movie'}, function(error, results) { 4 | if(error) { console.log('Error : '+ error); return; } 5 | 6 | console.log('Success !'); 7 | console.log(results); 8 | }); -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "allocine-api", 3 | "version": "0.1.9", 4 | "author": "Leeroy Brun ", 5 | "description": "Simple module used to access the Allocine API", 6 | "repository": { 7 | "type": "git", 8 | "url": "https://github.com/leeroybrun/node-allocine-api.git" 9 | }, 10 | "keywords": [ 11 | "allocine", 12 | "movies", 13 | "tv shows" 14 | ], 15 | "license": "MIT", 16 | "engines": [ 17 | "node" 18 | ], 19 | "main": "./lib/allocine-api.js" 20 | } 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # node-allocine-api 2 | 3 | [![NPM](https://nodei.co/npm/allocine-api.png)](https://nodei.co/npm/allocine-api/) 4 | 5 | Module Node.js permettant d'accéder à l'API d'Allociné. 6 | 7 | Aucune dépendance nécessaire ! Il utilise uniquement des modules du core. 8 | 9 | Il s'agit de l'un de mes premiers modules Node.js, donc soyez indulgents et n'hésitez pas à me suggerer des améliorations ! 10 | 11 | ## Installation 12 | 13 | Pour l'installer, ajoutez-le aux dépendances de votre fichier package.json : 14 | 15 | ```json 16 | "dependencies" : { 17 | ... 18 | "allocine-api": "*" 19 | } 20 | ``` 21 | 22 | Appelez ensuite simplement `npm install`, et npm installera le module pour vous. 23 | 24 | Vous pouvez aussi simplement appeler `npm install allocine-api`. 25 | 26 | ## Utilisation 27 | 28 | Pour l'utiliser, incluez-le simplement dans les fichiers de votre application : 29 | 30 | ```javascript 31 | var allocine = require('allocine-api'); 32 | ``` 33 | 34 | ### API 35 | 36 | Pour l'instant ce module ne comprend qu'une seule méthode pour accéder à l'API. Celle-ci est plus que suffisante puisqu'elle vous permet d'appeler l'API comme bon vous semble. D'autres méthodes feront peut-être leur apparition plus tard afin de faciliter l'accès à l'API. 37 | 38 | Pour plus d'informations sur l'API Allociné, je vous invite à vous rendre sur le wiki de Gromez : http://wiki.gromez.fr/dev/api/allocine_v3 39 | 40 | #### allocine.api(method, options, callback) 41 | 42 | Cette fonction va appeler l'API définie (`method`) en lui fournissant les `options` (objet) passées en paramètre, puis appelera la fonction de `callback`. Le callback reçoit deux paramètres, le premier est un objet d'erreur (null si aucune erreur), et le deuxième est le résultat retourné par l'API sous forme d'objet. 43 | 44 | Exemples : 45 | ```javascript 46 | // Recherche de tous les films "spiderman" 47 | allocine.api('search', {q: 'spiderman', filter: 'movie'}, function(error, results) { 48 | if(error) { console.log('Error : '+ error); return; } 49 | 50 | console.log('Voici les données retournées par l\'API Allociné:'); 51 | console.log(results); 52 | }); 53 | 54 | // Informations sur un film particulier 55 | allocine.api('movie', {code: '143067'}, function(error, result) { 56 | if(error) { console.log('Error : '+ error); return; } 57 | 58 | console.log('Voici les données retournées par l\'API Allociné:'); 59 | console.log(result); 60 | }); 61 | ``` 62 | 63 | [![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/leeroybrun/node-allocine-api/trend.png)](https://bitdeli.com/free "Bitdeli Badge") 64 | -------------------------------------------------------------------------------- /lib/allocine-api.js: -------------------------------------------------------------------------------- 1 | /* 2 | ------------------------------------------------------------------ 3 | Allocine API module for Node.js 4 | ------------------------------------------------------------------ 5 | 6 | Author : Leeroy Brun (leeroy.brun@gmail.com) 7 | Github repo : https://github.com/leeroybrun/node-allocine-api 8 | Version : 0.1.4 9 | Release date : 23.05.2013 10 | */ 11 | 12 | 13 | var crypto = require('crypto'), 14 | http = require('http'); 15 | 16 | var allocine = function() { 17 | 18 | // Configuration 19 | this.config = { 20 | apiHostName: 'api.allocine.fr', 21 | apiBasePath: '/rest/v3/', 22 | apiPartner: 'V2luZG93czg', 23 | apiSecretKey: 'e2b7fd293906435aa5dac4be670e7982', 24 | imgBaseUrl: 'http://images.allocine.fr', 25 | userAgent: 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Trident/6.0; MSAppHost/1.0)' 26 | }; 27 | 28 | // Presets for the API calls 29 | this.presets = { 30 | global: { 31 | partner: this.config.apiPartner, 32 | format: 'json' 33 | }, 34 | movielist: { profile: 'large' }, 35 | movie: { profile: 'large' }, 36 | tvserieslist: { filter: 'top', order: 'viewcount' }, 37 | tvseries: { profile: 'large' }, 38 | tvseriesbroadcastlist: { profile: 'large' }, 39 | season: { profile: 'large' }, 40 | seasonlist: { profile: 'small' }, 41 | news: { profile: 'large' }, 42 | newslist: { profile: 'large' }, 43 | media: { mediafmt: 'mp4' }, 44 | feature: { profile: 'large' }, 45 | featurelist: { profile: 'large' }, 46 | picturelist: { profile: 'large' }, 47 | videolist: { mediafmt: 'mp4' }, 48 | search: { filter: 'movie,tvseries,theater,news,video' } 49 | } 50 | 51 | // Extend an object with other objects 52 | this.extend = function (dst) { 53 | for (var i = 0; i < arguments.length; i++) { 54 | if (arguments[i] !== dst) { 55 | for(var key in arguments[i]) { 56 | dst[key] = arguments[i][key]; 57 | }; 58 | } 59 | }; 60 | 61 | return dst; 62 | } 63 | 64 | // Build path for accessing Allocine API 65 | this.buildPath = function(route, params) { 66 | var path = this.config.apiBasePath + route; 67 | 68 | // Extend params with route presets 69 | params = this.extend({}, this.presets.global, this.presets[route], params); 70 | 71 | if(params) { 72 | var tokens = []; 73 | 74 | // Fill the tokens array and sort it 75 | for(var param in params) { 76 | tokens.push(param +'='+ encodeURIComponent(params[param])); 77 | } 78 | tokens.sort(); 79 | 80 | path += '?'+ tokens.join('&'); 81 | 82 | // Build and encode path 83 | var date = new Date(); 84 | var sed = date.getFullYear() +''+ ('0'+ (date.getMonth()+1)).slice(-2) +''+ ('0'+ (date.getDate())).slice(-2); 85 | var sig = this.config.apiSecretKey + tokens.join('&') +'&sed='+ sed; 86 | 87 | // Hash "sig" parameter 88 | var shasum = crypto.createHash('sha1') 89 | sig = encodeURIComponent(shasum.update(sig, 'utf-8').digest('base64')); 90 | 91 | return path +'&sed='+ sed +'&sig='+ sig; 92 | } 93 | 94 | return path; 95 | } 96 | 97 | // Request the API with the given path 98 | this.request = function(path, callback) { 99 | var options = { 100 | hostname: this.config.apiHostName, 101 | path: path, 102 | headers: { 103 | 'User-Agent': this.config.userAgent 104 | } 105 | }; 106 | if (this.config.proxyHostName) { 107 | options.path="http://"+options.hostname+options.path; 108 | options.port=this.config.proxyPort; 109 | options.hostname=this.config.proxyHostName; 110 | } 111 | 112 | // Call the API, fetch returned data and pass it to the callback 113 | http.get(options, function(res) { 114 | if(res.statusCode === 200) { 115 | var data = ''; 116 | 117 | var headerContentLength = parseInt(res.headers['content-length'], 10); 118 | var dataContentLength=0; 119 | 120 | res.on('data', function(chunk) { 121 | data += chunk; 122 | dataContentLength += chunk.length; 123 | }); 124 | 125 | res.on('end', function() { 126 | // Verify the length of received data (perhaps not the declared size) 127 | if (!isNaN(headerContentLength) && headerContentLength!==dataContentLength) { 128 | return callback("Invalid size "+dataContentLength+"/"+headerContentLength); 129 | } 130 | // TODO : better error handling, if data contains "error" will be catch as error 131 | if(//i.test(data)) { 132 | callback(data, {}); 133 | } else { 134 | // Success 135 | callback(null, JSON.parse(data)); 136 | } 137 | }); 138 | } else { 139 | // Error 140 | callback(res.statusCode, {}); 141 | } 142 | }).on('error', function(e) { 143 | // Error, we need the error, not the message, to understand what is the problem ! (Timeout ?) 144 | callback(e, {}); 145 | });; 146 | } 147 | 148 | // Main method, used to call the API 149 | this.api = function(method, options, callback) { 150 | var path = this.buildPath(method, options); 151 | 152 | this.request(path, callback); 153 | } 154 | 155 | this.setProxy = function(proxyHostName, proxyPort) { 156 | this.config.proxyHostName=proxyHostName; 157 | this.config.proxyPort=proxyPort; 158 | } 159 | 160 | return this; 161 | } 162 | 163 | module.exports = new allocine(); --------------------------------------------------------------------------------