In Anlehnung an mein Skript zur Webseitenüberwachung, das ich sehr erfolgreich einsetze, kommt hier noch eine Sonderedition für die Verfolgung eines Zuges der DB.
Die aktuellen Verspätungen werden per Mail versendet, sobald sie sich ändern. Etwas tricky war die Entfernung der aktuellen Uhrzeit aus der Seite, sodass nicht minütlich versendet wird, sondern tatsächlich nur bei Neuigkeiten.
Ein solcher Service stünde der Bahn gut zu Gesicht (Mailadressen zum Datensammeln werden ja fleißig abgefragt bei der Bestellung). Drauf gekommen ist aber scheinbar noch keiner…
(Lizenz: GPLv3)
#!/bin/bash
# settings
date="28.02.11"
trainType="2" # 1=ICE, 2=EC/IC, 3=IR/D, 4=RE/RB
trainID="IC 428"
toAddr="mail@domain.de"; # to address for the mail
function wgetDownload () {
wget --post-data="productClassFilter=${trainType}&date=${date}&trainname=${trainID}&stationFilter=80" "http://mobile.bahn.de/bin/mobil/trainsearch.exe/dox?ld=96236&rt=1&use_realtime_filter=1&" --base="http://mobile.bahn.de/" --output-document "new.content";
}
function cleanNewFile () {
perl -i -pe 'BEGIN{undef $/;} s/.tr class="current".*?aktuelle Zeit.*?.tr..//smg' new.content
}
echo "Checking ${trainID}...";
# generate URL's SHA1 sum and pushd to that dir
urlhash=`echo "$URL" | sha1sum | sed 's/^\(\w*\)\s*\(.*\)/\1/'`;
echo $urlhash;
mkdir -p $urlhash;
pushd $urlhash > /dev/null;
if [ ! -f old.content ]
then
# if no file there, wget freshly
echo "No old file found.";
wgetDownload
cp "new.content" "old.content";
fi
#echo "Old file found.";
# else move old file, wget freshly
rm "old.content";
mv "new.content" "old.content";
wgetDownload
cleanNewFile
# compare SHA1 sums of both files
oldhash=`sha1sum "old.content" | sed 's/^\(\w*\)\s*\(.*\)/\1/'`;
newhash=`sha1sum "new.content" | sed 's/^\(\w*\)\s*\(.*\)/\1/'`;
echo $newhash" vs. "$oldhash;
# if not the same, send mail
if [ $newhash != $oldhash ]
then
diff old.content new.content > diff.file;
mailcontent='Content-Type: text/html; charset="iso8859-1"
'`cat new.content`;
echo "$mailcontent" | mail -s "News for ${trainID} (at "`date`")" "$toAddr";
fi
popd > /dev/null;