0

I have seen a few things on the site trying to help with this issue but I couldn't make head or tail as to what I'm doing wrong.

This bit of code is supposed to get a list of followers but no matter what I try I get a 429 error from the Twitter API:

def get_follow_list():
    next_cursor = -1

    while next_cursor != 0:
        response = twitter.get_followers_list(screen_name=current_user, cursor=next_cursor)
        following = response['users']
        follow_list = [following]
        time.sleep(1)
        cursor = response['next_cursor']

    return (follow_list)

How would I go about resolving this problem?

Edit: The code from the answer given is great but I get this error while trying to print the value from it: "UnicodeEncodeError: 'UCS-2' codec can't encode characters in position 205571-205571: Non-BMP character not supported in Tk". This in turn causes the problem calling the GUI class as stated earlier. I'm not sure how to change the encoding of a list to something the listbox in my app can handle.

bricky149
  • 81
  • 1
  • 1
  • 8

1 Answers1

1

As described in the Twitter Error Codes and Responses, a 429 response code means Too Many Requests. Therefore, the error is not in the code syntax per se, but instead in the number of calls you are performing on the Twitter API. Please take a look at the REST API Rate Limiting documentation to learn how to track how many calls you can perform (especially using the X-Rate-Limit-Remaining and other HTTP headers), and the rate limiting details for each REST endpoint.

Regarding your question about how to get to paginate after the first 20 results, check using cursors. Here, the condition on the cursor should be while cursor != 0: in order to be able to go to the next page. Then, it involves making sure you are not making too many calls to the Twitter API.

That being said, here is a better solution for you leveraging GET friends/ids. This enables you to retrieve the ids of users you follow, 5000 at once (instead of 20), and you can hydrate them just after using GET users/lookup. This will work for a large number of followings without the need to pause between each individual calls:

def get_follow_list():
    users = []
    users_ids = []
    # Fetch the followings as a cursored collection (up to 5000 per call).
    cursor = -1
    while cursor != 0:
        response = twitter.get_friends_ids(screen_name=current_user, cursor=cursor)
        users_ids += response['ids']
        cursor = response['next_cursor']
    # Lookup the users by chunks of 100.
    for i in range(0, len(users_ids), 100):
        chunk = users_ids[i:i + 100]
        users += twitter.lookup_user(user_id=chunk)
    # Return the user objects.
    return (users)
Romain Huet
  • 1,193
  • 11
  • 8
  • I am aware of that but that does not help me with wanting to get the next/last 20 people I follow. – bricky149 Mar 17 '14 at 16:28
  • @bricky149 I updated my reply above with the reason why your pagination doesn't work, and also a more efficient code sample using different API endpoints to gather the users you follow. – Romain Huet Mar 17 '14 at 18:06
  • Thanks for cleaning that up, but I now have another problem. The codes now causes an error: "_tkinter.TclError: character U+1f44d is above the range (U+0000-U+FFFF) allowed by Tcl" caused by calling the GUI class. – bricky149 Mar 18 '14 at 16:53