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
This commit is contained in:
Computerboer
2023-10-22 14:24:05 +02:00
parent cf1f695971
commit 4f12de7b26
5 changed files with 48 additions and 10 deletions

View File

@@ -46,6 +46,7 @@
<Folder Include="data\locationfiles\" /> <Folder Include="data\locationfiles\" />
<Folder Include="models\" /> <Folder Include="models\" />
<Folder Include="data\" /> <Folder Include="data\" />
<Folder Include="filecache\" />
<Folder Include="utils\" /> <Folder Include="utils\" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

2
app.py
View File

@@ -19,7 +19,7 @@ def gethome():
def getAllAuctions(countrycode): def getAllAuctions(countrycode):
try: try:
if countrycode not in ['NL', 'BE', 'DE']: if countrycode not in ['NL', 'BE', 'DE']:
print(f'country not available: {countrycode} ') print('country not available: ' + countrycode)
return jsonify('NOT AVAILABLE COUNTRY') return jsonify('NOT AVAILABLE COUNTRY')

View File

@@ -1,4 +1,10 @@
from datetime import datetime, timedelta from datetime import datetime, timedelta
import os.path
from pathlib import Path
import time
import json
from models.location import JsonEncoder
cache = {} cache = {}
@@ -29,6 +35,33 @@ class CacheObj:
self.time=datetime.now() self.time=datetime.now()
def isOlderThanHours(self, hours): 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) return self.time < datetime.now() - timedelta(hours=hours)
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)

View File

@@ -1,5 +1,7 @@
from app import getAllAuctions import requests
from models.location import Countrycode
response = requests.get('https://api.auctionviewer.ikbenhenk.nl//v2/auction/NL')
result = getAllAuctions('NL'); if(response.status_code ==200):
print('ran getauctions request successfull')
else:
print('A error occurred while running the getauctions request')

View File

@@ -1,6 +1,6 @@
from distutils.command import build from distutils.command import build
import requests import requests
from cache import Cache from cache import Cache, FileCache
from models.location import Auction, Auctionbrand, Countrycode, Maplocation from models.location import Auction, Auctionbrand, Countrycode, Maplocation
from utils.locationutils import getGeoLocationByCity from utils.locationutils import getGeoLocationByCity
from datetime import datetime from datetime import datetime
@@ -10,7 +10,9 @@ import math
def getAuctionlocations(countrycode: Countrycode): def getAuctionlocations(countrycode: Countrycode):
cachename = 'allauctions_' + countrycode cachename = 'allauctions_' + countrycode
res = Cache.get(cachename) res = FileCache.get(cachename, 23)
# res = Cache.get(cachename)
if(res): if(res):
return res return res
@@ -42,7 +44,7 @@ def getAuctionlocations(countrycode: Countrycode):
for auction in location.auctions: for auction in location.auctions:
del auction.geonamelocation #removes object to not have duplicate data send to the server del auction.geonamelocation #removes object to not have duplicate data send to the server
Cache.add(cachename, maplocations) FileCache.add(cachename, maplocations)
return maplocations return maplocations
def get_geonameid(auction): def get_geonameid(auction):