initial import boxroom 0.6.2
[boxroom-stian.git] / vendor / plugins / rubyzip-0.9.1 / lib / download_quizzes.rb
blob7bd2281f840c5ad604337e4757f085955a7d0eb0
1 #!/bin/env ruby\r
2 \r
3 rubygems = false\r
4 begin\r
5   require 'rubygems'\r
6   rubygems = true\r
7 rescue LoadError\r
8 end\r
9 \r
10 require 'open-uri'\r
11 require 'fileutils'\r
13 rubyzip = false\r
14 begin\r
15   require 'zip/zipfilesystem'\r
16   rubyzip = true\r
17 rescue LoadError\r
18 end\r
21 @address = "www.rubyquiz.com"\r
22 first = last = nil\r
24 first = ARGV[0].to_i if ARGV.size > 1\r
25 last  = ARGV[1].to_i if ARGV.size > 2\r
29 #\r
30 # Download a binary file from the rubyquiz url\r
31 #\r
32 def download(file, todir = '.')\r
33   begin\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
38     f.puts c\r
39     f.close\r
40   rescue => e\r
41     if not File.exists?(fullfile)\r
42       $stderr.puts "Could not download file #{file} form #{@address}."\r
43       $stderr.puts e.to_s\r
44     end\r
45   end\r
46 end\r
49 #\r
50 # Unzip the file using GNU's stand-alone unzip utility. \r
51 #\r
52 def unzip_gnu(file)\r
53   puts `unzip -o #{file}`\r
54 end\r
57 #\r
58 # Unzip the file using rubyzip library.\r
59 #\r
60 def unzip(x)\r
61   outdir = x.sub(/.*\//, '')\r
62   outdir = '.' if outdir == ""\r
63   Zip::ZipFile::open(x) { |zf|\r
64     zf.each { |e|\r
65       fpath = File.join(outdir, e.name)\r
66       FileUtils.mkdir_p(File.dirname(fpath))\r
67       zf.extract(e, fpath)\r
68     }\r
69   }\r
70 end\r
74 def get_index\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
78 end\r
80 if not first or not last\r
81   f, l = get_index()\r
82   last = l  unless last\r
83   first = f unless first\r
84 end\r
86 first = first.to_i\r
87 last  = last.to_i\r
90 puts "Downloading quizzess #{first} to #{last}"\r
91 quizzes = (first..last)\r
92 quizzes.each { |q|\r
93   dir      = "quiz#{q}"\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
100   end\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
108   end\r
110   # Unzip and remove .zip file\r
111   Dir.chdir dir\r
112   if rubyzip\r
113     unzip file\r
114   else\r
115     unzip_gnu file\r
116   end\r
117   FileUtils.rm file\r
118   Dir.chdir '..'\r