From 872df6777675c4edba5171948b6b72f783c4acc4 Mon Sep 17 00:00:00 2001 From: Dominik Roth Date: Sat, 30 Oct 2021 14:19:50 +0200 Subject: [PATCH] Changed cache workings to allow better controll when used as lib --- meuro.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/meuro.py b/meuro.py index bf582b1..c870b04 100755 --- a/meuro.py +++ b/meuro.py @@ -8,14 +8,16 @@ from dateutil.parser._parser import ParserError as DateParserError _years = None def _loadYearsTable(maxCacheSeconds=3600): + global _years if os.path.isfile('cache.json'): with open('cache.json', 'r') as f: cacheUpdate, cacheYears = json.loads(f.read()) if (datetime.now() - dparser.isoparse(cacheUpdate)).total_seconds() < maxCacheSeconds: # JSON does not allow integers as keys; so we convert them back here... cacheYears = {int(y):{int(m):float(n) for m,n in ms.items()} for y,ms in cacheYears.items()} - return cacheYears - return _loadYearsTableWeb() + _years = cacheYears + return + _years = _loadYearsTableWeb() def _loadYearsTableWeb(): print('[i] Fetching new data from ECB-Servers...') @@ -47,7 +49,7 @@ def _loadYearsTableWeb(): def exchangeRate(date=None): global _years if _years==None: - _years = _loadYearsTable() + _loadYearsTable() if date==None: date = datetime.now() month, year = date.month, date.year