Skip to content
This repository
Browse code

Actually adds the files.

  • Loading branch information...
commit d6789cad9ca30a0c4e9ebd18f8c28464dab96ad5 1 parent b51c9c0
Brian Link authored
4 .gitignore
... ... @@ -0,0 +1,4 @@
  1 +node_modules/
  2 +logs/*
  3 +pids/*
  4 +cluster.*
53 app.js
... ... @@ -0,0 +1,53 @@
  1 +
  2 +/**
  3 + * Module dependencies.
  4 + */
  5 +
  6 +var express = require('express');
  7 +var cluster = require('cluster');
  8 +var app = module.exports = express.createServer();
  9 +
  10 +var instagram = require('./instagram.js');
  11 +
  12 +var port = 3000;
  13 +
  14 +// Configuration
  15 +
  16 +app.configure(function(){
  17 + app.set('views', __dirname + '/views');
  18 + app.set('view engine', 'html');
  19 + app.register(".html", require('jqtpl').express);
  20 + app.use(express.bodyParser());
  21 + app.use(express.methodOverride());
  22 + app.use(app.router);
  23 + app.use(express.static(__dirname + '/public'));
  24 +});
  25 +
  26 +app.configure('development', function(){
  27 + app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
  28 + app.listen(port);
  29 +});
  30 +
  31 +app.configure('production', function(){
  32 + app.use(express.errorHandler());
  33 +
  34 + cluster(app)
  35 + .set('workers', 4)
  36 + .use(cluster.pidfiles())
  37 + .use(cluster.cli())
  38 + .use(cluster.debug())
  39 + .listen(port);
  40 +});
  41 +
  42 +// Routes
  43 +
  44 +app.get('/', function(req, res){
  45 + instagram.popular(req, res);
  46 +});
  47 +
  48 +app.get('/refresh', function(req, res) {
  49 + instagram.refresh();
  50 + res.redirect('/');
  51 +});
  52 +
  53 +console.log("Express server listening on port %d in %s mode", port, app.settings.env);
116 instagram.js
... ... @@ -0,0 +1,116 @@
  1 +// Constants
  2 +var API_HOST = 'api.instagram.com';
  3 +var API_BASE_URL = '/v1';
  4 +var CLIENT_ID = '99b52fe6e18b4205a4e71bfba2b5a078';
  5 +var CLIENT_SECRET = '7f73a192a5c04e458d0b8fc85bab9e7c';
  6 +
  7 +var OAuth= require('oauth').OAuth;
  8 +var https = require('https');
  9 +var util = require('util');
  10 +
  11 +var photos = [];
  12 +
  13 +// Display the latest popular photos.
  14 +var popular = exports.popular = function(req, res) {
  15 + fetchPopular(function(photos) {
  16 + res.render('index', {
  17 + title: 'Instagram Popular Photos',
  18 + photos: photos
  19 + });
  20 + });
  21 +}
  22 +
  23 +var refresh = exports.refresh = function() {
  24 + photos = [];
  25 +}
  26 +
  27 +// Fetch popular photos.
  28 +global.popularPhotos = [];
  29 +var fetchPopular = function(finish) {
  30 + if (!photos.length) {
  31 + console.log('Fetching New Photos...');
  32 + var options = {
  33 + host: API_HOST,
  34 + path: API_BASE_URL + '/media/popular?client_id=' + CLIENT_ID,
  35 + };
  36 + https.get(options, function(res) {
  37 + var data = '';
  38 + res.on('data', function(chunk) {
  39 + data += chunk;
  40 + });
  41 + res.on('end', function() {
  42 + var parsed = JSON.parse(data);
  43 + for (var i in parsed.data) {
  44 + photos.push(new photo(parsed.data[i]));
  45 + }
  46 + finish(photos);
  47 + });
  48 + }).on('error', function(e) {
  49 + console.error(e);
  50 + });
  51 + }
  52 + else {
  53 + console.log('Fetching Cached Photos...');
  54 + finish(photos);
  55 + }
  56 +}
  57 +
  58 +// Parse the properties we want from photo data.
  59 +var photo = function(data) {
  60 + if (data.caption) {
  61 + this.caption = data.caption.text;
  62 + }
  63 + if (typeof data.images.standard_resolution != 'undefined') {
  64 + this.image = data.images.standard_resolution;
  65 + }
  66 + if (typeof data.images.thumbnail != 'undefined') {
  67 + this.thumb = data.images.thumbnail;
  68 + }
  69 + if (typeof data.link != 'undefined') {
  70 + this.link = data.link;
  71 + }
  72 + if (typeof data.user != 'undefined') {
  73 + this.user = data.user;
  74 + }
  75 +};
  76 +photo.prototype = {
  77 + render: function() {
  78 + return '<img src="' + this.image.url + '" width="' + this.image.width + '" height="' + this.image.height + '">';
  79 + },
  80 + renderThumb: function() {
  81 + return '<img src="' + this.thumb.url + '" width="' + this.thumb.width + '" height="' + this.thumb.height + '">';
  82 + }
  83 +};
  84 +
  85 +/*
  86 +var oa = new OAuth(global.INSTAGRAM_REQUEST_TOKEN_URL,
  87 + global.INSTAGRAM_ACESS_TOKEN_URL,
  88 + global.INSTAGRAM_KEY,
  89 + global.INSTAGRAM_SECRET,
  90 + global.INSTAGRAM_VERSION,
  91 + null,
  92 + "HMAC-SHA1");
  93 +
  94 +oa.getOAuthRequestToken(function(error, oauth_token, oauth_token_secret, results){
  95 + if (error) return console.log('error :' + error)
  96 + console.log('oauth_token :' + oauth_token)
  97 + console.log('oauth_token_secret :' + oauth_token_secret)
  98 + console.log('requestoken results :', results)
  99 + console.log("Requesting access token")
  100 + oa.getOAuthAccessToken(oauth_token, oauth_token_secret,
  101 + function(error, oauth_access_token,
  102 + oauth_access_token_secret, results2) {
  103 + console.log('oauth_access_token :' + oauth_access_token)
  104 + console.log('oauth_token_secret :' + oauth_access_token_secret)
  105 + console.log('accesstoken results :', results2)
  106 + console.log("Requesting access token")
  107 + var data= "";
  108 + oa.getProtectedResource(
  109 + "http://term.ie/oauth/example/echo_api.php?foo=bar&too=roo", "GET",
  110 + oauth_access_token, oauth_access_token_secret,
  111 + function (error, data, response) {
  112 + console.log(data);
  113 + });
  114 + });
  115 +})
  116 +*/
