| 
									
										
										
										
											2013-12-20 01:31:16 -05:00
										 |  |  | var secrets = require('../config/secrets'); | 
					
						
							| 
									
										
										
										
											2013-12-07 00:50:37 -05:00
										 |  |  | var User = require('../models/User'); | 
					
						
							| 
									
										
										
										
											2013-12-10 01:16:03 -05:00
										 |  |  | var querystring = require('querystring'); | 
					
						
							| 
									
										
										
										
											2014-02-10 23:55:32 -05:00
										 |  |  | var validator = require('validator'); | 
					
						
							| 
									
										
										
										
											2013-11-30 00:28:30 -05:00
										 |  |  | var async = require('async'); | 
					
						
							| 
									
										
										
										
											2013-12-07 17:45:20 -05:00
										 |  |  | var cheerio = require('cheerio'); | 
					
						
							| 
									
										
										
										
											2013-12-07 18:53:00 -05:00
										 |  |  | var request = require('request'); | 
					
						
							| 
									
										
										
										
											2013-12-07 22:33:49 -05:00
										 |  |  | var graph = require('fbgraph'); | 
					
						
							| 
									
										
										
										
											2013-12-09 16:57:26 -05:00
										 |  |  | var LastFmNode = require('lastfm').LastFmNode; | 
					
						
							| 
									
										
										
										
											2013-12-07 00:34:23 -05:00
										 |  |  | var tumblr = require('tumblr.js'); | 
					
						
							| 
									
										
										
										
											2013-12-20 01:31:16 -05:00
										 |  |  | var foursquare = require('node-foursquare')({ secrets: secrets.foursquare }); | 
					
						
							| 
									
										
										
										
											2013-12-07 21:44:26 -05:00
										 |  |  | var Github = require('github-api'); | 
					
						
							| 
									
										
										
										
											2013-12-10 02:14:44 -05:00
										 |  |  | var Twit = require('twit'); | 
					
						
							| 
									
										
										
										
											2014-03-31 16:00:51 -04:00
										 |  |  | var stripe =  require('stripe')(secrets.stripe.apiKey); | 
					
						
							| 
									
										
										
										
											2014-02-06 08:09:54 -05:00
										 |  |  | var twilio = require('twilio')(secrets.twilio.sid, secrets.twilio.token); | 
					
						
							| 
									
										
										
										
											2014-02-27 14:48:09 -05:00
										 |  |  | var Linkedin = require('node-linkedin')(secrets.linkedin.clientID, secrets.linkedin.clientSecret, secrets.linkedin.callbackURL); | 
					
						
							| 
									
										
										
										
											2014-02-24 11:25:51 +00:00
										 |  |  | var clockwork = require('clockwork')({key: secrets.clockwork.apiKey}); | 
					
						
							| 
									
										
										
										
											2014-04-22 14:51:35 -04:00
										 |  |  | var ig = require('instagram-node').instagram(); | 
					
						
							| 
									
										
										
										
											2014-05-13 01:35:46 -04:00
										 |  |  | var Y = require('yui/yql'); | 
					
						
							| 
									
										
										
										
											2014-06-06 15:43:49 -04:00
										 |  |  | var _ = require('lodash'); | 
					
						
							| 
									
										
										
										
											2013-11-30 00:28:30 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-06 22:22:12 -05:00
										 |  |  | /** | 
					
						
							|  |  |  |  * GET /api | 
					
						
							| 
									
										
										
										
											2014-01-13 04:24:31 -05:00
										 |  |  |  * List of API examples. | 
					
						
							| 
									
										
										
										
											2013-12-06 22:22:12 -05:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2014-01-13 04:24:31 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-06 22:22:12 -05:00
										 |  |  | exports.getApi = function(req, res) { | 
					
						
							| 
									
										
										
										
											2014-01-07 13:39:25 -05:00
										 |  |  |   res.render('api/index', { | 
					
						
							| 
									
										
										
										
											2014-04-13 23:14:58 -04:00
										 |  |  |     title: 'API Examples' | 
					
						
							| 
									
										
										
										
											2013-11-19 17:28:04 -05:00
										 |  |  |   }); | 
					
						
							| 
									
										
										
										
											2013-11-19 16:20:18 -05:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-06 22:22:12 -05:00
										 |  |  | /** | 
					
						
							|  |  |  |  * GET /api/foursquare | 
					
						
							| 
									
										
										
										
											2014-01-13 04:24:31 -05:00
										 |  |  |  * Foursquare API example. | 
					
						
							| 
									
										
										
										
											2013-12-06 22:22:12 -05:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2014-01-13 04:24:31 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | exports.getFoursquare = function(req, res, next) { | 
					
						
							| 
									
										
										
										
											2014-06-06 16:02:37 -04:00
										 |  |  |   var token = _.find(req.user.tokens, { kind: 'foursquare' }); | 
					
						
							|  |  |  |   console.log(token); | 
					
						
							| 
									
										
										
										
											2013-11-30 00:56:31 -05:00
										 |  |  |   async.parallel({ | 
					
						
							| 
									
										
										
										
											2014-02-27 17:39:18 -05:00
										 |  |  |     trendingVenues: function(callback) { | 
					
						
							|  |  |  |       foursquare.Venues.getTrending('40.7222756', '-74.0022724', { limit: 50 }, token.accessToken, function(err, results) { | 
					
						
							|  |  |  |         callback(err, results); | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2013-11-30 01:12:28 -05:00
										 |  |  |     }, | 
					
						
							| 
									
										
										
										
											2014-02-27 17:39:18 -05:00
										 |  |  |     venueDetail: function(callback) { | 
					
						
							|  |  |  |       foursquare.Venues.getVenue('49da74aef964a5208b5e1fe3', token.accessToken, function(err, results) { | 
					
						
							|  |  |  |         callback(err, results); | 
					
						
							| 
									
										
										
										
											2013-11-30 01:52:29 -05:00
										 |  |  |       }); | 
					
						
							| 
									
										
										
										
											2014-02-27 17:39:18 -05:00
										 |  |  |     }, | 
					
						
							|  |  |  |     userCheckins: function(callback) { | 
					
						
							|  |  |  |       foursquare.Users.getCheckins('self', null, token.accessToken, function(err, results) { | 
					
						
							|  |  |  |         callback(err, results); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  |   function(err, results) { | 
					
						
							|  |  |  |     if (err) return next(err); | 
					
						
							|  |  |  |     res.render('api/foursquare', { | 
					
						
							|  |  |  |       title: 'Foursquare API', | 
					
						
							|  |  |  |       trendingVenues: results.trendingVenues, | 
					
						
							|  |  |  |       venueDetail: results.venueDetail, | 
					
						
							|  |  |  |       userCheckins: results.userCheckins | 
					
						
							| 
									
										
										
										
											2013-11-19 17:42:56 -05:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2014-02-27 17:39:18 -05:00
										 |  |  |   }); | 
					
						
							| 
									
										
										
										
											2013-11-19 16:20:18 -05:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-06 22:22:12 -05:00
										 |  |  | /** | 
					
						
							|  |  |  |  * GET /api/tumblr | 
					
						
							| 
									
										
										
										
											2014-01-13 04:24:31 -05:00
										 |  |  |  * Tumblr API example. | 
					
						
							| 
									
										
										
										
											2013-12-06 22:22:12 -05:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2014-01-13 04:24:31 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-06 22:22:12 -05:00
										 |  |  | exports.getTumblr = function(req, res) { | 
					
						
							| 
									
										
										
										
											2014-06-06 16:02:37 -04:00
										 |  |  |   var token = _.find(req.user.tokens, { kind: 'tumblr' }); | 
					
						
							| 
									
										
										
										
											2013-12-07 02:17:46 -05:00
										 |  |  |   var client = tumblr.createClient({ | 
					
						
							| 
									
										
										
										
											2013-12-20 01:31:16 -05:00
										 |  |  |     consumer_key: secrets.tumblr.consumerKey, | 
					
						
							|  |  |  |     consumer_secret: secrets.tumblr.consumerSecret, | 
					
						
							| 
									
										
										
										
											2013-12-20 13:48:33 -05:00
										 |  |  |     token: token.accessToken, | 
					
						
							|  |  |  |     token_secret: token.tokenSecret | 
					
						
							| 
									
										
										
										
											2013-12-07 02:17:46 -05:00
										 |  |  |   }); | 
					
						
							| 
									
										
										
										
											2014-04-13 19:26:17 -04:00
										 |  |  |   client.posts('withinthisnightmare.tumblr.com', { type: 'photo' }, function(err, data) { | 
					
						
							| 
									
										
										
										
											2013-12-07 02:50:07 -05:00
										 |  |  |     res.render('api/tumblr', { | 
					
						
							|  |  |  |       title: 'Tumblr API', | 
					
						
							| 
									
										
										
										
											2013-12-08 00:35:35 -05:00
										 |  |  |       blog: data.blog, | 
					
						
							| 
									
										
										
										
											2014-01-03 18:19:11 -05:00
										 |  |  |       photoset: data.posts[0].photos | 
					
						
							| 
									
										
										
										
											2013-12-07 02:50:07 -05:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2013-11-23 15:01:17 -05:00
										 |  |  |   }); | 
					
						
							| 
									
										
										
										
											2013-11-26 23:32:33 -05:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2013-11-23 15:01:17 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-06 22:22:12 -05:00
										 |  |  | /** | 
					
						
							|  |  |  |  * GET /api/facebook | 
					
						
							| 
									
										
										
										
											2014-01-13 04:24:31 -05:00
										 |  |  |  * Facebook API example. | 
					
						
							| 
									
										
										
										
											2013-12-06 22:22:12 -05:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2014-01-13 04:24:31 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-20 13:48:33 -05:00
										 |  |  | exports.getFacebook = function(req, res, next) { | 
					
						
							| 
									
										
										
										
											2014-06-06 16:02:37 -04:00
										 |  |  |   var token = _.find(req.user.tokens, { kind: 'facebook' }); | 
					
						
							| 
									
										
										
										
											2013-12-20 13:48:33 -05:00
										 |  |  |   graph.setAccessToken(token.accessToken); | 
					
						
							| 
									
										
										
										
											2013-12-07 22:50:27 -05:00
										 |  |  |   async.parallel({ | 
					
						
							| 
									
										
										
										
											2014-02-27 17:39:18 -05:00
										 |  |  |     getMe: function(done) { | 
					
						
							|  |  |  |       graph.get(req.user.facebook, function(err, me) { | 
					
						
							|  |  |  |         done(err, me); | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2013-12-07 22:50:27 -05:00
										 |  |  |     }, | 
					
						
							| 
									
										
										
										
											2014-02-27 17:39:18 -05:00
										 |  |  |     getMyFriends: function(done) { | 
					
						
							|  |  |  |       graph.get(req.user.facebook + '/friends', function(err, friends) { | 
					
						
							|  |  |  |         done(err, friends.data); | 
					
						
							| 
									
										
										
										
											2013-12-07 22:50:27 -05:00
										 |  |  |       }); | 
					
						
							| 
									
										
										
										
											2014-02-27 17:39:18 -05:00
										 |  |  |     } | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  |   function(err, results) { | 
					
						
							|  |  |  |     if (err) return next(err); | 
					
						
							|  |  |  |     res.render('api/facebook', { | 
					
						
							|  |  |  |       title: 'Facebook API', | 
					
						
							|  |  |  |       me: results.getMe, | 
					
						
							|  |  |  |       friends: results.getMyFriends | 
					
						
							| 
									
										
										
										
											2013-12-07 22:33:49 -05:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2014-02-27 17:39:18 -05:00
										 |  |  |   }); | 
					
						
							| 
									
										
										
										
											2013-11-23 15:01:17 -05:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2013-12-07 17:45:20 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-08 19:07:22 -05:00
										 |  |  | /** | 
					
						
							|  |  |  |  * GET /api/scraping | 
					
						
							| 
									
										
										
										
											2014-01-13 04:24:31 -05:00
										 |  |  |  * Web scraping example using Cheerio library. | 
					
						
							| 
									
										
										
										
											2013-12-08 19:07:22 -05:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2014-01-13 04:24:31 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-20 13:48:33 -05:00
										 |  |  | exports.getScraping = function(req, res, next) { | 
					
						
							|  |  |  |   request.get('https://news.ycombinator.com/', function(err, request, body) { | 
					
						
							|  |  |  |     if (err) return next(err); | 
					
						
							| 
									
										
										
										
											2013-12-07 18:53:00 -05:00
										 |  |  |     var $ = cheerio.load(body); | 
					
						
							|  |  |  |     var links = []; | 
					
						
							| 
									
										
										
										
											2014-02-26 05:26:59 +05:30
										 |  |  |     $(".title a[href^='http'], a[href^='https']").each(function() { | 
					
						
							| 
									
										
										
										
											2014-01-03 05:12:00 -05:00
										 |  |  |       links.push($(this)); | 
					
						
							| 
									
										
										
										
											2013-12-07 18:53:00 -05:00
										 |  |  |     }); | 
					
						
							|  |  |  |     res.render('api/scraping', { | 
					
						
							|  |  |  |       title: 'Web Scraping', | 
					
						
							| 
									
										
										
										
											2014-01-03 18:19:11 -05:00
										 |  |  |       links: links | 
					
						
							| 
									
										
										
										
											2013-12-07 18:53:00 -05:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2013-12-07 17:45:20 -05:00
										 |  |  |   }); | 
					
						
							| 
									
										
										
										
											2013-12-07 21:44:26 -05:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-19 13:22:42 -05:00
										 |  |  | /** | 
					
						
							|  |  |  |  * GET /api/github | 
					
						
							| 
									
										
										
										
											2014-01-13 04:24:31 -05:00
										 |  |  |  * GitHub API Example. | 
					
						
							| 
									
										
										
										
											2013-12-19 13:22:42 -05:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2013-12-07 21:44:26 -05:00
										 |  |  | exports.getGithub = function(req, res) { | 
					
						
							| 
									
										
										
										
											2014-06-06 16:02:37 -04:00
										 |  |  |   var token = _.find(req.user.tokens, { kind: 'github' }); | 
					
						
							| 
									
										
										
										
											2013-12-20 13:48:33 -05:00
										 |  |  |   var github = new Github({ token: token.accessToken }); | 
					
						
							| 
									
										
										
										
											2013-12-08 00:35:35 -05:00
										 |  |  |   var repo = github.getRepo('sahat', 'requirejs-library'); | 
					
						
							| 
									
										
										
										
											2013-12-07 21:44:26 -05:00
										 |  |  |   repo.show(function(err, repo) { | 
					
						
							|  |  |  |     res.render('api/github', { | 
					
						
							|  |  |  |       title: 'GitHub API', | 
					
						
							| 
									
										
										
										
											2014-01-03 18:19:11 -05:00
										 |  |  |       repo: repo | 
					
						
							| 
									
										
										
										
											2013-12-07 21:44:26 -05:00
										 |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-08 01:17:12 -05:00
										 |  |  | /** | 
					
						
							|  |  |  |  * GET /api/aviary | 
					
						
							| 
									
										
										
										
											2014-01-13 04:24:31 -05:00
										 |  |  |  * Aviary image processing example. | 
					
						
							| 
									
										
										
										
											2013-12-08 01:17:12 -05:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2014-01-13 04:24:31 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-08 01:17:12 -05:00
										 |  |  | exports.getAviary = function(req, res) { | 
					
						
							| 
									
										
										
										
											2013-12-08 01:49:18 -05:00
										 |  |  |   res.render('api/aviary', { | 
					
						
							| 
									
										
										
										
											2014-01-03 18:19:11 -05:00
										 |  |  |     title: 'Aviary API' | 
					
						
							| 
									
										
										
										
											2013-12-08 01:49:18 -05:00
										 |  |  |   }); | 
					
						
							| 
									
										
										
										
											2013-12-08 01:17:12 -05:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-08 19:07:22 -05:00
										 |  |  | /** | 
					
						
							|  |  |  |  * GET /api/nyt | 
					
						
							| 
									
										
										
										
											2014-01-13 04:24:31 -05:00
										 |  |  |  * New York Times API example. | 
					
						
							| 
									
										
										
										
											2013-12-08 19:07:22 -05:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2014-01-13 04:24:31 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-26 20:19:41 -05:00
										 |  |  | exports.getNewYorkTimes = function(req, res, next) { | 
					
						
							| 
									
										
										
										
											2013-12-20 01:31:16 -05:00
										 |  |  |   var query = querystring.stringify({ 'api-key': secrets.nyt.key, 'list-name': 'young-adult' }); | 
					
						
							| 
									
										
										
										
											2013-12-10 01:16:03 -05:00
										 |  |  |   var url = 'http://api.nytimes.com/svc/books/v2/lists?' + query; | 
					
						
							|  |  |  |   request.get(url, function(error, request, body) { | 
					
						
							| 
									
										
										
										
											2014-01-26 21:00:02 -05:00
										 |  |  |     if (request.statusCode === 403) return next(Error('Missing or Invalid New York Times API Key')); | 
					
						
							| 
									
										
										
										
											2013-12-20 13:48:33 -05:00
										 |  |  |     var bestsellers = JSON.parse(body); | 
					
						
							| 
									
										
										
										
											2013-12-10 01:16:03 -05:00
										 |  |  |     res.render('api/nyt', { | 
					
						
							|  |  |  |       title: 'New York Times API', | 
					
						
							| 
									
										
										
										
											2014-01-03 18:19:11 -05:00
										 |  |  |       books: bestsellers.results | 
					
						
							| 
									
										
										
										
											2013-12-10 01:16:03 -05:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2013-12-09 23:11:41 -05:00
										 |  |  |   }); | 
					
						
							| 
									
										
										
										
											2013-12-07 21:44:26 -05:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-08 19:07:22 -05:00
										 |  |  | /** | 
					
						
							|  |  |  |  * GET /api/lastfm | 
					
						
							| 
									
										
										
										
											2014-01-13 04:24:31 -05:00
										 |  |  |  * Last.fm API example. | 
					
						
							| 
									
										
										
										
											2013-12-08 19:07:22 -05:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2014-01-13 04:24:31 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | exports.getLastfm = function(req, res, next) { | 
					
						
							| 
									
										
										
										
											2013-12-20 01:31:16 -05:00
										 |  |  |   var lastfm = new LastFmNode(secrets.lastfm); | 
					
						
							| 
									
										
										
										
											2013-12-10 00:32:23 -05:00
										 |  |  |   async.parallel({ | 
					
						
							| 
									
										
										
										
											2014-02-27 17:39:18 -05:00
										 |  |  |     artistInfo: function(done) { | 
					
						
							| 
									
										
										
										
											2014-06-05 12:56:06 -04:00
										 |  |  |       lastfm.request('artist.getInfo', { | 
					
						
							|  |  |  |         artist: 'Sirenia', | 
					
						
							| 
									
										
										
										
											2014-02-27 17:39:18 -05:00
										 |  |  |         handlers: { | 
					
						
							|  |  |  |           success: function(data) { | 
					
						
							|  |  |  |             done(null, data); | 
					
						
							|  |  |  |           }, | 
					
						
							|  |  |  |           error: function(err) { | 
					
						
							|  |  |  |             done(err); | 
					
						
							| 
									
										
										
										
											2013-12-10 00:32:23 -05:00
										 |  |  |           } | 
					
						
							| 
									
										
										
										
											2014-02-27 17:39:18 -05:00
										 |  |  |         } | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2014-02-05 19:29:46 -05:00
										 |  |  |     }, | 
					
						
							| 
									
										
										
										
											2014-06-05 12:56:06 -04:00
										 |  |  |     artistTopTracks: function(done) { | 
					
						
							|  |  |  |       lastfm.request('artist.getTopTracks', { | 
					
						
							|  |  |  |         artist: 'Sirenia', | 
					
						
							|  |  |  |         handlers: { | 
					
						
							|  |  |  |           success: function(data) { | 
					
						
							|  |  |  |             var tracks = []; | 
					
						
							|  |  |  |             _.each(data.toptracks.track, function(track) { | 
					
						
							|  |  |  |               tracks.push(track); | 
					
						
							|  |  |  |             }); | 
					
						
							|  |  |  |             done(null, tracks.slice(0,10)); | 
					
						
							|  |  |  |           }, | 
					
						
							|  |  |  |           error: function(err) { | 
					
						
							|  |  |  |             done(err); | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     }, | 
					
						
							| 
									
										
										
										
											2014-02-27 17:39:18 -05:00
										 |  |  |     artistTopAlbums: function(done) { | 
					
						
							| 
									
										
										
										
											2014-06-05 12:56:06 -04:00
										 |  |  |       lastfm.request('artist.getTopAlbums', { | 
					
						
							|  |  |  |         artist: 'Sirenia', | 
					
						
							| 
									
										
										
										
											2014-02-27 17:39:18 -05:00
										 |  |  |         handlers: { | 
					
						
							|  |  |  |           success: function(data) { | 
					
						
							|  |  |  |             var albums = []; | 
					
						
							|  |  |  |             _.each(data.topalbums.album, function(album) { | 
					
						
							|  |  |  |               albums.push(album.image.slice(-1)[0]['#text']); | 
					
						
							|  |  |  |             }); | 
					
						
							|  |  |  |             done(null, albums.slice(0, 4)); | 
					
						
							|  |  |  |           }, | 
					
						
							|  |  |  |           error: function(err) { | 
					
						
							|  |  |  |             done(err); | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2013-12-10 00:32:23 -05:00
										 |  |  |       }); | 
					
						
							| 
									
										
										
										
											2014-02-27 17:39:18 -05:00
										 |  |  |     } | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  |   function(err, results) { | 
					
						
							|  |  |  |     if (err) return next(err.message); | 
					
						
							|  |  |  |     var artist = { | 
					
						
							|  |  |  |       name: results.artistInfo.artist.name, | 
					
						
							|  |  |  |       image: results.artistInfo.artist.image.slice(-1)[0]['#text'], | 
					
						
							|  |  |  |       tags: results.artistInfo.artist.tags.tag, | 
					
						
							|  |  |  |       bio: results.artistInfo.artist.bio.summary, | 
					
						
							|  |  |  |       stats: results.artistInfo.artist.stats, | 
					
						
							|  |  |  |       similar: results.artistInfo.artist.similar.artist, | 
					
						
							| 
									
										
										
										
											2014-06-05 12:56:06 -04:00
										 |  |  |       topAlbums: results.artistTopAlbums, | 
					
						
							|  |  |  |       topTracks: results.artistTopTracks | 
					
						
							| 
									
										
										
										
											2014-02-27 17:39:18 -05:00
										 |  |  |     }; | 
					
						
							|  |  |  |     res.render('api/lastfm', { | 
					
						
							|  |  |  |       title: 'Last.fm API', | 
					
						
							|  |  |  |       artist: artist | 
					
						
							| 
									
										
										
										
											2013-12-10 00:32:23 -05:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2014-02-27 17:39:18 -05:00
										 |  |  |   }); | 
					
						
							| 
									
										
										
										
											2013-12-09 21:20:41 -05:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-08 19:07:22 -05:00
										 |  |  | /** | 
					
						
							|  |  |  |  * GET /api/twitter | 
					
						
							| 
									
										
										
										
											2014-01-13 04:24:31 -05:00
										 |  |  |  * Twiter API example. | 
					
						
							| 
									
										
										
										
											2013-12-08 19:07:22 -05:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2014-01-13 04:24:31 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | exports.getTwitter = function(req, res, next) { | 
					
						
							| 
									
										
										
										
											2014-06-06 16:02:37 -04:00
										 |  |  |   var token = _.find(req.user.tokens, { kind: 'twitter' }); | 
					
						
							| 
									
										
										
										
											2013-12-10 02:14:44 -05:00
										 |  |  |   var T = new Twit({ | 
					
						
							| 
									
										
										
										
											2013-12-20 01:31:16 -05:00
										 |  |  |     consumer_key: secrets.twitter.consumerKey, | 
					
						
							|  |  |  |     consumer_secret: secrets.twitter.consumerSecret, | 
					
						
							| 
									
										
										
										
											2013-12-20 13:48:33 -05:00
										 |  |  |     access_token: token.accessToken, | 
					
						
							|  |  |  |     access_token_secret: token.tokenSecret | 
					
						
							| 
									
										
										
										
											2013-12-10 02:14:44 -05:00
										 |  |  |   }); | 
					
						
							| 
									
										
										
										
											2014-05-29 16:44:13 -04:00
										 |  |  |   T.get('search/tweets', { q: 'nodejs since:2013-01-01', geocode: '40.71448,-74.00598,5mi', count: 10 }, function(err, reply) { | 
					
						
							| 
									
										
										
										
											2014-01-13 04:24:31 -05:00
										 |  |  |     if (err) return next(err); | 
					
						
							| 
									
										
										
										
											2013-12-10 02:14:44 -05:00
										 |  |  |     res.render('api/twitter', { | 
					
						
							|  |  |  |       title: 'Twitter API', | 
					
						
							|  |  |  |       tweets: reply.statuses | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2013-12-07 21:44:26 -05:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2014-01-27 12:22:12 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-29 18:34:03 -04:00
										 |  |  | /** | 
					
						
							|  |  |  |  * POST /api/twitter | 
					
						
							|  |  |  |  * @param tweet | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-29 18:29:16 -04:00
										 |  |  | exports.postTwitter = function(req, res, next) { | 
					
						
							|  |  |  |   req.assert('tweet', 'Tweet cannot be empty.').notEmpty(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   var errors = req.validationErrors(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (errors) { | 
					
						
							|  |  |  |     req.flash('errors', errors); | 
					
						
							|  |  |  |     return res.redirect('/api/twitter'); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-06 16:02:37 -04:00
										 |  |  |   var token = _.find(req.user.tokens, { kind: 'twitter' }); | 
					
						
							| 
									
										
										
										
											2014-05-29 18:29:16 -04:00
										 |  |  |   var T = new Twit({ | 
					
						
							|  |  |  |     consumer_key: secrets.twitter.consumerKey, | 
					
						
							|  |  |  |     consumer_secret: secrets.twitter.consumerSecret, | 
					
						
							|  |  |  |     access_token: token.accessToken, | 
					
						
							|  |  |  |     access_token_secret: token.tokenSecret | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   T.post('statuses/update', { status: req.body.tweet }, function(err, data, response) { | 
					
						
							|  |  |  |     req.flash('success', { msg: 'Tweet has been posted.'}); | 
					
						
							|  |  |  |     res.redirect('/api/twitter'); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-05 16:37:48 +00:00
										 |  |  | /** | 
					
						
							|  |  |  |  * GET /api/steam | 
					
						
							|  |  |  |  * Steam API example. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-06 04:13:42 -05:00
										 |  |  | exports.getSteam = function(req, res, next) { | 
					
						
							| 
									
										
										
										
											2014-02-05 19:29:46 -05:00
										 |  |  |   var steamId = '76561197982488301'; | 
					
						
							| 
									
										
										
										
											2014-02-05 21:04:02 +00:00
										 |  |  |   var query = { l: 'english', steamid: steamId, key: secrets.steam.apiKey }; | 
					
						
							| 
									
										
										
										
											2014-02-05 19:29:46 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |   async.parallel({ | 
					
						
							| 
									
										
										
										
											2014-02-27 17:39:18 -05:00
										 |  |  |     playerAchievements: function(done) { | 
					
						
							|  |  |  |       query.appid = '49520'; | 
					
						
							|  |  |  |       var qs = querystring.stringify(query); | 
					
						
							|  |  |  |       request.get({ url: 'http://api.steampowered.com/ISteamUserStats/GetPlayerAchievements/v0001/?' + qs, json: true }, function(error, request, body) { | 
					
						
							|  |  |  |         if (request.statusCode === 401) return done(new Error('Missing or Invalid Steam API Key')); | 
					
						
							|  |  |  |         done(error, body); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     playerSummaries: function(done) { | 
					
						
							|  |  |  |       query.steamids = steamId; | 
					
						
							|  |  |  |       var qs = querystring.stringify(query); | 
					
						
							|  |  |  |       request.get({ url: 'http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?' + qs, json: true }, function(error, request, body) { | 
					
						
							|  |  |  |         if (request.statusCode === 401) return done(new Error('Missing or Invalid Steam API Key')); | 
					
						
							|  |  |  |         done(error, body); | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2014-02-05 21:04:02 +00:00
										 |  |  |     }, | 
					
						
							| 
									
										
										
										
											2014-02-27 17:39:18 -05:00
										 |  |  |     ownedGames: function(done) { | 
					
						
							|  |  |  |       query.include_appinfo = 1; | 
					
						
							|  |  |  |       query.include_played_free_games = 1; | 
					
						
							|  |  |  |       var qs = querystring.stringify(query); | 
					
						
							|  |  |  |       request.get({ url: 'http://api.steampowered.com/IPlayerService/GetOwnedGames/v0001/?' + qs, json: true }, function(error, request, body) { | 
					
						
							|  |  |  |         if (request.statusCode === 401) return done(new Error('Missing or Invalid Steam API Key')); | 
					
						
							|  |  |  |         done(error, body); | 
					
						
							| 
									
										
										
										
											2014-02-11 08:04:20 -05:00
										 |  |  |       }); | 
					
						
							| 
									
										
										
										
											2014-02-27 17:39:18 -05:00
										 |  |  |     } | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  |   function(err, results) { | 
					
						
							|  |  |  |     if (err) return next(err); | 
					
						
							|  |  |  |     res.render('api/steam', { | 
					
						
							|  |  |  |       title: 'Steam Web API', | 
					
						
							|  |  |  |       ownedGames: results.ownedGames.response.games, | 
					
						
							|  |  |  |       playerAchievemments: results.playerAchievements.playerstats, | 
					
						
							|  |  |  |       playerSummary: results.playerSummaries.response.players[0] | 
					
						
							| 
									
										
										
										
											2014-02-05 19:29:46 -05:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2014-02-27 17:39:18 -05:00
										 |  |  |   }); | 
					
						
							| 
									
										
										
										
											2014-02-06 07:46:47 -05:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-31 17:14:15 -04:00
										 |  |  | /** | 
					
						
							|  |  |  |  * GET /api/stripe | 
					
						
							|  |  |  |  * Stripe API example. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-14 17:34:51 -04:00
										 |  |  | exports.getStripe = function(req, res) { | 
					
						
							|  |  |  |   res.render('api/stripe', { | 
					
						
							|  |  |  |     title: 'Stripe API' | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2014-03-31 16:00:51 -04:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-31 17:14:15 -04:00
										 |  |  | /** | 
					
						
							| 
									
										
										
										
											2014-04-14 17:34:51 -04:00
										 |  |  |  * POST /api/stripe | 
					
						
							|  |  |  |  * @param stipeToken | 
					
						
							|  |  |  |  * @param stripeEmail | 
					
						
							| 
									
										
										
										
											2014-03-31 17:14:15 -04:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2014-03-31 16:00:51 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-14 17:34:51 -04:00
										 |  |  | exports.postStripe = function(req, res, next) { | 
					
						
							|  |  |  |   var stripeToken = req.body.stripeToken; | 
					
						
							|  |  |  |   var stripeEmail = req.body.stripeEmail; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   stripe.charges.create({ | 
					
						
							|  |  |  |     amount: 395, | 
					
						
							|  |  |  |     currency: 'usd', | 
					
						
							|  |  |  |     card: stripeToken, | 
					
						
							|  |  |  |     description: stripeEmail | 
					
						
							|  |  |  |   }, function(err, charge) { | 
					
						
							|  |  |  |     if (err && err.type === 'StripeCardError') { | 
					
						
							|  |  |  |       req.flash('errors', { msg: 'Your card has been declined.'}); | 
					
						
							|  |  |  |       res.redirect('/api/stripe'); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     req.flash('success', { msg: 'Your card has been charged successfully.'}); | 
					
						
							|  |  |  |     res.redirect('/api/stripe'); | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2014-03-31 16:00:51 -04:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-06 08:40:14 -05:00
										 |  |  | /** | 
					
						
							|  |  |  |  * GET /api/twilio | 
					
						
							|  |  |  |  * Twilio API example. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-02 14:47:35 -04:00
										 |  |  | exports.getTwilio = function(req, res) { | 
					
						
							| 
									
										
										
										
											2014-02-06 07:46:47 -05:00
										 |  |  |   res.render('api/twilio', { | 
					
						
							|  |  |  |     title: 'Twilio API' | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2014-02-06 08:09:54 -05:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2014-02-06 07:46:47 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-06 08:40:14 -05:00
										 |  |  | /** | 
					
						
							|  |  |  |  * POST /api/twilio | 
					
						
							|  |  |  |  * Twilio API example. | 
					
						
							| 
									
										
										
										
											2014-06-02 14:47:35 -04:00
										 |  |  |  * @param number | 
					
						
							|  |  |  |  * @param message | 
					
						
							| 
									
										
										
										
											2014-02-06 08:40:14 -05:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-06 08:09:54 -05:00
										 |  |  | exports.postTwilio = function(req, res, next) { | 
					
						
							| 
									
										
										
										
											2014-06-02 14:47:35 -04:00
										 |  |  |   req.assert('number', 'Phone number is required.').notEmpty(); | 
					
						
							|  |  |  |   req.assert('message', 'Message cannot be blank.').notEmpty(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   var errors = req.validationErrors(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (errors) { | 
					
						
							|  |  |  |     req.flash('errors', errors); | 
					
						
							|  |  |  |     return res.redirect('/api/twilio'); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-06 08:09:54 -05:00
										 |  |  |   var message = { | 
					
						
							| 
									
										
										
										
											2014-06-02 14:47:35 -04:00
										 |  |  |     to: req.body.number, | 
					
						
							| 
									
										
										
										
											2014-02-06 08:09:54 -05:00
										 |  |  |     from: '+13472235148', | 
					
						
							| 
									
										
										
										
											2014-06-02 14:47:35 -04:00
										 |  |  |     body: req.body.message | 
					
						
							| 
									
										
										
										
											2014-02-06 08:09:54 -05:00
										 |  |  |   }; | 
					
						
							| 
									
										
										
										
											2014-06-02 16:16:09 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-06 08:09:54 -05:00
										 |  |  |   twilio.sendMessage(message, function(err, responseData) { | 
					
						
							| 
									
										
										
										
											2014-02-06 08:33:37 -05:00
										 |  |  |     if (err) return next(err.message); | 
					
						
							| 
									
										
										
										
											2014-03-31 16:00:51 -04:00
										 |  |  |     req.flash('success', { msg: 'Text sent to ' + responseData.to + '.'}); | 
					
						
							| 
									
										
										
										
											2014-02-06 08:16:35 -05:00
										 |  |  |     res.redirect('/api/twilio'); | 
					
						
							| 
									
										
										
										
											2014-02-06 08:09:54 -05:00
										 |  |  |   }); | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2014-02-10 19:21:54 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-24 11:25:51 +00:00
										 |  |  | /** | 
					
						
							| 
									
										
										
										
											2014-02-27 19:42:07 -05:00
										 |  |  |  * GET /api/clockwork | 
					
						
							| 
									
										
										
										
											2014-02-24 11:25:51 +00:00
										 |  |  |  * Clockwork SMS API example. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-01 03:10:04 -05:00
										 |  |  | exports.getClockwork = function(req, res) { | 
					
						
							| 
									
										
										
										
											2014-02-24 11:25:51 +00:00
										 |  |  |   res.render('api/clockwork', { | 
					
						
							|  |  |  |     title: 'Clockwork SMS API' | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							| 
									
										
										
										
											2014-03-01 03:10:04 -05:00
										 |  |  |  * POST /api/clockwork | 
					
						
							| 
									
										
										
										
											2014-02-24 11:25:51 +00:00
										 |  |  |  * Clockwork SMS API example. | 
					
						
							|  |  |  |  * @param telephone | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | exports.postClockwork = function(req, res, next) { | 
					
						
							|  |  |  |   var message = { | 
					
						
							|  |  |  |     To: req.body.telephone, | 
					
						
							|  |  |  |     From: 'Hackathon', | 
					
						
							|  |  |  |     Content: 'Hello from the Hackathon Starter' | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  |   clockwork.sendSms(message, function(err, responseData) { | 
					
						
							| 
									
										
										
										
											2014-02-27 19:42:07 -05:00
										 |  |  |     if (err) return next(err.errDesc); | 
					
						
							| 
									
										
										
										
											2014-03-01 03:10:04 -05:00
										 |  |  |     req.flash('success', { msg: 'Text sent to ' + responseData.responses[0].to }); | 
					
						
							| 
									
										
										
										
											2014-02-24 11:25:51 +00:00
										 |  |  |     res.redirect('/api/clockwork'); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-27 18:05:10 -05:00
										 |  |  | /** | 
					
						
							|  |  |  |  * GET /api/venmo | 
					
						
							|  |  |  |  * Venmo API example. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-10 19:21:54 -05:00
										 |  |  | exports.getVenmo = function(req, res, next) { | 
					
						
							| 
									
										
										
										
											2014-06-06 16:02:37 -04:00
										 |  |  |   var token = _.find(req.user.tokens, { kind: 'venmo' }); | 
					
						
							| 
									
										
										
										
											2014-02-10 20:59:39 -05:00
										 |  |  |   var query = querystring.stringify({ access_token: token.accessToken }); | 
					
						
							| 
									
										
										
										
											2014-02-10 23:55:32 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-11 08:04:20 -05:00
										 |  |  |   async.parallel({ | 
					
						
							| 
									
										
										
										
											2014-02-27 17:39:18 -05:00
										 |  |  |     getProfile: function(done) { | 
					
						
							|  |  |  |       request.get({ url: 'https://api.venmo.com/v1/me?' + query, json: true }, function(err, request, body) { | 
					
						
							|  |  |  |         done(err, body); | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2014-02-27 14:48:09 -05:00
										 |  |  |     }, | 
					
						
							| 
									
										
										
										
											2014-02-27 17:39:18 -05:00
										 |  |  |     getRecentPayments: function(done) { | 
					
						
							|  |  |  |       request.get({ url: 'https://api.venmo.com/v1/payments?' + query, json: true }, function(err, request, body) { | 
					
						
							|  |  |  |         done(err, body); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-11 08:04:20 -05:00
										 |  |  |       }); | 
					
						
							| 
									
										
										
										
											2014-02-27 17:39:18 -05:00
										 |  |  |     } | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  |   function(err, results) { | 
					
						
							|  |  |  |     if (err) return next(err); | 
					
						
							|  |  |  |     res.render('api/venmo', { | 
					
						
							|  |  |  |       title: 'Venmo API', | 
					
						
							|  |  |  |       profile: results.getProfile.data, | 
					
						
							|  |  |  |       recentPayments: results.getRecentPayments.data | 
					
						
							| 
									
										
										
										
											2014-02-10 20:59:39 -05:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2014-02-27 17:39:18 -05:00
										 |  |  |   }); | 
					
						
							| 
									
										
										
										
											2014-02-10 23:55:32 -05:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2014-02-10 20:59:39 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-27 18:05:10 -05:00
										 |  |  | /** | 
					
						
							|  |  |  |  * POST /api/venmo | 
					
						
							|  |  |  |  * @param user | 
					
						
							|  |  |  |  * @param note | 
					
						
							|  |  |  |  * @param amount | 
					
						
							|  |  |  |  * Send money. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-10 23:55:32 -05:00
										 |  |  | exports.postVenmo = function(req, res, next) { | 
					
						
							|  |  |  |   req.assert('user', 'Phone, Email or Venmo User ID cannot be blank').notEmpty(); | 
					
						
							|  |  |  |   req.assert('note', 'Please enter a message to accompany the payment').notEmpty(); | 
					
						
							| 
									
										
										
										
											2014-02-11 08:14:50 -05:00
										 |  |  |   req.assert('amount', 'The amount you want to pay cannot be blank').notEmpty(); | 
					
						
							| 
									
										
										
										
											2014-02-10 23:55:32 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |   var errors = req.validationErrors(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (errors) { | 
					
						
							|  |  |  |     req.flash('errors', errors); | 
					
						
							|  |  |  |     return res.redirect('/api/venmo'); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-06 16:02:37 -04:00
										 |  |  |   var token = _.find(req.user.tokens, { kind: 'venmo' }); | 
					
						
							| 
									
										
										
										
											2014-02-11 07:50:47 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-10 23:55:32 -05:00
										 |  |  |   var formData = { | 
					
						
							|  |  |  |     access_token: token.accessToken, | 
					
						
							|  |  |  |     note: req.body.note, | 
					
						
							|  |  |  |     amount: req.body.amount | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (validator.isEmail(req.body.user)) { | 
					
						
							|  |  |  |     formData.email = req.body.user; | 
					
						
							| 
									
										
										
										
											2014-02-11 08:24:04 -05:00
										 |  |  |   } else if (validator.isNumeric(req.body.user) && | 
					
						
							| 
									
										
										
										
											2014-02-11 07:50:47 -05:00
										 |  |  |     validator.isLength(req.body.user, 10, 11)) { | 
					
						
							| 
									
										
										
										
											2014-02-10 23:55:32 -05:00
										 |  |  |     formData.phone = req.body.user; | 
					
						
							|  |  |  |   } else { | 
					
						
							|  |  |  |     formData.user_id = req.body.user; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-11 08:24:04 -05:00
										 |  |  |   request.post('https://api.venmo.com/v1/payments', { form: formData }, function(err, request, body) { | 
					
						
							| 
									
										
										
										
											2014-02-10 23:55:32 -05:00
										 |  |  |     if (err) return next(err); | 
					
						
							| 
									
										
										
										
											2014-02-11 00:06:33 -05:00
										 |  |  |     if (request.statusCode !== 200) { | 
					
						
							|  |  |  |       req.flash('errors', { msg: JSON.parse(body).error.message }); | 
					
						
							|  |  |  |       return res.redirect('/api/venmo'); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2014-02-11 07:50:47 -05:00
										 |  |  |     req.flash('success', { msg: 'Venmo money transfer complete' }); | 
					
						
							| 
									
										
										
										
											2014-02-10 23:55:32 -05:00
										 |  |  |     res.redirect('/api/venmo'); | 
					
						
							| 
									
										
										
										
											2014-02-10 19:21:54 -05:00
										 |  |  |   }); | 
					
						
							| 
									
										
										
										
											2014-02-10 23:55:32 -05:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2014-02-27 14:48:09 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-27 18:05:10 -05:00
										 |  |  | /** | 
					
						
							|  |  |  |  * GET /api/linkedin | 
					
						
							|  |  |  |  * LinkedIn API example. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-27 14:48:09 -05:00
										 |  |  | exports.getLinkedin = function(req, res, next) { | 
					
						
							| 
									
										
										
										
											2014-06-06 16:02:37 -04:00
										 |  |  |   var token = _.find(req.user.tokens, { kind: 'linkedin' }); | 
					
						
							| 
									
										
										
										
											2014-02-27 16:51:38 -05:00
										 |  |  |   var linkedin = Linkedin.init(token.accessToken); | 
					
						
							| 
									
										
										
										
											2014-02-27 14:48:09 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-27 17:29:16 -05:00
										 |  |  |   linkedin.people.me(function(err, $in) { | 
					
						
							|  |  |  |     if (err) return next(err); | 
					
						
							|  |  |  |     res.render('api/linkedin', { | 
					
						
							|  |  |  |       title: 'LinkedIn API', | 
					
						
							|  |  |  |       profile: $in | 
					
						
							| 
									
										
										
										
											2014-02-27 14:48:09 -05:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2014-02-27 17:29:16 -05:00
										 |  |  |   }); | 
					
						
							| 
									
										
										
										
											2014-02-27 14:48:09 -05:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2014-04-22 14:51:35 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * GET /api/instagram | 
					
						
							|  |  |  |  * Instagram API example. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | exports.getInstagram = function(req, res, next) { | 
					
						
							| 
									
										
										
										
											2014-06-06 16:02:37 -04:00
										 |  |  |   var token = _.find(req.user.tokens, { kind: 'instagram' }); | 
					
						
							| 
									
										
										
										
											2014-04-22 14:51:35 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |   ig.use({ client_id: secrets.instagram.clientID, client_secret: secrets.instagram.clientSecret }); | 
					
						
							| 
									
										
										
										
											2014-04-30 00:17:28 -04:00
										 |  |  |   ig.use({ access_token: token.accessToken }); | 
					
						
							| 
									
										
										
										
											2014-04-22 14:51:35 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |   async.parallel({ | 
					
						
							|  |  |  |     searchByUsername: function(done) { | 
					
						
							| 
									
										
										
										
											2014-06-05 12:56:06 -04:00
										 |  |  |       ig.user_search('richellemead', function(err, users, limit) { | 
					
						
							| 
									
										
										
										
											2014-04-22 14:51:35 -04:00
										 |  |  |         done(err, users); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     searchByUserId: function(done) { | 
					
						
							|  |  |  |       ig.user('175948269', function(err, user) { | 
					
						
							|  |  |  |         done(err, user); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     popularImages: function(done) { | 
					
						
							|  |  |  |       ig.media_popular(function(err, medias) { | 
					
						
							|  |  |  |         done(err, medias); | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2014-04-30 00:12:33 -04:00
										 |  |  |     }, | 
					
						
							|  |  |  |     myRecentMedia: function(done) { | 
					
						
							|  |  |  |       ig.user_self_media_recent(function(err, medias, pagination, limit) { | 
					
						
							|  |  |  |         done(err, medias); | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2014-04-22 14:51:35 -04:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2014-04-30 00:12:33 -04:00
										 |  |  |   }, function(err, results) { | 
					
						
							|  |  |  |     if (err) return next(err); | 
					
						
							| 
									
										
										
										
											2014-04-22 14:51:35 -04:00
										 |  |  |     res.render('api/instagram', { | 
					
						
							|  |  |  |       title: 'Instagram API', | 
					
						
							|  |  |  |       usernames: results.searchByUsername, | 
					
						
							|  |  |  |       userById: results.searchByUserId, | 
					
						
							| 
									
										
										
										
											2014-04-30 00:12:33 -04:00
										 |  |  |       popularImages: results.popularImages, | 
					
						
							|  |  |  |       myRecentMedia: results.myRecentMedia | 
					
						
							| 
									
										
										
										
											2014-04-22 14:51:35 -04:00
										 |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2014-05-13 01:35:46 -04:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * GET /api/yahoo | 
					
						
							|  |  |  |  * Yahoo API example. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | exports.getYahoo = function(req, res) { | 
					
						
							|  |  |  |   Y.YQL('SELECT * FROM weather.forecast WHERE (location = 10007)', function(response) { | 
					
						
							|  |  |  |     var location = response.query.results.channel.location; | 
					
						
							|  |  |  |     var condition = response.query.results.channel.item.condition; | 
					
						
							|  |  |  |     res.render('api/yahoo', { | 
					
						
							|  |  |  |       title: 'Yahoo API', | 
					
						
							|  |  |  |       location: location, | 
					
						
							|  |  |  |       condition: condition | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2014-04-22 14:51:35 -04:00
										 |  |  | }; |