From 4f12de7b263f57f818a702f2426da298cd52c2a7 Mon Sep 17 00:00:00 2001 From: Computerboer Date: Sun, 22 Oct 2023 14:24:05 +0200 Subject: [PATCH] Updated to have a cache that survives the restart of the application Added ability to call the api to let the filecache refresh every night --- Auctionviewer.api.pyproj | 1 + app.py | 2 +- cache.py | 37 +++++++++++++++++++++++++++++++++++-- runapi.py | 10 ++++++---- utils/auctionutils.py | 8 +++++--- 5 files changed, 48 insertions(+), 10 deletions(-) diff --git a/Auctionviewer.api.pyproj b/Auctionviewer.api.pyproj index ef18e18..819bcdc 100644 --- a/Auctionviewer.api.pyproj +++ b/Auctionviewer.api.pyproj @@ -46,6 +46,7 @@ + diff --git a/app.py b/app.py index 455c0bc..7d3b454 100644 --- a/app.py +++ b/app.py @@ -19,7 +19,7 @@ def gethome(): def getAllAuctions(countrycode): try: if countrycode not in ['NL', 'BE', 'DE']: - print(f'country not available: {countrycode} ') + print('country not available: ' + countrycode) return jsonify('NOT AVAILABLE COUNTRY') diff --git a/cache.py b/cache.py index 202c2fd..d946f31 100644 --- a/cache.py +++ b/cache.py @@ -1,4 +1,10 @@ from datetime import datetime, timedelta +import os.path +from pathlib import Path +import time +import json + +from models.location import JsonEncoder cache = {} @@ -29,6 +35,33 @@ class CacheObj: self.time=datetime.now() def isOlderThanHours(self, hours): - #print(f'checking time cacheobject {self.time} < {datetime.now() - timedelta(hours=hours)}') + print('checking time cacheobject ' + self.key + ': ' + str(self.time) + " < " + str(datetime.now() - timedelta(hours=hours))) return self.time < datetime.now() - timedelta(hours=hours) - \ No newline at end of file + + +class FileCache(): + def get(key, notOlderThanHours = 24): + + filepath = "./filecache/" + key + ".json" + cachefile = Path(filepath) + if cachefile.is_file(): + ti_m = os.path.getmtime(filepath) + #checks last modified age of file, and removes it if it is too old + print('checking time cachefile ' + filepath + ': ' + str(ti_m) + " < " + str(time.time() - (3600 * notOlderThanHours))) + if(ti_m < time.time() - (3600 * notOlderThanHours)): + print() + os.remove(filepath); + return None; + + with open(filepath) as json_file: + json_data = json.load(json_file); + print('returning json data from cachefile: ' + key) + return json_data; + + return None + + def add(key, obj): + print(str(datetime.now()) + ' adding filecacheobject ' + key); + json_data = JsonEncoder().encode(obj) + with open("./filecache/" + key + ".json", 'w') as f: + f.write(json_data) \ No newline at end of file diff --git a/runapi.py b/runapi.py index c9fa9de..59b7206 100644 --- a/runapi.py +++ b/runapi.py @@ -1,5 +1,7 @@ -from app import getAllAuctions -from models.location import Countrycode +import requests - -result = getAllAuctions('NL'); \ No newline at end of file +response = requests.get('https://api.auctionviewer.ikbenhenk.nl//v2/auction/NL') +if(response.status_code ==200): + print('ran getauctions request successfull') +else: + print('A error occurred while running the getauctions request') \ No newline at end of file diff --git a/utils/auctionutils.py b/utils/auctionutils.py index f59fc6a..c444488 100644 --- a/utils/auctionutils.py +++ b/utils/auctionutils.py @@ -1,6 +1,6 @@ from distutils.command import build import requests -from cache import Cache +from cache import Cache, FileCache from models.location import Auction, Auctionbrand, Countrycode, Maplocation from utils.locationutils import getGeoLocationByCity from datetime import datetime @@ -10,7 +10,9 @@ import math def getAuctionlocations(countrycode: Countrycode): cachename = 'allauctions_' + countrycode - res = Cache.get(cachename) + res = FileCache.get(cachename, 23) + + # res = Cache.get(cachename) if(res): return res @@ -42,7 +44,7 @@ def getAuctionlocations(countrycode: Countrycode): for auction in location.auctions: del auction.geonamelocation #removes object to not have duplicate data send to the server - Cache.add(cachename, maplocations) + FileCache.add(cachename, maplocations) return maplocations def get_geonameid(auction):