12 package.json
... ... @@ -0,0 +1,12 @@
  1 +{
  2 + "name": "application-name"
  3 + , "version": "0.0.1"
  4 + , "private": true
  5 + , "dependencies": {
  6 + "express": "2.4.0"
  7 + , "jqtpl": ">= 0.0.1"
  8 + , "mongoose": ">= 0.0.1"
  9 + , "oauth": ">= 0.0.1"
  10 + , "cluster": ">= 0.0.1"
  11 + }
  12 +}
50 public/stylesheets/style.css
... ... @@ -0,0 +1,50 @@
  1 +html {
  2 + margin: 0;
  3 + padding: 0;
  4 + background: #ddd;
  5 +}
  6 +
  7 +body {
  8 + width: 960px;
  9 + margin: 10px auto;
  10 + padding: 10px 20px;
  11 + font: 14px "Lucida Grande", Helvetica, Arial, sans-serif;
  12 + background: #f4f4f4;
  13 + border: 1px solid #bbb;
  14 +
  15 + border-radius: 10px;
  16 + box-shadow: 0px 1px 2px 2px rgba(0, 0, 0, 0.05);
  17 +}
  18 +
  19 +a {
  20 + color: #00B7FF;
  21 +}
  22 +
  23 +header {
  24 + position: relative;
  25 +}
  26 +
  27 +#refresh {
  28 + position: absolute;
  29 + top: 0;
  30 + right: 0;
  31 +}
  32 +
  33 +h1 {
  34 + padding-bottom: 10px;
  35 + border-bottom: 1px solid #ccc;
  36 +}
  37 +
  38 +ul {
  39 + list-style: none;
  40 + margin: 0;
  41 + padding: 0;
  42 +}
  43 +li {
  44 + display: inline-block;
  45 + list-style: none;
  46 + padding: 14px;
  47 + margin: 5px;
  48 + background: #666;
  49 + border-radius: 10px;
  50 +}
10 views/index.html
... ... @@ -0,0 +1,10 @@
  1 +<header id="page-header">
  2 + <h1>${title}</h1>
  3 + <a id="refresh" href="/refresh">Refresh</a>
  4 +</header>
  5 +
  6 +<ul>
  7 + {{each(i, photo) photos}}
  8 + <li>{{html photo.renderThumb.call(photo)}}</li>
  9 + {{/each}}
  10 +</ul>
14 views/layout.html
... ... @@ -0,0 +1,14 @@
  1 +<!DOCTYPE html>
  2 +<html lang="en">
  3 + <head>
  4 + <meta charset="utf-8">
  5 + <title>${title}</title>
  6 + <link rel="stylesheet" href="/stylesheets/style.css" type="text/css">
  7 + </head>
  8 + <body>
  9 + <div id="page">
  10 + {{html body}}
  11 + <div>
  12 + </body>
  13 +</html>
  14 +

0 comments on commit d6789ca

Please sign in to comment.
Something went wrong with that request. Please try again.