Write reply to disk on error

This commit is contained in:
Christopher Teutsch 2019-05-24 19:41:09 +02:00
parent 08a1b5cd96
commit bcdc8d29b0
Signed by: iwonder
GPG Key ID: 0EE33D788D50130D

View File

@ -1,4 +1,4 @@
#!/usr/bin/env python3 #!/usr/bin/env python3.6
import requests import requests
import json import json
from typing import List 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] ALL_MODES = [LONG_DISTANCE_TRAIN, REGIONAL_TRAIN, COMMUTER_TRAIN, UNDERGROUND_TRAIN, TRAM, BUS, ELEVATED_TRAIN]
last_reply = requests.Response
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument("-s", "--setup", help="Run the setup routine.", action="store_true", dest="setup") 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: def get_data(request_data: dict, headers: dict = None, cookies: dict = None) -> dict:
url = 'https://abfahrtsmonitor.vrr.de/backend/api/stations/table' url = 'https://abfahrtsmonitor.vrr.de/backend/api/stations/table'
reply = HTTP.post(url, data=request_data, headers=headers, cookies=cookies) reply = HTTP.post(url, data=request_data, headers=headers, cookies=cookies)
last_reply = reply
reply.raise_for_status() reply.raise_for_status()
print('Request time elapsed: ' + str(reply.elapsed), file=sys.stderr) print('Request time elapsed: ' + str(reply.elapsed), file=sys.stderr)
return reply.json() return reply.json()
@ -301,7 +304,13 @@ def main():
cursor = cxn.cursor() cursor = cxn.cursor()
cursor.execute(TABLE) cursor.execute(TABLE)
try:
wait(cxn, USE_STATION_ID) 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() main()