danbooru-bot/config.py

48 lines
1.2 KiB
Python

import configparser
from typing import Optional
import requests
_c = configparser.ConfigParser()
_c.read("config.ini", "utf-8")
class _General:
token: str
sourceurl: Optional[str]
def __init__(self):
self.token = _c.get("General", "Token")
self.sourceurl = _c.get("General", "SourceUrl")
if not self.sourceurl.startswith("http://") and not self.sourceurl.startswith(
"https://"
):
self.sourceurl = None
class _App:
name: str
host: str
hostname: str
def __init__(self):
self.name = _c.get("App", "Name")
self.host = _c.get("App", "Host")
# noinspection PyBroadException
try:
response = requests.get(f"http://{self.host}/status.json")
if response.status_code != 200:
raise
except:
print("Unable validate App.Host in config.ini.")
exit(1)
self.hostname = _c.get("App", "HostName")
if not self.hostname:
self.hostname = self.host
self.protocol = "http"
if eval(_c.get("App", "PreferSecureProtocol")):
self.protocol = "https"
general = _General()
app = _App()