Skip to content

Instantly share code, notes, and snippets.

@vp777
Created April 22, 2019 15:29
Show Gist options
  • Save vp777/44f56245845e7db0253e0ce457592f11 to your computer and use it in GitHub Desktop.
Save vp777/44f56245845e7db0253e0ce457592f11 to your computer and use it in GitHub Desktop.
#! /bin/bash
#A script for automatic course registration through bannerweb.
#Last time this script was used was in 2016, so i expect to need some changes before it resurrects again.
base_url="https://bannerweb.server:9091"
user_agent="Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36"
cookies_file="./.ucycookies"
#cookies_file=`date +%s%N`
rm "$cookies_file" 2>/dev/null
user="username" #username
pwd="password" #password
courses=(13159) #courses
season=`date '+%Y%m'`
#season=201301
rest_time=0
attempt_interval=1 #seconds
attempt_interval_prod=1 #seconds
max_execution=600
debug_file=debug.html
function usage(){
echo "
Options:
-s seconds to begin
-m minutes to begin
-h hours to begin
-t exact time to begin
-x max execution time
-k signup attempt interval
-z manually set season
-a minutes to accelerate
-e acceleration attempt interval
"
exit 1
}
function timeToSecconds(){
local hours=`echo ${1%%:*}|sed 's/^0*//'`
local secconds=`echo ${1##*:}|sed 's/^0*//'`
local substract=${1#*:}
local minutes=`echo ${substract%:*}|sed 's/^0*//'`
local currentHour=`date '+%H'|sed 's/^0*//'`
local currentMinute=`date '+%M'|sed 's/^0*//'`
local currentSeccond=`date '+%S'|sed 's/^0*//'`
if [ "$currentHour:$currentMinute" == "$hours:$minutes" ]; then
sleep_time=0
return 1
fi
local destSecs=$((hours*3600+minutes*60+secconds))
local localSecs=$((currentHour*3600+currentMinute*60+currentSeccond))
sleep_time=$((destSecs-localSecs))
if [ $sleep_time -lt 0 ]; then
sleep_time=$(((24-currentHour)*3600-currentMinute*60-currentSeccond+destSecs))
fi
}
function getc(){
local sim_url="${base_url}/PRODel-CY/twbkwbis.P_GenMenu?name=homepage"
local sim_referer="${base_url}/PRODel-CY/twbkwbis.P_GenMenu?name=homepage"
curl -s -o/dev/null --cookie-jar "$cookies_file" -A "$user_agent" --referer "$sim_referer" "$sim_url"
local sim_url="${base_url}/PRODel-CY/twbkwbis.P_WWWLogin"
local sim_referer="${base_url}/PRODel-CY/twbkwbis.P_GenMenu?name=homepage"
curl -s -o/dev/null --cookie-jar "$cookies_file" -A "$user_agent" --referer "$sim_referer" "$sim_url"
}
function login(){
local signin_url="${base_url}/PRODel-CY/twbkwbis.P_ValLogin"
local signin_referer="${base_url}/PRODel-CY/twbkwbis.P_WWWLogin"
local signin_d1="sid=$user"
local signin_d2="PIN=$pwd"
local signin_text="WELCOME"
curl -s -o/dev/null --cookie-jar "$cookies_file" --cookie "$cookies_file" -A "$user_agent" --referer "$signin_referer" --data-urlencode "$signin_d1" --data-urlencode "$signin_d2" "$signin_url"
}
function am_i_in(){
local check_url="${base_url}/PRODel-CY/twbkwbis.P_GenMenu?name=bmenu.P_StuMainMnu"
local check_referer="${base_url}/PRODel-CY/twbkwbis.P_GenMenu?name=bmenu.P_GenMnu"
local check_txt="twbkwbis.P_GenMenu?name=bmenu.P_RegMnu"
resp=`curl -s --cookie-jar "$cookies_file" --cookie "$cookies_file" -A "$user_agent" --referer "$check_referer" "$check_url"`
found_txt=`echo "$resp"|grep -i "$check_txt"`
found_txt=${#found_txt}
}
function getin(){
am_i_in
wait
if [ "$found_txt" -eq 0 ]; then
echo "Login at: `date '+%H:%M:%S'`"
getc
wait
login
wait
fi
}
function signup1(){
local signup_url="${base_url}/PRODel-CY/bwskfreg.P_AltPin1"
local signup_referer="${base_url}/PRODel-CY/bwskfcls.P_GetCrse"
local signup_txt=""
local signup_d="crn=dummy&rsts=dummy&TERM_IN=$season&sel_crn=dummy&assoc_term_in=dummy&ADD_BTN=dummy"
local signup_regstr="sel_crn=_math_+$season&assoc_term_in=$season"
for i in "${courses[@]}"; do signup_d="$signup_d&${signup_regstr/_math_/$i}"; done
signup_d="$signup_d&ADD_BTN=%CE%95%CE%B3%CE%B3%CF%81%CE%B1%CF%86%CE%AE"
curl -s --cookie-jar "$cookies_file" -o/dev/null --cookie "$cookies_file" -d "p_calling_proc=P_CrseSearch&p_term=$season" -A "$user_agent" --referer "${base_url}/PRODel-CY/bwskfcls.p_sel_crse_search" "${base_url}/PRODel-CY/bwckgens.p_proc_term_date"
curl -s --cookie-jar "$cookies_file" -o"$debug_file" --cookie "$cookies_file" -d "$signup_d" -A "$user_agent" --referer "$signup_referer" "$signup_url"
}
function signup2(){
local signup_url="${base_url}/PRODel-CY/bwckcoms.P_Regs"
local signup_referer="${base_url}/PRODel-CY/bwckcoms.P_Regs"
local signup_txt=""
local signup_d="term_in=$season&RSTS_IN=DUMMY&assoc_term_in=DUMMY&CRN_IN=DUMMY&start_date_in=DUMMY&end_date_in=DUMMY&SUBJ=DUMMY&CRSE=DUMMY&SEC=DUMMY&LEVL=DUMMY&CRED=DUMMY&GMOD=DUMMY&TITLE=DUMMY&MESG=DUMMY&REG_BTN=DUMMY"
local signup_regstr="RSTS_IN=RW&CRN_IN=_math_&assoc_term_in=&start_date_in=&end_date_in="
for i in `seq 0 9`; do signup_d="$signup_d&${signup_regstr/_math_/${courses[$i]}}"; done
signup_d="$signup_d&regs_row=0&wait_row=0&add_row=10&REG_BTN=%CE%9A%CE%B1%CF%84%CE%B1%CF%87%CF%8E%CF%81%CE%B7%CF%83%CE%B7"
curl -s --cookie-jar "$cookies_file" -o"$debug_file" --cookie "$cookies_file" -d "$signup_d" -A "$user_agent" --referer "$signup_referer" "$signup_url"
}
secconds=0
minutes=0
hours=0
time_to_start=""
while getopts "s:m:h:t:x:k:z:a:e:" OPTION
do
case $OPTION in
s)secconds=$OPTARG;;
m)minutes=$OPTARG;;
h)hours=$OPTARG;;
t)time_to_start=$OPTARG;;
x)max_execution=$OPTARG;;
k)attempt_interval=$OPTARG;;
z)season=$OPTARG;;
a)rest_time=$OPTARG;;
e)attempt_interval_prod=$OPTARG;;
?)usage;;
esac
done
if [ -n "$time_to_start" ]; then
timeToSecconds $time_to_start
else
sleep_time=$((hours*3600+minutes*60+secconds))
fi
echo -e "Max execution: $max_execution minutes"
echo -e "Try every: $attempt_interval seconds initially"
echo -e "User: $user"
echo -e "Accelerate in: $rest_time minutes, $attempt_interval_prod s/t"
echo -e "season: $season\n\n"
echo "Executed at: `date '+%H:%M:%S'`"
echo "Sleeping $sleep_time seconds"
sleep $sleep_time 2>/dev/null
total_execution_time=0
method=1
while [ $total_execution_time -lt $((max_execution*60)) ]; do
echo "Start at: `date '+%H:%M:%S'`"
if [ $((rest_time*60)) -le $total_execution_time ]; then
rest_time=$max_execution
attempt_interval=$attempt_interval_prod
echo "Turbo time : Try every $attempt_interval_prod seconds"
fi
stime=`date +%s`
getin
method=$((method%3))
#every 3 attempts, it's expected that 2 will be performed through signup2, which is faster
[ $method -eq 1 ] && signup1
[ $method -ne 1 ] && signup2
wait
method=$((method+1))
etime=`date +%s`
sleep_time=$((attempt_interval-etime+stime))
sleep $sleep_time 2>/dev/null
total_execution_time=$((total_execution_time+`date +%s`-stime)) #i.e. $SECONDS
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment