更新取得、これでいいかな
uri-info.rb
require 'open-uri' # 10分。これ以下の指定は無視される min =600 if ARGV[0]==nil puts "usage: uri-info %0 [%1] =>type last-modified of %0 [timer:%1sec.]" puts " default interval= #{min}sec." exit end def getext(a) ext =a.scan(/[^\.]+/)[-1] if ext =~ /\// then ext="" end ext end url =ARGV[0] $savf ="ss" $savf = url.scan(/([^\/]+\.){1,}/)[-1][0] +url.scan(/[^\.]+/)[-1] $savf.gsub!(/\//,'_') $ext =getext(url) print "保存ファイル名: #{$savf}" log ="uri-last-mod.txt" uri = URI.parse(url) puts def key(req) puts "[head]" a={} keys= req.each_key{} keys.each{|a,b| print a,": ",b,"\n" } end def modified(uri,fl) http =Net::HTTP.start( uri.host, 80 ) req = http.head(uri.request_uri) key(req) if(fl) keys= req.each_key{} ans="" modi=keys["last-modified"] if modi!=nil ans ="last_modified: "+modi[0] else ans="(no-field)" end puts [ans,http] end def getlast(fi) exist = false ans ="" open(fi,"a+"){|log| log.each_line{|line| data =line.chomp if data =~ /^last_modified:/ ans = data end } } ans end def putlast(fi,uri,dat) open(fi,"w"){|ou| ou.puts uri ou.puts dat } end def save(f,tm) print "getting: " name =getname($savf,$ext,tm) open(name,"wb"){|writ| writ.puts f.read } puts name,"saved" end def tprint(tm) sprintf("-%d%02d%02d%02d%02d",tm.year,tm.month,tm.day,tm.hour,tm.min) end def getname(name,ext,tm) #tm =Time.now tim=tprint(tm) name +tim +'.' +ext end def loo(log,uri) last = getlast(log) modi ,http = modified uri,false p last,modi #,http putlast(log,uri,modi) if modi!=last p "new" open(uri){|f| save(f,f.last_modified) } end end timer=min if ( ARGV[1]!=nil and ARGV[1].to_i>min ) then timer=ARGV[1].to_i end loop{ loo(log,uri) puts # count down timer.times{|co| sleep 1 print "\r#{Time.now} (#{timer-co-1}) \r" } puts }