Skip to content

Instantly share code, notes, and snippets.

@upsilon
Created August 9, 2011 13:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save upsilon/1134102 to your computer and use it in GitHub Desktop.
Save upsilon/1134102 to your computer and use it in GitHub Desktop.
Tumblrを使はない人向けatig.rb用入力フィルタ
#! /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