-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_data.py
executable file
·31 lines (23 loc) · 889 Bytes
/
get_data.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#!/usr/bin/python3
import requests
import json
import pickle
import sys
import config
headers = {'x-application': 'signpi', 'x-api-key': config.api_key}
try:
with requests.get(config.goodservice_url) as req:
body = req.json()
# now = body['timestamp']
# TODO: both directions, filter routes
trips = body['upcoming_trips'][config.subway.get('direction', 'north')]
# for n in range(min(4, len(trips))):
# t = trips[n]
# eta_min = round((t['estimated_current_stop_arrival_time'] - now) / 60)
# print(f"{n}. {t['route_id']} {t['destination_stop']} {eta_min} min")
with open('trips.json', 'w') as f:
json.dump(trips[:min(config.subway.getint('pages', 4), len(trips))], f)
except ConnectionError:
with open('trips.json') as f:
json.dump([], f)
sys.exit(1)