diff options
| author | Teddy Wing | 2013-04-28 00:56:35 -0400 |
|---|---|---|
| committer | Teddy Wing | 2013-04-28 00:56:35 -0400 |
| commit | a1a3d44c8703f7f63a51106fa8777c2f3eee8e89 (patch) | |
| tree | dcb558930638a3103fe4d5e2b551c8a18bae3c6c | |
| parent | 6331e7dbb295700772548ac6b07a501c0309013b (diff) | |
| download | Who-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
| -rw-r--r-- | app.js | 1 | ||||
| -rw-r--r-- | config.js | 8 | ||||
| -rw-r--r-- | public/javascripts/main.js | 2 | ||||
| -rw-r--r-- | routes/character-images.js | 75 | ||||
| -rw-r--r-- | views/index.ejs | 2 |
5 files changed, 62 insertions, 26 deletions
@@ -5,6 +5,7 @@ var express = require('express') , routes = require('./routes') + , config = require('./config') , http = require('http') , path = require('path'); diff --git a/config.js b/config.js new file mode 100644 index 0000000..8c417ee --- /dev/null +++ b/config.js @@ -0,0 +1,8 @@ +var config = {} + +config.tms = { + host: 'http://data.tmsapi.com', + key: process.env.TMS_API_KEY +}; + +module.exports = config;
\ No newline at end of file diff --git a/public/javascripts/main.js b/public/javascripts/main.js index c06d9d6..c24ecd2 100644 --- a/public/javascripts/main.js +++ b/public/javascripts/main.js @@ -11,7 +11,7 @@ return cf; }; - initialise_content_flow(); + // initialise_content_flow(); // Populate characters 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 diff --git a/views/index.ejs b/views/index.ejs index 0f11731..d7282b6 100644 --- a/views/index.ejs +++ b/views/index.ejs @@ -48,7 +48,7 @@ <script type="text/template" id="character-image-template"> <div class="item"> - <img class="content" src="{{ image }}" alt="{{ name }}" /> + <img class="content" src="http://developer.tmsimg.com/{{ image }}" alt="{{ name }}" /> <div class="caption">{{ name }}</div> </div> </script> |
