-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstation.py
153 lines (122 loc) · 4.56 KB
/
station.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
from irc3 import asyncio
from irc3.plugins.command import command
import io
import re
import json
import aiohttp
from datetime import datetime
from dateutil import tz
from difflib import get_close_matches
URL = "https://evag-live.wla-backend.de/node/v1/departures/{extId}"
RE_FIX = "(,\\s*|\\s*-\\s*){component}|{component}((,\\s*)|\\s*-\\s*)"
def lookup_by_id(extId):
"""
Lookup station by extId and return the pretty concatenation of name, parent
"""
with io.open("data/stations.latest.json") as fp:
for obj in json.load(fp):
if obj["id"] == extId:
return "{parent}, {name}".format(**obj)
def lookup_by_name(name, parent):
"""
Lookup station by (parent, name) and return its extId.
"""
with io.open("data/stations.latest.json") as fp:
data = json.load(fp)
candidates = [obj["name"] for obj in data if obj["parent"] == parent]
match = get_close_matches(name, candidates, 1, 0.3)
if not match:
return None
for obj in data:
if obj["parent"] == parent and obj["name"] == match[0]:
return str(obj["id"])
def remove_component(targetlocation, component):
"""
Fix API locations like "Erfurt, Daberstedt" to just "Daberstedt"
"""
return re.sub(RE_FIX.format(component=component), "", targetlocation)
@command(permission="view")
@asyncio.coroutine
def station(bot, mask, target, args):
"""
Show departures for station: 151213 (Leipziger Platz, Erfurt). The service
URL is consumed by Erfurt Mobil (Android/iOS).
A list of all known stations in the VMT area can be viewed here:
https://evag-live.wla-backend.de/stations/latest.json
%%station
%%station <name>...
"""
# load configuration
config = bot.config.get(__name__, {})
city = config.get("city")
if not city:
city = "Erfurt"
# determine extId (VMT internal id) and friendly name
extid = " ".join(args["<name>"])
if not extid:
extid = str(config.get("id", 151213))
if extid == "help":
return "Use !station NAME/EXTID to get recent departures"
name = None
if extid.isdigit():
name = lookup_by_id(extid)
else:
extid = lookup_by_name(extid, city)
if extid is not None:
name = lookup_by_id(extid)
if extid is None:
return "Unknown station: " + " ".join(args["<name>"])
with aiohttp.Timeout(10):
with aiohttp.ClientSession(loop=bot.loop) as session:
resp = yield from session.get(URL.format(extId=extid))
if resp.status != 200:
bot.privmsg(target, "Error while retrieving traffic data.")
raise Exception()
body = yield from resp.read()
try:
body = json.loads(body.decode('utf-8'))
except json.JSONDecodeError:
bot.privmsg(target, "Error while retrieving traffic data.")
try:
# use local timezone
tzinfo = tz.tzlocal()
data = []
for departure in body.get("departures", []):
delay = 0 # in seconds
try:
timestamp = departure.get("timestamp")
timestamp_rt = departure.get("timestamp_rt", 0)
delay = max(0, timestamp_rt - timestamp) // 60
except ValueError:
pass
data.append({
"type": "Tram" if departure["type"] == "Strab" else "Bus",
"line": departure["line"],
"target": remove_component(departure["targetLocation"], city),
"time": datetime.fromtimestamp(departure["timestamp"], tzinfo),
"delay": delay
})
# limit output to 10 max
data = data[:10]
# add padding to all departures if any is delayed
delayed = any(map(lambda d: d["delay"] > 0, data))
# get max line number
maxline = max(map(lambda d: len(d["line"]), data))
bot.privmsg(target, name)
for departure in data:
delay = ""
if departure["delay"]:
delay = " +{:d}".format(departure["delay"])
elif delayed:
delay = " "
bot.privmsg(
target,
"{:4}{} | {:4} {} -> {}".format(
departure["time"].strftime("%H:%M"),
delay,
departure["type"],
departure["line"].rjust(maxline),
departure["target"]))
except KeyError:
bot.privmsg(target, "Error while retrieving traffic data.")
raise Exception()