Bon Voyage! (german railway edition)

1 07 2011

So. This might be especially helpful to our german friends who quickly want to know when the next train goes from A to B. Or when the trains go on a special date.
Opens your favourite browser with the travelling-information-page without the need to klick a lot before.
Usage:
$ bahnanfrage start ziel [nv]
or
$ bahnanfrage start ziel dd.mm.yy hh:mm [nv]
The last parameter nv is optional, meaning that no ICE or IC trains are to be used (Nur Nahverkehr).

#!/bin/bash
# bahnanfrage - short bash tool to launch browser with right data feed
## Help
if [ "$1" = "-h" ]
then echo "bahnanfrage start ziel [nv]
	or
bahnanfrage start ziel dd.mm.yy hh:mm [nv] "
exit 1
fi
## Variables
browser=firefox
von=$1
nach=$2
if [ "$3" = "nv"  ] || [ "$3" = ""  ]
then
	wochentag=$( date +%a)
	datum=$(date +%d.%m.%y)
            zeit=$(date +%H:%M)
	if [ "$3" = "nv"   ]
		then
		        nahverkehr="&journeyProducts=1016"
		else
		        nahverkehr=""
	fi

else
	wochentag=$( date -d $(echo $5-$4-$3) +%a)
	datum=$3
	zeit=$4
	if [ "$5" = "nv"   ]
	then    
	        nahverkehr="&journeyProducts=1016"
	else
	        nahverkehr=""
	fi  
fi
$browser 'http://reiseauskunft.bahn.de/bin/query.exe/dn?revia=yes&existOptimizePrice=1&country=DEU&initialAvail=ON&dbkanal_007=L01_S01_D001_KIN0001_qf-bahn_LZ003&start=1&S='${von}'&REQ0JourneyStopsSID=&Z='${nach}'&REQ0JourneyStopsZID=&trip-type=single&date='${wochentag}'%2C+'${datum}'&time='${zeit}'&timesel=depart&returnTimesel=depart&optimize=0&travelProfile=-1&adult-number=1&children-number=0&infant-number=0&tariffTravellerType.1=E&tariffTravellerReductionClass.1=0&tariffTravellerAge.1=&qf-trav-bday-1=&tariffClass=2'$nahverkehr