#! /usr/local/bin/ruby require 'net/http' require 'nkf' TITLE = 'asahi.com' SERVER = "www.asahi.com" PORT = 80 PATH = '/' CACHEFILE = ENV['HOME'] + '/.headline_asahi.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 articles = [] 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) toptitle = "" topbody = "" if body =~ /(.+)/m toparticle = NKF.nkf('-Se', $1) toparticle =~ /]+>([^<]+)<\/a>/m ## / toptitle = $1.gsub(/\r?\n/, '') toparticle =~ /([^<]+)<\/font>/m ## / topbody = $1.gsub(/\r?\n/, '') else body =~ /\n]+>([^<]+)<\/a>\n/m toptitle = NKF.nkf('-Se', $1).gsub(/\r?\n/, '') body =~ /(.+)/m topbody = NKF.nkf('-Se', $1).gsub(/\r?\n/, '') end articles << toptitle + ' ¡½¡½ ' + topbody body =~ /\n(.+)\n/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