| 1 | # |
|---|
| 2 | # aboutme.rb: aboutme.jp plugin for tDiary |
|---|
| 3 | # |
|---|
| 4 | # Copyright (C) 2008 MATSUI Shinsuke <poppen@karashi.org> |
|---|
| 5 | # You can redistribute it and/or modify it under GPL2. |
|---|
| 6 | # |
|---|
| 7 | # Acknowledgements: |
|---|
| 8 | # * Based on iddy.rb by TADA. |
|---|
| 9 | |
|---|
| 10 | require 'open-uri' |
|---|
| 11 | require 'timeout' |
|---|
| 12 | require 'rexml/document' |
|---|
| 13 | |
|---|
| 14 | def aboutme( id ) |
|---|
| 15 | begin |
|---|
| 16 | cache = "#{@cache_path}/aboutme.xml" |
|---|
| 17 | xml = open( cache ) {|f| f.read } |
|---|
| 18 | if Time::now > File::mtime( cache ) + 60*60 then |
|---|
| 19 | File::delete( cache ) # clear cache 1 hour later |
|---|
| 20 | end |
|---|
| 21 | rescue Errno::ENOENT |
|---|
| 22 | begin |
|---|
| 23 | xml = aboutme_call_api( id ) |
|---|
| 24 | open( cache, 'wb' ) {|f| f.write( xml ) } |
|---|
| 25 | rescue Timeout::Error |
|---|
| 26 | return '<div class="aboutme error">No Profile.</div>' |
|---|
| 27 | end |
|---|
| 28 | end |
|---|
| 29 | |
|---|
| 30 | doc = REXML::Document::new( xml ) |
|---|
| 31 | case doc.elements['/response/status/code'].text.to_i |
|---|
| 32 | when 400, 403, 503 |
|---|
| 33 | return '<div class="aboutme error">aboutme.jp returns fail.</div>' |
|---|
| 34 | when 404 |
|---|
| 35 | return '<div class="aboutme error">No profile URL on aboutme.jp.</div>' |
|---|
| 36 | end |
|---|
| 37 | |
|---|
| 38 | user = doc.elements.to_a( '*/*/user' )[0].elements |
|---|
| 39 | profileurl = user.to_a( 'url' )[0] |
|---|
| 40 | imageurl = user.to_a( 'images/large' )[0] |
|---|
| 41 | nickname = user.to_a( 'nickname' )[0] |
|---|
| 42 | |
|---|
| 43 | html = '<div class="aboutme">' |
|---|
| 44 | html << %Q|<a href="#{profileurl.text}">| |
|---|
| 45 | html << %Q|<span class="aboutme-image"><img src="#{imageurl.text}" alt="image" width="96" height="96"></span>| if imageurl |
|---|
| 46 | html << %Q|<span class="aboutme-name">#{nickname.text}</span>| |
|---|
| 47 | html << '</a>' |
|---|
| 48 | html << %Q|<span class="aboutme-powered">| |
|---|
| 49 | html << %Q|<a href="http://aboutme.jp/" title="アバウトミー - 自分発見プロフィール" target="_blank">powerd by アバウトミー:@nifty</a>| |
|---|
| 50 | html << '</span>' |
|---|
| 51 | html << '</div>' |
|---|
| 52 | @conf.to_native( html ) |
|---|
| 53 | end |
|---|
| 54 | |
|---|
| 55 | def aboutme_call_api( id ) |
|---|
| 56 | request = "http://api.aboutme.jp/api/v1/users/show/#{id}" |
|---|
| 57 | |
|---|
| 58 | proxy = @conf['proxy'] |
|---|
| 59 | proxy = 'http://' + proxy if proxy |
|---|
| 60 | timeout( 10 ) do |
|---|
| 61 | open( request, :proxy => proxy ) {|f| f.read } |
|---|
| 62 | end |
|---|
| 63 | end |
|---|
| 64 | |
|---|
| 65 | # Local Variables: |
|---|
| 66 | # mode: ruby |
|---|
| 67 | # indent-tabs-mode: t |
|---|
| 68 | # tab-width: 3 |
|---|
| 69 | # ruby-indent-level: 3 |
|---|
| 70 | # End: |
|---|
| 71 | # vi: ts=3 sw=3 |
|---|