Update BotYamPoster.py

Error handler
This commit is contained in:
Lucy Joshua 2023-02-25 00:20:01 +02:00 committed by GitHub
parent e1a7247c2a
commit eca40cf0f2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -11,15 +11,30 @@ def post_reply(conn, victim_bank, tweet, words, reply_text_bank, postcounter):
if any(word in tweet.data['text'] for word in words): if any(word in tweet.data['text'] for word in words):
reply_text = reply_text_bank[random.randint(0, (len(reply_text_bank)-1))] reply_text = reply_text_bank[random.randint(0, (len(reply_text_bank)-1))]
# Post reply # Post reply
try:
if tweet.data['author_id'] in victim_bank['author_id'] and not "@FromBotYam" in tweet.data['text']: if tweet.data['author_id'] in victim_bank['author_id'] and not "@FromBotYam" in tweet.data['text']:
reply_text = victim_bank['reply'][random.randint(0,2)] + "\nו" + reply_text reply_text = victim_bank['reply'][random.randint(0,2)] + "\nו" + reply_text
res = conn.api.create_tweet( res = conn.api.create_tweet(
text=reply_text, text=reply_text,
in_reply_to_tweet_id=tweet["id"] in_reply_to_tweet_id=tweet["id"]
) )
postcounter = postcounter + 1 except tweepy.errors.TwitterServerError as e:
print("ERROR: An error occured, we'll try again in a few minutes. The error: " + str(e))
try:
if tweet.data['author_id'] in victim_bank['author_id'] and not "@FromBotYam" in tweet.data['text']:
reply_text = victim_bank['reply'][random.randint(0,2)] + "\nו" + reply_text
res = conn.api.create_tweet(
text=reply_text,
in_reply_to_tweet_id=tweet["id"]
)
response_data = f"RESPONDING: {res.data['text']}" response_data = f"RESPONDING: {res.data['text']}"
print(response_data) print(response_data)
except Exception as e:
print("ERROR: An exception occured. The error: " + str(e))
except Exception as e:
print("ERROR: An exception occured. The error: " + str(e))
postcounter = postcounter + 1
return postcounter return postcounter
class BotYamPoster(tweepy.StreamingClient): class BotYamPoster(tweepy.StreamingClient):