root/platform/tdiary/plugin/twitter.rb

Revision 14417, 1.2 kB (checked in by hsbt, 5 months ago)

shared option value for twitter_user.

Line 
1# twitter.rb $Revision: 1.1 $
2# Copyright (C) 2007 Michitaka Ohno <elpeo@mars.dti.ne.jp>
3# You can redistribute it and/or modify it under GPL2.
4
5require 'timeout'
6require 'time'
7require 'open-uri'
8require 'rexml/document'
9
10@twitter_statuses = []
11
12if /^(latest|day)$/ =~ @mode then
13        add_header_proc do
14                xml = nil
15                timeout( 5 ) do
16                        begin
17                                xml = open( "http://twitter.com/statuses/user_timeline/#{@conf['twitter.user']}.xml" ){|f| f.read}
18                        rescue Exception
19                        end
20                end
21                doc = REXML::Document.new( xml ).root if xml
22                if doc then
23                        doc.elements.each( 'status' ) do |e|
24                                @twitter_statuses << [@conf.to_native( e.elements['text'].text ), Time.parse( e.elements['created_at'].text ).localtime]
25                        end
26                end
27                ''
28        end
29end
30
31add_body_leave_proc do |date|
32        today_statuses = []
33        @twitter_statuses.each do |t, d|
34                 today_statuses << [t, d] if d.to_a[3,3] == date.to_a[3,3]
35        end
36        if !today_statuses.empty?
37                r = %Q[<div class="section">]
38                r << %Q[<h3><a href="http://twitter.com/#{@conf['twitter.user']}">Twitter statuses</a></h3>]
39                today_statuses.sort{|a, b| b.last<=>a.last}.each do |t, d|
40                        r << %Q[<p><strong>#{CGI::escapeHTML( t )}</strong> (#{d.strftime( '%H:%M:%S' )})</p>]
41                end
42                r << %Q[</div>]
43        else
44                ''
45        end
46end
Note: See TracBrowser for help on using the browser.