root/platform/tdiary/plugin/twitter_badge.rb

Revision 13100, 2.1 kB (checked in by hsbt, 6 months ago)

merge from http://gaju.jp/diary/20080602.html#p02

Line 
1#
2# twitter_badge.rb: twitter status plugin for tDiary
3#
4# Copyright (C) 2007 by Nishimoto Masaki <gaju@gaju.jp>
5# Distributed under GPL.
6#
7
8require 'open-uri'
9require 'timeout'
10require 'rexml/document'
11
12def twitter_badge( id, count )
13        begin
14                cache = "#{@cache_path}/twitter_badge.xml"
15                xml = open( cache ) {|f| f.read }
16                if Time::now > File::mtime( cache ) + 10*60 then
17                        File::delete( cache )  # clear cache 10 minutes later
18                end
19        rescue Errno::ENOENT
20                begin
21                        xml = twitter_badge_call_api( id )
22                        open( cache, 'wb' ) {|f| f.write( xml ) }
23                rescue Timeout::Error, StandardError
24                        return %Q|<div class="twitter-badge error">twitter_badge: #{$!}</div>|
25                end
26        end
27
28        begin
29                doc = REXML::Document::new( xml )
30                if doc then
31                        html = '<div class="twitter-badge">'
32                        html << '<p class="twitter-badge-title">'
33                        html << %Q|<a href="http://twitter.com/#{id}">| << 'What am I doing...</a>'
34                        html << '</p>'
35                        html << '<ul class="twitter-badge-body">'
36                        i = 0
37                        doc.elements.each( 'statuses/status' ) do |status|
38                                created_at = Time.parse( status.elements.to_a( 'created_at' )[0].text )
39                                text = status.elements.to_a( 'text' )[0].text
40                                if /^\@(.*)?/.match( text ) == nil and i < count then
41                                        html << '<li class="twitter-badge-status">'
42                                        if Time.now > created_at + 60*60*23 then
43                                                time = created_at.localtime.strftime( '%b %d %H:%M' )
44                                        else
45                                                time = created_at.localtime.strftime( '%H:%M' )
46                                        end
47                                        html << '<span class="twitter-badge-text">' << %Q|#{text}| << '</span> '
48                                        html << '<span class="twitter-badge-time">(' << %Q|#{time}| << ')</span>'
49                                        html << '</li>'
50                                        i += 1
51                                end
52                        end
53                        html << '</ul></div>'
54                        @conf.to_native( html )
55                else
56                        return '<div class="twitter-badge error">twitter_badge: Failed to open file</div>'
57                end
58        rescue REXML::ParseException
59                return '<div class="twitter-badge error">twitter_badge: Failed to parse XML</div>'
60        end
61end
62
63def twitter_badge_call_api( id )
64        request = "http://twitter.com/statuses/user_timeline/#{id}.xml"
65
66        proxy = @conf['proxy']
67        proxy = 'http://' + proxy if proxy
68        timeout( 10 ) do
69                open( request, :proxy => proxy ) {|f| f.read }
70        end
71end
Note: See TracBrowser for help on using the browser.