Skip to content

Instantly share code, notes, and snippets.

@radinreth
Created May 9, 2017 11:06
Show Gist options
  • Save radinreth/f8da34feda5b4d3ef1b21baaf0dc85fc to your computer and use it in GitHub Desktop.
Save radinreth/f8da34feda5b4d3ef1b21baaf0dc85fc to your computer and use it in GitHub Desktop.
#!/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