Skip to content

Instantly share code, notes, and snippets.

@andys8
Last active March 17, 2020 12:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andys8/86cbc65ed90d1f9c6aaab83ff34c2862 to your computer and use it in GitHub Desktop.
Save andys8/86cbc65ed90d1f9c6aaab83ff34c2862 to your computer and use it in GitHub Desktop.
mvg
#!/bin/bash
# Request of <https://www.mvg.de/dienste/verbindungen.html>
set -eu pipefail
url="https://www.mvg.de/api/fahrinfo/routing/?fromStation=de:09162:2&toStation=de:09179:6180&transportTypeBus=false&transportTypeUnderground=false&transportTypeBoat=false&transportTypeTram=false&transportTypeCable=false&changeLimit=0&sapTickets=true"
json=$(curl -s "$url" || '')
if [[ $json == "" ]]; then
echo "$(tput setaf 3)Failed$(tput sgr 0)"
exit 1
fi
toDate="ts => new Date(ts)"
toTime="d => d.getHours() + ':' + d.getMinutes()"
departure=$(echo "$json" | fx ".connectionList[0].departure" "$toDate" "$toTime")
echo "$(tput setaf 2)$departure$(tput setaf 8) (departure)$(tput sgr 0)"
# Delay
delay=$(echo "$json" | fx ".connectionList[0].connectionPartList[0].delay")
delayMin="+${delay} min"
if [[ $delay != "0" ]]; then
echo "$(tput setaf 3)$delayMin$(tput sgr 0)"
fi
# Notifications
toText="(ns = []) => ns.map(n => n.title + ': ' + n.description).join('\n\n')"
notifcations=$(echo "$json" | fx ".connectionList[0].connectionPartList[0].notifications" "$toText")
if [[ $notifcations != "" ]]; then
echo "$(tput setaf 9)$notifcations$(tput sgr 0)"
fi
# Desktop notification
notify-send "$departure" "$delayMin"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment