Created
October 15, 2011 23:42
-
-
Save siyo/1290324 to your computer and use it in GitHub Desktop.
無理矢理短縮URL展開 earthquake.gem plugin
This file contains hidden or 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.init do | |
once do | |
SHORT_URL_REGEXP = %r|https?://\w+\.\w+/\w+[^\W]| | |
end | |
output_filter do |item| | |
next if item.nil? || item["text"].nil? || (urls = item["text"].scan(SHORT_URL_REGEXP)).empty? | |
f = lambda{|url| | |
uri = URI(url) | |
Net::HTTP.start(uri.host, uri.port) {|http| | |
http.open_timeout = 3 | |
http.head(uri.request_uri)["Location"] || url | |
} | |
} | |
urls.each{|url| | |
tmp = url | |
while SHORT_URL_REGEXP =~ tmp do | |
begin | |
break tmp if tmp == (t = f.call(tmp)) | |
tmp = t | |
rescue | |
break | |
end | |
end | |
item["text"].sub!(url,tmp.force_encoding(Encoding::UTF_8)) | |
} | |
true | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment