#! /usr/local/bin/ruby require 'net/http' require 'xmlparser' require 'uconv' TITLE = 'Yendot' SERVER = "ukai.org" PORT = 80 PATH = '/mu/?format=rdf' CACHEFILE = ENV['HOME'] + '/.headline_yendot.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 = [] context = [] title = false XMLParser.new.parse(body) do |ev, name, data| case ev when XMLParser::START_ELEM context.push(name) title = (name == 'title' and context[-2] == 'item') when XMLParser::END_ELEM context.pop when XMLParser::CDATA if title articles << Uconv::u8toeuc(data) title = false end end end mesg = '[' + TITLE + '] вз' + articles.join(' вз') + "\n" print mesg open(CACHEFILE, "w") do |f| f.print mesg end