开发者

Expand Tumblr shortened link

I have a Tumblr link like this: http://tumblr.com/XXXXXXXX

In order to comm开发者_如何学JAVAunicate with their API I need the hostname of the blog, therefore I need to expand the link to the complete link. Something like: http://blogname.tumblr.com/post/XXXXXX

How can I expand a tumblr shortened link?


Within the Tumblr API, I do not believe it is possible as mentioned by Derek Gottfrid. If you are using it within your app or service you can try looking at the headers.

For example in python, you can use urllib2

import urllib2
tumb = urllib2.urlopen('http://tumblr.com/XXXXXXXX')
print tumb.url

In PHP, you can use the get_headers method

$url = 'http://tumblr.com/XXXXXXXX'
print_r(get_headers($url))


Here's another way to do it in Ruby. It needs to follow tumblr redirection. From http://tmblr.co/XXXXX to http://www.tumblr.com/XXXXX and lastly to the expanded URL. From Net::HTTP documentation:

require 'net/http'
require 'uri'

def get_permalink(uri_str, limit=5)
  # You should choose better exception.
  raise ArgumentError, 'HTTP redirect too deep' if limit == 0

  response = Net::HTTP.get_response(URI.parse(uri_str))
  case response
    when Net::HTTPOK then uri_str
    when Net::HTTPMovedPermanently
      get_permalink(response['location'], limit-1)
    when Net::HTTPFound
      get_permalink(response['location'], limit-1)
  end
end

Hope it helps someone

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