#! /usr/local/bin/ruby require 'net/http' require 'nkf' TITLE = 'YOMIURI ON-LINE' SERVER = "www.yomiuri.co.jp" PORT = 80 PATH = '/main.htm' CACHEFILE = ENV['HOME'] + '/.headline_yomiuri.cache' INTERVAL = 10 * 60 if FileTest.exist?(CACHEFILE) && File.stat(CACHEFILE).mtime + INTERVAL > Time.now open(CACHEFILE) do |f| print f.read end exit 0 end if ENV['http_proxy'] proxy_server = ENV['http_proxy'].sub('^http://([^:/]+).*', '\1') proxy_port = ENV['http_proxy'].sub('^http://[^:/]+:(\d+).*', '\1').to_i proxy_port = 80 if proxy_port == 0 end http = Net::HTTP.new(SERVER, PORT, proxy_server, proxy_port) resp, body = http.get(PATH) articles = [] body =~ /(.*)/m top = NKF.nkf('-Se', $1) top =~ /([^<]+)<\/b>/m #/ toptitle = $1.gsub(/\r?\n/, '') top =~ /]+>([^<]+)<\/font>/m #/ topbody = $1.gsub(/\r?\n/, '') articles << toptitle + ' ¡½¡½' + topbody body =~ /(.*)/m top5 = NKF.nkf('-Se', $1) top5.scan("¢¡([^<]+)") do |m| articles << m[0].gsub(/\r?\n/, '') end mesg = '[' + TITLE + '] ' + articles.join(' ¢§') + "\n" print mesg open(CACHEFILE, "w") do |f| f.print mesg end