Created
May 29, 2012 05:37
-
-
Save momo-lab/2822811 to your computer and use it in GitHub Desktop.
短縮URLを展開するearthquake.gem用プラグイン。…短縮じゃないやつでもひたすら展開するけど。
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
# -*- coding: utf-8 -*- | |
# earthquake.gem plugin | |
# expand url | |
require 'open-uri' | |
require 'uri' | |
require 'net/http' | |
def get_location(url) | |
begin | |
url = URI.parse(url.to_s) | |
rescue URI::InvalidURIError | |
url = URI.parse(URI.escape(url.to_s)) | |
end | |
proxy = url.find_proxy | |
if proxy | |
clazz = Net::HTTP::Proxy(proxy.host, proxy.port, proxy.user, proxy.password) | |
else | |
clazz = Net::HTTP | |
end | |
http = clazz.new(url.host, url.port) | |
if url.class == URI::HTTPS | |
require 'net/https' | |
http.use_ssl = true | |
http.verify_mode = OpenSSL::SSL::VERIFY_PEER | |
http.cert_store = OpenSSL::X509::Store.new.set_default_paths | |
end | |
http.open_timeout = 3 | |
http.read_timeout = 3 | |
begin | |
http.start do | |
http.request_head url.request_uri do |res| | |
case res | |
when Net::HTTPMovedPermanently, # 301 | |
Net::HTTPFound, # 302 | |
Net::HTTPSeeOther, # 303 | |
Net::HTTPTemporaryRedirect # 307 | |
begin | |
location = URI.parse(res['location']) | |
rescue URI::InvalidURIError | |
return nil | |
end | |
location = url + location if location.relative? | |
return location.to_s | |
end | |
end | |
end | |
rescue TimeoutError | |
puts "expand timeout: #{url.to_s}" if config[:debug] | |
end | |
nil | |
end | |
def expand_url(url) | |
while true | |
location = get_location(url) | |
break if location.nil? | |
puts "expanded: #{url} => #{location}" if config[:debug] | |
url = location | |
end | |
url | |
end | |
Earthquake.init do | |
output_filter do |item| | |
next unless item['text'] | |
text = item['text'] | |
entities = (item["retweeted_status"] && item["truncated"]) ? | |
item["retweeted_status"]["entities"] : item["entities"] | |
if entities | |
entities.values_at("urls", "media").flatten.compact.each do |entity| | |
url, expanded_url = entity.values_at("url", "expanded_url") | |
if url && expanded_url | |
deep_expanded_url = expand_url(expanded_url) | |
text.sub!(url, expanded_url) | |
text.sub!(expanded_url, deep_expanded_url) | |
end | |
end | |
else | |
URI.extract(text.dup, %w[http https]) do |url| | |
expanded_url = expand_url(url) | |
text.sub!(url, expanded_url) | |
end | |
end | |
true | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment