サイトの更新チェック

サイトの一部が更新されたかどうかのチェック。
cronからの起動を想定。

#!/usr/local/bin/ruby
require "nokogiri"
require "open-uri"

def check(url:, xpath: nil, css: nil, file:)
    doc = Nokogiri::HTML.parse(open(url).read)
    text = doc.search(xpath||css).text

    file = File.expand_path(file,"~")
    if text != (open(file).read rescue nil)
        open(file,"w"){|f| f.print text}
        puts File.basename(file)
        puts text
    end
end

check(
    url:   "https://rubyinstaller.org/downloads/",
    xpath: "//li[@class='exe']",
    file:  "~/lib/rubyinstaller"
)

check(
    url:   "http://ruby.morphball.net/refm-remix.html",
    xpath: "//table/thead/tr/th",
    file:  "~/lib/ruby-refm"
)