Skip to content

Instantly share code, notes, and snippets.

@andys8
Last active Mar 17, 2020
Embed
What would you like to do?
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