Added error handling on ovm response

This commit is contained in:
Computerboer
2024-01-07 00:23:18 +01:00
parent 50024cf1ca
commit bbd20f7915
5 changed files with 52 additions and 31 deletions

View File

@@ -31,6 +31,7 @@
<Compile Include="app.py" />
<Compile Include="utils\auctionutils.py" />
<Compile Include="cache.py" />
<Compile Include="utils\helperutils.py" />
<Compile Include="utils\locationutils.py" />
<Compile Include="models\location.py" />
</ItemGroup>

7
app.py
View File

@@ -1,3 +1,4 @@
from utils.helperutils import log
from traceback import print_exc
from flask import Flask, jsonify
from flask_cors import CORS, cross_origin
@@ -19,16 +20,16 @@ def gethome():
def getAllAuctions(countrycode):
try:
if countrycode not in ['NL', 'BE', 'DE']:
print('country not available: ' + countrycode)
log('country not available: ' + countrycode)
return jsonify('NOT AVAILABLE COUNTRY')
log('incoming api request')
res = getAuctionlocations(countrycode)
#return json.dumps(res, sort_keys=True, default=str)
return JsonEncoder().encode(res)
except Exception as e:
print('something went wrong ')
log('something went wrong ')
print_exc(e)
return 'internal server error', 500

View File

@@ -5,6 +5,7 @@ import time
import json
from models.location import JsonEncoder
from utils.helperutils import log
cache = {}
@@ -16,14 +17,14 @@ class Cache():
if(not cache):
return None
if(cacheobj.isOlderThanHours(notOlderThanHours)):
print('removing cacheobject ' + key)
log('removing cacheobject ' + key)
del cache[key]
return None
print(str(datetime.now()) + ' returning cacheobject ' + key)
log( 'returning cacheobject ' + key)
return cacheobj.obj
def add(key, obj):
print(str(datetime.now()) + ' adding cacheobject ' + key)
log('adding cacheobject ' + key)
cacheobj = CacheObj(key, obj)
cache[key] = cacheobj
@@ -35,7 +36,7 @@ class CacheObj:
self.time=datetime.now()
def isOlderThanHours(self, hours):
print('checking time cacheobject ' + self.key + ': ' + str(self.time) + " < " + str(datetime.now() - timedelta(hours=hours)))
log('checking time cacheobject ' + self.key + ': ' + str(self.time) + " < " + str(datetime.now() - timedelta(hours=hours)))
return self.time < datetime.now() - timedelta(hours=hours)
@@ -47,21 +48,21 @@ class FileCache():
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)))
log('checking time cachefile ' + filepath + ': ' + str(ti_m) + " < " + str(time.time() - (3600 * notOlderThanHours)))
if(ti_m < time.time() - (3600 * notOlderThanHours)):
print()
log('removing old filecache')
os.remove(filepath);
return None;
with open(filepath) as json_file:
json_data = json.load(json_file);
print('returning json data from cachefile: ' + key)
log('returning json data from cachefile: ' + key)
return json_data;
return None
def add(key, obj):
print(str(datetime.now()) + ' adding filecacheobject ' + key);
log('adding filecacheobject ' + key);
json_data = JsonEncoder().encode(obj)
with open("./filecache/" + key + ".json", 'w') as f:
f.write(json_data)

View File

@@ -1,8 +1,9 @@
from distutils.command import build
import imp
import requests
from cache import Cache, FileCache
from models.location import Auction, Auctionbrand, Countrycode, Maplocation
from models.location import Auction, Auctionbrand, Countrycode, Maplocation, JsonEncoder
from utils.locationutils import getGeoLocationByCity
from utils.helperutils import log
from datetime import datetime
import re
import math
@@ -60,8 +61,8 @@ def getTWKUrl():
if(response.status_code ==200):
buildid = re.search(r'"buildId":"([^"]*)', response.text, re.MULTILINE )
twkDataUrl = 'https://www.troostwijkauctions.com/_next/data/' + str(buildid[1]) + '/nl/'
print('buildid: ' + str(buildid[1]))
print('twkDataUrl: ' + twkDataUrl)
log('buildid: ' + str(buildid[1]))
log('twkDataUrl: ' + twkDataUrl)
return twkDataUrl
return None
@@ -82,7 +83,7 @@ def getTwkAuctions(countrycode):
response = requests.get(twkDataUrl + "auctions.json?countries=" + countrycode)
if(response.status_code ==200):
print('Got Twk Auctions')
log('Got Twk Auctions')
data = response.json();
auctions = [];
@@ -91,7 +92,7 @@ def getTwkAuctions(countrycode):
# for result in data['pageProps']['auctionList']:
for i in range(1,pages,1):
print("getting page " + str(i) + ' of ' + str(pages))
log("getting page " + str(i) + ' of ' + str(pages))
if(i > 1):
response = requests.get(twkDataUrl + "auctions.json?countries=" + countrycode + "&page=" + str(i));
data = response.json();
@@ -130,10 +131,17 @@ def getOVMAuctions():
if(res):
return res
try:
response = requests.get("https://onlineveilingmeester.nl/rest/nl/veilingen?status=open&domein=ONLINEVEILINGMEESTER")
except:
log("The OVM auctions call threw a error")
if(response is None):
return None
if(response.status_code ==200):
print('Got Ovm Auctions')
log('Got Ovm Auctions')
try:
data = response.json()
auctions = []
for result in data['veilingen']:
@@ -145,4 +153,9 @@ def getOVMAuctions():
auctions.append(a)
Cache.add(cachename, auctions)
return auctions
except:
log('Something went wrong in the mapping of OVM auctions to auctionviewer objects. The reason was: ' + response.reason + '. The response was: ' + JsonEncoder().encode(response.json()))
else:
log("The OVM auctions call didn't gave a 200 response but a " + str(response.status_code) + ". With the reason: " + response.reason)
return None

5
utils/helperutils.py Normal file
View File

@@ -0,0 +1,5 @@
# from datetime import datetime, timedelta
def log(value):
# print(str(datetime.now()) + ' ' + str(value))
print( str(value))