You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
49 lines
1.2 KiB
Python
49 lines
1.2 KiB
Python
5 years ago
|
import toml
|
||
|
import os
|
||
|
import db_model as db
|
||
|
import requests
|
||
|
import json
|
||
|
from peewee import SqliteDatabase
|
||
|
import datetime
|
||
|
|
||
|
|
||
|
def config_laden():
|
||
|
configfile = os.path.join(SKRIPTPFAD, "config.toml")
|
||
|
with open(configfile) as file:
|
||
|
return toml.loads(file.read())
|
||
|
|
||
|
|
||
|
SKRIPTPFAD = os.path.abspath(os.path.dirname(__file__))
|
||
|
CONFIG = config_laden()
|
||
|
|
||
|
|
||
|
def headers_erstellen():
|
||
|
headers = {"Authorization": "Bearer {token}".format(token=CONFIG["token"]),
|
||
|
"Prefer": "resolution=merge-duplicates"}
|
||
|
return headers
|
||
|
|
||
|
|
||
|
def tabelle_auslesen(tabelle, headers):
|
||
|
url = f"{CONFIG['url']}{tabelle}"
|
||
|
r = requests.get(url, headers=headers)
|
||
|
erg = json.loads(r.text)
|
||
|
return erg
|
||
|
|
||
|
|
||
|
def main():
|
||
|
db.DATABASE.initialize(SqliteDatabase(os.path.join(SKRIPTPFAD, "corona.db3")))
|
||
|
db.create_tables()
|
||
|
headers = headers_erstellen()
|
||
|
|
||
|
erg = tabelle_auslesen(CONFIG['tablename_countryliste'], headers)
|
||
|
for data_dict in erg:
|
||
|
db.CoronaCountry.create(**data_dict)
|
||
|
|
||
|
erg = tabelle_auslesen(CONFIG["tablename_daten"], headers)
|
||
|
for data_dict in erg:
|
||
|
data_dict["ts"] = datetime.datetime.strptime(data_dict["ts"], "%Y-%m-%dT%H:%M:%S")
|
||
|
db.CoronaDaten.create(**data_dict)
|
||
|
|
||
|
|
||
|
main()
|