Skip to content

Instantly share code, notes, and snippets.

@simonharrer
Created April 6, 2014 17:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simonharrer/10009040 to your computer and use it in GitHub Desktop.
Save simonharrer/10009040 to your computer and use it in GitHub Desktop.
# ARGS
url = "TODO"
username = "TODO"
password = "TODO"
folder = "TARGET_FOLDER"
# LOGIC
require "fileutils"
FileUtils.rm_rf folder
FileUtils.mkdir folder
require 'nokogiri'
require 'open-uri'
doc = Nokogiri::HTML(open(url, http_basic_authentication: [username, password]))
doc.css('a').each do |link|
if link['href'] =~ /\b.+.pdf/
begin
filename = "#{folder}/#{link.text.strip.gsub("/","_")}.pdf"
filename = filename.gsub(":","_")
filename = filename.gsub("?", "_")
File.open(filename, 'wb') do |file|
download_url = url + link['href']
puts "downloading file #{download_url} to #{filename}"
downloaded_file = open(download_url, http_basic_authentication: [username, password])
file.write(downloaded_file.read())
end
rescue => ex
puts "Something went wrong.... #{ex.inspect}"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment