1

I want to get a user's friends with Tweepy and there is a limit on the number of friends I can get in a particular time interval. To handle this query limit I've added a condition with try-except. The logic is to let the Tweepy try to get a next friend and if it comes with an error wait for 15 minutes. But I realize the next() method does not work. What did I do wrong?

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)


def get_user_friends(screen_name):
    friends = []
    for user in tweepy.Cursor(api.friends, screen_name=screen_name).items(200):
        friends.append(user.screen_name)

        try:
            test = user.next()  # Here
        except:
            time.sleep(60 * 15)
            continue

    return friends
ECub Devs
  • 165
  • 3
  • 10
  • If you get an error on the current friend, do you want to wait 15 minutes and retry, or do you want to go to the next friend? – jrmylow Sep 22 '20 at 00:00
  • @jrmylow I want `user.next()` to check the access to the next friend. If it is not possible, Tweepy will return a limitation error. Thus, the program has to wait 15 minutes. But if it was no error with this call, `for` loop can continue. – ECub Devs Sep 22 '20 at 05:49

1 Answers1

0

You need to use the next() method of the cursor, something like this would work:

import time
def limit_handled(cursor):
    while True:
        try:
            yield cursor.next()
        except tweepy.RateLimitError:
            time.sleep(15 * 60)
        except StopIteration:
            return
        
def get_user_friends(screen_name):
    friends = []
    for user in limit_handled(tweepy.Cursor(api.friends, screen_name=screen_name).items(200)):
        friends.append(user.screen_name)
    
    return friends

You can also check the documentation here

Melinda
  • 245
  • 1
  • 9
  • OSError: [Errno 65] No route to host – ECub Devs Sep 22 '20 at 11:49
  • Where this error is coming from? How did you execute this code? – Melinda Sep 22 '20 at 11:55
  • I just replace it with `def get_user_friends(screen_name):` block. – ECub Devs Sep 22 '20 at 15:00
  • What did you replace? You should replace your get_user_friends with the 2 functions I send. – Melinda Sep 22 '20 at 15:11
  • This error is not related to the code (the code I send you is working, I have tested it). This seems to be more related to your network connection. Are you able to get anything using tweepy? Just try to search for any tweet to see if the connection is working. Also you can check this questions : https://stackoverflow.com/questions/43074486/oserror-errno-65-no-route-to-host-python-3-5-3 , or any other related to your error. – Melinda Sep 22 '20 at 18:15