Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
#!/bin/bash -e
# Options for curl(1), e.g., -vvv for verbose and -s for silent.
# Use HTTPS if it's available.
SCHEME=$(if curl -s "https://${SIS}" &>/dev/null; then echo "https"; else echo "http"; fi)
printf "\033[32m[*] Using ${SCHEME}.\033[0m\n" 1>&2
# Make a temporary file to store cookies in. If the temporary file contains an
# equals (=) character, it will mess up curl, so generate a new one.
TEMPCOOKIES=$(mktemp -t siscookies.XXXXX)
if [[ "${TEMPCOOKIES}" == *=* ]]; then
TEMPCOOKIES=$(TMPDIR=/tmp mktemp -t siscookies.XXXXX)
printf "\033[32m[*] Worked around problematic cookie jar filename.\033[0m\n" 1>&2
# Log in to SIS
printf "\033[32m[*] Logging in to SIS.\033[0m\n" 1>&2
curl ${CURLOPTS} \
--cookie "TESTID=set" \
--cookie-jar "${TEMPCOOKIES}" \
-F "sid=${RIN}" -F "PIN=${PIN}" \
"${SCHEME}://${SIS}/rss/twbkwbis.P_ValLogin" >/dev/null
# Get the terms for which grades are available
printf "\033[32m[*] Looking up most recent semester with available grades.\033[0m\n" 1>&2
curl ${CURLOPTS} \
--cookie "${TEMPCOOKIES}" \
--cookie-jar "${TEMPCOOKIES}" \
"${SCHEME}://${SIS}/rss/bwskogrd.P_ViewTermGrde" \
| grep -m 1 -Eo "<OPTION VALUE=\"[0-9]+\"" \
| cut -c 16-21
printf "\033[32m[*] Found semester ${SEMESTER}.\033[0m\n" 1>&2
# Request the grades for this term
curl ${CURLOPTS} \
--cookie "${TEMPCOOKIES}" \
--cookie-jar "${TEMPCOOKIES}" \
-F "term_in=${SEMESTER}" \
"${SCHEME}://${SIS}/rss/bwskogrd.P_ViewGrde" \
| grep -E "^(<TR>|<TD CLASS=\"dddefault\">)" \
| sed '1,21d' \
| sed -n -e :a -e '1,25!{P;N;D;};N;ba' \
| sed -e 's/ *<[^>]*> *//g'
# Delete the cookie jar
rm -f "${TEMPCOOKIES}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment