mirror of
https://github.com/FAUSheppy/ths-reference-data-collector
synced 2025-12-06 06:51:35 +01:00
feat: better error handling & feedback
This commit is contained in:
@@ -1,8 +1,26 @@
|
|||||||
import glob
|
import glob
|
||||||
import datetime
|
import datetime
|
||||||
import os
|
import os
|
||||||
|
import tkinter
|
||||||
|
import tkinter.messagebox
|
||||||
|
from plyer import notification
|
||||||
|
|
||||||
SKIP_LINES = 13
|
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):
|
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):
|
def generate(master_dir, from_time, to_time, cache_file, dtype):
|
||||||
|
|
||||||
|
global IGNORE_MISSING
|
||||||
|
|
||||||
if dtype == "lufttemperatur-aussen" or dtype == "luftfeuchte":
|
if dtype == "lufttemperatur-aussen" or dtype == "luftfeuchte":
|
||||||
base_name = "/produkt_tu_stunde*.txt"
|
base_name = "/produkt_tu_stunde*.txt"
|
||||||
elif dtype == "windgeschwindigkeit" or dtype == "windrichtung":
|
elif dtype == "windgeschwindigkeit" or dtype == "windrichtung":
|
||||||
@@ -56,10 +76,23 @@ def generate(master_dir, from_time, to_time, cache_file, dtype):
|
|||||||
|
|
||||||
# read files
|
# read files
|
||||||
files = glob.glob(master_dir + base_name)
|
files = glob.glob(master_dir + base_name)
|
||||||
|
|
||||||
if not files:
|
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:
|
for fname in files:
|
||||||
|
|
||||||
start = None
|
start = None
|
||||||
@@ -89,6 +122,7 @@ def generate(master_dir, from_time, to_time, cache_file, dtype):
|
|||||||
|
|
||||||
# parse date #
|
# parse date #
|
||||||
date = datetime.datetime.strptime(fulldate, "%Y%m%d%H")
|
date = datetime.datetime.strptime(fulldate, "%Y%m%d%H")
|
||||||
|
last_date = date
|
||||||
|
|
||||||
# append data #
|
# append data #
|
||||||
data.append((date, float(primary), float(secondary)))
|
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)
|
print(dtype, from_time, to_time)
|
||||||
|
|
||||||
# find a fitting frame #
|
# find a fitting frame #
|
||||||
|
to_time = last_date - datetime.timedelta(hours=12)
|
||||||
for start, end, data in timeframes:
|
for start, end, data in timeframes:
|
||||||
if from_time >= start and to_time <= end:
|
if from_time >= start and to_time <= end:
|
||||||
return cache_content(from_time, to_time, data, dtype)
|
return cache_content(from_time, to_time, data, dtype)
|
||||||
|
|||||||
3
main.py
3
main.py
@@ -61,7 +61,6 @@ def downloadFlugfeldData(fromTime, toTime, dtype):
|
|||||||
|
|
||||||
# check response code #
|
# check response code #
|
||||||
if r.status_code != 200 or "nicht gefunden" in r.text.lower():
|
if r.status_code != 200 or "nicht gefunden" in r.text.lower():
|
||||||
print("Flugfeld kapott")
|
|
||||||
content = fallback_csv.generate("./dwd", fromTime, toTime, cacheFile, dtype)
|
content = fallback_csv.generate("./dwd", fromTime, toTime, cacheFile, dtype)
|
||||||
else:
|
else:
|
||||||
content = r.content.decode('utf-8', "ignore") # ignore bad bytes
|
content = r.content.decode('utf-8', "ignore") # ignore bad bytes
|
||||||
@@ -101,8 +100,6 @@ def checkLastMonths(backwardsMonths=6):
|
|||||||
if start > end:
|
if start > end:
|
||||||
return ""
|
return ""
|
||||||
|
|
||||||
print(start, end)
|
|
||||||
|
|
||||||
for dtype in dtypes:
|
for dtype in dtypes:
|
||||||
content = downloadFlugfeldData(start, end, dtype)
|
content = downloadFlugfeldData(start, end, dtype)
|
||||||
dataList = parse(content, dtype)
|
dataList = parse(content, dtype)
|
||||||
|
|||||||
Reference in New Issue
Block a user