mercredi 16 septembre 2020

Favorite if the tweet is not favourited by me | Tweepy - Python

On Tweepy, I get too many "You have already favorite this tweet" error and when it gives this error repeatedly, the API rate limit error[code 429] is issued.

Before attempting to favorite the tweet to resolve this issue, before favorite the tweet I want to check if the tweet is not favourited by me. But I haven't been able to enter the while / else command in my code, I would appreciate if anyone could help.

import tweepy
import time

auth = tweepy.OAuthHandler('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx','xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx')
auth.set_access_token('xxxxxxxxxxxxxxxxxxxxxxxxxxx','xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx')

api = tweepy.API(auth, wait_on_rate_limit=True, wait_on_rate_limit_notify=True)
user = api.me()


import time

time_limit_sec = 600
start_time = time.time()

for tweet in tweepy.Cursor(api.search, q="books", result_type="recent").items(70):
    if (time.time()-start_time) > time_limit_sec:
        break

    try: 
        print('Tweet Liked')
        tweet.favorite()

        time.sleep(10)
    except tweepy.TweepError as e:
        print(e.reason)
    except StopIteration:
        break

Aucun commentaire:

Enregistrer un commentaire