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):
|
def get_next_refresh(data: dict):
|
||||||
times = []
|
times = []
|
||||||
for trip in data['departureData']:
|
if data is not None:
|
||||||
times.append(trip['orgFullTime'])
|
for trip in data['departureData']:
|
||||||
times.append(trip['fullTime'])
|
times.append(trip['orgFullTime'])
|
||||||
times = [int(time) for time in times if int(time) > datetime.datetime.now().timestamp()]
|
times.append(trip['fullTime'])
|
||||||
times.sort()
|
times = [int(time) for time in times if int(time) > datetime.datetime.now().timestamp()]
|
||||||
for time in times:
|
times.sort()
|
||||||
if (datetime.datetime.fromtimestamp(time) - datetime.datetime.now()) > datetime.timedelta(seconds=30):
|
for time in times:
|
||||||
if (datetime.datetime.fromtimestamp(time) - datetime.datetime.now()) > datetime.timedelta(minutes=5):
|
if (datetime.datetime.fromtimestamp(time) - datetime.datetime.now()) > datetime.timedelta(seconds=30):
|
||||||
return (datetime.datetime.now() + datetime.timedelta(minutes=5)).timestamp()
|
if (datetime.datetime.fromtimestamp(time) - datetime.datetime.now()) > datetime.timedelta(minutes=5):
|
||||||
return time
|
return (datetime.datetime.now() + datetime.timedelta(minutes=5)).timestamp()
|
||||||
|
return time
|
||||||
return (datetime.datetime.now() + datetime.timedelta(seconds=60)).timestamp()
|
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):
|
def update(station_id: int):
|
||||||
reply_data = get_data(
|
try:
|
||||||
make_request_data(
|
reply_data = get_data(
|
||||||
station_id,
|
make_request_data(
|
||||||
8,
|
station_id,
|
||||||
lines=lines_filter
|
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']:
|
for trip in reply_data['departureData']:
|
||||||
print_trip(trip)
|
print_trip(trip)
|
||||||
return reply_data
|
return reply_data
|
||||||
@ -220,15 +226,16 @@ def wait(cxn: mysql.connector.MySQLConnection, station_id: int):
|
|||||||
cur = cxn.cursor()
|
cur = cxn.cursor()
|
||||||
while True:
|
while True:
|
||||||
data = update(station_id)
|
data = update(station_id)
|
||||||
for t in data['departureData']:
|
if data is not None:
|
||||||
cur.execute('REPLACE INTO vrr '
|
for t in data['departureData']:
|
||||||
'(line_code, direction_code, station_id, orig_datetime, status, delay_value)'
|
cur.execute('REPLACE INTO vrr '
|
||||||
' VALUES (%s, %s, %s, '
|
'(line_code, direction_code, station_id, orig_datetime, status, delay_value)'
|
||||||
'from_unixtime(%s), %s, %s)',
|
' VALUES (%s, %s, %s, '
|
||||||
(t['lineCode'], t['directionCode'], station_id,
|
'from_unixtime(%s), %s, %s)',
|
||||||
t['orgFullTime'], _make_status_value_for_sql(t), _make_delay_value_for_sql(t['delay'])
|
(t['lineCode'], t['directionCode'], station_id,
|
||||||
))
|
t['orgFullTime'], _make_status_value_for_sql(t), _make_delay_value_for_sql(t['delay'])
|
||||||
cxn.commit()
|
))
|
||||||
|
cxn.commit()
|
||||||
next_refresh = get_next_refresh(data)
|
next_refresh = get_next_refresh(data)
|
||||||
print("Sleeping until " + datetime.datetime.fromtimestamp(next_refresh).isoformat(), file=sys.stderr)
|
print("Sleeping until " + datetime.datetime.fromtimestamp(next_refresh).isoformat(), file=sys.stderr)
|
||||||
pause.until(next_refresh)
|
pause.until(next_refresh)
|
||||||
|
Loading…
Reference in New Issue
Block a user