-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtramTracker
executable file
·119 lines (105 loc) · 4.19 KB
/
tramTracker
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
#!/bin/bash
tram() {
# initialisation
unset schedules
db=("" "lille flandres" "lille europe" "romarin" "botanique" "saint maur" "buisson" "brossolette" "clemenceau hipp." "croise laroche" "foch" "le quesne" "cerisaie" "chateau rouge" "cartelot" "grand cottignies" "triez" "trois suisses" "faidherbe" "ma campagne" "pont hydraulique" "victoire" "tourcoing centre" "acacias" "pont wasquehal" "la terrasse" "pave de lille" "le sart" "planche epinoy" "la marque" "villa cavrois" "bol d'air" "parc barbieux" "hopital v.provo" "jean moulin" "alfred mongy" "euroteleport")
readarray -t currenttime <<< $(tr ':' '\n' <<< $(date +%T))
if [[ -z "$1" || -z "$2" || "$1" == "-h" || "$1" == "--help" ]]; then
tramhelp
return
fi
# analysing input
case "$1" in
"tourcoing" | "t") direction="tourcoing centre";;
"lille" | "l") direction="lille flandres";;
*) tramhelp; return
esac
for ((i=1; i < 36; i++)); do
station=$(grep -i "${*:2}" <<< "${db[i]}")
if [[ -n "$station" ]]; then
break
fi
done
if [[ -z "$station" ]]; then tramhelp; return; fi
# sending request
echo -e "\n\e[35m> $station > $direction\e[0m\n"
station=$(printf '%s' "$station" | tr ' ' '+' | tr '[:lower:]' '[:upper:]')
direction=$(printf '%s' "$direction" | tr ' ' '+' | tr '[:lower:]' '[:upper:]')
passages=$(curl -s "https://opendata.lillemetropole.fr/api/records/1.0/search/?dataset=ilevia-prochainspassages&timezone=CET&refine.nomstation=$station&refine.sensligne=$direction")
readarray -t departures <<< $(echo "$passages" | jq -r .records[].fields.heureestimeedepart)
readarray -t updatedTemp <<< $(echo "$passages" | jq -r .records[].fields.datemodification)
monthDay=$(date +%m-%d)
for ((i=0; i < "${#departures[@]}"; i++)); do
schedules[i]="${departures[i]:11:-6}"
updated[i]="${updatedTemp[i]:5:-6}"
if [[ "${updated[i]:0:5}" == "$monthDay" ]]; then
updated[i]="${updated[i]:6:-3}"
else
updated[i]="${updated[i]/T/ }"
fi
done
# watchdog
if [[ -z "$schedules" ]]; then
echo "Service down or wrong station name"
return
fi
# analysing / exploiting response
for ((i=0; i < "${#schedules[@]}"; i++)); do
readarray -t tramtime <<< $(tr ':' '\n' <<< "${schedules[i]}")
currentsec=$(("${currenttime[0]#0} * 3600 + ${currenttime[1]#0} * 60 + ${currenttime[2]#0}"))
tramsec=$(("${tramtime[0]#0} * 3600 + ${tramtime[1]#0} * 60 + ${tramtime[2]#0}"))
if (( "$tramsec" < "$currentsec" )); then
((tramsec+=86400))
fi
deltasec=$(("$tramsec" - "$currentsec"))
if (( "$deltasec" > 43200 )); then
echo -e "\e[31m> ${schedules[i]} \e[0m•\e[31m Alredy happened\e[0m"
else
minutes=$(("$deltasec" / 60))
seconds=$(("$deltasec" % 60))
if (( "$minutes" < 10 )); then
minutes="0$minutes"
fi
if (( "$seconds" < 10 )); then
seconds="0$seconds"
fi
echo -e "\e[33m> ${schedules[i]}\e[0m • \e[32mIn $(tput bold)$minutes:$seconds$(tput sgr0) \e[34m(since ${updated[i]})\e[0m"
fi
done
echo
}
tramhelp() {
echo
echo "usage : tram <direction> <station>"
echo
echo " lille flandres | 1"
echo " lille europe | 2"
echo " romarin | 3"
echo " botanique | 4"
echo " saint maur | 5"
echo " buisson | 6"
echo " brossolette | 7"
echo " clemenceau hipp. | 8"
echo " croise laroche | 9"
echo " ___|___"
echo " | |"
echo " foch | 10 23 | acacias"
echo " le quesne | 11 24 | pont wasquehal"
echo " cerisaie | 12 25 | la terrasse"
echo " chateau rouge | 13 26 | pave de lille"
echo " cartelot | 14 27 | le sart"
echo "grand cottignies | 15 28 | planche epinoy"
echo " triez | 16 29 | la marque"
echo " trois suisses | 17 30 | villa cavrois"
echo " faidherbe | 18 31 | bol d'air"
echo " ma campagne | 19 32 | parc barbieux"
echo "pont hydraulique | 20 33 | hopital v.provo"
echo " victoire | 21 34 | jean moulin"
echo "tourcoing centre | 22 35 | alfred mongy"
echo " 36 | euroteleport"
echo
echo "directions :"
echo " - lille / l"
echo " - tourcoing / t"
echo
}