Created
September 8, 2009 15:55
-
-
Save kayakaya/183029 to your computer and use it in GitHub Desktop.
friendfeed_badge.rb: show the FriendFeed profile picture plugin for tDiary
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# friendfeed_badge.rb: show the FriendFeed profile picture plugin for tDiary | |
# | |
# Copyright (C) 2009 KAYA Satoshi <http://kayakaya.net/> | |
# Distributed under GPLv2. | |
# | |
require 'net/http' | |
Net::HTTP.version_1_2 | |
def friendfeed_picture(feed_id) | |
begin | |
cache_dir = "#{@cache_path}/friendfeed_badge" | |
Dir::mkdir(cache_dir) unless File::directory?(cache_dir) | |
cache_file = "#{cache_dir}/picture.txt" | |
picture_url = open(cache_file) {|f| f.read } | |
if Time::now > File::mtime(cache_file) + 60 * 60 then | |
File::delete(cache_file) #clear cache 1 hour later | |
end | |
rescue Errno::ENOENT | |
begin | |
location = call_friendfeed_picture_api(feed_id) | |
open(cache_file, 'wb') {|f| f.puts(location)} | |
rescue Timeout::Error | |
return '<div class="friendfeed-picture">friendfeed.com no data.</div>' | |
end | |
end | |
html = '<div class="friendfeed-picture">' | |
html << %Q|<a href="http://friendfeed.com/#{feed_id}">| | |
html << %Q|<span class="friendfeed-image"><img src="#{picture_url}" alt="FriendFeed" width="75" height="75"></span>| | |
html << '</a>' | |
html << %Q|<span class="friendfeed-powered">Powered by <a href="http://friendfeed.com">FriendFeed</a></span>| | |
html << '</div>' | |
@conf.to_native(html) | |
end | |
# This method is indivisual, because FriendFeed API for pictures returns 302 code. | |
def call_friendfeed_picture_api(feed_id) | |
url = URI.parse("http://friendfeed-api.com/v2/picture/#{feed_id}?size=large") | |
req = Net::HTTP::Get.new(url.path) | |
res = Net::HTTP.start(url.host, url.port) {|http| | |
http.request(req) | |
} | |
if res.class == Net::HTTPFound | |
location = res['Location'] | |
end | |
return location | |
end | |
# Local Variables: | |
# ruby-indent-level: 3 | |
# tab-width: 3 | |
# indent-tabs-mode: t | |
# End: |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment