Created
April 6, 2014 17:28
-
-
Save simonharrer/10009040 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# 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