Skip to content

Instantly share code, notes, and snippets.

@milligramme
Forked from jaspertandy/expand_tco.rb
Last active December 14, 2015 21:09
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 milligramme/5149099 to your computer and use it in GitHub Desktop.
Save milligramme/5149099 to your computer and use it in GitHub Desktop.
Timeline上の t.co を展開して表示する Earthquake.gem プラグイン
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