From e4673eb32288382fdb9e991441395529cc4df8e0 Mon Sep 17 00:00:00 2001 From: ironballista <87903825+ironballista@users.noreply.github.com> Date: Fri, 27 Dec 2024 20:24:41 +0100 Subject: [PATCH] Implemented very basic event logging. --- schedule_script.py | 35 +++++++++++++++++++++++++++++------ 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/schedule_script.py b/schedule_script.py index 183b2bd..deb4768 100644 --- a/schedule_script.py +++ b/schedule_script.py @@ -1,18 +1,29 @@ #!/usr/bin/env python3 -import schedule import time import random import os +import logging +import hashlib from glob import iglob import atproto +import atproto.exceptions +import schedule def auth_client() -> atproto.Client: handle, password, = os.getenv('BLUESKY_USERNAME'), os.getenv('BLUESKY_PASSWORD') + client: atproto.Client = atproto.Client() - profile: atproto.models.AppBskyActorDefs.ProfileViewDetailed = client.login(handle, password) + + try: + _profile: atproto.models.AppBskyActorDefs.ProfileViewDetailed = client.login(handle, password) + except atproto.exceptions.AtProtocolError as e: + logging.error("Could not log into profile `%s`: %s", hashlib.sha256(handle.encode()).hexdigest(), e) + raise + + logging.info("Logged into profile `%s` successfully.", hashlib.sha256(handle.encode()).hexdigest()) return client def sample_quote(client: atproto.Client, simulation_mode: bool = False) -> None: @@ -22,18 +33,27 @@ def sample_quote(client: atproto.Client, simulation_mode: bool = False) -> None: with open(path, 'r') as f: post_text: str = f.read().strip() - if not simulation_mode: - client.send_post(post_text) - else: + if simulation_mode: print(post_text) + return + + try: + client.send_post(post_text) + except atproto.exceptions.AtProtocolError as e: + logging.error("Error occurred when attempting to post `%s`: %s", path, e) + raise def main() -> None: global quotes, indices + logging.getLogger().setLevel(os.getenv("LOGGING") or "INFO") + client: atproto.Client = auth_client() quotes = tuple(iglob('src/**/*.txt', recursive=True)) indices = [] + logging.debug("Initialized `quotes` tuple with the following values: %s", quotes) + schedule.every().hour.at(':00').do(sample_quote, client) schedule.every().hour.at(':30').do(sample_quote, client) # schedule.every(10).seconds.do(sample_quote, client, simulation_mode=True) @@ -41,7 +61,10 @@ def main() -> None: while True: if len(indices) == 0: indices = random.sample(range(len(quotes)), k=len(quotes)) - # [print(quotes[i]) for i in indices] + + if logging.getLogger().isEnabledFor(logging.DEBUG): + shuffled_quotes = tuple(quotes[i] for i in indices) + logging.debug("Shuffled `quotes` tuple as follows: %s", shuffled_quotes) schedule.run_pending() time.sleep(1) -- 2.51.2