aboutsummaryrefslogtreecommitdiffstats
path: root/routes/character-images.js
blob: 581f5b63fc8e874243c1c0fabe2215c03e23b43a (plain)
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
var request = require('request');
var config = require('../config');

module.exports = function(req, res) {
	this.series_id = req.params.series_id;
	this.data = {};
	
	this.data.character_images = {
		characters: []
	};
	
	this.series_request = function(series_id) {
		var that = this;
		var url = config.tms.host + '/v1/series/' + series_id + '?api_key=' + config.tms.key;
		request(url, function(error, response, body) {
			if (!error && response.statusCode == 200) {
				var r = JSON.parse(body);
				
				var request_count = 0;
				var actor_count = r.cast.length;
				var the_other = that;
				for (var i = 0; i < r.cast.length; i++) {
					if (r.cast[i].role === 'Actor') {
						that.person_request(r.cast[i].personId, {character_name: r.cast[i].characterName}, function(success) {
							if (request_count == (r.cast.length - 1)) {
								the_other.render();
							}
							
							if (!success) {
								actor_count--;
							}
							request_count++;
						});
					}
				}
			}
		});
	};
	
	this.person_request = function(person_id, params, callback) {
		var that = this;
		var params = params || {
			character_name: ''
		};
		var url = config.tms.host + '/v1/celebs/' + person_id + '?api_key=' + config.tms.key;
		request(url, function(error, response, body) {
			if (!error && response.statusCode == 200) {
				var r = JSON.parse(body);
				
				that.data.character_images.characters.push({
					image: 'http://developer.tmsimg.com/' + r.preferredImage.uri + '?api_key=' + config.tms.key + '&amp;h=100',
					name: params.character_name,
					tms_personId: r.personId
				});
				console.log(r);
				
				callback(true);
			}
			else {
				callback(false);
			}
		});
	};
	
	this.series_request(this.series_id);
	
	this.render = function() {
		res.render('character-images', this.data);
	};
};