Last active
September 2, 2015 13:12
-
-
Save rkachowski/236c554f5f6defa4e355 to your computer and use it in GitHub Desktop.
mass downloadin
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
require 'oga' | |
require 'open-uri' | |
require 'uri' | |
require 'rest-client' | |
require 'pry' | |
require 'thread' | |
$log_lock = Mutex.new | |
def get_urls | |
page_url = ARGV[0] | |
page = Oga.parse_xml RestClient.get(page_url) | |
links = page.xpath("//a").map { |l| l.get "href" } | |
links.shift | |
linkurls = links.map {|b| URI.encode(page_url+b)} | |
File.open("urls","w"){ |f| f << linkurls} | |
end | |
def download_files | |
get_urls unless File.exists? "urls" | |
urls = File.open("urls") {|f|eval(f.read)} | |
threads = [] | |
urls.each { |url| | |
threads << create_download_thread(url) | |
} | |
threads.each {|t| t.join} | |
end | |
def create_download_thread download | |
Thread.new { | |
$log_lock.synchronize { puts "downloading #{download}..." } | |
RestClient.get(download){ |response, request, result,&block| | |
name = URI.decode File.basename(download) | |
$log_lock.synchronize { puts "downloaded #{download} ! "} | |
File.open(name, "w") { |f| f << response} | |
} | |
} | |
end | |
download_files |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment