-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
67 lines (54 loc) · 1.45 KB
/
index.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
var request = require('superagent');
// Tomato factory
module.exports = function tomatoes(key) {
return new Tomato(key);
};
// Constructor
function Tomato(key) {
this.key = key;
}
// Search API
Tomato.prototype.search = function(title, done) {
request
.get('http://api.rottentomatoes.com/api/public/v1.0/movies.json')
.send({ q: title })
.send({ apikey: this.key })
.send({ page_limit: 50 })
.end(function(err, res) {
var body = toJSON(res.text);
var result = body && body.movies || [];
return done(err, result);
});
};
// Get API
Tomato.prototype.get = function(id, done) {
request
.get('http://api.rottentomatoes.com/api/public/v1.0/movies/' + id + '.json')
.send({ apikey: this.key })
.end(function(err, res) {
var body = toJSON(res.text);
var result = body && body.id ? body : undefined;
return done(err, result);
});
};
// Get List
// listType : movies or dvds
// listName: box_office, in_theaters etc...
Tomato.prototype.getList = function(listType, listName, done) {
request
.get('http://api.rottentomatoes.com/api/public/v1.0/lists/' + listType + '/' + listName + '.json')
.send({ apiKey: this.key })
.end(function(err, res) {
var body = toJSON(res.text);
var result = body ? body : undefined;
return done(err, result);
});
};
// Utils
function toJSON(str) {
var result;
try {
result = JSON.parse(str);
} catch (err) {}
return result;
}