-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathddm
executable file
·294 lines (239 loc) · 7.99 KB
/
ddm
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
#!/bin/bash
#
### LICENSE ###
# ddm - dresden-departure-monitor:
# getting departures for stops in Dresden
#
# Copyright (C) 2011 don_philipe - don_philipe@gmx.de
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
###
#
OPTIONS="-q --keep-session-cookies --save-cookies cookies --load-cookies cookies -T 100"
URL="www.dvb.de"
VERSION="Version: 1.1"
# default:
displayed=15 # number of displayed departures
loop=false
loop_sec=30 # repeat getting departuretimes every x seconds
city="Dresden"
stop=""
headlinelength=50 # length of the headline without stop
helptext()
{
cat <<HELP
Syntax: ddm [options] stop
Shows the departuretimes, routes, vehicles and directions for a given stop in Dresden.
Passing the stop becomes obsolete if you add one in the script.
Options:
-d num changes the number of displayed departures (default: 15)
-h, --help shows this helptext
-l [sec] get departuretimes repeatedly every 30 second or add an
own interval in seconds (greater than 0)
-v, --version returns the scriptversion
HELP
}
getSessionId()
{
grep /de/Fahrplan/Abfahrtsmonitor/haltestellenauswahl.do?uk_id abfahrt > sessionid
sessionid="$(sed 's/ <form action="\/de\/Fahrplan\/Abfahrtsmonitor\/haltestellenauswahl.do?uk_id=//;s/#result" id="am_form" method="post" >//' sessionid)"
}
clearDir()
{
rm abfahrt
rm cookies
rm sessionid
}
getDepartures()
{
wget $OPTIONS -O abfahrt --post-data "amform%5Bdatum%5D=${cur_date}&amform%5Bzeit%5D=${cur_time}&amform%5Bstartort%5D=${city}&amform%5Bstartname%5D=${stop}&amform%5Bstarttype%5D=stop" "${URL}/de/Fahrplan/Abfahrtsmonitor/haltestellenauswahl.do?uk_id=${sessionid}" #%20
# test if stop is valid:
if [ -n "$(grep Unbekannte\ Haltestelle\. abfahrt)" ]
then
echo -e "\nUnknown stop!\n"
clearDir
exit 0
fi
# get correct stopname:
stop="$(grep \>$city, abfahrt)"
stop="${stop##*,}"
# replacing umlauts
stop="$(echo $stop | sed 's/\ä/ä/g;s/\ö/ö/g;s/ü/ü/g;s/\ß/\ß/g')"
#s//Ä/g
#s//Ü/g
#s//Ö/g
# adds the length of the stopname to the headlinelength:
let headlinelength=$headlinelength+${#stop}
for (( i=1 ; i<= $headlinelength ; i++ ))
do
underline="${underline}="
done
getSessionId
# getting departuretimes again with only two vehicles
wget $OPTIONS -O abfahrt --post-data 'amform%5Bverkehrsmittelcheckboxen%5D=strassenbahn&amform%5Bverkehrsmittelcheckboxen%5D=stadtbus' "${URL}/de/Fahrplan/Abfahrtsmonitor/abfahrten.do?uk_id=${sessionid}"
}
clearTime() # remove the leading 0 if present
{
tmp="${1}"
if [ "$(echo ${tmp} | cut -c 1)" == "0" ]
then
tmp=$(echo ${tmp} | cut -c 2)
fi
echo ${tmp}
}
calcRemainingTime() # doesnt work correct around 0 o'clock
{
dep_time_h=$(clearTime ${timearray[$i]%%:*})
cur_time_h=$(clearTime ${cur_time%%:*})
dep_time_m=$(clearTime ${timearray[$i]##*:})
cur_time_m=$(clearTime ${cur_time##*:})
if [ $dep_time_h -lt $cur_time_h ]
then
timeshift=24
else
timeshift=0
fi
if [ $(( $dep_time_h - $cur_time_h )) -eq 0 ] # same hour
then
remaining_time[$i]="$(( $dep_time_m - $cur_time_m ))"
else # 1,2,... hour later
if [ $(( $dep_time_m - $cur_time_m )) -lt 0 ]
then #remaining_time not greater than 59 min
minutes="$(( 60 + $(( $dep_time_m - $cur_time_m )) ))"
if [ $(( $dep_time_h - $cur_time_h )) -lt 0 ]
then # for daychange
hours="$(( 23 + $(( $dep_time_h - $cur_time_h )) ))"
remaining_time[$i]="$hours:$minutes"
else
remaining_time[$i]=$minutes
fi
else # remaining_time greater than 59 min
remaining_time[$i]="$(( $timeshift + $dep_time_h - $cur_time_h )):$(( $dep_time_m - $cur_time_m ))"
fi
fi
}
processHtml()
{
startline="$(grep -n \"abfahrten_container\" abfahrt)"
startline="${startline%%:*}"
endline="$(grep -n /de/Fahrplan/Abfahrtsmonitor/spaeter.do abfahrt)"
endline="${endline%%:*}"
dep_times="$(sed -n ''${startline}','${endline}'p' abfahrt | sed '/[012][0-9]\:[0-5][0-9]/!d;s/[^0-9\:]//g')"
vehicle="$(sed -n ''${startline}','${endline}'p' abfahrt | sed '/^.*pikto_[a-z]-\?[a-z]*\.gif.*$/!d;s/^.*pikto_//g;s/\.gif.*$/ /g;s/\b./\U&/g')"
route="$(sed -n ''${startline}','${endline}'p' abfahrt | sed '/<td>[1-9][0-9]\?[0-9]\?<\/td>/!d;s/[^0-9]//g;')" #s/[1-9][^0-9]/&\ /g')"
direction="$(sed -n ''${startline}','${endline}'p' abfahrt | sed '/ \t.*[A-ZÄÖÜ][a-zäöüß]*.*[A-ZÄÖÜ]\?[a-zäöüß]*\t/!d;s/ \t//g;s/\ /+/g;s/\t/ /g')"
for (( i=1 ; i <= $displayed ; i++ )) # loop starts with 1 for the calculation two lines below
do
if [ "$dep_times" == "" ]
then
break
fi
timearray[$i]="$(echo ${dep_times:0:6})" # ${String:Position:Length}
dep_times="$(echo $dep_times | sed 's/[0-9][0-9]\:[0-9][0-9]//')" # cut the currently used time
vehiclearray[$i]="$(echo `expr "$vehicle" : '\([A-ZÄÖÜ]-\?B\?[a-zäöüß]*\ \)'`)" # -\?B\? for S-Bahn
vehicle="$(echo $vehicle | sed 's/[A-ZÄÖÜ]-\?B\?[a-zäöüß]*\ //')"
routearray[$i]="$(echo `expr "$route" : '\([1-9][0-9][0-9]\|[1-9][0-9]\|[1-9]\)'`)" #\|S\ [1-9] - add for S-Bahn at the end
route="$(echo $route | sed 's/[1-9][0-9][0-9]\ \|[1-9][0-9]\ \|[1-9]\ //')" #\|S\ [1-9]\ - add for S-Bahn at the end
directionarray[$i]="$(echo `expr "$direction" : '\([A-ZÄÖÜ][A-ZÄÖÜa-zäöüß+\.,]*\)'`)"
directionarray[$i]=$(echo ${directionarray[$i]} | sed 's/+/\ /g')
direction="$(echo $direction | sed 's/[A-ZÄÖÜ][A-ZÄÖÜa-zäöüß+\.,]*\ //')"
calcRemainingTime
done
}
output()
{
echo -e "\nDeparturemonitor for ${stop}, ${city}, ${cur_date}, ${cur_time}:\n${underline}\n"
echo -e "Time\tRoute\tVehicle\t\tDirection\t\tRemaining time" # tableheader
# output lines:
for (( i=1 ; i <= $displayed ; i++ ))
do
if [ "${timearray[$i]}" == "" ] # break loop if not enough departures
then
break
fi
tabs="\t\t"
if [ ${#directionarray[$i]} -lt 8 ]
then
tabs="\t\t\t"
fi
if [ ${#directionarray[$i]} -gt 15 ]
then
tabs="\t"
fi
echo -e "${timearray[$i]}\t${routearray[$i]}\t${vehiclearray[$i]}\t\t${directionarray[$i]}$tabs${remaining_time[$i]}"
done
echo -e "\n"
}
######################
# Script starts here #
######################
while [ "${1}" != '' ]
do
[ "${1}" == "-d" ] && shift && displayed="${1}" && shift
[ "${1}" == "-h" ] || [ "${1}" == "--help" ] && helptext && exit 0
[ "${1}" == "-l" ] && shift && loop=true && loop_sec="${1}" && shift
[ "${1}" == "-v" ] || [ "${1}" == "--version" ] && echo $VERSION && exit 0
[ -n "${1}" ] && stop="${1} ${2}" && shift && shift # 2x shift for stops containing two words
if [ $(( $displayed + 0 )) -eq 0 ] ; then
echo "Syntax error! Parameter not found or 0."
exit 0
fi
if [ "$(echo $loop_sec | sed '/[0-9]/!d')" == "" ] ; then
stop="${loop_sec} ${stop}"
loop_sec=30
fi
done
# get website for initial sessionid:
wget $OPTIONS -O abfahrt "${URL}/de/Fahrplan/Abfahrtsmonitor"
# set date and time:
cur_date="$(date +%d.%m.%Y)"
cur_time="$(date +%H:%M)"
if [ "$(cat abfahrt)" == "" ]
then
echo -e "\nCan't reach the server!\n"
clearDir
exit 0
fi
getSessionId
clear
echo "Download departuretimes..."
getDepartures
echo "Process information..."
processHtml
output
clearDir
while $loop
do
sleep $loop_sec
# get website for initial sessionid:
wget $OPTIONS -O abfahrt "${URL}/de/Fahrplan/Abfahrtsmonitor"
# set date and time:
cur_date="$(date +%d.%m.%Y)"
cur_time="$(date +%H:%M)"
if [ "$(cat abfahrt)" == "" ]
then
echo -e "\nCan't reach the server!\n"
clearDir
exit 0
fi
headlinelength=50
underline=""
getSessionId
getDepartures
processHtml
clear
output
clearDir
done
exit 0