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.

51 lines
1.5 KiB
Python

5 years ago
import datetime
import os
import requests
import toml
import setup_logging
from db_postgrest import Zusatzwetterdaten, sende_daten
def config_laden():
configfile = os.path.join(SKRIPTPFAD, "wetterconfig.toml")
with open(configfile) as file:
return toml.loads(file.read())
SKRIPTPFAD = os.path.abspath(os.path.dirname(__file__))
CONFIG = config_laden()
LOGGER = setup_logging.create_logger("feinstaub", CONFIG["loglevel"])
def read_data(url, stationsname):
datenliste = []
try:
r = requests.get(url, timeout=10)
data = r.json()
except requests.exceptions.ConnectionError:
daten = None
else:
daten = {"pm10": data["sensordatavalues"][0]["value"],
"pm2_5": data["sensordatavalues"][1]["value"]}
timestamp = datetime.datetime.now().timestamp()
for key, value in daten.items():
datenliste.append(Zusatzwetterdaten(timestamp, stationsname, key, value, True))
return datenliste
def main():
headers = {f"Authorization": "{user} {token}".format(user=CONFIG["zieldb"]["postgrest"]["user"],
token=CONFIG["zieldb"]["postgrest"]["token"])}
url = CONFIG["zieldb"]["postgrest"]["url"]
if not url.endswith("/"):
url = f"{url}/"
daten = read_data(CONFIG["feinstaub"]["url"], CONFIG["grafana"]["grafana_name"])
sende_daten(url, CONFIG["zieldb"]["postgrest"]["tablename_zusatzwetterdaten"], headers, daten, LOGGER)
if __name__ == "__main__":
main()