Created
August 9, 2011 13:54
-
-
Save upsilon/1134102 to your computer and use it in GitHub Desktop.
Tumblrを使はない人向けatig.rb用入力フィルタ
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
#! /opt/local/bin/ruby -w | |
# -*- mode:ruby; coding:utf-8 -*- | |
require 'atig/util' | |
require 'atig/http' | |
require 'json' | |
module Atig | |
module IFilter | |
class ExpandTumblr | |
include Util | |
def initialize(context) | |
@log = context.log | |
@http = Atig::Http.new @log | |
end | |
def call(status) | |
target = %r{(http://.+\.tumblr\.com)/post/(\d+)/?[a-zA-Z0-9\-]*} | |
status.merge :text => status.text.gsub(target) { |target_url| | |
'[tumblr] ' + expand(target_url, $1, $2) | |
} | |
end | |
def expand(url, base, post_id) | |
log :debug, url | |
api_url = URI("#{base}/api/read/json?debug=1&id=#{post_id}") | |
req = @http.req(:get, api_url) | |
res = @http.http(api_url, 3, 2).request(req) | |
json = JSON.parse(res.body) | |
post = json['posts'][0] | |
if post.key? 'link-url' then | |
post['link-url'] | |
elsif post.key? 'photo-link-url' then | |
post['photo-link-url'] | |
else | |
url | |
end | |
rescue Errno::ETIMEDOUT, JSON::ParserError, IOError, Timeout::Error, Errno::ECONNRESET => e | |
log :error, e | |
url | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment