args: implemented argparse for config file, log, and dry run
This commit is contained in:
parent
5a3e3333b3
commit
98d1a3ba45
1 changed files with 44 additions and 10 deletions
54
discorss.py
54
discorss.py
|
|
@ -22,23 +22,34 @@ import os
|
|||
import sys
|
||||
import argparse
|
||||
import re
|
||||
from types import SimpleNamespace
|
||||
|
||||
|
||||
class Discorss:
|
||||
FEED_TIMEOUT_SECONDS = 15
|
||||
DRY_RUN = True
|
||||
HASH_HISTORY_LIMIT = 10
|
||||
|
||||
def __init__(self):
|
||||
def __init__(self, args=None):
|
||||
if args is None:
|
||||
args = SimpleNamespace(
|
||||
dry_run=False,
|
||||
config_file=None,
|
||||
log_file=None,
|
||||
)
|
||||
|
||||
self.DRY_RUN = args.dry_run
|
||||
self.config_dir = os.environ.get("XDG_CONFIG_HOME")
|
||||
home_dir = Path.home()
|
||||
if self.config_dir is None:
|
||||
self.config_file_path = str(home_dir) + "/.config/discorss/discorss.conf"
|
||||
self.config_dir = str(home_dir) + "/.config/discorss"
|
||||
default_config_file_path = str(home_dir) + "/.config/discorss/discorss.conf"
|
||||
else:
|
||||
self.config_file_path = self.config_dir + r"/discorss/discorss.conf"
|
||||
self.log_dir = r"/var/log/discorss"
|
||||
self.log_file_path = r"/app.log"
|
||||
default_config_file_path = self.config_dir + r"/discorss/discorss.conf"
|
||||
self.config_file_path = args.config_file or default_config_file_path
|
||||
self.config_dir = str(Path(self.config_file_path).parent)
|
||||
|
||||
default_log_file_path = "/var/log/discorss/app.log"
|
||||
self.log_file_path = args.log_file or default_log_file_path
|
||||
self.log_dir = str(Path(self.log_file_path).parent)
|
||||
# Yes, I know you "can't parse HTML with regex", but
|
||||
# just watch me.
|
||||
self.html_filter = re.compile(r"\<\/?([A-Za-z0-9 \:\.\-\/\"\=])*\>")
|
||||
|
|
@ -165,7 +176,7 @@ class Discorss:
|
|||
self.logger.debug(
|
||||
"Dry run, not actually posting to webhook, faking return code 200"
|
||||
)
|
||||
response.status_code = 200
|
||||
response = SimpleNamespace(status_code=200)
|
||||
if response.status_code not in self.success_codes:
|
||||
self.logger.error(
|
||||
"Error %d while trying to post %s", response.status_code, hook["name"]
|
||||
|
|
@ -290,7 +301,7 @@ class Discorss:
|
|||
# Set up logging
|
||||
self.logger = logging.getLogger(__name__)
|
||||
logging.basicConfig(
|
||||
filename=str(self.log_dir + self.log_file_path),
|
||||
filename=self.log_file_path,
|
||||
encoding="utf-8",
|
||||
level=logging.DEBUG,
|
||||
datefmt="%m/%d/%Y %H:%M:%S",
|
||||
|
|
@ -321,7 +332,30 @@ class Discorss:
|
|||
|
||||
|
||||
def main():
|
||||
app = Discorss()
|
||||
parser = argparse.ArgumentParser(
|
||||
description="DiscoRSS: publish feed updates to Discord webhooks."
|
||||
)
|
||||
parser.add_argument(
|
||||
"-d",
|
||||
"--dry-run",
|
||||
action="store_true",
|
||||
help="Parse feeds and update state without posting to Discord.",
|
||||
)
|
||||
parser.add_argument(
|
||||
"-c",
|
||||
"--config-file",
|
||||
default=None,
|
||||
help="Alternate config file path. Defaults to the existing config location.",
|
||||
)
|
||||
parser.add_argument(
|
||||
"-l",
|
||||
"--log-file",
|
||||
default=None,
|
||||
help="Alternate log file path. Defaults to the existing log location.",
|
||||
)
|
||||
args = parser.parse_args()
|
||||
|
||||
app = Discorss(args)
|
||||
app.process()
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue