Created
April 19, 2010 00:01
-
-
Save wtn/370634 to your computer and use it in GitHub Desktop.
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
@@hydra ||= Typhoeus::Hydra.new( max_concurrency: 100 ) | |
def batch_download(pages) | |
fibers = pages.map(&:uncached_download_fiber).compact | |
requests = fibers.map(&:resume) | |
requests.each {|req| @@hydra.queue req } | |
@@hydra.run | |
requests.map.with_index {|req,i| fibers[i].resume(req.response) } | |
end | |
def uncached_download_fiber | |
cached? ? nil : process_markup | |
end | |
def process_markup | |
request = Typhoeus::Request.new(url) | |
Fiber.new do | |
response = Fiber.yield(request) | |
response.code == 200 and File.open(file_path, 'w') {|f| f.puts(remote_markup) } | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment