aboutsummaryrefslogtreecommitdiffstats
path: root/routes
diff options
context:
space:
mode:
authorTeddy Wing2013-04-28 00:56:35 -0400
committerTeddy Wing2013-04-28 00:56:35 -0400
commita1a3d44c8703f7f63a51106fa8777c2f3eee8e89 (patch)
treedcb558930638a3103fe4d5e2b551c8a18bae3c6c /routes
parent6331e7dbb295700772548ac6b07a501c0309013b (diff)
downloadWho-am-I-a1a3d44c8703f7f63a51106fa8777c2f3eee8e89.tar.bz2
Dynamically fetch character images from TMS API
* /character-images endpoint now dynamically fetches images from the TMS API * Add a config.js file
Diffstat (limited to 'routes')
-rw-r--r--routes/character-images.js75
1 files changed, 51 insertions, 24 deletions
diff --git a/routes/character-images.js b/routes/character-images.js
index dd63c83..49fe3ae 100644
--- a/routes/character-images.js
+++ b/routes/character-images.js
@@ -1,30 +1,57 @@
+var request = require('request');
+var config = require('../config');
+
module.exports = function(req, res) {
- var data = {};
+ this.data = {};
- data.character_images = {
- characters: [
- {
- image: '/images/characters-sample/homer.png',
- name: 'Homer'
- },
- {
- image: '/images/characters-sample/marge.png',
- name: 'Marge'
- },
- {
- image: '/images/characters-sample/lisa.png',
- name: 'Lisa'
- },
- {
- image: '/images/characters-sample/bart.png',
- name: 'Bart'
- },
- {
- image: '/images/characters-sample/maggie.png',
- name: 'Maggie'
+ 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 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, function() {
+ if (request_count == (r.cast.length - 1)) {
+ the_other.render();
+ }
+
+ request_count++;
+ });
+ }
+ }
}
- ]
+ });
};
- res.render('character-images', data);
+ this.person_request = function(person_id, callback) {
+ var that = this;
+ 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: r.preferredImage.uri + '?api_key=' + config.tms.key,
+ name: r.preferredImage.caption.content
+ });
+
+ callback();
+ }
+ });
+ };
+
+ this.series_request(8680539);
+
+ this.render = function() {
+ res.render('character-images', this.data);
+ };
}; \ No newline at end of file