Catch HTTP errors
This commit is contained in:
parent
0fa0126dbc
commit
f05d639fe5
57
monitor.py
57
monitor.py
@ -159,16 +159,17 @@ def print_trip(trip: dict) -> None:
|
||||
|
||||
def get_next_refresh(data: dict):
|
||||
times = []
|
||||
for trip in data['departureData']:
|
||||
times.append(trip['orgFullTime'])
|
||||
times.append(trip['fullTime'])
|
||||
times = [int(time) for time in times if int(time) > datetime.datetime.now().timestamp()]
|
||||
times.sort()
|
||||
for time in times:
|
||||
if (datetime.datetime.fromtimestamp(time) - datetime.datetime.now()) > datetime.timedelta(seconds=30):
|
||||
if (datetime.datetime.fromtimestamp(time) - datetime.datetime.now()) > datetime.timedelta(minutes=5):
|
||||
return (datetime.datetime.now() + datetime.timedelta(minutes=5)).timestamp()
|
||||
return time
|
||||
if data is not None:
|
||||
for trip in data['departureData']:
|
||||
times.append(trip['orgFullTime'])
|
||||
times.append(trip['fullTime'])
|
||||
times = [int(time) for time in times if int(time) > datetime.datetime.now().timestamp()]
|
||||
times.sort()
|
||||
for time in times:
|
||||
if (datetime.datetime.fromtimestamp(time) - datetime.datetime.now()) > datetime.timedelta(seconds=30):
|
||||
if (datetime.datetime.fromtimestamp(time) - datetime.datetime.now()) > datetime.timedelta(minutes=5):
|
||||
return (datetime.datetime.now() + datetime.timedelta(minutes=5)).timestamp()
|
||||
return time
|
||||
return (datetime.datetime.now() + datetime.timedelta(seconds=60)).timestamp()
|
||||
|
||||
|
||||
@ -198,13 +199,18 @@ def _make_status_value_for_sql(trip: dict) -> str:
|
||||
|
||||
|
||||
def update(station_id: int):
|
||||
reply_data = get_data(
|
||||
make_request_data(
|
||||
station_id,
|
||||
8,
|
||||
lines=lines_filter
|
||||
try:
|
||||
reply_data = get_data(
|
||||
make_request_data(
|
||||
station_id,
|
||||
8,
|
||||
lines=lines_filter
|
||||
)
|
||||
)
|
||||
)
|
||||
except requests.exceptions.HTTPError as e:
|
||||
print("Could not get the data: " + str(e))
|
||||
return None
|
||||
|
||||
for trip in reply_data['departureData']:
|
||||
print_trip(trip)
|
||||
return reply_data
|
||||
@ -220,15 +226,16 @@ def wait(cxn: mysql.connector.MySQLConnection, station_id: int):
|
||||
cur = cxn.cursor()
|
||||
while True:
|
||||
data = update(station_id)
|
||||
for t in data['departureData']:
|
||||
cur.execute('REPLACE INTO vrr '
|
||||
'(line_code, direction_code, station_id, orig_datetime, status, delay_value)'
|
||||
' VALUES (%s, %s, %s, '
|
||||
'from_unixtime(%s), %s, %s)',
|
||||
(t['lineCode'], t['directionCode'], station_id,
|
||||
t['orgFullTime'], _make_status_value_for_sql(t), _make_delay_value_for_sql(t['delay'])
|
||||
))
|
||||
cxn.commit()
|
||||
if data is not None:
|
||||
for t in data['departureData']:
|
||||
cur.execute('REPLACE INTO vrr '
|
||||
'(line_code, direction_code, station_id, orig_datetime, status, delay_value)'
|
||||
' VALUES (%s, %s, %s, '
|
||||
'from_unixtime(%s), %s, %s)',
|
||||
(t['lineCode'], t['directionCode'], station_id,
|
||||
t['orgFullTime'], _make_status_value_for_sql(t), _make_delay_value_for_sql(t['delay'])
|
||||
))
|
||||
cxn.commit()
|
||||
next_refresh = get_next_refresh(data)
|
||||
print("Sleeping until " + datetime.datetime.fromtimestamp(next_refresh).isoformat(), file=sys.stderr)
|
||||
pause.until(next_refresh)
|
||||
|
Loading…
Reference in New Issue
Block a user