15 require 'zip/zipfilesystem'
\r
21 @address = "www.rubyquiz.com"
\r
24 first = ARGV[0].to_i if ARGV.size > 1
\r
25 last = ARGV[1].to_i if ARGV.size > 2
\r
30 # Download a binary file from the rubyquiz url
\r
32 def download(file, todir = '.')
\r
34 puts "Downloading file #{file} from #{@address}"
\r
35 c = open("http://#{@address}/#{file}").read
\r
36 Dir.mkdir(todir) if not File.directory?(todir)
\r
37 f = open("#{todir}/#{file}", 'wb')
\r
41 if not File.exists?(fullfile)
\r
42 $stderr.puts "Could not download file #{file} form #{@address}."
\r
50 # Unzip the file using GNU's stand-alone unzip utility.
\r
53 puts `unzip -o #{file}`
\r
58 # Unzip the file using rubyzip library.
\r
61 outdir = x.sub(/.*\//, '')
\r
62 outdir = '.' if outdir == ""
\r
63 Zip::ZipFile::open(x) { |zf|
\r
65 fpath = File.join(outdir, e.name)
\r
66 FileUtils.mkdir_p(File.dirname(fpath))
\r
67 zf.extract(e, fpath)
\r
75 c = open("http://#{@address}/").read
\r
76 quizzes = c.scan /quiz(\d+).html/
\r
77 return [ quizzes[0][0], quizzes[-1][0] ]
\r
80 if not first or not last
\r
82 last = l unless last
\r
83 first = f unless first
\r
90 puts "Downloading quizzess #{first} to #{last}"
\r
91 quizzes = (first..last)
\r
95 #### Download HTML description
\r
96 file = "quiz#{q}.html"
\r
97 fullfile = "#{dir}/#{file}"
\r
98 if not File.exists?(fullfile)
\r
99 download( file, dir )
\r
102 #### Download zip file
\r
103 file = "quiz#{q}_sols.zip"
\r
104 fullfile = "#{dir}/#{file}"
\r
106 if not File.exists?(fullfile)
\r
107 download( file, dir )
\r
110 # Unzip and remove .zip file
\r