diff --git a/server-web.py b/server-web.py index 2b4240f..644ca20 100644 --- a/server-web.py +++ b/server-web.py @@ -113,8 +113,21 @@ def createDriver(id): driver.response_interceptor = interceptor return driver +def startRequest(driver): + try: + driver.get('https://dus-travis.dus.com/') + except: + pass + #sleep(10) + #startRequest(driver) + +def assertWorking(): + return lastaccess > datetime.now(timezone.utc) - timedelta(seconds=10) + if __name__ == '__main__': + print("Starting the Server") + # start flask server threading.Thread(target=lambda: app.run(host='0.0.0.0', port=80, debug=False, use_reloader=False)).start() @@ -126,7 +139,7 @@ if __name__ == '__main__': d0 = createDriver(0) # start initial driver - d0.get('https://dus-travis.dus.com/') + startRequest(d0) lastaccess = datetime.now(timezone.utc) # loop and restart with restartWait Interval @@ -134,14 +147,14 @@ if __name__ == '__main__': sleep(30) if datetime.now(timezone.utc) > lastaccess + restartWait: d1 = createDriver(1) - d1.get('https://dus-travis.dus.com/') + startRequest(d1) print ("New Failover Driver started, waiting 10 seconds ...") sleep(10) killChildren(d0) del d0 print ("Original driver deleted") d0 = createDriver(0) - d0.get('https://dus-travis.dus.com/') + startRequest(d0) print ("Recreating original driver and waiting 10 seconds ...") sleep(10) killChildren(d1) @@ -150,7 +163,7 @@ if __name__ == '__main__': lastaccess = datetime.now(timezone.utc) l = filter(idIsRecent,list(valuestore.keys())) - if len(list(l)) < 1 and lastUpdate < datetime.now(timezone.utc) - timedelta(seconds=90): + if len(list(l)) < 1 and not assertWorking(): restartWait = timedelta(minutes=3) print ("Something stupid happened, setting interval to 3m") else: diff --git a/server.py b/server.py deleted file mode 100644 index 200977b..0000000 --- a/server.py +++ /dev/null @@ -1,71 +0,0 @@ -from seleniumwire import webdriver # Import from seleniumwire -from seleniumwire.utils import decode -from time import sleep -from collections import deque -from flask import Flask as f -from flask import Response -import json -import re - -options = webdriver.ChromeOptions() -options.add_argument('--headless') -options.add_argument("--incognito") -options.add_argument("--nogpu") -options.add_argument("--disable-gpu") -options.add_argument("--window-size=1280,1280") -options.add_argument("--no-sandbox") -options.add_argument("--enable-javascript") -options.add_argument('--disable-dev-shm-usage') - -# Create a new instance of the Chrome driver -driver = webdriver.Chrome(options=options) -q = deque() -valuestore = {} -app = f("web") - -def interceptor(request, response): # A response interceptor takes two args - if 'ws-travis.dus.com/socket.io/?EIO=3&transport=polling' in request.url : - body = decode(response.body, response.headers.get('Content-Encoding', 'identity')) - x = re.split("\d\d\d:\d\d\/dus,", body.decode("utf-8")) - for i in [i for i in x if i]: - try: - j = json.loads(i) - handleJson(j) - except ValueError: - if i.find("online-level") > 0: - print ("Fixed: ", i) - j = json.loads(i[:i.rfind(']') + 1]) - handleJson(j) - else: - print ("Err: ", i) - - -def handleJson(jsonObj): - if isinstance(jsonObj, list): - if 'online-level' in jsonObj: - #typ = jsonObj[1]["LevelValues"][0]["Type"] - value = jsonObj[1]["LevelValues"][0]["Values"] - NmtId = jsonObj[1]["NmtId"] - #time = jsonObj[1]["Time"] - valuestore[NmtId] = value - -# Gibt den letzten bekannten Wert fuer die NMTID zurueck -@app.route('/nmt/') -def nmtid(id): - if id in valuestore: - return valuestore[id] - else: - return "Not Found" - -# Gibt eine Liste mit verfuegbaren ids zurueck -@app.route('/ids') -def ids(): - l = list(valuestore.keys()) - return ','.join(map(str,l)) - -if __name__ == '__main__': - driver.response_interceptor = interceptor - - driver.get('https://dus-travis.dus.com/') - - app.run(host='0.0.0.0', port=80, debug=False)