-
Notifications
You must be signed in to change notification settings - Fork 0
/
bfahrplan.py
executable file
·326 lines (274 loc) · 10 KB
/
bfahrplan.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#!/usr/bin/env python
# -*- coding: utf8 -*-
# Parse output of BEG API and generate warnings if Trains are late.
# This is a very tailored output for my own needs. Feel free to fork and modify for your requirements
#
# data used from http://www.bayern-fahrplan.de/m5/de/
# WORK IN PROGRESS. This is not yet working
#
# The MIT License (MIT)
#
# Copyright (c) 2016 Andreas Sieferlinger
import requests
from requests.adapters import HTTPAdapter
import pprint
import datetime
import json
import time
import sys
import logging
import hashlib
import pickle
import os
import toml
from pushbullet import Pushbullet
CONFDIR = os.environ['HOME'] + "/.meridian"
if not os.path.exists(CONFDIR):
os.makedirs(CONFDIR)
with open(CONFDIR + "/beg.toml") as conffile:
config = toml.loads(conffile.read())
# set up the logger
logger = logging.getLogger('bayernfahrplan')
logger.setLevel(logging.DEBUG)
# output logs to stderr
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
logger.addHandler(ch)
BASE_URL = 'https://www.bayern-fahrplan.de/jqm/beg_lite'
LOCATION_ROSENHEIM = 80000821
LOCATION_GROSSKARO = 80000680
LOCATION_MUNICH_EAST = 91000005
LOCATION_MUNICH_MAIN = 91000100
def fetch_departure_data(locationid, loadFromFile=None):
"""
construct URL and fetch the data we will work on later
"""
urlparams = {
"convertStopsPTKernel2LocationServer": 1,
"itOptionsActive": 1,
"limit": 20,
"locationServerActive": 1,
"mode": "direct",
"name_dm": locationid, # ID of location we are searching things for
"ptOptionsActive": 1,
"stateless": 1,
"type_dm": "any",
"useAllStops": 1,
"useRealtime": 1,
"coordOutputFormat": "MRCV",
"canChangeMOT": 0,
"depType": "stopEvents",
"includeCompleteStopSeq": 1,
"mergeDep": 1,
"nIPL": 1,
"language": "de",
"imparedOptionsActive": 1,
"includedMeans": 1,
"inclMOT_0": 1,
"inclMOT_1": 1,
"inclMOT_2": 1,
"inclMOT_3": 1,
"inclMOT_4": 1,
"inclMOT_5": 1,
"inclMOT_6": 1,
"inclMOT_7": 1,
"inclMOT_8": 1,
"inclMOT_9": 1,
"inclMOT_10": 1,
"inclMOT_11": 1,
"inclMOT_12": 1,
"inclMOT_13": 1,
"inclMOT_14": 1,
"inclMOT_15": 1,
"inclMOT_16": 1,
"inclMOT_17": 1,
"MOT" : 0,
"_": int(time.time()),
}
# url = "http://www.bayern-fahrplan.de/jqm/beg_lite/XML_DM_REQUEST?convertStopsPTKernel2LocationServer=1&itOptionsActive=1&limit=40&locationServerActive=1&mode=direct&name_dm=80000821&ptOptionsActive=1&stateless=1&type_dm=any&useAllStops=1&useRealtime=1&coordOutputFormat=MRCV&canChangeMOT=0&depType=stopEvents&includeCompleteStopSeq=1&mergeDep=1&nIPL=1&language=de&imparedOptionsActive=1&includedMeans=1&inclMOT_0=1&inclMOT_1=1&inclMOT_2=1&inclMOT_3=1&inclMOT_4=1&inclMOT_5=1&inclMOT_6=1&inclMOT_7=1&inclMOT_8=1&inclMOT_9=1&inclMOT_10=1&inclMOT_11=1&inclMOT_12=1&inclMOT_13=1&inclMOT_14=1&inclMOT_15=1&inclMOT_16=1&inclMOT_17=1&_=1482177011491"
# get a requests session and configure some settings
s = requests.Session()
s.mount(BASE_URL, HTTPAdapter(max_retries=3))
# fetch the data
r = s.get('%s/XML_DM_REQUEST' % BASE_URL, params=urlparams, timeout=5)
if r.status_code != requests.codes.ok: # pylint: disable=E1101
logger.error("Could not fetch data via HTTP")
sys.exit(1)
data = r.json()
# for development purposes we can also read from a json file
if loadFromFile is not None:
logger.info('loading data from file')
with open('request.json') as data_file:
data = json.load(data_file, 'utf8')
return data
def get_or_none(data, key):
# we can only work on dicts
if not isinstance(data, dict):
return None
try:
d = data[key]
except KeyError:
d = None
return d
class BFNote(object):
"""
Object to handle notes in a nicer way and add some convenience functions
"""
def __init__(self, data):
self._load(data)
def _load(self, data):
self.appearance = get_or_none(data, 'appearance')
self.header = get_or_none(data, 'header')
self.priority = get_or_none(data, 'priority')
self.text = get_or_none(data, 'text')
self.alltext = ""
textlist = []
if self.header is not None:
textlist.append(self.header)
if self.text is not None:
textlist.append(self.text)
if len(textlist) >=1:
for i in textlist:
self.alltext += i
def normal_prio(self):
"""
does the note have a "normal" priority?
"""
# blacklist some notifications that could be in every train
if self.header:
if 'Bauarbeiten' in self.header or 'Fahrplanabweichung' in self.header:
return False
if self.priority is not None:
if 'veryLow' in self.priority:
return False
return True
return False
class BFDeparture(object):
"""
object holding all information for a departure
its created from the json data we fetch from BEG
"""
def __init__(self, data):
self.id = None
self._load(data)
def _load(self, data):
self.coords = data['coords']
self.depart_time = datetime.datetime.strptime("%s:%s" % (data['dateTime']['date'], data['dateTime']['time']), "%d.%m.%Y:%H:%M")
self.depart_from = data['name']
self.next_stops = []
for stop in data['nextStops']:
splitted = stop.split(';')
if len(splitted) >=1:
try:
self.next_stops.append(splitted[1])
except IndexError:
continue
except UnicodeEncodeError:
print(splitted)
raise
self.mode = data['mode']
self.train_type = data['mode']['name']
self.destination = data['mode']['destination']
self.number = data['mode']['number']
self.network = data['mode']['diva']['network']
try:
self.platform = data['ref']['platform'].replace('(', '').replace(')', '')
except KeyError:
self.platform = None
self.code = data['mode']['code']
try:
self.delay = int(data['mode']['delay'])
except KeyError:
self.delay = 0
self.notes = []
if data['notes'] is not None:
for i in data['notes']:
self.notes.append(BFNote(i))
# note texts
self.notetexts = []
for note in self.notes:
if note.normal_prio():
self.notetexts.append(note.alltext)
self._compute_id()
def _compute_id(self):
"""
calculcate an hash over the text
id changes with every content change, so that we send an renotification
"""
hashstr = "%s %s %s %s" % (self.number, self.depart_time, self.notetexts, self.delay)
did = hashlib.sha256(hashstr.encode('UTF8')).hexdigest()
self.id = did
return did
def shall_we_notifiy(self):
if self.delay >= 5:
return True
if len(self.notetexts) > 0:
return True
return False
def interesting_train_type(self):
"""
we are only interested in a few "products" / "train types", so let's just work on them
"""
full_list = ['Meridian', 'EuroCity', 'EC', 'IC', 'InterCity']
short_list = ['Meridian']
if self.train_type in short_list:
return True
else:
return False
def stops_at(self, stop_name):
"""
Find out if this train stops at the give location name
"""
if stop_name in self.destination:
return True
for stop in self.next_stops:
if stop_name in stop:
return True
return False
def pushbullet(self):
"""
send a notification to a push bullet channel
"""
# intialize PushBullet API
pb = Pushbullet(config['pushbullet']['api_key'])
# print d.delay, d.depart_time, d.number, d.destination, d.platform, d.notetexts
text = u""" %s nach %s von %s %s
Abfahrt: %s
Verspätung: %s Minuten
Hinweise:
%s
""" % (self.number, self.destination, self.depart_from, self.platform, self.depart_time, self.delay, '\n'.join(self.notetexts))
headline = u"%s nach %s" % (self.number, self.destination)
for channel in pb.channels:
if config['pushbullet']['channel'] in channel.name:
channel.push_note(headline, text)
return text
def run(departure_location, target_name):
"""
actually glue everything together and run
"""
departure_data = fetch_departure_data(departure_location, loadFromFile=None)
# load a file to check what we already have notified
try:
already_notified = pickle.load(open(CONFDIR + "/beg_notify_store.bin", "rb"))
except IOError:
logger.warning('Could not load already_notified list. Starting with an empty list')
already_notified = []
for departure in departure_data['departures']:
#print pprint.pprint(departure)
d = BFDeparture(departure)
if d.id not in already_notified:
if d.stops_at(target_name) and d.interesting_train_type():
# print(d.delay, d.depart_time, d.number, 'nach', d.destination, d.platform, ' '.join(d.notetexts))
if d.shall_we_notifiy():
print('notification:')
print(d.delay, d.depart_time, d.number, d.destination, d.platform, ' '.join(d.notetexts))
d.pushbullet()
already_notified.append(d.id)
# store the list of notified items back to disk
try:
pickle.dump(already_notified, open(CONFDIR + "/beg_notify_store.bin", "wb"))
except IOError as e:
logger.warning('Could not save already_notified list %s', e)
run(LOCATION_GROSSKARO, u'München')
run(LOCATION_MUNICH_EAST, u'karolinenfeld')