Skip to content

Instantly share code, notes, and snippets.

@breunigs
Created August 14, 2013 10:37
Show Gist options
  • Save breunigs/6229889 to your computer and use it in GitHub Desktop.
Save breunigs/6229889 to your computer and use it in GitHub Desktop.
utility to look up public transport connections directly using a shell or launcher like dmenu.
#!/bin/bash
# Example usage:
# ./bahn berlin_hbf frankfurt_hbf
# ./bahn berlin_hbf frankfurt_hbf @23h23 localonly # arrive at 23h23 in two days, not using “I’m broke” mode (no EC,IC,ICE)
# bahn v0.2.1 (2012-03-16)
#
# 0.2.1 fix automatic selection of tomorrow if time given has
# already passed
# 0.2 times may be written like '13h37'
# new parameter 'localonly' to exclude EC, IC, ICE
# be more intelligent about the given time: detect if
# 'tomorrow at that time' is meant and select
# tomorrow if no date is given.
# 0.1 initial release
if (( "$#" < 2 )); then
echo "USAGE: bahn from to [[@][h]h:mm] [+n|yyyy-mm-dd] [localonly]"
echo
echo "If time [h]h:mm is omitted 'now' is assumed."
echo "Prepend an '@' if you want to specfiy 'time"
echo "of arrival'. If not it's 'time of depature'."
echo
echo "If date is omitted 'today' is assumed. You"
echo "can either specify a full date in the 'yyyy"
echo "-mm-dd' format or use '+n' for n days from"
echo "today, e.g. +1 for tomorrow."
echo
echo "Add localonly as last argument if you want"
echo "to exclude EC, IC and ICE connections."
echo
echo "For your convenience underscores will be re-"
echo "placed with spaces. This will be useful when"
echo "using bahn with e. g. dmenu, which does not"
echo "support grouping with quotes."
exit
fi
echo
start=${1//_/ }
dest=${2//_/ }
echo "starting station: ${start}"
echo "destination station: ${dest}"
# see if any of the arguments is 'local only' and remove it from
# the input args since it may appear anywhere
if [ "${!#}" = "localonly" ]; then
localonlyurl="&REQ0JourneyProduct_prod_list=4:0001111111000000"
echo "excluded trains: EC, IC, ICE"
fi
# extract time information
if [[ "${3:0:1}" == "@" ]] ; then
timesel="arrive"
time="${3:1:5}"
timedesc="time of arrival: "
else
timesel="depart"
time="${3:0:5}"
timedesc="time of departure: "
fi
# remove the 'h' in time, if it was used
time=${time//h/:}
# syntax check input. Otherwise bahn.de will annoy the user
# with an extra step.
if [[ $time =~ ^[0-9]{1,2}[:h][0-9]{2}$ ]]; then
echo "${timedesc} ${time}"
timeurl="&time=${time}"
else
# do not whine about no input or 'localonly'
if [[ ${#time} > 0 ]] && [[ "${time}" -ne "localonly" ]]; then
echo "WARNING:"
echo "Could not parse given time; it should be"
echo "in the [@][h]h:mm format. Your input was:"
echo $3
fi
fi
# find correct date, if there is any input
if [[ ${#4} > 0 ]] && [[ "$4" -ne "localonly" ]]; then
if [[ $4 =~ ^20[0-9]{2}-[0-9]{2}-[0-9]{2}$ ]]; then
dateurl="&date=${4:8:2}.${4:5:2}.${4:0:4}"
echo "date of travel: ${4:0:4}-${4:5:2}-${4:8:2}"
elif [[ $4 =~ ^\+[0-9]+$ ]]; then
d=$(date --date "${4} days" +%d.%m.%Y)
dateurl="&date=${d}"
d=$(date --date "${4} days" +%Y-%m-%d)
echo "date of travel: ${d}"
else
echo "WARNING:"
echo "Could not parse given date; it should be"
echo "in either of the following formats:"
echo "\tyyyy-mm-dd"
echo "\t+n"
echo "Your input was:"
echo ${4}
fi
# if a time was given but no date has been specified and the time
# is in the past (e.g. 9:00 is given, but it’s 11:30 already) then
# automatically set to search for the next day.
# Note the -gt: For some reason > doesn’t work here.
elif [[ ${#timeurl} > 0 ]] && [[ $(( $(date "+%s") - $(date --date="$time" "+%s") )) -gt 0 ]]; then
d=$(date --date "1 days" +%d.%m.%Y)
dateurl="&date=${d}"
d=$(date --date "1 days" +%Y-%m-%d)
echo "date of travel: ${d} (assuming you meant tomorrow)"
fi
url="http://reiseauskunft.bahn.de/bin/query.exe/dn?start=1&S=${start}&Z=${dest}&timesel=${timesel}&optimize=1&tariffTravellerType.1=E&tariffTravellerReductionClass.1=0&tariffTravellerAg${timeurl}${dateurl}${localonlyurl}"
# very simple way to encode spaces, if there are any. Still won’t
# output correctly encoded URL if start or destination contain
# non URL chars.
url=${url// /%20}
echo
echo
echo "${url}"
echo
echo "Opening browser…"
x-www-browser "${url}"
echo
echo "Sang ju vor trevelling wifs deutsche bahn."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment