Created
November 21, 2011 23:16
-
-
Save mseeley/1384313 to your computer and use it in GitHub Desktop.
Node script to grab tiles from http://xkcd.com/980/huge/
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
var http = require('http'), | |
fs = require('fs'), | |
output = './money_tiles7/', | |
// Tokens and ranges for the 1:1 detail level | |
zoom = 0, | |
xmin = 0, | |
xmax = 48, | |
ymin = 0, | |
ymax = 32, | |
x = 0, | |
y = 0, | |
i = 0; | |
function pad (str, length) { | |
var filler = '0'; | |
// Yuck. | |
return new Array(length - (String(str).length - 1)).join(filler) + str; | |
} | |
function next () { | |
var filename = 'tile_' + pad(zoom, 3) + '_' + pad(x, 3) + '_' + pad(y, 3) + '.png', | |
options = { | |
host: 'imgs.xkcd.com', | |
path: '/money_tiles7/' + filename, | |
port: 80 | |
}; | |
http.get(options, function (res) { | |
var imagedata = ''; | |
res.setEncoding('binary') | |
res.on('data', function (chunk) { | |
imagedata += chunk; | |
}); | |
res.on('end', function () { | |
// Favor row-biased linear sequence naming. Join tiles via ImageMagick's montage binary | |
// montage -mode concatenate -tile 49x tile_*.png joined.png | |
fs.writeFile(output + 'tile_' + pad(i++, 4) + '.png', imagedata, 'binary', function(err){ | |
if (err) throw err; | |
var clampx = x === xmax, | |
clampy = y === ymax; | |
if (clampx && clampy) { | |
console.log('Done'); | |
return; | |
} | |
if (clampx) { | |
x = xmin; | |
y = clampy ? ymin : y + 1; | |
} else { | |
x++; | |
} | |
next(); | |
}); | |
}); | |
}); | |
} | |
next(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment