From d295f182268745dd4ea363e62f4353e74ab947c9 Mon Sep 17 00:00:00 2001 From: Computerboer Date: Sun, 13 Oct 2024 21:12:33 +0200 Subject: [PATCH] made sure that the filecache keeps is not removed when auction refresh fails --- app.py | 6 ++---- cache.py | 23 +++++++++++++---------- runapi.py | 19 ++++++++++++++----- utils/auctionutils.py | 7 +++++-- 4 files changed, 34 insertions(+), 21 deletions(-) diff --git a/app.py b/app.py index ca81867..5c4451e 100644 --- a/app.py +++ b/app.py @@ -1,11 +1,9 @@ from utils.helperutils import log from traceback import print_exc from flask import Flask, jsonify -from flask_cors import CORS, cross_origin -import requests +from flask_cors import CORS from cache import Cache -from utils.auctionutils import getTwkAuctions, getAuctionlocations -from utils.locationutils import getGeoLocationByCity +from utils.auctionutils import getAuctionlocations from models.location import JsonEncoder app = Flask(__name__) diff --git a/cache.py b/cache.py index ee4fef3..cf3b7c7 100644 --- a/cache.py +++ b/cache.py @@ -41,28 +41,31 @@ class CacheObj: class FileCache(): - def get(key, notOlderThanHours = 24): + def get(key, notOlderThanHours = None): 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 - log('checking time cachefile ' + filepath + ': ' + str(ti_m) + " < " + str(time.time() - (3600 * notOlderThanHours))) - if(ti_m < time.time() - (3600 * notOlderThanHours)): - log('removing old filecache') - os.remove(filepath); - return None; + + if(notOlderThanHours is not None): + #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( ti_m < time.time() - (3600 * notOlderThanHours)): + log('removing old filecache') + os.remove(filepath) + return None 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) - return json_data; + return json_data return None def add(key, obj): - log('adding filecacheobject ' + key); + log('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 59b7206..7f558c4 100644 --- a/runapi.py +++ b/runapi.py @@ -1,7 +1,16 @@ -import requests +# import requests +from utils.auctionutils import getAuctionlocations -response = requests.get('https://api.auctionviewer.ikbenhenk.nl//v2/auction/NL') -if(response.status_code ==200): +try: + getAuctionlocations('NL', True) print('ran getauctions request successfull') -else: - print('A error occurred while running the getauctions request') \ No newline at end of file +except Exception as e: + 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') \ No newline at end of file diff --git a/utils/auctionutils.py b/utils/auctionutils.py index ab66028..5d3cb68 100644 --- a/utils/auctionutils.py +++ b/utils/auctionutils.py @@ -8,10 +8,13 @@ from datetime import datetime import re import math -def getAuctionlocations(countrycode: Countrycode): +def getAuctionlocations(countrycode: Countrycode, clearcache:bool = False): cachename = 'allauctions_' + countrycode - res = FileCache.get(cachename, 23) + if(clearcache): + res = FileCache.get(cachename, 1) + else: + res = FileCache.get(cachename) if(res): return res