-
-
Save milligramme/5149099 to your computer and use it in GitHub Desktop.
Timeline上の t.co を展開して表示する 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
require "open-uri" | |
require "openssl" | |
Earthquake.init do | |
output_filter do |item| | |
next unless item['text'] | |
text = item["text"] | |
text = item['retweeted_status']['text'] unless item['retweeted_status'].nil? | |
text.scan( /https?:\/\/t\.co\/[a-zA-Z0-9\-]+/ ).each do |url| | |
uri = URI.parse url | |
if uri.port==80 | |
Net::HTTP::start( uri.host , uri.port ) do |connection| | |
connection.request_get uri.path do |response| | |
text.gsub! url , response['Location'] | |
end | |
end | |
elsif uri.port==443 | |
begin | |
open(uri, :ssl_verify_mode => OpenSSL::SSL::VERIFY_NONE) do |file| | |
text.gsub! url ,file.base_uri.to_s | |
end | |
rescue Exception => e | |
puts "#{e}".c(43, 91) | |
# RuntimeError: redirection forbidden | |
# - https://vimeo.com => http://vimeo.com | |
# - https://soundcloud.com => http://soundcloud.com | |
# OpenURI::HTTPError: 404 Not Found | |
# - https://t.co/redirect?url=xxxxx | |
end | |
end | |
end | |
text | |
end | |
end | |
# TODO verify certificate | |
# https://gist.github.com/milligramme/5149099 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment