From bcdc8d29b0d6cec8e47d2bbbb181d52ee459d700 Mon Sep 17 00:00:00 2001 From: Christopher Teutsch Date: Fri, 24 May 2019 19:41:09 +0200 Subject: [PATCH] Write reply to disk on error --- monitor.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/monitor.py b/monitor.py index 74b4b5d..b073fe0 100755 --- a/monitor.py +++ b/monitor.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python3 +#!/usr/bin/env python3.6 import requests import json from typing import List @@ -33,6 +33,8 @@ class MOT: ALL_MODES = [LONG_DISTANCE_TRAIN, REGIONAL_TRAIN, COMMUTER_TRAIN, UNDERGROUND_TRAIN, TRAM, BUS, ELEVATED_TRAIN] +last_reply = requests.Response + parser = argparse.ArgumentParser() parser.add_argument("-s", "--setup", help="Run the setup routine.", action="store_true", dest="setup") @@ -144,6 +146,7 @@ def make_request_data(station_id: int, result_count: int = 8, modes: List = MOT. def get_data(request_data: dict, headers: dict = None, cookies: dict = None) -> dict: url = 'https://abfahrtsmonitor.vrr.de/backend/api/stations/table' reply = HTTP.post(url, data=request_data, headers=headers, cookies=cookies) + last_reply = reply reply.raise_for_status() print('Request time elapsed: ' + str(reply.elapsed), file=sys.stderr) return reply.json() @@ -301,7 +304,13 @@ def main(): cursor = cxn.cursor() cursor.execute(TABLE) - wait(cxn, USE_STATION_ID) + try: + wait(cxn, USE_STATION_ID) + except (ValueError, TypeError) as e: + with open("fault.json", "wb") as o: + if last_reply is not None: + o.write(last_reply.content) # requests.Response.content is a ByteIO + raise e main()