Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

working on caching flickr images locally and posting json data for se…

…rver storage
  • Loading branch information...
commit 984ec250eb9e8edf5f8e0b8ac49224b7061409e2 1 parent 0a975ac
@ninjascience authored
View
87 app.js
@@ -6,6 +6,9 @@
var express = require('express')
, routes = require('./routes')
, url = require('url')
+ , phantom = require('phantom')
+ , fs = require('fs')
+ , path = require('path')
, http = require('http');
var app = module.exports = express.createServer();
@@ -31,41 +34,73 @@ app.configure('production', function(){
});
+// Routes
+
+app.get('/', routes.index);
+app.get('/dahlia', routes.dahlia);
+app.get('/dahlia/search', routes.dahlia.search);
+
+
// image proxy
app.get('/flickr_image/:image_url', function(request_from_client, response_to_client){
//sys.puts("Starting proxy");
var image_url = request_from_client.params.image_url;
-
- var image_host_name = url.parse(image_url).hostname
- var filename = url.parse(image_url).pathname.split("/").pop()
-
- var http_client = http.createClient(80, image_host_name);
- var image_get_request = http_client.request('GET', image_url, {"host": image_host_name});
- image_get_request.addListener('response', function(proxy_response){
- var current_byte_index = 0;
- var response_content_length = parseInt(proxy_response.header("Content-Length"));
- var response_body = new Buffer(response_content_length);
-
- proxy_response.setEncoding('binary');
- proxy_response.addListener('data', function(chunk){
- response_body.write(chunk, current_byte_index, "binary");
- current_byte_index += chunk.length;
- });
- proxy_response.addListener('end', function(){
- response_to_client.contentType(filename);
- response_to_client.send(response_body);
- });
+ var pathname = url.parse(image_url).pathname;
+ var filename = pathname.split("/").pop();
+
+ var http_client;
+ var image_get_request;
+ var image_host_name;
+var port;
+
+ fs.stat('public/images/cache/'+filename, function (err, stats) {
+ if(err)
+ {
+ console.log('cache exists for: ' + filename);
+ image_host_name = 'localhost'; //TODO: get the current hostname
+ image_url = 'http://'+image_host_name+':3000/images/cache/'+filename; //TODO: read the filesystem image and return
+ port = 3000;
+ }
+ else
+ {
+ console.log('no cache for: ' + filename);
+ image_host_name = url.parse(image_url).hostname;
+ port = 80;
+ }
+
+ http_client = http.createClient(port, image_host_name);
+ image_get_request = http_client.request('GET', image_url, {"host": image_host_name, "port":port});
+ image_get_request.addListener('response', function(proxy_response){
+ var current_byte_index = 0;
+ var response_content_length = parseInt(proxy_response.header("Content-Length"));
+ var response_body = new Buffer(response_content_length);
+
+ proxy_response.setEncoding('binary');
+ proxy_response.addListener('data', function(chunk){
+ response_body.write(chunk, current_byte_index, "binary");
+ current_byte_index += chunk.length;
+ });
+ proxy_response.addListener('end', function(){
+ response_to_client.contentType(filename);
+ response_to_client.send(response_body);
+ });
+ });
+ image_get_request.end();
});
- image_get_request.end();
-});
-// Routes
+
+});
-app.get('/', routes.index);
-app.get('/dahlia', routes.dahlia);
-app.get('/dahlia/search', routes.dahlia.search);
+app.post('/hues/json', function(request_from_client, response_to_client){
+ var image_url = request_from_client.params.hues_json;
+ var stream = fs.createWriteStream(image_url+".json");
+ stream.once('open', function(fd) {
+ stream.write(image_url+"\n");
+ });
+ response_to_client.send('');
+});
app.listen(3000);
console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);
View
110 public/javascripts/dahlia/search.js
@@ -7,7 +7,7 @@ $(document).ready(function(){
var items = [];
$.each(data.photoset.photo, function(key, photo) {
photos[photo.id] = photo;
- var url = '/flickr_image/'+getPhotoUrl(photo, "s");
+ var url = '/flickr_image/'+getPhotoUrl(photo, "");
$('#dahliaThumbs').append('<li class="thumbnail" id="photo_' + photo.id + '"><a href="' + url + '" data-photoid="'+ photo.id +'"><img src="'+url+'" /></li>');
});
@@ -25,17 +25,37 @@ $(document).ready(function(){
context.drawImage(this, 0, 0);
var imageData = context.getImageData(0, 0, canvas.width, canvas.height);
var data = imageData.data;
-
+ var hues = [];
+ for (var h = 0; h < 360; h++)
+ {
+ hues[h] = [];
+ for(var b = 0; b < 100; b++)
+ {
+ hues[h][b] = 0;
+ }
+ }
// to quickly iterate over all pixels, use a for loop like this
for (var i = 0; i < data.length; i += 4) {
var red = data[i]; // red
var green = data[i + 1]; // green
var blue = data[i + 2]; // blue
+ var col = chroma.rgb(red, green, blue);
+ var hsl = col.hsl();
+ var hue = Math.floor(hsl[0]);
+ if(! isNaN(hue))
+ {
+
+ var brightness = Math.floor(hsl[2] * 100);
+ hues[hue][brightness]++;
+ }
// i+3 is alpha (the fourth element)
}
+ //var encoded = JSON.stringify(hues);
+ //$('#output').innerHTML(encoded);
+ drawChart(hues);
};
- imageObj.src = '/flickr_image/' + getPhotoUrl(photo, "z");
+ imageObj.src = '/flickr_image/' + getPhotoUrl(photo, "");
});
});
});
@@ -43,6 +63,10 @@ $(document).ready(function(){
function getPhotoUrl(photo, size)
{
//http://farm{farm-id}.staticflickr.com/{server-id}/{id}_{secret}_[mstzb].jpg
+ if(size != "")
+ {
+ size = "_" + size;
+ }
var url = 'http://farm'
+ photo.farm
+ '.staticflickr.com/'
@@ -51,9 +75,87 @@ function getPhotoUrl(photo, size)
+ photo.id
+ '_'
+ photo.secret
- + '_'
+ size
+ '.jpg';
url = encodeURIComponent(url);
return url;
+}
+
+function drawChart(hueData)
+{
+ $('#chart').empty();
+ var colWidth = 1,
+ w=colWidth*360,
+ h=360,
+ x = d3.scale.linear().range([0,w]),
+ y = d3.scale.linear().range([0,h - 40]);
+ var svg = d3.select("#chart")
+ .append("svg:svg")
+ .attr("height", h)
+ .attr("width", w + 40)
+ .attr("height", h + 20)
+ .append("svg:g")
+ .attr("transform","translate(" + x(1) + "," + (h-20) + ")scale(-1,-1)");
+
+ var body = svg.append("svg:g")
+ .attr("transform", "translate(0,0)");
+
+ //d3.json("/json/hues.json", function(data) {
+ //var stackData = d3.layout.stack()(hueData);
+
+ var b0 = 0,
+ b1 = d3.max(hueData, function(h){
+ return d3.sum(h, function(b){
+ return b;
+ });
+ }),
+ h0 = 0,
+ h1 = 359;
+
+ x.domain([0, 360]);
+ y.domain([0, b1]);
+
+ var hue = 0;
+ var bStart = 0;
+ var hues = body.selectAll("g")
+ .data(hueData)
+ .enter()
+ .append("svg:g")
+ .attr("transform", function(d, i){
+ //hue = i;
+ bStart = 0;
+ return "translate(" + x(359-i) + ",0)";
+ }).selectAll("rect")
+ .data(function(d, i){
+ bStart = 0;
+ return d;
+ })
+ .enter().append("svg:rect")
+ .attr("x", 0)
+ .attr("y", function(d,i){
+ var yPx = y(d);
+ yVal = bStart;
+ bStart += y(d);
+
+ if(i == 99)
+ {
+ bStart = 0;
+
+ }
+ //console.info(d, bStart);
+ return yVal;
+ })
+ .attr("width", colWidth)
+ .attr("height", function(d){
+ var yPx = y(d);
+ return yPx;
+ })
+ .attr("fill", function(d,i){
+ if(i == 99)
+ {
+ hue+=1;
+ }
+ return d3.hsl(hue, .75, i/100);
+ });
+ //});
}
View
5 public/stylesheets/style.css
@@ -5,4 +5,9 @@ body {
a {
color: #00B7FF;
+}
+
+svg {
+ shape-rendering: crispEdges;
+
}
View
5 routes/index.js
@@ -11,8 +11,3 @@ exports.index = function(req, res){
exports.dahlia = function(req, res){
res.render('dahlia/dahlia', { title: 'Dahlia' })
};
-
-
-exports.dahlia.search = function(req, res){
- res.render('dahlia/search', { title: 'Dahlia Flickr Search' })
-};
View
8 views/dahlia/dahlia.jade
@@ -3,5 +3,11 @@ div
h1= title
- canvas#imageCanvas(width=640,height=640)
+ ul.media-grid
+ li
+ a
+ canvas#imageCanvas(width=500,height=500)
+ li
+ a
+ div#chart(width=360,height=360)
ul#dahliaThumbs.media-grid
View
3  views/layout.jade
@@ -6,4 +6,7 @@ html
link(rel="stylesheet/less", href="/stylesheets/bootstrap/bootstrap.less")
script(src="/javascripts/jquery-1.7.1.js")
script(src="/javascripts/less-1.2.1.min.js")
+ script(src="/javascripts/chroma.min.js")
+ script(src="/javascripts/mbostock-d3-7d6080d/d3.js")
+ script(src="/javascripts/mbostock-d3-7d6080d/d3.layout.js")
body!= body
Please sign in to comment.
Something went wrong with that request. Please try again.