-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhafas_fetcher.py
173 lines (153 loc) · 6.02 KB
/
hafas_fetcher.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import json
from itertools import islice
from requests import get
from hafas_event import HAFASEvent
from helper import Helper, log
from hosted import CONFIG
from mapping import API_MAPPING
class HAFASFetcher:
def __init__(self):
self.data_sources = CONFIG["data_sources"]
self.departures = []
self.arrivals = []
self.events = []
def fetch_and_parse(self, stop_id):
stop_info = self._fetch(stop_id)
departures = []
for dep in stop_info["Departure"]:
departures.append(HAFASEvent(dep))
departures = sorted(departures)
for n, dep in enumerate(departures):
for follow in islice(departures, n + 1, None):
if dep.symbol == follow.symbol and (
(dep.platform != "" and dep.platform == follow.platform)
or dep.destination == follow.destination
):
dep.follow = follow
break
self.departures.extend(departures)
arrivals = []
for arr in stop_info["Arrival"]:
arrivals.append(HAFASEvent(arr))
if self.data_sources == "both":
journeys = list(map(lambda d: d.id, self.departures))
arrivals = [arr for arr in arrivals if arr.id not in journeys]
arrivals = sorted(arrivals)
for n, arr in enumerate(arrivals):
for follow in islice(arrivals, n + 1, None):
if arr.symbol == follow.symbol and arr.origin == follow.origin:
arr.follow = follow
break
self.arrivals.extend(arrivals)
def _fetch_url(self, stop_id, url):
log(
"Requesting {stop} info from {url}".format(
stop=stop_id,
url=url,
)
)
r = get(url)
r.raise_for_status()
return r.json()
def _fetch(self, stop_id):
key = CONFIG["api_key"].strip()
data = {
"Departure": [],
"Arrival": [],
}
if key.startswith("http://") or key.startswith("https://"):
key = key.rstrip("/")
url = "{prefix}/{stop}.json".format(
prefix=key,
stop=stop_id,
)
payload = self._fetch_url(stop_id, url)
if not self.data_sources == "arrivals":
data["Departure"] = payload["Departure"]
if not self.data_sources == "departures":
data["Arrival"] = payload["Arrival"]
else:
url = lambda ep: API_MAPPING[CONFIG["api_provider"]].format(
endpoint=ep,
stop=stop_id,
minutes=CONFIG["request_hours"] * 60,
key=key,
)
if not self.data_sources == "arrivals":
payload = self._fetch_url(stop_id, url("departureBoard"))
data["Departure"] = payload["Departure"]
if not self.data_sources == "departures":
payload = self._fetch_url(stop_id, url("arrivalBoard"))
data["Arrival"] = payload["Arrival"]
return data
def _sort_and_deduplicate(self, events, locator):
events = sorted(events)
for n, ev in enumerate(events):
for follow in islice(events, n + 1, None):
if (
locator(ev) == locator(follow)
and ev.symbol == follow.symbol
and (
(
ev.stop != follow.stop
and abs(
Helper.to_unixtimestamp(ev.realtime)
- Helper.to_unixtimestamp(follow.realtime)
)
<= 120
)
or (
ev.stop == follow.stop
and abs(
Helper.to_unixtimestamp(ev.realtime)
- Helper.to_unixtimestamp(follow.realtime)
)
<= 10
)
)
):
follow.duplicate = True
break
events = [ev for ev in events if not ev.duplicate]
events = [ev for ev in events if not ev.ignore_destination]
return events
def sort_and_deduplicate(self):
self.departures = self._sort_and_deduplicate(
self.departures, lambda ev: ev.destination
)
self.arrivals = self._sort_and_deduplicate(self.arrivals, lambda ev: ev.origin)
events = []
events.extend(self.departures)
events.extend(self.arrivals)
self.events.extend(sorted(events))
def write_json(self):
log("writing {} events to json".format(len(self.events)))
out = []
for dep in self.events:
departure = {
"category": dep.category,
"delay": dep.delay,
"departure": dep.destination is not None,
"direction": (
dep.destination if dep.destination is not None else dep.origin
),
"icon": dep.category_icon,
"id": dep.id,
"next_time": (
dep.follow.realtime.strftime("%H:%M") if dep.follow else ""
),
"next_timestamp": (
Helper.to_unixtimestamp(dep.follow.realtime) if dep.follow else 0
),
"notes": dep.notes,
"operator": dep.operator,
"platform": dep.platform,
"stop": dep.stop,
"symbol": dep.symbol,
"time": dep.realtime.strftime("%H:%M"),
"timestamp": Helper.to_unixtimestamp(dep.realtime),
}
departure.update(dep.line_colour)
out.append(departure)
with file("events.json", "wb") as f:
f.write(json.dumps(out, ensure_ascii=False).encode("utf8"))