Search code examples
twitter

Twitter API: What is the difference in the following URLs?


What is going on in the following?

import json, urllib
url = "funnyfurniture.net/p/10/oops-chair/"
url2 = "http://funnyfurniture.net/p/10/oops-chair/"
tw_url = "http://urls.api.twitter.com/1/urls/count.json?url=%s" %url
tw_url2 = "http://urls.api.twitter.com/1/urls/count.json?url=%s" %url2
js2 = json.load(urllib.urlopen(tw_url))
js = json.load(urllib.urlopen(tw_url2))
print js2, js

It gives

{u'count': 0, u'url': u'http://funnyfurniture.net/p/10/oops-chair/'} {u'count': 1, u'url': u'http://funnyfurniture.net/p/10/oops-chair/'}

What is the difference?


Solution

  • The Twitter API normalizes URLs, so when you pass in cnn.com it converts it to http://cnn.com automatically:

    % curl 'http://urls.api.twitter.com/1/urls/count.json?url=foo'
    {"count":0,"url":"http://foo/"}
    

    The discrepancy in the counts you saw may have been a temporary bug on Twitter's side, e.g., calculating the counts before normalizing the URLs.