Skip to content

Instantly share code, notes, and snippets.

@sirishn
Created August 24, 2013 15:46
Show Gist options
  • Save sirishn/6328836 to your computer and use it in GitHub Desktop.
Save sirishn/6328836 to your computer and use it in GitHub Desktop.
4chan download images from a single thread usage ruby 4chan.rb board OP# example ruby 4chan.rb s4s 816878
#!/usr/bin/ruby
# 4chan downloader (images only)
require 'rubygems'
require 'json'
require 'open-uri'
def get_images(board, post)
@board = board
@post = post
#@board = "b"
#@post = "449783501"
thread = JSON.parse(open("http://api.4chan.org/#{@board}/res/#{@post}.json").read)
thread["posts"].each do |post|
unless post["filename"] == nil
#p post["tim"]
#p post["ext"]
directory = @board + "/" + @post
Dir.mkdir(@board) unless File.directory?(@board)
Dir.mkdir(directory) unless File.directory?(directory)
filename = post["tim"].to_s + post["ext"]
url = "http://images.4chan.org/#{@board}/src/#{filename}"
p filename
p url
unless File.exist?(directory+"/"+filename)
File.open(directory+"/"+filename, 'wb') do |fo|
fo.write open(url).read
end
end
end
end
end
if __FILE__ == $0
board = ARGV[0]
post = ARGV[1]
get_images board, post
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment