Skip to content

Instantly share code, notes, and snippets.

@skord
Created August 10, 2012 13:19
Show Gist options
  • Select an option

  • Save skord/3314153 to your computer and use it in GitHub Desktop.

Select an option

Save skord/3314153 to your computer and use it in GitHub Desktop.
4chan board sync
Bundler.require(:default)
require 'open-uri'
require 'net/http'
require "em-synchrony"
require "em-synchrony/em-http"
require 'em-synchrony/fiber_iterator'
class KisamaDownloader
attr_reader :url, :live_thread, :images
def initialize(url)
@url = url
@live_thread = thread_alive?
if thread_alive?
@images = get_image_urls
else
@images = []
end
end
def response
uri = URI.parse(@url)
response = Net::HTTP.get_response(uri)
end
def thread_alive?
if response.code == "200"
true
else
false
end
end
def get_image_urls
Nokogiri::HTML(open(@url)).css('.fileText a').collect {|x| x['href']}
end
def host
URI(@url).host
end
def path
URI(@url).path
end
def local_files
directory_inventory
end
def images_to_sync
to_sync = []
local_file_basenames = local_files.collect {|x| File.basename(x)}
@images.each do |image|
unless local_file_basenames.include?(File.basename(image))
to_sync << image
end
end
to_sync
end
def async_download
itscopy = images_to_sync.length
if images_to_sync.length > 0
EM.synchrony do
concurrency = 4
urls = images_to_sync
EM::Synchrony::FiberIterator.new(urls, concurrency).each do |url|
resp = EventMachine::HttpRequest.new(url).get
File.open("#{directory}/#{File.basename(url)}",'w') {|f| f.write(resp.response)}
end
EventMachine.stop
end
end
itscopy
end
def post
match_array = @url.scan(/res\/(\d{1,})/)
unless match_array.nil?
return match_array[0][0]
end
end
def board
match_array = @url.scan(/4chan\.org\/(\w{1,})/)
unless match_array.nil?
return match_array[0][0]
end
end
def directory_check
unless Dir.exists?('public')
Dir.mkdir('public')
end
unless Dir.exists?('public/4chan')
Dir.mkdir('public/4chan')
end
unless Dir.exists?("public/4chan/#{board}")
Dir.mkdir("public/4chan/#{board}")
end
unless Dir.exists?("public/4chan/#{board}/#{post}")
Dir.mkdir("public/4chan/#{board}/#{post}")
end
Dir.exists?("public/4chan/#{board}/#{post}")
end
def directory
directory_check
"public/4chan/#{board}/#{post}"
end
def directory_inventory
directory_check
Dir.glob("#{directory}/*")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment