From 8c16ff10f8953c7d5ed6a4095b6dace71dd423c8 Mon Sep 17 00:00:00 2001 From: Yannik Schmidt Date: Sun, 9 May 2021 13:05:35 +0200 Subject: [PATCH] initial --- .gitignore | 3 +++ main.py | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 .gitignore create mode 100755 main.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4dc18a5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +cache/ +*.swp +*.data diff --git a/main.py b/main.py new file mode 100755 index 0000000..9b3feca --- /dev/null +++ b/main.py @@ -0,0 +1,68 @@ +#!/usr/bin/python3 + +import datetime as dt +import dateutil.relativedelta +import os + +CACHE_DIR = "cache" +CACHE_FILE_TEMPLATE = "cache_{}_{}_{}.data" +NFF_URL_TIMEFORMAT = "%d.%m.%Y" +NFF_INPUT_TIMEFORMAT = "%d.%m.%Y %H:%M" +OUTSIDE_DATA_URL = "http://umweltdaten.nuernberg.de/csv/wetterdaten/messstation-nuernberg-flugfeld/archiv/csv-export/SUN/nuernberg-flugfeld/{dtype}/individuell/{fromDate}/{toDate}/export.csv" + +dtypes = [ "lufttemperatur-aussen", "luftfeuchte", "luftdruck" ] + +def downloadFlugfeldData(fromTime, toTime, dtype): + + # prepare strings # + cacheDir = CACHE_DIR + fromTimeStr = fromTime.strftime(NFF_URL_TIMEFORMAT) + toTimeStr = toTime.strftime(NFF_URL_TIMEFORMAT) + cacheFile = CACHE_FILE_TEMPLATE.format(dtype, fromTimeStr, toTimeStr) + fullpath = os.path.join(cacheDir, cacheFile) + print(cacheFile) + + # check for cache file + content = None + if not os.path.isfile(fullpath): + url = OUTSIDE_DATA_URL.format(dtype=dtype, fromDate=fromTimeStr, toDate=toTimeStr) + print(url) + r = requests.get(url) + content = r.content.decode('utf-8', "ignore") # ignore bad bytes + + # cache data + if not os.path.isdir(cacheDir): + os.mkdir(cacheDir) + with open(fullpath, 'w') as f: + f.write(content) + else: + with open(fullpath) as f: + content = f.read() + + return content + +def checkLastMonths(backwardsMonths=6): + + today = dt.datetime.today() + monthsToCheck = [ today.month - x for x in range(0, backwardsMonths) ] + monthsToCheckFixed = list(map(lambda x: x if x > 0 else x + 12, monthsToCheck)) + + for monthNumber in monthsToCheckFixed: + + year = today.year + if monthNumber > today.month: + year = today.year - 1 + start = dt.datetime(year=year, month=monthNumber, day=1) + end = start + dateutil.relativedelta.relativedelta(months=+1, seconds=-1) + + # check special cases # + if end > today: + end = today - dt.timedelta(days=1) + if start > end: + return "" + + for dtype in dtypes: + content = downloadFlugfeldData(start, end, dtype) + +if __name__ == "__main__": + checkLastMonths()