Permalink
Browse files

Actually adds the files.

  • Loading branch information...
1 parent b51c9c0 commit d6789cad9ca30a0c4e9ebd18f8c28464dab96ad5 @cpsubrian committed Jul 5, 2011
Showing with 259 additions and 0 deletions.
  1. +4 −0 .gitignore
  2. +53 −0 app.js
  3. +116 −0 instagram.js
  4. +12 −0 package.json
  5. +50 −0 public/stylesheets/style.css
  6. +10 −0 views/index.html
  7. +14 −0 views/layout.html
View
4 .gitignore
@@ -0,0 +1,4 @@
+node_modules/
+logs/*
+pids/*
+cluster.*
View
53 app.js
@@ -0,0 +1,53 @@
+
+/**
+ * Module dependencies.
+ */
+
+var express = require('express');
+var cluster = require('cluster');
+var app = module.exports = express.createServer();
+
+var instagram = require('./instagram.js');
+
+var port = 3000;
+
+// Configuration
+
+app.configure(function(){
+ app.set('views', __dirname + '/views');
+ app.set('view engine', 'html');
+ app.register(".html", require('jqtpl').express);
+ app.use(express.bodyParser());
+ app.use(express.methodOverride());
+ app.use(app.router);
+ app.use(express.static(__dirname + '/public'));
+});
+
+app.configure('development', function(){
+ app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
+ app.listen(port);
+});
+
+app.configure('production', function(){
+ app.use(express.errorHandler());
+
+ cluster(app)
+ .set('workers', 4)
+ .use(cluster.pidfiles())
+ .use(cluster.cli())
+ .use(cluster.debug())
+ .listen(port);
+});
+
+// Routes
+
+app.get('/', function(req, res){
+ instagram.popular(req, res);
+});
+
+app.get('/refresh', function(req, res) {
+ instagram.refresh();
+ res.redirect('/');
+});
+
+console.log("Express server listening on port %d in %s mode", port, app.settings.env);
View
116 instagram.js
@@ -0,0 +1,116 @@
+// Constants
+var API_HOST = 'api.instagram.com';
+var API_BASE_URL = '/v1';
+var CLIENT_ID = '99b52fe6e18b4205a4e71bfba2b5a078';
+var CLIENT_SECRET = '7f73a192a5c04e458d0b8fc85bab9e7c';
+
+var OAuth= require('oauth').OAuth;
+var https = require('https');
+var util = require('util');
+
+var photos = [];
+
+// Display the latest popular photos.
+var popular = exports.popular = function(req, res) {
+ fetchPopular(function(photos) {
+ res.render('index', {
+ title: 'Instagram Popular Photos',
+ photos: photos
+ });
+ });
+}
+
+var refresh = exports.refresh = function() {
+ photos = [];
+}
+
+// Fetch popular photos.
+global.popularPhotos = [];
+var fetchPopular = function(finish) {
+ if (!photos.length) {
+ console.log('Fetching New Photos...');
+ var options = {
+ host: API_HOST,
+ path: API_BASE_URL + '/media/popular?client_id=' + CLIENT_ID,
+ };
+ https.get(options, function(res) {
+ var data = '';
+ res.on('data', function(chunk) {
+ data += chunk;
+ });
+ res.on('end', function() {
+ var parsed = JSON.parse(data);
+ for (var i in parsed.data) {
+ photos.push(new photo(parsed.data[i]));
+ }
+ finish(photos);
+ });
+ }).on('error', function(e) {
+ console.error(e);
+ });
+ }
+ else {
+ console.log('Fetching Cached Photos...');
+ finish(photos);
+ }
+}
+
+// Parse the properties we want from photo data.
+var photo = function(data) {
+ if (data.caption) {
+ this.caption = data.caption.text;
+ }
+ if (typeof data.images.standard_resolution != 'undefined') {
+ this.image = data.images.standard_resolution;
+ }
+ if (typeof data.images.thumbnail != 'undefined') {
+ this.thumb = data.images.thumbnail;
+ }
+ if (typeof data.link != 'undefined') {
+ this.link = data.link;
+ }
+ if (typeof data.user != 'undefined') {
+ this.user = data.user;
+ }
+};
+photo.prototype = {
+ render: function() {
+ return '<img src="' + this.image.url + '" width="' + this.image.width + '" height="' + this.image.height + '">';
+ },
+ renderThumb: function() {
+ return '<img src="' + this.thumb.url + '" width="' + this.thumb.width + '" height="' + this.thumb.height + '">';
+ }
+};
+
+/*
+var oa = new OAuth(global.INSTAGRAM_REQUEST_TOKEN_URL,
+ global.INSTAGRAM_ACESS_TOKEN_URL,
+ global.INSTAGRAM_KEY,
+ global.INSTAGRAM_SECRET,
+ global.INSTAGRAM_VERSION,
+ null,
+ "HMAC-SHA1");
+
+oa.getOAuthRequestToken(function(error, oauth_token, oauth_token_secret, results){
+ if (error) return console.log('error :' + error)
+ console.log('oauth_token :' + oauth_token)
+ console.log('oauth_token_secret :' + oauth_token_secret)
+ console.log('requestoken results :', results)
+ console.log("Requesting access token")
+ oa.getOAuthAccessToken(oauth_token, oauth_token_secret,
+ function(error, oauth_access_token,
+ oauth_access_token_secret, results2) {
+ console.log('oauth_access_token :' + oauth_access_token)
+ console.log('oauth_token_secret :' + oauth_access_token_secret)
+ console.log('accesstoken results :', results2)
+ console.log("Requesting access token")
+ var data= "";
+ oa.getProtectedResource(
+ "http://term.ie/oauth/example/echo_api.php?foo=bar&too=roo", "GET",
+ oauth_access_token, oauth_access_token_secret,
+ function (error, data, response) {
+ console.log(data);
+ });
+ });
+})
+*/
View
12 package.json
@@ -0,0 +1,12 @@
+{
+ "name": "application-name"
+ , "version": "0.0.1"
+ , "private": true
+ , "dependencies": {
+ "express": "2.4.0"
+ , "jqtpl": ">= 0.0.1"
+ , "mongoose": ">= 0.0.1"
+ , "oauth": ">= 0.0.1"
+ , "cluster": ">= 0.0.1"
+ }
+}
View
50 public/stylesheets/style.css
@@ -0,0 +1,50 @@
+html {
+ margin: 0;
+ padding: 0;
+ background: #ddd;
+}
+
+body {
+ width: 960px;
+ margin: 10px auto;
+ padding: 10px 20px;
+ font: 14px "Lucida Grande", Helvetica, Arial, sans-serif;
+ background: #f4f4f4;
+ border: 1px solid #bbb;
+
+ border-radius: 10px;
+ box-shadow: 0px 1px 2px 2px rgba(0, 0, 0, 0.05);
+}
+
+a {
+ color: #00B7FF;
+}
+
+header {
+ position: relative;
+}
+
+#refresh {
+ position: absolute;
+ top: 0;
+ right: 0;
+}
+
+h1 {
+ padding-bottom: 10px;
+ border-bottom: 1px solid #ccc;
+}
+
+ul {
+ list-style: none;
+ margin: 0;
+ padding: 0;
+}
+li {
+ display: inline-block;
+ list-style: none;
+ padding: 14px;
+ margin: 5px;
+ background: #666;
+ border-radius: 10px;
+}
View
10 views/index.html
@@ -0,0 +1,10 @@
+<header id="page-header">
+ <h1>${title}</h1>
+ <a id="refresh" href="/refresh">Refresh</a>
+</header>
+
+<ul>
+ {{each(i, photo) photos}}
+ <li>{{html photo.renderThumb.call(photo)}}</li>
+ {{/each}}
+</ul>
View
14 views/layout.html
@@ -0,0 +1,14 @@
+<!DOCTYPE html>
+<html lang="en">
+ <head>
+ <meta charset="utf-8">
+ <title>${title}</title>
+ <link rel="stylesheet" href="/stylesheets/style.css" type="text/css">
+ </head>
+ <body>
+ <div id="page">
+ {{html body}}
+ <div>
+ </body>
+</html>
+

0 comments on commit d6789ca

Please sign in to comment.