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.
abflussdaten_sammeln/abflussdaten_sammeln.py

77 lines
2.5 KiB
Python

import pygeohash
import toml
import os
import hnd_scraping
import hydris_data
import db_model as db
def config_laden(configfile):
with open(configfile) as file:
return toml.loads(file.read())
PFAD = os.path.abspath(os.path.dirname(__file__))
CONFIG = config_laden(os.path.join(PFAD, "cfg_grenz.toml"))
def daten_sammeln():
daten = []
for messstelle in CONFIG["hnd"]:
lat = CONFIG["hnd"][messstelle]["lat"]
lon = CONFIG["hnd"][messstelle]["lon"]
bezeichnung = CONFIG["hnd"][messstelle]["text"]
geohash = pygeohash.geohash.encode(lat, lon)
durchfluss = max(hnd_scraping.scrap_messwerte_hnd(CONFIG["hnd"][messstelle]["url_q"]))
pegelstand = max(hnd_scraping.scrap_messwerte_hnd(CONFIG["hnd"][messstelle]["url_h"]))
daten.append({"ts": durchfluss[0], "messstelle": bezeichnung, "geohash": geohash,
"durchfluss": durchfluss[1], "pegelstand": pegelstand[1]})
hydris_daten = hydris_data.get_hydris_data("https://www.salzburg.gv.at/wasser/hydro/grafiken/data.json")
hydris_daten_gefiltert = [hydris_data.get_station(hydris_daten, station_id=str(number))
for number in CONFIG["hydris"]["station_id"]]
for station in hydris_daten_gefiltert:
geohash = pygeohash.geohash.encode(station.location.lat, station.location.lon)
daten.append({"ts": station.flow["15m.Cmd.RunOff"].timestamp, "messstelle": station.name, "geohash": geohash,
"durchfluss": station.flow["15m.Cmd.RunOff"].value,
"pegelstand": station.water_level["15m.Cmd.WiskiWeb"].value})
return daten
def send_with_ssh() -> None:
db_adapter = CONFIG["db"]
port = CONFIG[db_adapter]["port"]
from sshtunnel import SSHTunnelForwarder
config = config_laden(CONFIG["pfad_ssh_auth"])
with SSHTunnelForwarder(
(config["ip_server"], config["ssh_port"]),
ssh_username=config["user"],
ssh_password=config["pw"],
remote_bind_address=("127.0.0.1", port),
) as server:
CONFIG[db_adapter]["port"] = server.local_bind_port
init()
def init() -> None:
db_adapter = CONFIG["db"]["db"]
db_ = db.init_db(CONFIG["db"][db_adapter]["database"], db_adapter, CONFIG["db"].get(db_adapter))
db.DB_PROXY.initialize(db_)
db.create_tables()
daten = daten_sammeln()
db.insert_many(daten)
def main() -> None:
if CONFIG["db"]["ssh_tunnel"]:
send_with_ssh()
else:
init()
if __name__ == "__main__":
main()