From a152ccadc8fae7a132524019fc2725de3ae5bf6e Mon Sep 17 00:00:00 2001 From: don philipe Date: Sun, 28 Jan 2024 09:06:31 +0100 Subject: [PATCH] Init repo with first working version --- .gitignore | 4 ++ config.example.json | 10 +++++ main.py | 102 ++++++++++++++++++++++++++++++++++++++++++++ requirements.txt | 2 + 4 files changed, 118 insertions(+) create mode 100644 .gitignore create mode 100644 config.example.json create mode 100644 main.py create mode 100644 requirements.txt diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1fc3c1d --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +config.json +__pycache__ +*.swp +venv diff --git a/config.example.json b/config.example.json new file mode 100644 index 0000000..2c5179a --- /dev/null +++ b/config.example.json @@ -0,0 +1,10 @@ +{ + "db_user": "", + "db_passwd": "", + "db_host": "localhost", + "db_port": 3306, + "db_db": "", + "db_table": "water_level_elbe_dresden", + "sleep_sec": 600, + "url": "https://www.umwelt.sachsen.de/umwelt/infosysteme/hwims/portal/web/wasserstand-pegel-501060" +} diff --git a/main.py b/main.py new file mode 100644 index 0000000..ef4a9b0 --- /dev/null +++ b/main.py @@ -0,0 +1,102 @@ +import json +import mariadb +import sys +import time +from urllib.request import urlopen +from bs4 import BeautifulSoup +from datetime import datetime + + +def read_config(file="config.json"): + """ + Read config file to get database configuration and other stuff. + """ + with open(file, "r") as f: + config = json.load(f) + return config + + +def prepare_db(): + """ + Setup database table, if it not exists. + """ + try: + conn = mariadb.connect( + user=config["db_user"], + password=config["db_passwd"], + host=config["db_host"], + port=config["db_port"], + database=config["db_db"] + ) + except mariadb.Error as e: + print(f"Error connecting to MariaDB Platform: {e}") + sys.exit(1) + + cur = conn.cursor() + cur.execute("SHOW TABLES") + + table_exists = False + for table in cur: + if table == config["db_table"]: + table_exists = True + + if not table_exists: + cur.execute("CREATE TABLE ? (timestamp TIMESTAMP PRIMARY KEY, level_cm INT, flow_m3_s INT)", (config["db_table"])) + + conn.close() + + +def write_db(timestamp, level: int, flow: int): + """ + Write data set to database. + @param timestamp: timestamp of data set + @param level: water level in cm + @param flow: water flow in m3/s + """ + try: + conn = mariadb.connect( + user=config["db_user"], + password=config["db_passwd"], + host=config["db_host"], + port=config["db_port"], + database=config["db_db"] + ) + except mariadb.Error as e: + print(f"Error connecting to MariaDB Platform: {e}") + sys.exit(1) + + cur = conn.cursor() + try: + cur.execute("INSERT INTO ? (timestamp,level_cm,flow_m3_s) VALUES (?, ?, ?)", (config["db_table"], timestamp, level, flow)) + except mariadb.Error as e: + print(f"Error: {e}") + finally: + conn.close() + + +def retrieve(url: str): + """ + Get webpage and parse it to get timestamp, water level and flow. + @param url: the URL to call + @return: 3-tuple with timestamp, level, flow + """ + page = urlopen(url) + html = page.read().decode("utf-8") + soup = BeautifulSoup(html, "html.parser") + + latest = soup.select(".quickbarTable tbody")[0].select("tr")[0] + latest_ts = latest.select("td")[0].text + ts = datetime.strptime(latest_ts.strip(), "%d.%m.%Y %H:%M") + latest_level = latest.select("td")[1].text + latest_flow = latest.select("td")[2].text + + return ts, int(latest_level), int(latest_flow) + + +config = read_config() +# prepare_db() +while True: + ts, latest_level, latest_flow = retrieve(config["url"]) + # write_db(ts, latest_level, latest_flow) + print(str(ts) + ": " + str(latest_level) + "cm " + str(latest_flow) + "m3/s") + time.sleep(config["sleep_sec"]) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..c78e451 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +beautifulsoup4==4.12.3 +mariadb==1.1.9