'Tweepy multiple auth handler when rate limit exceeded

I need to get lots of tweets in very short time and want to switch between multiple auth handlers(I have 20 different twitter developer account). I did not find anything related to this problem in Tweepy documentation and saw this method but it doesn't work, I'm not sure, I guess because of different API versin.

auths = []
for consumer_key, consumer_secret, access_key, access_secret in twitter_accs:
    auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
    auth.set_access_token(access_key, access_secret)
    auths.append(auth)

# api
api = tweepy.API(auths)
currentCursor = tweepy.Cursor(api.search, q = 'bitcoin')
c = currentCursor.items()

switch = 0
api.auth_idx = switch
tweets = []
maxId = 0

while True:
    try:
        for tweet in c:
            tweets.append(tweet)
            maxId = tweet.id

    except tweepy.TweepError as e:
        print(e)
        print("---------------------------Rate limit exceeded.---------------------------")
        print("switching keys...")
        switch += 1
        api.auth_idx = switch           # naechster Key
        currentCursor = tweepy.Cursor(api.search, q='bitcoin', max_id=maxId)

And got this error

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_13904/1614165536.py in <module>
     17 while True:
     18     try:
---> 19         for tweet in c:
     20             tweets.append(tweet)
     21             maxId = tweet.id

~\AppData\Local\Programs\Python\Python38\lib\site-packages\tweepy\cursor.py in __next__(self)
     49 
     50     def __next__(self):
---> 51         return self.next()
     52 
     53     def next(self):

~\AppData\Local\Programs\Python\Python38\lib\site-packages\tweepy\cursor.py in next(self)
    241         if self.current_page is None or self.page_index == len(self.current_page) - 1:
    242             # Reached end of current page, get the next page...
--> 243             self.current_page = self.page_iterator.next()
    244             while len(self.current_page) == 0:
    245                 self.current_page = self.page_iterator.next()

~\AppData\Local\Programs\Python\Python38\lib\site-packages\tweepy\cursor.py in next(self)
    130 
    131         if self.index >= len(self.results) - 1:
--> 132             data = self.method(max_id=self.max_id, parser=RawParser(), *self.args, **self.kwargs)
    133 
    134             if hasattr(self.method, '__self__'):

~\AppData\Local\Programs\Python\Python38\lib\site-packages\tweepy\binder.py in _call(*args, **kwargs)
    251                 return method
    252             else:
--> 253                 return method.execute()
    254         finally:
    255             method.session.close()

~\AppData\Local\Programs\Python\Python38\lib\site-packages\tweepy\binder.py in execute(self)
    174                 auth = None
    175                 if self.api.auth:
--> 176                     auth = self.api.auth.apply_auth()
    177 
    178                 # Request compression if configured

AttributeError: 'list' object has no attribute 'apply_auth'


Solution 1:[1]

According to the Tweepy documentation, the argument for the tweepy.API method can not be a list. Why don't you just create a list of authenticated api like you created a list of auths ?

Finally, be carefull because your attempt could be a violation of the Twitter developper terms.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mickael Martinez