made sure that the filecache keeps is not removed when auction refresh fails

This commit is contained in:
Computerboer
2024-10-13 21:12:33 +02:00
parent 28adf3dcf3
commit d295f18226
4 changed files with 34 additions and 21 deletions

6
app.py
View File

@@ -1,11 +1,9 @@
from utils.helperutils import log from utils.helperutils import log
from traceback import print_exc from traceback import print_exc
from flask import Flask, jsonify from flask import Flask, jsonify
from flask_cors import CORS, cross_origin from flask_cors import CORS
import requests
from cache import Cache from cache import Cache
from utils.auctionutils import getTwkAuctions, getAuctionlocations from utils.auctionutils import getAuctionlocations
from utils.locationutils import getGeoLocationByCity
from models.location import JsonEncoder from models.location import JsonEncoder
app = Flask(__name__) app = Flask(__name__)

View File

@@ -41,28 +41,31 @@ class CacheObj:
class FileCache(): class FileCache():
def get(key, notOlderThanHours = 24): def get(key, notOlderThanHours = None):
filepath = "./filecache/" + key + ".json" filepath = "./filecache/" + key + ".json"
cachefile = Path(filepath) cachefile = Path(filepath)
if cachefile.is_file(): if cachefile.is_file():
ti_m = os.path.getmtime(filepath) ti_m = os.path.getmtime(filepath)
#checks last modified age of file, and removes it if it is too old
log('checking time cachefile ' + filepath + ': ' + str(ti_m) + " < " + str(time.time() - (3600 * notOlderThanHours))) if(notOlderThanHours is not None):
if(ti_m < time.time() - (3600 * notOlderThanHours)): #checks last modified age of file, and removes it if it is too old
log('removing old filecache') log('checking time cachefile ' + filepath + ': ' + str(ti_m) + " < " + str(time.time() - (3600 * notOlderThanHours)))
os.remove(filepath);
return None; if( ti_m < time.time() - (3600 * notOlderThanHours)):
log('removing old filecache')
os.remove(filepath)
return None
with open(filepath) as json_file: with open(filepath) as json_file:
json_data = json.load(json_file); json_data = json.load(json_file)
log('returning json data from cachefile: ' + key) log('returning json data from cachefile: ' + key)
return json_data; return json_data
return None return None
def add(key, obj): def add(key, obj):
log('adding filecacheobject ' + key); log('adding filecacheobject ' + key)
json_data = JsonEncoder().encode(obj) json_data = JsonEncoder().encode(obj)
with open("./filecache/" + key + ".json", 'w') as f: with open("./filecache/" + key + ".json", 'w') as f:
f.write(json_data) f.write(json_data)

View File

@@ -1,7 +1,16 @@
import requests # import requests
from utils.auctionutils import getAuctionlocations
response = requests.get('https://api.auctionviewer.ikbenhenk.nl//v2/auction/NL') try:
if(response.status_code ==200): getAuctionlocations('NL', True)
print('ran getauctions request successfull') print('ran getauctions request successfull')
else: except Exception as e:
print('A error occurred while running the getauctions request') print('A error occurred while running the getauctions request')
print(e)
# 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')

View File

@@ -8,10 +8,13 @@ from datetime import datetime
import re import re
import math import math
def getAuctionlocations(countrycode: Countrycode): def getAuctionlocations(countrycode: Countrycode, clearcache:bool = False):
cachename = 'allauctions_' + countrycode cachename = 'allauctions_' + countrycode
res = FileCache.get(cachename, 23) if(clearcache):
res = FileCache.get(cachename, 1)
else:
res = FileCache.get(cachename)
if(res): if(res):
return res return res