From dba78e616c8b8650635b19adda62af0b4647a8bd Mon Sep 17 00:00:00 2001 From: retoor Date: Wed, 27 Nov 2024 11:28:05 +0100 Subject: [PATCH] Added decent logging --- src/ragnar/__init__.py | 12 ++++++++++++ src/ragnar/api.py | 2 +- src/ragnar/bot.py | 28 +++++++++++++--------------- src/ragnar/cli.py | 5 ++--- 4 files changed, 28 insertions(+), 19 deletions(-) diff --git a/src/ragnar/__init__.py b/src/ragnar/__init__.py index e69de29..10a02e3 100644 --- a/src/ragnar/__init__.py +++ b/src/ragnar/__init__.py @@ -0,0 +1,12 @@ +import logging +import sys + +logging.basicConfig( + level=logging.INFO, # Adjust as needed + format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", + handlers=[ + logging.StreamHandler(sys.stdout), # Log to stdout + ] +) + +log = logging.getLogger(__name__) diff --git a/src/ragnar/api.py b/src/ragnar/api.py index c16a5ec..6681e0b 100644 --- a/src/ragnar/api.py +++ b/src/ragnar/api.py @@ -1,7 +1,7 @@ import requests, json from ragnar.cache import method_cache - +from ragnar import log class Api: diff --git a/src/ragnar/bot.py b/src/ragnar/bot.py index f899a42..bea2d93 100644 --- a/src/ragnar/bot.py +++ b/src/ragnar/bot.py @@ -3,7 +3,7 @@ import time import random from ragnar.cache import method_cache import re - +from ragnar import log class Bot: @@ -24,16 +24,14 @@ class Bot: self.name ) self.auth = None - self.triggers = [ "$", "crypto", "hacker", "recovery", - {"regex": "\([+,(,0-9,),-]{7,}"}, + {"regex": r"\([+,(,0-9,),-]{7,}"}, "money", ] - self.api = Api(username=self.username, password=self.password) def rsleepii(self): @@ -44,9 +42,9 @@ class Bot: self.rsleepii() self.auth = self.api.login() if not self.auth: - print("Authentication for {} failed.".format(self.username)) + log.error("Authentication for {} failed.".format(self.username)) raise Exception("Login error") - print("Authentication succesful for {}.".format(self.username)) + log.info("Authentication succesful for {}.".format(self.username)) def clean_rant_text(self, rant_text): return rant_text.replace(" ", "").lower() @@ -59,10 +57,10 @@ class Bot: if trigger.get("regex"): regex = trigger["regex"] if re.search(regex, clean_text): - print("Regex trigger {} matched!".format(regex)) + log.info("Regex trigger {} matched!".format(regex)) return True elif trigger in clean_text: - print("Trigger {} matched!".format(trigger)) + log.info("Trigger {} matched!".format(trigger)) return True return False @@ -82,7 +80,7 @@ class Bot: profile = self.api.get_profile(user_id) score = profile["score"] if score < 5: - print("User {} is sus with his score of only {}.".format(username, score)) + log.warning("User {} is sus with his score of only {}.".format(username, score)) return True else: return False @@ -96,18 +94,18 @@ class Bot: rants = self.api.get_rants("recent", 5, 0) for rant in rants: if not self.is_user_sus(rant["user_username"]): - print("User {} is trusted.".format(rant["user_username"])) + log.info("User {} is trusted.".format(rant["user_username"])) continue if not self.is_sus_rant(rant["id"], rant["text"]): - print("Rant by {} is not sus.".format(rant["user_username"])) + log.info("Rant by {} is not sus.".format(rant["user_username"])) continue if self.is_flagged_as_sus(rant["id"], rant.get("num_comments")): continue - print("Rant is not {} flagged as sus yet.".format(rant["user_username"])) - print("Flagging rant by {} as sus.".format(rant["user_username"])) + log.warning("Rant is not {} flagged as sus yet.".format(rant["user_username"])) + log.warning("Flagging rant by {} as sus.".format(rant["user_username"])) self.mark_as_sus(rant) self.down_vote_rant(rant) def down_vote_rant(self, rant): - print("Downvoting rant by {}.".format(rant["user_username"])) - print(self.api.post_rant_vote(rant["id"], 4)) + log.warning("Downvoting rant by {}.".format(rant["user_username"])) + log.debug(self.api.post_rant_vote(rant["id"], 4)) diff --git a/src/ragnar/cli.py b/src/ragnar/cli.py index 2430706..095d022 100644 --- a/src/ragnar/cli.py +++ b/src/ragnar/cli.py @@ -3,7 +3,7 @@ from ragnar.bot import Bot import random import time from concurrent.futures import ThreadPoolExecutor as Executor - +from ragnar import log def parse_args(): parser = argparse.ArgumentParser(description="Process username and password.") @@ -15,6 +15,7 @@ def parse_args(): def bot_task(username, password): + log.info("Created new but task. Username: {}".format(username)) time.sleep(random.randint(1, 20)) bot = Bot(username=username, password=password) bot.login() @@ -32,8 +33,6 @@ def main(): for x in range(1, 5): username = "no-spam{}@molodetz.nl".format(str(x)) password = args.password - time.sleep(1) - print("Starting bot {}.".format(username)) executor.submit(bot_task, username, password) executor.shutdown(wait=True)