Skip to content

Instantly share code, notes, and snippets.

@KevinTCoughlin
Last active August 29, 2015 13:57
Show Gist options
  • Save KevinTCoughlin/9422540 to your computer and use it in GitHub Desktop.
Save KevinTCoughlin/9422540 to your computer and use it in GitHub Desktop.
iTunes Top 100 Collage
var request = require('request')
, sys = require('sys')
, fs = require('fs')
, mkdirp = require('mkdirp')
, gm = require('gm').subClass({ imageMagick: true });
var n = 100;
var l = Math.sqrt(n);
var w = Math.sqrt(n);
var width = 170;
var height = 170;
var explicit = true;
var canvasHeight = n * 170;
var canvasWidth = n * 170;
var itunesURL = "https://itunes.apple.com/us/rss/toppodcasts/limit=" + n + "/explicit=" + explicit + "/json";
/*
request(itunesURL, function (error, response, body) {
if (!error && response.statusCode == 200) {
var result = JSON.parse(body);
var entries = result.feed.entry;
for (var i = 0; i < l; i++) {
for (var k = 0; k < w; k++) {
var pos = i * k;
var entry = entries[pos];
var imageURL = entry['im:image'][2].label;
request(imageURL).pipe(fs.createWriteStream(__dirname + '/tmp/' + i * k + '.jpg'));
}
}
}
});
*/
var output = gm();
fs.readdir(__dirname + '/tmp/', function(err, files){
if (err) throw err;
//http://stackoverflow.com/questions/6377362/calculating-or-rows-and-columns
var num = files.length;
if (num % 2 !== 0) num--;
var rows = Math.floor(Math.sqrt(num));
while(num % rows !== 0) {
rows--;
}
var columns = num / rows;
var counter = 0;
for (var i = 0; i < rows; i++) {
for (var k = 0; k < columns; k++) {
var file = files[counter];
output.in('-page', '+' + i * 170 + '+' + k * 170);
output.in('/tmp/' + file);
counter++;
}
}
output
.mosaic()
.write('output.jpg', function (err) {
if (err) console.log(err);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment