Skip to content

Instantly share code, notes, and snippets.

@pokutuna
Created November 5, 2012 07:23
Show Gist options
  • Save pokutuna/4015793 to your computer and use it in GitHub Desktop.
Save pokutuna/4015793 to your computer and use it in GitHub Desktop.
require 'open-uri'
require 'nokogiri'
require 'fileutils'
USER = 'M1325'
PAGE = 'http://hsi.ksc.kwansei.ac.jp/~kono/cgi-bin/2010hci-ex/report/report.cgi?act_display_report=1&c=2&r=R2'
PATTERN = /R2.pdf|R2-1.zip/
puts 'password >>'
@auth = { :http_basic_authentication => [USER, gets] }
def open_with_auth(url)
open(url, @auth)
end
def savename(url)
h = { :userid => url[/s=(\d+)/, 1], :filename => url[/i=([^&=]+)/, 1] }
File.join("#{h[:userid]}", "#{h[:userid]}_#{h[:filename]}")
end
def save_with_auth(url)
name = savename(url)
p File.dirname(name)
FileUtils.mkdir_p(File.dirname(name)) unless File.dirname(name) =~ /\.|\//
open(name, 'wb'){ |file|
file.write(open_with_auth(url).read)
}
end
def to_absolute(path)
return path if path =~ /^http/
PAGE[0..PAGE.rindex('/')] + path
end
doc = Nokogiri::HTML(open_with_auth(PAGE))
files = doc.css('a').map{ |e| e.attr('href') }.select{ |e| e =~ PATTERN }.map{ |p| to_absolute(p) }
files.each do |url|
p url
save_with_auth(url)
end
def unzip_all
Dir.glob("**/*.zip").each { |zip|
`unzip #{zip} -d #{zip.sub(/\.zip$/, '')}`
}
end
unzip_all
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment