Skip to content

Instantly share code, notes, and snippets.

@johnconroy
Created November 13, 2010 13:50
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 johnconroy/675340 to your computer and use it in GitHub Desktop.
Save johnconroy/675340 to your computer and use it in GitHub Desktop.
Resolving bit.ly urls to their original using bit.ly API
# resolving urls shortened with Bit.ly back to their original form
# Bit.ly has a beautiful, simple API for this. And the Bitly API wrapper is simple to use.
# I did this for analysing which domains in a large list were the most popular.
import bitly #bitly API wrapper
import time
import random
fr="C:\\somedir\\bitlyurls.txt"
fw="C:\\somedir\\bitlys_RESOLVED.txt"
api=bitly.Api(login='myBitlyUname', apikey='myBitlyApiKey')
#populate a list with bitly urls
fileread=open(fr,'r')
bitlys=fileread.readlines()
fileread.close()
#for each bitly in list, resolve it via api
for x in range(len(bitlys)):
thisbitly=bitlys[x][:-1]
try:
long_url=api.expand(thisbitly)
filewrite=open(fw,'a')
filewrite.write(long_url+"\n")
filewrite.close()
time.sleep(random.randint(4,10)) #be kind to their servers
except:
filewrite=open(fw,'a')
filewrite.write("***COULD NOT RESOLVE***\n")
filewrite.close
print "Could not resolve Pos. ",x, " ",bitlys[x]
time.sleep(random.randint(4,10))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment