You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

40 lines
1.9 KiB

#!/bin/bash
# author: phga
# date: 2020-07-28
# desc: Script to fetch grades from primuss...Not more, not less
DEST="https://www3.primuss.de/cgi-bin/login/index.pl?FH=fhin"
NOTEN="https://www3.primuss.de/cgi-bin/pg_Notenbekanntgabe/index.pl"
SHIBBLE="https://www3.primuss.de/Shibboleth.sso/SAML2/POST"
FU="https://www3.primuss.de/cgi-bin/pg_Notenbekanntgabe/showajax.pl"
USERNAME=phg0107
PASSWORD=lelelele
TMPPATH=$(mktemp -d)
FINALFILE=/some/path/to/result.txt
COOKIES=$TMPPATH/.cookies
HEADERS=$TMPPATH/.headers
curl -s -L -c $COOKIES -D $HEADERS "$DEST" > /dev/null
LOGIN=$(cat $HEADERS | rg -o -e "Location: (.*)" -r '$1' | tail -1 | sed 's/\;.*?/?/')
# Remove trailing \r
LOGIN=${LOGIN%$'\r'}
# This was quite helpful to look at the POST data and headers sent by client
# --trace-ascii stdout
# The param format and -d option were super crucial for this shit to work
VALS=($(curl -s -b $COOKIES -c $COOKIES -d "j_username=$USERNAME&j_password=$PASSWORD&_eventId_proceed=" $LOGIN | rg -o -e 'value="(.*)"' -r '$1'))
RELAYSTATE=$(echo ${VALS[0]} | sed 's/:/%3A/g')
# OIDDDAAAA die Plus-Zeichen eyyyy >:(
SAMLRESP=$(echo ${VALS[1]} | sed 's/=/%3D/g' | sed 's/+/%2B/g')
SESSION=$(curl -L -s -b $COOKIES -c $COOKIES -d "RelayState=$RELAYSTATE&SAMLResponse=$SAMLRESP" $SHIBBLE | rg -o -e 'name="Session" value="(.*)"' -r '$1' | head -1)
AJAXDATA=$(curl -s -b $COOKIES -c $COOKIES -d "User=$USERNAME&Session=$SESSION&Language=de&FH=fhin&Portal=1" $NOTEN | rg -o -e 'data: *"(.*)"' -r '$1' | head -1)
# Finally...
cp $FINALFILE $TMPPATH
curl -s -b $COOKIES -c $COOKIES -d $AJAXDATA $FU | awk 'f{ if (/<\/tbody>/){printf "%s", buf; f=0; buf=""} else buf = buf $0 ORS}; /<tbody>/{f=1}' | rg -o -e '<b>(.*)</b>' -r '$1' | sudo -u http tee $FINALFILE
# If there is a change in the file, send an email
D=$(diff $FINALFILE $TMPPATH/noten.html)
if [ "$D" != "" ]; then
cat $FINALFILE | mail -s "NEUE NOTEN" meine@mail.de
fi
rm -rf $TMPPATH