Create config file automatically if missing

This commit is contained in:
Jim Martens 2020-01-07 08:07:27 +01:00
parent d1a75a2b49
commit 2fa1b1dd51
2 changed files with 25 additions and 1 deletions

1
.gitignore vendored
View File

@ -124,3 +124,4 @@ dmypy.json
# End of https://www.gitignore.io/api/web,python
tm-allris-scraper-config.ini

View File

@ -32,11 +32,34 @@ from twomartens.allrisscraper import meeting
ALLRIS_LOGIN: str = "https://2martens.de/allris-eimsbüttel"
ALLRIS_OPEN: str = "https://2martens.de/bezirk-eimsbüttel"
user_agent = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3112.50 Safari/537.36'
_CONFIG_PROPS = {
"Default": {
"username": "",
"password": "",
"pdflocation": ""
}
}
def main() -> None:
config_file = f"{os.getcwd()}/tm-allris-scraper-config.ini"
try:
with open(config_file, "r"):
# if we reach this branch then the file exists and everything is fine
return
except FileNotFoundError:
with open(config_file, "w") as file:
parser = configparser.ConfigParser()
for section in _CONFIG_PROPS:
parser[section] = {}
for option in _CONFIG_PROPS[section]:
default = _CONFIG_PROPS[section][option]
parser[section][option] = default
parser.write(file)
config = configparser.ConfigParser()
config.read("config.ini")
config.read(config_file)
username = config["Default"]["username"]
password = config["Default"]["password"]
pdf_location = config["Default"]["pdflocation"]