Skip to content

Instantly share code, notes, and snippets.

@j3lte
Last active December 15, 2015 21:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save j3lte/5326383 to your computer and use it in GitHub Desktop.
Save j3lte/5326383 to your computer and use it in GitHub Desktop.
NodeJS 4chan image downloaderFirst attempt, downloads all images from a board. OUTDATED! SEE: https://github.com/j3lte/chanarchive
//
// ******************************************************************
// THIS GIST IS OUTDATED. SEE : https://github.com/j3lte/chanarchive
// ******************************************************************
// URL Representation
/*
JSON representations of threads and indexes are exposed at the following URLs:
http(s)://api.4chan.org/board/res/threadnumber.json
http(s)://api.4chan.org/board/pagenumber.json (0 is main index)
A JSON representation of all thread OPs from an individual board can be found at the following URL:
http(s)://api.4chan.org/board/catalog.json
A list of boards is exposed at the following URL:
http(s)://api.4chan.org/boards.json
*/
// Vars
var path = require('path')
, fs = require('fs')
, request = require('request')
, mkdirp = require('mkdirp')
, saveFolder = './download/'
, numFiles = 0;
var thrd = process.argv[2],
test = /^http:\/\/boards.4chan.org\/(\w){1,}\/res\/(\d){1,}/;
if (typeof(thrd) === 'undefined' || thrd === null || !test.test(thrd)){
showUsage();
} else {
console.log(thrd);
var board = thrd.split(/\/|\?|&|=|\./g)[5],
thrdnum = thrd.split(/\/|\?|&|=|\./g)[7];
downloadBoard(board,thrdnum);
}
function showUsage(){
console.log('Usage: node main.js http://boards.4chan.org/<board>/res/<thread>');
process.exit(1);
}
function downloadBoard(board,thread){
var boardUrl = 'http://api.4chan.org/'+board+'/res/'+thread+'.json';
request(boardUrl, function (error, response, body) {
if (!error && response.statusCode == 200 && response.headers['content-type'] === 'application/json') {
console.log("Downloaded: "+boardUrl);
saveFolder += board+'/'+thrdnum+'/';
mkdirp(saveFolder, function(err){
if (err) {
console.error('Error creating folder! '+err);
} else {
console.log('Files saved in: ' + saveFolder);
var jsonBody = JSON.parse(body);
fs.writeFile(saveFolder+thread+'.json.txt',JSON.stringify(jsonBody,null,4));
jsonBody.posts.forEach(
function(post){
if (post.filename){
numFiles++;
var orig = post.filename+'_'+post.tim+post.ext,
ren = 'http://images.4chan.org/'+board+'/src/'+post.tim+post.ext;
downloadPic(ren,orig);
}
}
);
}
});
} else {
console.log("Not found! Tried: "+boardUrl);
}
});
}
// Download
function downloadPic (urlPic,orig){
var localPath = saveFolder+orig;
var r = request(urlPic).pipe(fs.createWriteStream(localPath));
r.on('close',function(){
numFiles--;
console.log('Downloaded ' + localPath + ' [ ' + numFiles + ' remaining ]');
});
r.on('error',function(){
numFiles--;
console.log('Error downloading ' + localPath);
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment