Source code for my personal quote bot project.
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Implemented very basic event logging.

+29 -6
+29 -6
schedule_script.py
··· 1 1 #!/usr/bin/env python3 2 2 3 - import schedule 4 3 import time 5 4 import random 6 5 import os 6 + import logging 7 + import hashlib 7 8 8 9 from glob import iglob 9 10 10 11 import atproto 12 + import atproto.exceptions 13 + import schedule 11 14 12 15 def auth_client() -> atproto.Client: 13 16 handle, password, = os.getenv('BLUESKY_USERNAME'), os.getenv('BLUESKY_PASSWORD') 17 + 14 18 client: atproto.Client = atproto.Client() 15 - profile: atproto.models.AppBskyActorDefs.ProfileViewDetailed = client.login(handle, password) 19 + 20 + try: 21 + _profile: atproto.models.AppBskyActorDefs.ProfileViewDetailed = client.login(handle, password) 22 + except atproto.exceptions.AtProtocolError as e: 23 + logging.error("Could not log into profile `%s`: %s", hashlib.sha256(handle.encode()).hexdigest(), e) 24 + raise 25 + 26 + logging.info("Logged into profile `%s` successfully.", hashlib.sha256(handle.encode()).hexdigest()) 16 27 return client 17 28 18 29 def sample_quote(client: atproto.Client, simulation_mode: bool = False) -> None: ··· 22 33 with open(path, 'r') as f: 23 34 post_text: str = f.read().strip() 24 35 25 - if not simulation_mode: 36 + if simulation_mode: 37 + print(post_text) 38 + return 39 + 40 + try: 26 41 client.send_post(post_text) 27 - else: 28 - print(post_text) 42 + except atproto.exceptions.AtProtocolError as e: 43 + logging.error("Error occurred when attempting to post `%s`: %s", path, e) 44 + raise 29 45 30 46 def main() -> None: 31 47 global quotes, indices 32 48 49 + logging.getLogger().setLevel(os.getenv("LOGGING") or "INFO") 50 + 33 51 client: atproto.Client = auth_client() 34 52 quotes = tuple(iglob('src/**/*.txt', recursive=True)) 35 53 indices = [] 54 + 55 + logging.debug("Initialized `quotes` tuple with the following values: %s", quotes) 36 56 37 57 schedule.every().hour.at(':00').do(sample_quote, client) 38 58 schedule.every().hour.at(':30').do(sample_quote, client) ··· 41 61 while True: 42 62 if len(indices) == 0: 43 63 indices = random.sample(range(len(quotes)), k=len(quotes)) 44 - # [print(quotes[i]) for i in indices] 64 + 65 + if logging.getLogger().isEnabledFor(logging.DEBUG): 66 + shuffled_quotes = tuple(quotes[i] for i in indices) 67 + logging.debug("Shuffled `quotes` tuple as follows: %s", shuffled_quotes) 45 68 46 69 schedule.run_pending() 47 70 time.sleep(1)