From c9b12f3ea9e6733a4d538b930bb13d85a24a6f10 Mon Sep 17 00:00:00 2001 From: Lucy Joshua <43093984+notBscalE@users.noreply.github.com> Date: Wed, 22 Mar 2023 14:44:54 +0200 Subject: [PATCH] bugfix of timestamps --- src/BotYamPoster.py | 21 +++++++++++---------- src/connector.py | 10 +++++++--- src/main.py | 7 ++++--- 3 files changed, 22 insertions(+), 16 deletions(-) diff --git a/src/BotYamPoster.py b/src/BotYamPoster.py index 7a905b2..c36ce98 100644 --- a/src/BotYamPoster.py +++ b/src/BotYamPoster.py @@ -4,9 +4,10 @@ import re import requests import time from connector import Connector +from connector import printlog def init_streamobject(conn): - print(time.time() + ": " + "Loading Stream object...") + printlog("Loading Stream object...") return BotYamPoster(conn.get_bearer()) def expand_tco_url(url): @@ -29,9 +30,9 @@ def post_reply(conn, victim_bank, tweet, words, reply_text_bank, postcounter): in_reply_to_tweet_id=tweet["id"] ) response_data = f"RESPONDING: {res.data['text']}" - print(time.time() + ": " + response_data) + printlog(response_data) except tweepy.errors.TwitterServerError as e: - print(time.time() + ": " + "ERROR: An error occured, we'll try again in a few minutes. The error: " + str(e)) + printlog("ERROR: An error occured, we'll try again in a few minutes. The error: " + str(e)) time.sleep(5) try: if tweet.data['author_id'] in victim_bank['author_id'] and not "@FromBotYam" in tweet.data['text']: @@ -42,11 +43,11 @@ def post_reply(conn, victim_bank, tweet, words, reply_text_bank, postcounter): ) response_data = f"RESPONDING: {res.data['text']}" - print(time.time() + ": " + response_data) + printlog(response_data) except Exception as e: - print(time.time() + ": " + "ERROR: An exception occured. The error: " + str(e)) + printlog("ERROR: An exception occured. The error: " + str(e)) except Exception as e: - print(time.time() + ": " + "ERROR: An exception occured. The error: " + str(e)) + printlog("ERROR: An exception occured. The error: " + str(e)) postcounter = postcounter + 1 return postcounter @@ -69,11 +70,11 @@ class BotYamPoster(tweepy.StreamingClient): # Debug tweet_data = f"NEW TWEET from @{conn.api.get_user(id=tweet.data['author_id']).data['username']}: {tweet.data['text']}" - print(time.time() + ": " + tweet_data) + printlog(tweet_data) # Spare me if starts with RT if tweet.data['text'][:2] == "RT": - print(time.time() + ": " + "Skipping retweet...") + printlog("Skipping retweet...") return # Run on all gags @@ -99,7 +100,7 @@ class BotYamPoster(tweepy.StreamingClient): postcounter) if (postcounter == 0 and "@FromBotYam" in tweet.data['text']): - print(time.time() + ": " + "Post counter for this tweet: 0! Posting tilt.") + printlog("Post counter for this tweet: 0! Posting tilt.") if any(tilter in tweet.data['text'] for tilter in reply_bank['special_gags']['tilt']['keywords']): postcounter = post_reply( @@ -122,4 +123,4 @@ class BotYamPoster(tweepy.StreamingClient): # Define a callback function to handle errors def on_error(self, status_code): # Print the error code - print(time.time() + ": " + "ERROR: " + status_code) + printlog("ERROR: " + status_code) diff --git a/src/connector.py b/src/connector.py index 71f00cb..9442742 100644 --- a/src/connector.py +++ b/src/connector.py @@ -2,10 +2,14 @@ import os import redis import time import tweepy +from datetime import datetime + +def printlog(text): + print((datetime.fromtimestamp(time.time())).strftime("%d-%m-%Y, %H:%M:%S") + ": " + text) class Connector(): def __init__(self): - print(time.time() + ": " + "Loading Redis connector...") + printlog("Loading Redis connector...") # TODO: Add token self.dbconn = redis.Redis( host=os.getenv("REDIS_HOST"), @@ -14,11 +18,11 @@ class Connector(): ) self.api = Connector.init_api(self.dbconn) - print(time.time() + ": " + "Success loading connectors!") + printlog("Success loading connectors!") def init_api(dbconn): # Syslog report - print(time.time() + ": " + "Loading Twitter API connector...") + printlog("Loading Twitter API connector...") return tweepy.Client( bearer_token=dbconn.hget("api", "bearer").decode("utf-8"), diff --git a/src/main.py b/src/main.py index 430beba..d3ed4e0 100644 --- a/src/main.py +++ b/src/main.py @@ -4,16 +4,17 @@ import time from BotYamPoster import BotYamPoster from BotYamPoster import init_streamobject from connector import Connector +from connector import printlog def main(): - print(time.time() + ": " + "BOT-YAM - VERSION 3.0.2 >>>>") + printlog("BOT-YAM - VERSION 3.0.2 >>>>") conn = Connector() stream = init_streamobject(conn) - print(time.time() + ": " + "Adding stream rules...") + printlog("Adding stream rules...") stream.add_rules(tweepy.StreamRule(conn.get_victims()['stream_filter'])) stream.add_rules(tweepy.StreamRule("@FromBotYam")) # Start listening for tweets - print(time.time() + ": " + "Starting Twitter stream!") + printlog("Starting Twitter stream!") stream.filter(expansions="author_id") if __name__ == "__main__":