chore: moved code to __init__, added some comments, small changes
This commit is contained in:
parent
cd07e9806f
commit
64111aca16
1 changed files with 45 additions and 42 deletions
87
discorss.py
87
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",
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue