Monday, January 14, 2013

Get original urls from short urls created by services like bitly, tinyurl, twitter etc using ruby and open-uri

Pretty straight forward it is. Writing it here coz i couldn't find anything like this explained for ruby, found a lotta blogs for python though.

So i thought of explaining it here. So here goes. Sometimes we come across urls like http://bit.ly/y2uy321oy3 which can be for any website. There's this simple way to retrieve its original url.



We'll use open-uri for this:

require 'open-uri'
url = "http://bit.ly/2Szbi2"
open(url) do |response|
  original_url = response.base_uri.to_s
end
original_url = "http://twitter.com/blogger"

And that's that. You have the original url, which is the blogger page on twitter in this case :)

No comments:

Post a Comment