diff --git a/Auctionviewer.api.pyproj b/Auctionviewer.api.pyproj index 819bcdc..6c7a943 100644 --- a/Auctionviewer.api.pyproj +++ b/Auctionviewer.api.pyproj @@ -31,6 +31,7 @@ + diff --git a/app.py b/app.py index 7d3b454..ca81867 100644 --- a/app.py +++ b/app.py @@ -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 diff --git a/cache.py b/cache.py index d946f31..883fa9d 100644 --- a/cache.py +++ b/cache.py @@ -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) \ No newline at end of file diff --git a/utils/auctionutils.py b/utils/auctionutils.py index c444488..560bc4c 100644 --- a/utils/auctionutils.py +++ b/utils/auctionutils.py @@ -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(); @@ -108,7 +109,7 @@ def getTwkAuctions(countrycode): def getTWKAuction(twkDataUrl, auctionurlslug): response = requests.get(twkDataUrl + "a/" + auctionurlslug + '.json') - if(response.status_code ==200): + if(response.status_code == 200): data = response.json(); if(len(data['pageProps']['lots']['results']) ==0): return None; @@ -130,19 +131,31 @@ def getOVMAuctions(): if(res): return res - response = requests.get("https://onlineveilingmeester.nl/rest/nl/veilingen?status=open&domein=ONLINEVEILINGMEESTER") + 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') - data = response.json() - auctions = [] - for result in data['veilingen']: - cityname ="Nederland" if result['isBezorgVeiling'] else result['afgifteAdres']['plaats'] - cityname = "Nederland" if cityname is None else cityname #there can be auctions where you have to make an appointment to retrieve the lots - startdatetime = result['openingsDatumISO'].replace("T", " ").replace("Z", "") - enddatetime = result['sluitingsDatumISO'].replace("T", " ").replace("Z", "") - a = Auction(Auctionbrand.OVM, cityname,result['land'], result['naam'],startdatetime, enddatetime, str(result['land']).lower() + '/veilingen/' + str(result['id']) + '/kavels', 'images/150x150/' + str(result['id']) + '/' + result['image'], result['totaalKavels'] ) - auctions.append(a) - Cache.add(cachename, auctions) - return auctions + log('Got Ovm Auctions') + try: + data = response.json() + auctions = [] + for result in data['veilingen']: + cityname ="Nederland" if result['isBezorgVeiling'] else result['afgifteAdres']['plaats'] + cityname = "Nederland" if cityname is None else cityname #there can be auctions where you have to make an appointment to retrieve the lots + startdatetime = result['openingsDatumISO'].replace("T", " ").replace("Z", "") + enddatetime = result['sluitingsDatumISO'].replace("T", " ").replace("Z", "") + a = Auction(Auctionbrand.OVM, cityname,result['land'], result['naam'],startdatetime, enddatetime, str(result['land']).lower() + '/veilingen/' + str(result['id']) + '/kavels', 'images/150x150/' + str(result['id']) + '/' + result['image'], result['totaalKavels'] ) + 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 \ No newline at end of file diff --git a/utils/helperutils.py b/utils/helperutils.py new file mode 100644 index 0000000..49ab8b1 --- /dev/null +++ b/utils/helperutils.py @@ -0,0 +1,5 @@ +# from datetime import datetime, timedelta + +def log(value): + # print(str(datetime.now()) + ' ' + str(value)) + print( str(value)) \ No newline at end of file