4chan image downloader

I made this because it gets inconvenient when ppl dump nice prn in /s/ without uploading it to a file host.
Download windows binary here: http://www.mediafire.com/?qzau3k7rv2o253v

This code is outdated, since newer versions of axcel has been released:

#include "axcel.h"
using namespace axcel;

struct fcpost {
	String author;
	String img; 
	long double size; 
	ulong height; 
	ulong width;
};

int main(int argc, char** argv) {
	unless (argc > 1)
		die(
			"4chan Thread Downloader by Jakash3\n"
			"Download all images from a thread\n"
			"Usage: %s THREAD_URL [-o dir] [-s max] [-d height width] [-a author]\n\n"
			"-o dir            Output directory of files (default: current directory)\n"
			"-s max            Maximum size in kilobytes of images to download\n"
			"-d height width   Maximum height and width of images to download\n"
			"-a author         Only download images posted by specified author\n",
			argv[0]
		);
		
	TCP sock;
	String s = argv[1], t, u;
	File f;
	struct fcpost post;
	ulong i, j, k;
	
	long double max = 0;
	ulong height = 0, width = 0;
	String author;
	
	for (i = 2; i < argc; i++) {
		if (eq(argv[i], "-o")) cd(argv[++i]);
		else if (eq(argv[i], "-s")) max = stof<long double>(argv[++i]);
		else if (eq(argv[i], "-d")) { height = stoi<ulong>(argv[++i]); width = stoi<ulong>(argv[++i]); }
		else if (eq(argv[i], "-a")) author = argv[++i];
	}
	s = s.munch("http://");
	unless (sock.connect(s.burn('/'), "80"))
		die ("Failed to connect!\n");
	sock <<
		"GET /" << s.slurp('/') << " HTTP/1.1\r\n"
		"Host: " << s.burn('/') << "\r\n\r\n";
	until (t.ends("\r\n\r\n")) t += sock.gets();
	con.echo(t.tok(0, '\r'));
	s.clear();
	for (i = 0, j = atoi(t.slurp("Content-Length: ")); i < j; i++)
		s += sock.getc();
	sock.close();
	for (s.rewind(), t = s.gets(); !s.eof(); t = s.gets()) {
		if (t.str("<a href=\"http://images.4chan.org") == -1) continue;
		post.author = t.slurp("<span class=\"commentpostername\">").burn('<');
		post.img = t.slurp("<span class=\"filesize\">").slurp("<a href=\"").burn('\"');
		u = t.slurp("<span class=\"filesize\">").slurp("-(");
		if (u.slurp(' ').starts("KB")) post.size = stof<long double>(u.burn(' '));
		else if (u.slurp(' ').starts("MB")) post.size = stof<long double>(u.burn(' ')) * 1024;
		else post.size = 0;
		post.height = atoi(u.slurp(", "));
		post.width = atoi(u.slurp('x'));
		if (max) if (post.size > max) continue;
		if (height) if (post.height > height) continue;
		if (width) if (post.width > width) continue;
		if (!author.empty()) if (post.author != author) continue;
		if (!f.open(post.img.rslurp('/'), "wb")) {
			con << "Failed to open " << post.img.rslurp('/') << " for writing!\n";
			return 1;
		}
		unless (sock.connect("images.4chan.org", "80"))
			die ("Failed to connect to images.4chan.org!\n");
		u.clear();
		con.echo(post.img);
		sock << "GET " << post.img.slurp(".org") << " HTTP/1.1\r\nHost: images.4chan.org\r\n\r\n";
		until (u.ends("\r\n\r\n")) u += sock.gets();
		j = atoi(u.slurp("Content-Length: "));
		con << "Downloaded 0 / " << j / 1024 << " KB";
		for (i = 0, k = 1024; i < j; i++) {
			f.putc(sock.getc());
			if (i == k) {
				con.putcr(); con << "Downloaded " << i / 1024 << "/" << j / 1024 << " KB"; k += 1024;
			}
		}
		con.echo();
		con.echo();
		f.close();
		sock.close();
	}
	
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: