summaryrefslogtreecommitdiff
path: root/contrib/train.sh
blob: bcd35401005ada849e94a1dbbf8465e79970a260 (plain)
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
#!/bin/bash

# Interval for refreshing Data and giving it to Polybar
INTERVAL=1

# Delay Notification Variables
notificationDelaySent="false"
notificationLastDelay=0
notificationNextStopSent="true"
notificationNextStopTime=""

# Place your API Key here
API_KEY=
NOTIFICATIONS_NEXT_STOP="true"
NOTIFICATIONS_DELAY="true"
LANGUAGE="DE"
SYMBOL=""


while true; do
	# curl'ing of all needed Data from https://travelynx.de
	isCheckedIn=$(curl -s https://travelynx.de/api/v1/status/$API_KEY | jq .checkedIn | sed 's/"//' | sed 's/"//') 
	echo "$isCheckedIn" > /dev/null;
	trainType=$(curl -s https://travelynx.de/api/v1/status/$API_KEY | jq .train.type | sed 's/"//' | sed 's/"//')
	echo "$trainType" > /dev/null;
	trainNo=$(curl -s https://travelynx.de/api/v1/status/$API_KEY | jq .train.no | sed 's/"//' | sed 's/"//')
	echo "$trainNo" > /dev/null;
	trainLine=$(curl -s https://travelynx.de/api/v1/status/$API_KEY | jq .train.line | sed 's/"//' | sed 's/"//')
	echo "$trainLine" > /dev/null;
	toStation=$(curl -s https://travelynx.de/api/v1/status/$API_KEY | jq .toStation.name | sed 's/"//' | sed 's/"//')
	echo "$toStation" > /dev/null;
	arrivalTime=$(curl -s https://travelynx.de/api/v1/status/$API_KEY | jq .toStation.scheduledTime)
	echo "$arrivalTime" > /dev/null;	
	actualArrivalTime=$(curl -s https://travelynx.de/api/v1/status/$API_KEY | jq .toStation.realTime)
	echo "$actualArrivalTime" > /dev/null;
	arrivalTimeDate=$(date +%H:%M -d @$arrivalTime)
	actualArrivalTimeDate=$(date +%H:%M -d @$actualArrivalTime)
	
	nextStationTime=$(curl -s https://travelynx.de/api/v1/status/$API_KEY | jq .intermediateStops[].realArrival)
	nextStationName=$(curl -s https://travelynx.de/api/v1/status/$API_KEY | jq .intermediateStops[].name | sed 's/"//' | sed 's/"//')


	SAVEIFS=$IFS
	IFS=$'\n'
	nextStationTime=($nextStationTime)
	nextStationName=($nextStationName)
	IFS=$SAVEIFS

	if [ "$(date +%H:%M)" != "$notificationNextStopTime" ]
	then
		for (( i=0; i<${#nextStationTime[@]}; i++ ))
		do
		
			if [ "$(date +%H:%M)" == "$(date +%H:%M -d @${nextStationTime[$i]})" ] && [ "$NOTIFICATIONS_NEXT_STOP" == "true" ] && [ "$LANGUAGE" == "DE" ]
			then
				notify-send "Nächster Halt:" "${nextStationName[$i]} um $(date +%H:%M -d @${nextStationTime[$i]})" 
				notificationNextStopTime=$(date +%H:%M -d @${nextStationTime[$i]})
			fi

			if [ "$(date +%H:%M)" == "$(date +%H:%M -d @${nextStationTime[$i]})" ] && [ "$NOTIFICATIONS_NEXT_STOP" == "true" ] && [ "$LANGUAGE" == "EN" ]
			then
				notify-send "Next Stop:" "${nextStationName[$i]} at $(date +%H:%M -d @${nextStationTime[$i]})" 
				notificationNextStopTime=$(date +%H:%M -d @${nextStationTime[$i]})
			fi
		done
	fi


	# Checking if Arrival Time changed to send a new Notification
	if [ "$actualArrivalTime" -gt "$notificationLastDelay" ]
	then
		notificationDelaySent="false"
	fi

	# Checking if Arrival Time changed to send a new Notification
	if [ "$actualArrivalTime" != "$notificationLastDelay" ] && [ "$actualArrivalTime" != "$arrivalTime" ]
	then
		notificationDelaySent="false"
	fi

        
        # Sending a Notification if an ICE Train is delayed	
	if [ $isCheckedIn = "true" ] && [ "$actualArrivalTime" -gt "$arrivalTime" ] && [ $notificationDelaySent == "false" ] && [ $trainType == "ICE" ] && [ $NOTIFICATIONS_DELAY == "true" ] && [ $LANGUAGE == "DE" ]
	then
		notificationDelaySent="true"
		notify-send "Zugverspätung:" "Information zu $trainType $trainNo nach $toStation Ankuft heute $actualArrivalTimeDate anstatt $arrivalTimeDate"
	fi

        # Sending a Notification if an ICE Train is delayed	
	if [ $isCheckedIn = "true" ] && [ "$actualArrivalTime" -gt "$arrivalTime" ] && [ $notificationDelaySent == "false" ] && [ $trainType == "ICE" ] && [ $NOTIFICATIONS_DELAY == "true" ] && [ $LANGUAGE == "EN" ]
	then
		notificationDelaySent="true"
		notify-send "Delay:" "Information on $trainType $trainNo to $toStation arrival today $actualArrivalTimeDate instead of $arrivalTimeDate"
	fi

	# Sending a Notification if an IC Train is delayed
	if [ $isCheckedIn = "true" ] && [ "$actualArrivalTime" -gt "$arrivalTime" ] && [ $notificationDelaySent == "false" ] && [ $trainType == "IC" ] && [ $NOTIFICATIONS_DELAY == "true" ] && [ $LANGUAGE == "DE" ]
	then
		notificationDelaySent="true"
		notify-send "Zugverspätung:" "Information zu $trainType $trainNo nach $toStation Ankuft heute $actualArrivalTimeDate anstatt $arrivalTimeDate"
	fi

	# Sending a Notification if an IC Train is delayed
	if [ $isCheckedIn = "true" ] && [ "$actualArrivalTime" -gt "$arrivalTime" ] && [ $notificationDelaySent == "false" ] && [ $trainType == "IC" ] && [ $NOTIFICATIONS_DELAY == "true" ] && [ $LANGUAGE == "EN" ]
	then
		notificationDelaySent="true"
		notify-send "Delay:" "Information on $trainType $trainNo to $toStation arrival today $actualArrivalTimeDate instead of $arrivalTimeDate"
	fi

	# Sending a Notification if other (not ICE/IC) Train is	delayed
	if [ $isCheckedIn = "true" ] && [ "$actualArrivalTime" -gt "$arrivalTime" ] && [ $notificationDelaySent == "false" ] && [ $trainType != "ICE" ] && [ $trainType != "IC" ] && [ $NOTIFICATIONS_DELAY == "true" ] && [ $LANGUAGE == "DE" ] 
	then
		notificationDelaySent="true"
		notify-send "Zugverspätung:" "Information zu $trainType $trainLine nach $toStation Ankuft heute $actualArrivalTimeDate anstatt $arrivalTimeDate"
	fi


	# Sending a Notification if other (not ICE/IC) Train is	delayed
	if [ $isCheckedIn = "true" ] && [ "$actualArrivalTime" -gt "$arrivalTime" ] && [ $notificationDelaySent == "false" ] && [ $trainType != "ICE" ] && [ $trainType != "IC" ] && [ $NOTIFICATIONS_DELAY == "true" ] && [ $LANGUAGE == "EN" ]
	then
		notificationDelaySent="true"
		notify-send "Delay:" "Information on $trainType $trainLine to $toStation arrival today $actualArrivalTimeDate instead of $arrivalTimeDate"
	fi

	# Sending a Notification if the ICE Train is on time again
	if [ "$actualArrivalTime" -eq "$arrivalTime" ] && [ $notificationDelaySent == "true" ] && [ $isCheckedIn == "true" ] && [ $trainType == "ICE" ] && [ $NOTIFICATION_DELAY == "true" ] && [ $LANGUAGE == "DE" ]
	then
		notify-send "Information:" "Information zu $trainType $trainNo nach $toStation ist wieder pünktlich"	
	fi

	# Sending a Notification if the ICE Train is on time again
	if [ "$actualArrivalTime" -eq "$arrivalTime" ] && [ $notificationDelaySent == "true" ] && [ $isCheckedIn == "true" ] && [ $trainType == "ICE" ] && [ $NOTIFICATION_DELAY == "true" ] && [ $LANGUAGE == "EN" ]
	then
		notify-send "Information:" "Information on $trainType $trainNo to $toStation is on time again"	
	fi

	# Sending a Notification if the IC Train is on time again
	if [ "$actualArrivalTime" -eq "$arrivalTime" ] && [ $notificationDelaySent == "true" ] && [ $isCheckedIn == "true" ] && [ $trainType == "IC" ] && [ $NOTIFICATION_DELAY == "true" ] && [ $LANGUAGE == "DE" ]
	then
		notify-send "Information:" "Information zu $trainType $trainNo nach $toStation ist wieder pünktlich"	
	fi

	# Sending a Notification if the IC Train is on time again
	if [ "$actualArrivalTime" -eq "$arrivalTime" ] && [ $notificationDelaySent == "true" ] && [ $isCheckedIn == "true" ] && [ $trainType == "IC" ] && [ $NOTIFICATION_DELAY == "true" ] && [ $LANGUAGE == "EN" ]
	then
		notify-send "Information:" "Information on $trainType $trainNo to $toStation is on time again"	
	fi

	# Sending a Notification if a other (not ICE/IC) Train is on time again
	if [ "$actualArrivalTime" -eq "$arrivalTime" ] && [ $notificationDelaySent == "true" ] && [ $isCheckedIn == "true" ] && [ $trainType != "ICE" ] && [ $trainType != "IC" ] && [ $NOTIFICATION_DELAY = "true" ] && [ $LANGUAGE == "DE" ]
	then
		notify-send "Information:" "Information zu $trainType $trainLine nach $toStation ist wieder pünktlich"	
	fi

	# Sending a Notification if a other (not ICE/IC) Train is on time again
	if [ "$actualArrivalTime" -eq "$arrivalTime" ] && [ $notificationDelaySent == "true" ] && [ $isCheckedIn == "true" ] && [ $trainType != "ICE" ] && [ $trainType != "IC" ] && [ $NOTIFICATION_DELAY = "true" ] && [ $LANGUAGE == "EN" ]
	then
		notify-send "Information:" "Information on $trainType $trainLine ti $toStation is on time again"	
	fi
	
	# Saving the Delay from the latest Notification
	notificationLastDelay=$actualArrivalTime
	
	# Showing the Label for Polybar
	if [ $isCheckedIn == "true" ] && [ $trainType != "ICE" ] && [ $trainType != "IC" ] && [ $LANGUAGE == "DE" ] 
	then
		echo "$SYMBOL" $trainType $trainLine "nach" $toStation
	elif [ $isCheckedIn == "true" ] && [ $trainType != "ICE" ] && [ $trainType != "IC" ] && [ $LANGUAGE == "EN" ]
	then
		echo "$SYMBOL" $trainType $trainLine "to" $toStation
	elif [ $isCheckedIn == "true" ] && [ $trainType == "IC" ] && [ $LANGUAGE == "DE" ]
	then
		echo "$SYMBOL" $trainType $trainNo "nach" $toStation
	elif [ $isCheckedIn == "true" ] && [ $trainType == "IC" ] && [ $LANGUAGE == "EN" ]
	then
	        echo "$SYMBOL" $trainType $trainNo "to" $toStation
	elif [ $isCheckedIn == "true" ] && [ $trainType == "ICE" ] && [ $LANGUAGE == "DE" ]
	then
		echo "$SYMBOL" $trainType $trainNo "nach" $toStation
	elif [ $isCheckedIn == "true" ] && [ $trainType == "ICE" ] && [ $LANGUAGE == "EN" ]
	then
		echo "$SYMBOL" $trainType $trainNo "to" $toStation
	elif [ $isCheckedIn == "false" ] && [ $LANGUAGE == "EN" ]
	then
		echo "$SYMBOL"" not checked in"
                notificationDelaySent="false"
                notificationLastDelay=0
                notificationNextStopSent="true"
	else
		echo "$SYMBOL"" nicht eingecheckt"
		notificationDelaySent="false"
		notificationLastDelay=0
		notificationNextStopSent="true"
		notificationNextStopTime=""
	fi
	sleep $INTERVAL

done