Mozilla Firefox 3を3分ごとにダウンロードするスクリプト
- なんだかおもしろそうなのでDownload-Dayに乗ってみた
- ダウンロードするファイルのURIはプラットフォームに応じて適宜変更してね
- 公開してみたものの、サーバーに優しくないからあまり使うべきではないかも
#!/usr/local/bin/ruby require 'net/http' Net::HTTP.version_1_2 # おまじない interval = ARGV[0] ? ARGV[0].to_i : 180 limit = Time.gm(2008, 6, 18, 17) path = "http://download.mozilla.org/?product=firefox-3.0&os=win&lang=ja" count = 0 # リファレンスマニュアルから流用 def fetch(uri_str, limit = 10) raise ArgumentError, 'http redirect too deep' if limit == 0 response = Net::HTTP.get_response(URI.parse(uri_str)) case response when Net::HTTPSuccess then response when Net::HTTPRedirection then fetch(response['location'], limit - 1) else response.error! end end while Time.now < limit do outname = "firefox3_#{count}.exe" print("trying to download #{outname} ... ") begin output = fetch(path).body File.open(outname, "wb") {|f| f.write(output) } puts("done") rescue puts("error!") ensure puts("sleep #{interval}-sec") sleep(interval) count += 1 end end
同一ホストからの複数ダウンロードはダウンロード回数に含まれるのかな?