From 64111aca1697e97a2716147c7aca67cb133b37a8 Mon Sep 17 00:00:00 2001 From: "A.M. Rowsell" Date: Fri, 12 Jun 2026 13:36:06 -0400 Subject: [PATCH] chore: moved code to __init__, added some comments, small changes --- discorss.py | 87 +++++++++++++++++++++++++++-------------------------- 1 file changed, 45 insertions(+), 42 deletions(-) diff --git a/discorss.py b/discorss.py index be0ce2c..2ac9284 100755 --- a/discorss.py +++ b/discorss.py @@ -60,6 +60,47 @@ class Discorss: self.app_config = {} print(f"Logging to {self.log_file_path}") + self.now = time.mktime(time.localtime()) + # Set up logging + self.logger = logging.getLogger(__name__) + logging.basicConfig( + filename=self.log_file_path, + encoding="utf-8", + level=logging.DEBUG, + datefmt="%m/%d/%Y %H:%M:%S", + format="%(asctime)s [%(threadName)s] -> %(levelname)s: %(message)s", + ) + # Check for log and config files/paths, create empty directories if needed + # TODO: change output to log file, as warning/error + if not Path(self.log_dir).exists(): + self.logger.warning( + "No log file path exists. Yark! We'll try and make %s...", self.log_dir + ) + try: + Path(self.log_dir).mkdir(parents=True, exist_ok=True) + except FileExistsError: + self.logger.critical( + "The path {} already exists and is not a directory!".format( + self.log_dir + ) + ) + if not Path(self.config_file_path).exists(): + self.logger.warning( + "No config file at {}! Snarf. We'll try and make {}...".format( + self.config_file_path, self.config_dir + ) + ) + try: + Path(self.config_dir).mkdir(parents=True, exist_ok=True) + except FileExistsError: + self.warning.critical( + "The config dir {} already exists and is not a directory! Please fix manually. Quitting!".format( + self.config_dir + ) + ) + sys.exit(255) + return + async def _fetch_feed(self, hook): response = await asyncio.to_thread( requests.get, @@ -325,52 +366,14 @@ class Discorss: return False if mime_type and str(mime_type).lower().startswith("image/"): return True + # this will fix urls with ? in them for parameters. this should work + # unless the server depends on the parameter to creat the image, but + # in that case we'll just hope it has a mime_type instead return str(url).lower().split("?", 1)[0].endswith(self.IMAGE_EXTENSIONS) - # Some of this could go in __init__ def setup(self): os.environ["TZ"] = "America/Toronto" time.tzset() - self.now = time.mktime(time.localtime()) - # Set up logging - self.logger = logging.getLogger(__name__) - logging.basicConfig( - filename=self.log_file_path, - encoding="utf-8", - level=logging.DEBUG, - datefmt="%m/%d/%Y %H:%M:%S", - format="%(asctime)s [%(threadName)s] -> %(levelname)s: %(message)s", - ) - # Check for log and config files/paths, create empty directories if needed - # TODO: change output to log file, as warning/error - if not Path(self.log_dir).exists(): - self.logger.warning( - "No log file path exists. Yark! We'll try and make %s...", self.log_dir - ) - try: - Path(self.log_dir).mkdir(parents=True, exist_ok=True) - except FileExistsError: - self.logger.critical( - "The path {} already exists and is not a directory!".format( - self.log_dir - ) - ) - if not Path(self.config_file_path).exists(): - self.logger.warning( - "No config file at {}! Snarf. We'll try and make {}...".format( - self.config_file_path, self.config_dir - ) - ) - try: - Path(self.config_dir).mkdir(parents=True, exist_ok=True) - except FileExistsError: - self.warning.critical( - "The config dir {} already exists and is not a directory! Please fix manually. Quitting!".format( - self.config_dir - ) - ) - sys.exit(255) - return # Loading the config file with open(self.config_file_path, "r") as config_file: self.app_config = json.load(config_file) @@ -402,7 +405,7 @@ class Discorss: def main(): parser = argparse.ArgumentParser( - description="DiscoRSS: publish feed updates to Discord webhooks." + description="\x1b[1;34mDisco\x1b[33mRSS\x1b[0m: publish feed updates to Discord webhooks." ) parser.add_argument( "-d",