From 46fe0966ecddb77e31a9614f2e72bbd84652b1b5 Mon Sep 17 00:00:00 2001 From: Lucy Joshua <43093984+notBscalE@users.noreply.github.com> Date: Sat, 25 Feb 2023 00:22:15 +0200 Subject: [PATCH] Error handling (#4) * Update BotYamPoster.py Error handler * Update main.py --- src/BotYamPoster.py | 29 ++++++++++++++++++++++------- src/main.py | 2 +- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/src/BotYamPoster.py b/src/BotYamPoster.py index 4873c49..1cfd605 100644 --- a/src/BotYamPoster.py +++ b/src/BotYamPoster.py @@ -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): reply_text = reply_text_bank[random.randint(0, (len(reply_text_bank)-1))] # Post reply - if tweet.data['author_id'] in victim_bank['author_id'] and not "@FromBotYam" in tweet.data['text']: + 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"] - ) + res = conn.api.create_tweet( + text=reply_text, + in_reply_to_tweet_id=tweet["id"] + ) + 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']}" + 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 - response_data = f"RESPONDING: {res.data['text']}" - print(response_data) return postcounter class BotYamPoster(tweepy.StreamingClient): diff --git a/src/main.py b/src/main.py index a83c6ca..897e8d4 100644 --- a/src/main.py +++ b/src/main.py @@ -5,7 +5,7 @@ from BotYamPoster import init_streamobject from connector import Connector def main(): - print("BOT-YAM - VERSION 3.0.1 >>>>") + print("BOT-YAM - VERSION 3.0.2 >>>>") conn = Connector() stream = init_streamobject(conn) print("Adding stream rules...")