Search code examples
pythonurl-shortener

Connection timeout error in bitly URL shortener


I am trying to use bitly-api-python library to shorten all the urls in an array.

def bitly3_shorten_oauth(url):
    c = bitly3.Connection(access_token= bitly_access_token)
    sh = c.shorten(url)
    return sh['url']

for i in arr:
    print i[1] , bitly3_shorten_oauth(i[1])

I am calling them one after other without any timeout, since I couldn't find any such precaution in the best practices documentation of bitly.

Here is my complete code, please have a look : http://pastie.org/8419004

but what is happening is that it shortens 2 or 3 of the urls and then goes to a connection timeout error

connection timeout error

What might be causing this error and how do I debug it ?


Solution

  • From the documentation you linked:

    bitly currently institutes per-hour, per-minute, 
    and per-IP rate limits for each API method
    

    And

    High-Volume Shorten Requests
    If you need to shorten a large number of URLs at once, we recommend that 
    you leave ample time to spread these requests out over many hours. Our API 
    rate limits reset hourly, and rate limited batch requests can be resumed at 
    the top of the hour.
    

    So it does look like you simply need to slow down your code.