Created
May 23, 2011 16:00
-
-
Save cryptix/986940 to your computer and use it in GitHub Desktop.
4chan dumper node.js style
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 fs = require('fs'), | |
util = require('util'), | |
path = require('path'), | |
http=require('http'), | |
url=require('url'), | |
jsdom = require('jsdom'); | |
var boardUrl=process.argv.length>2 ? process.argv[2] : 'http://boards.4chan.org/w/res/1230239', | |
threadNum = path.basename(boardUrl); | |
//console.dir(u); | |
//console.dir(options); | |
fs.mkdir(threadNum, '0700', function(err) { | |
if(!err) console.log('Created:', threadNum); | |
}); | |
jsdom.env(boardUrl, | |
['http://code.jquery.com/jquery-1.5.min.js'], | |
function(err, window) { | |
var $ = window.$; | |
var reg = new RegExp(/images\.4chan\.org/); | |
$('.filesize > a').each(function() { | |
var lnk = $(this).attr('href'); | |
if(reg.test(lnk)) { | |
var fname = path.basename(lnk); | |
var u = url.parse(lnk); | |
var opt = {host: u.host, port: u.port||80, path: u.pathname||'/'}; | |
http.get(opt,function(res) { | |
var file = fs.createWriteStream(path.join(threadNum,fname)); | |
if(res.statusCode === 200) { | |
util.pump(res, file); | |
} | |
res.on('end', function() { | |
console.log('saved:',file.path); | |
}); | |
res.on('error', function(e) { | |
console.log("Got error: " + e.message); | |
}); | |
}); | |
} | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment