Move settings to settings.py and read from a config file

This commit is contained in:
2021-12-12 21:59:27 +01:00
parent c8bf725780
commit 203525becb
4 changed files with 51 additions and 8 deletions

View File

@ -1,14 +1,12 @@
import os
import sys
import typer
from dhooks import Webhook
from reader import FeedExistsError, make_reader
reader = make_reader("db.sqlite")
from discord_rss_bot.settings import Settings
app = typer.Typer()
hook = Webhook("")
app = typer.Typer() # For CLI (https://typer.tiangolo.com/)
hook = Webhook(Settings.webhook_url) # For Webhooks (https://github.com/kyb3r/dhooks)
reader = make_reader(Settings.db_file) # For RSS (https://github.com/lemon24/reader)
@app.command()

View File

@ -0,0 +1,44 @@
import configparser
import os
import sys
from platformdirs import user_data_dir
class Settings:
data_dir = user_data_dir(
appname="discord-rss-bot", # The name of application.
appauthor="TheLovinator", # The name of the app author or distributing body for this application
roaming=True, # Whether to use the roaming appdata directory on Windows
)
# Create the data directory if it doesn't exist
os.makedirs(data_dir, exist_ok=True)
# Store the database file in the data directory
db_file = os.path.join(data_dir, "db.sqlite")
# Store the config in the data directory
config_location = os.path.join(data_dir, "config.conf")
if not os.path.isfile(config_location):
# TODO: Add config for db_file and config_location
print("No config file found, creating one...")
with open(config_location, "w") as config_file:
config = configparser.ConfigParser()
config.add_section("config")
config.set(
"config",
"webhook_url",
"https://discord.com/api/webhooks/1234/567890/ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz",
)
config.write(config_file)
sys.exit(f"Please edit the config file at {config_location}")
# Read the config file
config = configparser.ConfigParser()
config.read(config_location)
# Get the webhook url from the config file
webhook_url = config.get("config", "webhook_url")