Created
November 13, 2010 13:50
-
-
Save johnconroy/675340 to your computer and use it in GitHub Desktop.
Resolving bit.ly urls to their original using bit.ly API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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