Question

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 ?

Was it helpful?

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.

OTHER TIPS

If anybody finds this outdated post as a starting point, please note that the Bit.ly API rejects non-OAuth API keys nowadays.

In python get your API key with curl:

curl -u "username:password" -X POST "https://api-ssl.bitly.com/oauth/access_token"

Doc link

As of 2019, there is the bitlyshortener package, although it works only with Python ≥3.7. I have not experienced any error using it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top