Created
November 30, 2012 17:44
-
-
Save stottsan/4177305 to your computer and use it in GitHub Desktop.
Client-side (jQuery/HTML) and server-side (Node.js) code for my Heroku-based Instagram feed reader.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<link rel="stylesheet" href="/css/instagram.css" type="text/css" /> | |
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> | |
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.18/jquery-ui.min.js"></script> | |
<div class="instagram"></div> | |
<div class="load-more btn">Gimme more photos...</div> | |
<div class="error">Oops, there was an error getting the photos...</div> | |
<script src="/js/instagram.js"></script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This should be included from instagram.html | |
$(function(){ | |
function loadInstagrams(max_id) { | |
var userid = 3900639; | |
var baseurl = "http://your-node-app.herokuapp.com/feed/" + userid; | |
var url = typeof max_id != 'undefined' ? baseurl + "?max_id=" + max_id : baseurl; | |
$.getJSON(url, | |
function(res) { | |
var length = typeof res.data != 'undefined' ? res.data.length : 0; | |
var next_max_id = typeof res.pagination.next_max_id != 'undefined' ? res.pagination.next_max_id : 0; | |
if(length > 0) { | |
$('.instagram').append('<div class="chunk" id="' + next_max_id + '"></div>'); | |
var chunk = '.chunk[id="' + next_max_id + '"]'; | |
for(var i = 0; i < res.data.length; i++) { | |
$('.instagram > ' + chunk).append( | |
createPhotoElement(res.data[i]) | |
); | |
} | |
$(chunk).fadeIn(500, function() { | |
if ($(chunk).position().top > 100) { | |
$('html,body').animate({ scrollTop: $(chunk).offset().top }, 500, 'easeInOutSine'); | |
} | |
if (next_max_id != 0) { | |
$('.load-more') | |
.attr('id', next_max_id) | |
.fadeIn(250); | |
} else { | |
$('.load-more').hide(); | |
} | |
}); | |
} | |
else { | |
$('.instagram').append(createEmptyElement()); | |
} | |
}) | |
.error(function() { $('.error').fadeIn(250); } | |
); | |
} | |
function createPhotoElement(photo) { | |
var thecap = photo.caption != null ? photo.caption.text : "" | |
return $('<div>') | |
.addClass('instagram-placeholder') | |
.attr('id', photo.id) | |
.append( | |
$('<a>') | |
.attr('target', '_blank') | |
.attr('href', photo.link) | |
.attr('title', thecap) | |
.append( | |
$('<img>') | |
.addClass('instagram-image') | |
.attr('src', photo.images.thumbnail.url) | |
//.attr('src', photo.images.low_resolution.url) | |
) | |
); | |
} | |
function createEmptyElement() { | |
return $('<div>') | |
.addClass('instagram-placeholder') | |
.attr('id', 'empty') | |
.append($('<p>').html('No photos found... Sorry!')); | |
} | |
loadInstagrams(); | |
$('.load-more').click(function(){ | |
loadInstagrams(this.id); | |
}) | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Run this on Heroku using a Procfile with "web: node web.js" | |
var express = require('express'); | |
var sys = require('util'); | |
if (process.env.REDISTOGO_URL) { | |
var rtg = require("url").parse(process.env.REDISTOGO_URL); | |
var redis = require("redis").createClient(rtg.port, rtg.hostname); | |
redis.auth(rtg.auth.split(":")[1]); | |
} else { | |
var redis = require("redis").createClient(); | |
} | |
redis.on("error", function (err) { | |
console.log("error event - " + redis.host + ":" + redis.port + " - " + err); | |
}); | |
var app = express.createServer(); | |
// Configure Instagram NodeJS library | |
var Instagram = require("instagram-node-lib"); | |
Instagram.set('client_id', process.env.INSTAGRAM_CLIENT_ID); | |
Instagram.set('client_secret', process.env.INSTAGRAM_CLIENT_SECRET); | |
Instagram.set('access_token', process.env.INSTAGRAM_ACCESS_TOKEN); | |
app.get('/feed/:userid', function(req, res) { | |
// Allow Cross Domain Request from anywhere... | |
res.header("Access-Control-Allow-Origin", "*"); | |
res.header("Access-Control-Allow-Headers", "X-Requested-With"); | |
var key = req.params.userid + ":" + req.query["max_id"]; | |
redis.get(key, function(err, value) { | |
if (value != null) { | |
console.log("Read " + key + " from redis"); | |
res.send(JSON.parse(value)); | |
} else { | |
console.log("Didn't find " + key + " from redis"); | |
Instagram.users.recent({ | |
user_id: req.params.userid, | |
max_id: req.query["max_id"], | |
count: 25, | |
complete: function(data, pagination) { | |
chunk = { 'data': data, 'pagination': pagination }; | |
redis.set(key, JSON.stringify(chunk)); | |
redis.expire(key, 60); | |
console.log("Wrote " + key + " to redis with a 60 second expiration"); | |
res.send(chunk); | |
} | |
}); | |
} | |
}); | |
}); | |
var port = process.env.PORT || 3000; | |
app.listen(port, function() { | |
console.log("Listening on " + port); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment