-
Notifications
You must be signed in to change notification settings - Fork 0
/
tmdb.js
109 lines (88 loc) · 2.3 KB
/
tmdb.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*jshint node:true*/
"use strict";
var moviedb = require("moviedb");
var request = require("request");
var when = require("when");
var tmdb;
var THROTTLE_MSECS = 333;
var currentRequest = when.resolve();
function throttledRequest(method, data, callback) {
// Ensure we got configuration first
getConfiguration().then(function() {
throttledNoConfigRequest(method, data, callback);
}, function(err) {
callback(err);
});
}
function throttledNoConfigRequest(method, data, callback) {
var current = currentRequest;
var next = when.defer();
currentRequest = next.promise;
current.then(function doRequest() {
tmdb[method](data, function(err, data) {
callback(err, data);
setTimeout(function() {
next.resolve();
}, THROTTLE_MSECS);
});
});
}
var configuration;
function getConfiguration() {
if (configuration) {
return when(configuration);
} else {
var d = when.defer();
throttledNoConfigRequest("configuration", {}, function(e, data) {
if (e) {
d.reject(e);
} else {
configuration = data;
d.resolve(data);
}
});
return d.promise;
}
}
module.exports = {
setAPIKey: function(key) {
tmdb = moviedb(key);
},
findShowImages: function(show, cb) {
if (!tmdb) {
return cb(new Error("API key for TheMovieDB was not set"));
}
throttledRequest("searchTV", { query: show }, function(e, data) {
if (e) {
cb(e);
} else if (data.results.length === 0) {
cb(null, {});
} else {
// Try to find exact show title match, or fall back to first result
var match = data.results.filter(function(result) {
return result.name.trim().toLowerCase() === show.trim().toLowerCase();
})[0] || data.results[0];
var urls = [];
if (match.poster_path) {
urls.push(["poster", match.poster_path]);
}
if (match.backdrop_path) {
urls.push(["backdrop", match.backdrop_path]);
}
var buffers = {};
when.map(urls, function(u) {
var d = when.defer();
request({ url: configuration.images.base_url + "original" + u[1], encoding: null }, function(err, response, data) {
if (!err && response.statusCode === 200) {
buffers[u[0]] = { data: data, type: response.headers["content-type"] };
}
d.resolve();
});
return d.promise;
}).then(function() {
cb(null, buffers);
});
}
});
}
};