From 1dc3a4671a6d4f2042a29a188f9fd99fc61b138a Mon Sep 17 00:00:00 2001 From: Yannik Schmidt Date: Fri, 9 Feb 2024 17:18:52 +0100 Subject: [PATCH] feat: better error handling & feedback --- fallback_csv.py | 39 +++++++++++++++++++++++++++++++++++++-- main.py | 3 --- req.txt | 4 ++-- 3 files changed, 39 insertions(+), 7 deletions(-) diff --git a/fallback_csv.py b/fallback_csv.py index a925f63..2195a20 100644 --- a/fallback_csv.py +++ b/fallback_csv.py @@ -1,8 +1,26 @@ import glob import datetime import os +import tkinter +import tkinter.messagebox +from plyer import notification SKIP_LINES = 13 +IGNORE_MISSING = False + +DTYPES_MISSING_NOTIFIED = [] + +def send_warning_ignore(dtype): + + if dtype in DTYPES_MISSING_NOTIFIED: + return + else: + DTYPES_MISSING_NOTIFIED.append(dtype) + + notification.notify( + title='Klimadaten EXCEL', + message='Datei fehlt für: {}'.format(dtype), + ) def cache_content(from_time, to_time, data, dtype): @@ -38,6 +56,8 @@ def cache_content(from_time, to_time, data, dtype): def generate(master_dir, from_time, to_time, cache_file, dtype): + global IGNORE_MISSING + if dtype == "lufttemperatur-aussen" or dtype == "luftfeuchte": base_name = "/produkt_tu_stunde*.txt" elif dtype == "windgeschwindigkeit" or dtype == "windrichtung": @@ -56,10 +76,23 @@ def generate(master_dir, from_time, to_time, cache_file, dtype): # read files files = glob.glob(master_dir + base_name) - if not files: - raise ValueError("Keine DWD_Datei für {} in: {} gefunden. Bitte herunterladen und entpacken! https://www.dwd.de/DE/leistungen/klimadatendeutschland/klarchivstunden.html;jsessionid=C423E76B30D18F24C43F4E7E36744C8C.live21073?nn=16102".format(dtype, os.getcwd() + ", " + master_dir)) + if IGNORE_MISSING: + send_warning_ignore(dtype) + return "" + description = "Keine DWD_Datei für {} gefunden!".format(dtype) + description += "\nAlle fehlenden Dateien ignorieren und weiter? ('Nein' bricht den Durchlauf ab)" + root = tkinter.Tk() + response = tkinter.messagebox.askyesno("Achtung", description) + root.withdraw() + if response: + send_warning_ignore(dtype) + IGNORE_MISSING=True + return "" + + + last_date = None for fname in files: start = None @@ -89,6 +122,7 @@ def generate(master_dir, from_time, to_time, cache_file, dtype): # parse date # date = datetime.datetime.strptime(fulldate, "%Y%m%d%H") + last_date = date # append data # data.append((date, float(primary), float(secondary))) @@ -105,6 +139,7 @@ def generate(master_dir, from_time, to_time, cache_file, dtype): print(dtype, from_time, to_time) # find a fitting frame # + to_time = last_date - datetime.timedelta(hours=12) for start, end, data in timeframes: if from_time >= start and to_time <= end: return cache_content(from_time, to_time, data, dtype) diff --git a/main.py b/main.py index 04a027d..d26e70c 100755 --- a/main.py +++ b/main.py @@ -61,7 +61,6 @@ def downloadFlugfeldData(fromTime, toTime, dtype): # check response code # if r.status_code != 200 or "nicht gefunden" in r.text.lower(): - print("Flugfeld kapott") content = fallback_csv.generate("./dwd", fromTime, toTime, cacheFile, dtype) else: content = r.content.decode('utf-8', "ignore") # ignore bad bytes @@ -101,8 +100,6 @@ def checkLastMonths(backwardsMonths=6): if start > end: return "" - print(start, end) - for dtype in dtypes: content = downloadFlugfeldData(start, end, dtype) dataList = parse(content, dtype) diff --git a/req.txt b/req.txt index 22b20dd..f113c7f 100644 --- a/req.txt +++ b/req.txt @@ -1,6 +1,6 @@ pyexcel pyexcel-xls pyexcel-xlsx -calendar requests -dateutil +tk +plyer