root/platform/tdiary/plugin/iddy.rb

Revision 4634, 2.4 kB (checked in by hsbt, 11 months ago)

platform/tdiary/lib,plugin: import form official repos.

Line 
1#
2# iddy.rb: iddy.jp plugin for tDiary
3#
4# Copyright (C) 2007 by TADA Tadashi <sho@spc.gr.jp>
5# Distributed under GPL.
6#
7
8######################################################################
9# If you will modify or release another version of this code,
10# please get your own application key from iddy.jp and replace below.
11######################################################################
12@iddy_key = 'f93d2f38442fae3a1f08e82f84d335112cca0855'
13
14require 'open-uri'
15require 'timeout'
16require 'rexml/document'
17
18def iddy( id )
19        begin
20                cache = "#{@cache_path}/iddy.xml"
21                xml = open( cache ) {|f| f.read }
22                if Time::now > File::mtime( cache ) + 60*60 then
23                        File::delete( cache )  # clear cache 1 hour later
24                end
25        rescue Errno::ENOENT
26                begin
27                        xml = iddy_call_api( id, @iddy_key )
28                        open( cache, 'wb' ) {|f| f.write( xml ) }
29                rescue Timeout::Error
30                        return '<div class="iddy error">No Profile.</div>'
31                end
32        end
33
34        doc = REXML::Document::new( xml )
35        if doc.elements[1].attribute( 'status' ).to_s == 'fail' then
36                return '<div class="iddy error">idd.jp returns fail.</div>'
37        end
38
39        user = doc.elements.to_a( '*/*/user' )[0].elements
40        profileurl = user.to_a( 'profileurl' )[0]
41        unless profileurl then
42                return '<div class="iddy error">No profile URL on iddy.jp.</div>'
43        end
44
45        imageurl = user.to_a( 'imageurl' )[0]
46        name = user.to_a( 'name' )[0]
47        nameroma = user.to_a( 'nameroma' )[0]
48        mail = user.to_a( 'mail' )[0]
49        submail = user.to_a( 'submail' )[0]
50       
51        html = '<div class="iddy">'
52        html << %Q|<a href="#{profileurl.text}">|
53        html << %Q|<span class="iddy-image"><img src="#{imageurl.text}" alt="image" width="96" height="96"></span>| if imageurl
54        if name then
55                html << %Q|<span class="iddy-name">#{name.text}</span>|
56        elsif nameroma
57                html << %Q|<span class="iddy-name">#{nameroma.text}</span>|
58        else
59                html << %Q|<span class="iddy-name">#{id}</span>|
60        end
61        if submail then
62                html << %Q|<span class="iddy-mail">#{submail}</span>|
63        elsif mail
64                html << %Q|<span class="iddy-mail">#{mail}</span>|
65        end
66        html << '</a>'
67        html << %Q|<span class="iddy-powered">Powerd by <a href="http://iddy.jp/">iddy.jp</a></span>|
68        html << '</div>'
69        @conf.to_native( html )
70end
71
72def iddy_call_api( id, key )
73        request = "http://iddy.jp/api/user/?apikey=#{key}"
74        request << "&accountname=#{id}"
75
76        proxy = @conf['proxy']
77        proxy = 'http://' + proxy if proxy
78        timeout( 10 ) do
79                open( request, :proxy => proxy ) {|f| f.read }
80        end
81end
Note: See TracBrowser for help on using the browser.