Created
May 9, 2017 11:06
-
-
Save radinreth/f8da34feda5b4d3ef1b21baaf0dc85fc to your computer and use it in GitHub Desktop.
Download all ebooks on http://hewgill.com/~greg/stackoverflow/ebooks
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
#!/usr/bin/env ruby | |
require 'rubygems' | |
require 'open-uri' | |
require 'nokogiri' | |
require 'net/http' | |
url = 'http://hewgill.com/~greg/stackoverflow/ebooks/' | |
root = uri.parse(url) | |
document = nokogiri::html(open(url)) | |
ebooks = document.css("a").to_a.map { |link| link['href'] } | |
ebooks.delete_if {|e| e[0,2] != "st"} | |
net::http.start(root.host, root.port) do |http| | |
ebooks.each do |ebook| | |
puts "downloading... #{ebook}" | |
resp = http.get("http://hewgill.com/~greg/stackoverflow/ebooks/#{ebook}") | |
open(ebook, "wb") do |file| | |
file.write(resp.body) | |
end | |
puts "download completed" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment