1 /*jslint 2 browser: true, 3 nomen: false, 4 debug: true, 5 forin: true, 6 undef: true, 7 white: false, 8 onevar: false 9 */ 10 var sc, DOMParser, jQuery, sch; 11 12 /** 13 * A library to interact with the API for theMovieDB.org 14 * @see <a href="http://api.themoviedb.org/2.1/">The API docs</a> 15 */ 16 17 /** 18 * events raised here 19 */ 20 if (!sc.events) { sc.events = {}; } 21 sc.events.tmdbMethodSuccess = 'tmdbMethodSuccess'; 22 sc.events.tmdbMethodFailure = 'tmdbMethodFailure'; 23 sc.events.tmdbMovieSearchSuccess = 'tmdbMovieSearchSuccess'; 24 sc.events.tmdbMovieSearchFailure = 'tmdbMovieSearchFailure'; 25 sc.events.tmdbMovieIMDBLookupSuccess = 'tmdbMovieIMDBLookupSuccess'; 26 sc.events.tmdbMovieIMDBLookupFailure = 'tmdbMovieIMDBLookupFailure'; 27 sc.events.tmdbMovieGetInfoSuccess = 'tmdbMovieGetInfoSuccess'; 28 sc.events.tmdbMovieGetInfoFailure = 'tmdbMovieGetInfoFailure'; 29 sc.events.tmdbMovieGetImagesSuccess = 'tmdbMovieGetImagesSuccess'; 30 sc.events.tmdbMovieGetImagesFailure = 'tmdbMovieGetImagesFailure'; 31 sc.events.tmdbPersonSearchSuccess = 'tmdbPersonSearchSuccess'; 32 sc.events.tmdbPersonSearchFailure = 'tmdbPersonSearchFailure'; 33 sc.events.tmdbPersonGetInfoSuccess = 'tmdbPersonGetInfoSuccess'; 34 sc.events.tmdbPersonGetInfoFailure = 'tmdbPersonGetInfoFailure'; 35 sc.events.tmdbHashGetInfoSuccess = 'tmdbHashGetInfoSuccess'; 36 sc.events.tmdbHashGetInfoFailure = 'tmdbHashGetInfoFailure'; 37 38 39 40 /** 41 * @constructor 42 * @param {Object} opts 43 * @param {string} opts.apikey the api key 44 * @param {string} [opts.lang] a language code. default is 'en' 45 * @param {string} [opts.format] the data format to return. default is 'json' 46 * @param {DOMElement} [opts.eventTarget] what to target triggered events with. default is the document element 47 */ 48 function SpazTMDB(opts) { 49 50 /* 51 set defaults 52 */ 53 opts = sch.defaults({ 54 'apikey':null, 55 'lang' :'en', 56 'format':'json', 57 'eventTarget':document 58 }, opts); 59 60 this.apikey = opts.apikey; 61 this.lang = opts.lang; 62 this.format = opts.format; 63 this.eventTarget = opts.eventTarget; 64 65 this.baseURL = 'http://api.themoviedb.org/2.1/'; 66 67 } 68 69 /** 70 * Sets the API key 71 * @param {string} apikey the api key used to access the API 72 */ 73 SpazTMDB.prototype.setAPIKey = function(apikey) { 74 this.apikey = apikey; 75 }; 76 77 /** 78 * Gets the API key 79 * @returns {string} the api key that was previously set 80 */ 81 SpazTMDB.prototype.getAPIKey = function() { 82 return this.apikey; 83 }; 84 85 /** 86 * Search for movies by title 87 * @param {string} value the value passed to the search method 88 * @param {function} [onSuccess] a callback 89 * @param {function} [onFailure] a callback 90 */ 91 SpazTMDB.prototype.movieSearch = function(value, onSuccess, onFailure) { 92 this.callMethod({ 93 'method':'Movie.search', 94 'value' :value, 95 'successEvent':sc.events.tmdbMovieSearchSuccess, 96 'failureEvent':sc.events.tmdbMovieSearchFailure, 97 'onSuccess':onSuccess, 98 'onFailure':onFailure 99 }); 100 }; 101 102 103 104 /** 105 * Get info for a movie 106 * @param {string|number} id The id of the movie (numeric) 107 * @param {function} [onSuccess] a callback 108 * @param {function} [onFailure] a callback 109 */ 110 SpazTMDB.prototype.movieInfo = function(id, onSuccess, onFailure) { 111 this.callMethod({ 112 'method':'Movie.getInfo', 113 'value' :id, 114 'successEvent':sc.events.tmdbMovieGetInfoSuccess, 115 'failureEvent':sc.events.tmdbMovieGetInfoFailure, 116 'onSuccess':onSuccess, 117 'onFailure':onFailure 118 }); 119 }; 120 121 122 123 /** 124 * Get images for a movie 125 * @param {string|number} id The id of the movie (numeric) 126 * @param {function} [onSuccess] a callback 127 * @param {function} [onFailure] a callback 128 */ 129 SpazTMDB.prototype.movieImages = function(id, onSuccess, onFailure) { 130 this.callMethod({ 131 'method':'Movie.getImages', 132 'value' :id, 133 'successEvent':sc.events.tmdbMovieGetInfoSuccess, 134 'failureEvent':sc.events.tmdbMovieGetInfoFailure, 135 'onSuccess':onSuccess, 136 'onFailure':onFailure 137 }); 138 }; 139 140 141 142 /** 143 * Lookup a movie by IMDB id 144 * @param {string} id The IMDB id of the movie. ex "tt0137523" 145 * @param {function} [onSuccess] a callback 146 * @param {function} [onFailure] a callback 147 */ 148 SpazTMDB.prototype.movieInfoIMDB = function(id, onSuccess, onFailure) { 149 this.callMethod({ 150 'method':'Movie.imdbLookup', 151 'value' :id, 152 'successEvent':sc.events.tmdbMovieIMDBLookupSuccess, 153 'failureEvent':sc.events.tmdbMovieIMDBLookupFailure, 154 'onSuccess':onSuccess, 155 'onFailure':onFailure 156 }); 157 }; 158 159 160 161 /** 162 * Search for a person 163 * @param {string|number} id The id of the person (numeric) 164 * @param {function} [onSuccess] a callback 165 * @param {function} [onFailure] a callback 166 */ 167 SpazTMDB.prototype.personInfo = function(id, onSuccess, onFailure) { 168 this.callMethod({ 169 'method':'Person.getInfo', 170 'value' :id, 171 'successEvent':sc.events.tmdbPersonGetInfoSuccess, 172 'failureEvent':sc.events.tmdbPersonGetInfoFailure, 173 'onSuccess':onSuccess, 174 'onFailure':onFailure 175 }); 176 }; 177 178 179 180 /** 181 * Search for a person 182 * @param {string} name The name to search for 183 * @param {function} [onSuccess] a callback 184 * @param {function} [onFailure] a callback 185 */ 186 SpazTMDB.prototype.personSearch = function(name, onSuccess, onFailure) { 187 this.callMethod({ 188 'method':'Person.search', 189 'value' :name, 190 'successEvent':sc.events.tmdbPersonSearchSuccess, 191 'failureEvent':sc.events.tmdbPersonSearchFailure, 192 'onSuccess':onSuccess, 193 'onFailure':onFailure 194 }); 195 }; 196 197 198 199 200 /** 201 * Get movie info by file hash 202 * @param {string} hash The hash corresponding to the movie 203 * @param {function} [onSuccess] a callback 204 * @param {function} [onFailure] a callback 205 * @see <a href="http://trac.opensubtitles.org/projects/opensubtitles/wiki/HashSourceCodes">Hash Source Codes</a> 206 */ 207 SpazTMDB.prototype.movieInfoHash = function(hash, onSuccess, onFailure) { 208 this.callMethod({ 209 'method':'Hash.getInfo', 210 'value' :hash, 211 'successEvent':sc.events.tmdbHashGetInfoSuccess, 212 'failureEvent':sc.events.tmdbHashGetInfoFailure, 213 'onSuccess':onSuccess, 214 'onFailure':onFailure 215 }); 216 }; 217 218 219 220 221 222 223 /** 224 * Method to construct an API URL from the passed method and value strings 225 * @param {string} method the string for this parameter. See API docs for list 226 * @param {string} value the value we're passing to the API method. This will be encoded using encodeURIComponent() 227 * @returns {string} the URL string 228 */ 229 SpazTMDB.prototype.getURL = function(method, value) { 230 var url = this.baseURL + method + "/" + this.lang + "/" + this.format + "/" + this.apikey + "/" + encodeURIComponent(value); 231 return url; 232 }; 233 234 235 236 237 /** 238 * a general purpose method for calling API methods via ajax and raising 239 * events on success/failure. callbacks can optionally be set for success 240 * or failure as well 241 * @param {Object} opts options for the method call 242 * @param {string} opts.method the method to call 243 * @param {string} opts.value value passed to method 244 * @param {string} [opts.successEvent] the type of event to raise on success. default is {@link sc.events.tmdbMethodSuccess} 245 * @param {string} [opts.failureEvent] the type of event to raise on failure. default is {@link sc.events.tmdbMethodFailure} 246 * @param {function} [opts.onSuccess] a callback function called on success. takes args data, textStatus 247 * @param {function} [opts.onFailure] a callback function called on failure. takes args xhr, msg, exc 248 * 249 */ 250 SpazTMDB.prototype.callMethod = function(opts) { 251 var that = this; 252 253 opts = sch.defaults({ 254 'method' :'Movie.search', 255 'value' :'Road House', 256 'successEvent':sc.events.tmdbMethodSuccess, 257 'failureEvent':sc.events.tmdbMethodFailure, 258 'onSuccess' :null, // callback on success 259 'onFailure' :null // callback on failure 260 }, opts); 261 262 var url = this.getURL(opts.method, opts.value); 263 264 jQuery.ajax({ 265 'url' :url, 266 'type':'GET', 267 'success':function(data, textStatus) { 268 if (opts.onSuccess) { 269 opts.onSuccess.call(that, data, textStatus); 270 } 271 sch.trigger(opts.successEvent, that.eventTarget, data); 272 }, 273 'error':function(xhr, msg, exc) { 274 if (opts.onFailure) { 275 opts.onFailure.call(that, xhr, msg, exc); 276 } 277 sch.trigger(opts.failure, that.eventTarget, {'url':url, 'xhr':xhr, 'msg':msg}); 278 } 279 }); 280 }; 281 282