Skip to content

Instantly share code, notes, and snippets.

@taka2
Created August 23, 2008 12:13
Show Gist options
  • Save taka2/6910 to your computer and use it in GitHub Desktop.
Save taka2/6910 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'rubeus'
require 'uri'
require 'net/http'
require 'rss'
class RealTimeRssReader
include Rubeus::Swing
# Reflesh rate [ms]
REFLESH_RATE = 20000
def initialize
JFrame.new do |f|
f.layout = BoxLayout.new(:Y_AXIS)
p = JPanel.new do |p|
p.layout = BoxLayout.new(:X_AXIS)
lbl = JLabel.new("Feed URI: ")
@input_uri = JTextField.new("http://d.hatena.ne.jp/diarylist?mode=rss")
end
jsp = JScrollPane.new do
@editor = JEditorPane.new
@editor.setContentType("text/html")
@editor.setEditable(false)
@editor.add_hyperlink_listener do |event|
if event.getEventType == javax.swing.event.HyperlinkEvent::EventType::ACTIVATED
java.awt.Desktop.getDesktop.browse(event.getURL.toURI)
end
end
end
now = Time.now
latestTime = now
t = Timer.new(REFLESH_RATE) do
uri = p.getComponent(1).getText
if uri != ""
# URIを解析する
uri_arr = URI.split(uri)
host = uri_arr[2]
port = uri_arr[3] ||= 80
path = uri_arr[5]
query = uri_arr[7]
if query != nil
queryPath = "#{path}?#{query}"
else
queryPath = path
end
# RSSフィードを取得する
Net::HTTP.start(host, port) do |http|
begin
response = http.get(queryPath)
# 取得したRSSフィードを解析する
rss = RSS::Parser.parse(response.body)
# 取得したフィード一覧をHTMLに変換する
contents = ""
items = rss.items.reverse
items.each do |item|
formatted_date_time = (item.date + now.utc_offset).strftime("%Y/%m/%d %H:%M:%S")
if item.date > latestTime
formatted_date_time = "<font color = red>#{formatted_date_time}</font>"
end
contents = contents + "#{formatted_date_time} | <a href = \"#{item.link}\">#{item.title}</a><br>\n"
end
latestTime = items.pop.date
# 結果をビューに反映
editorPane = jsp.getComponent(0).getComponent(0)
editorPane.setText(contents)
rescue e
puts "HTTP GET failed #{uri} #{e.to_s}"
end
end
end
end
t.initial_delay = 0
t.start
f.title = "Realtime rss reader"
f.size = "800 x 600"
f.visible = true
end
end
end
RealTimeRssReader.new
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment