Last active
October 17, 2018 11:35
-
-
Save hfossli/ee5b3310fc9f714c59aa5a91e9e596cf to your computer and use it in GitHub Desktop.
Get notified when new booking hours is available at Politihuset Grønland or Politihuset Sandvika
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
first_is_earlier() { | |
y1=$(date -jf '%d.%m.%y%y' "$1" +'%Y' 2> /dev/null) | |
y2=$(date -jf '%d.%m.%y%y' "$2" +'%Y' 2> /dev/null) | |
m1=$(date -jf '%d.%m.%y%y' "$1" +'%m' 2> /dev/null) | |
m2=$(date -jf '%d.%m.%y%y' "$2" +'%m' 2> /dev/null) | |
d1=$(date -jf '%d.%m.%y%y' "$1" +'%d' 2> /dev/null) | |
d2=$(date -jf '%d.%m.%y%y' "$2" +'%d' 2> /dev/null) | |
if [ $y2 -gt $y1 ] ; then | |
return 0 | |
fi | |
if [ $m2 -gt $m1 ] ; then | |
return 0 | |
fi | |
if [ $d2 -gt $d1 ] ; then | |
return 0 | |
fi | |
return 1 | |
} | |
test_first_is_earlier() { | |
if first_is_earlier $1 $2; then | |
echo "$1 IS earlier than $2"; | |
else | |
echo "$1 IS NOT earlier than $2"; | |
fi | |
} | |
# day | |
echo "" && echo "day IS earlier" | |
test_first_is_earlier 05.05.2018 21.05.2018 | |
test_first_is_earlier 15.05.2018 21.05.2018 | |
echo "" && echo "day IS NOT earlier" | |
test_first_is_earlier 21.05.2018 21.05.2018 | |
test_first_is_earlier 22.05.2018 21.05.2018 | |
test_first_is_earlier 22.05.2018 15.05.2018 | |
echo "" && echo "month IS earlier" | |
test_first_is_earlier 21.04.2018 21.05.2018 | |
test_first_is_earlier 21.01.2018 21.05.2018 | |
test_first_is_earlier 21.02.2018 21.12.2018 | |
echo "" && echo "month IS NOT earlier" | |
test_first_is_earlier 21.05.2018 21.05.2018 | |
test_first_is_earlier 21.06.2018 21.05.2018 | |
test_first_is_earlier 21.11.2018 21.01.2018 | |
echo "" && echo "year IS earlier" | |
test_first_is_earlier 21.05.2018 21.05.2019 | |
test_first_is_earlier 21.05.2018 21.05.2020 | |
test_first_is_earlier 21.05.2018 21.05.2022 | |
echo "" && echo "year IS NOT earlier" | |
test_first_is_earlier 21.05.2018 21.05.2018 | |
test_first_is_earlier 21.05.2019 21.05.2018 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28.05.2018 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
CLEAR='\033[0m' | |
RED='\033[0;31m' | |
YELLOW='\033[38;5;208m' | |
ORANGE='\033[0;33m' | |
GREEN='\033[0;32m' | |
LIGHTGREY='\033[38;5;244m' | |
GREY='\033[38;5;237m' | |
function welcome() { | |
if [ -z "$QUIET" ]; then | |
printf """$LIGHTGREY\ | |
__ _ | |
____ ____ ___________/ /_(_)___ ___ ___ Get notified when new | |
/ __ \/ __ \`/ ___/ ___/ __/ / __ \`__ \/ _ \\ booking hours is available | |
/ /_/ / /_/ (__ |__ ) /_/ / / / / / / __/ at Politihuset Grønland or | |
/ .___/\__,_/____/____/\__/_/_/ /_/ /_/\___/ Politihuset Sandvika | |
/_/ | |
$CLEAR""" | |
fi | |
} | |
function usage() { | |
if [ -n "$1" ]; then | |
echo -e "${RED}👉 $1${CLEAR}\n"; | |
fi | |
echo "Usage: $0 [-n number-of-people] [-s section-id] [-c cache-file]" | |
echo " -n, --number-of-people The number of people" | |
echo " -s, --section-id Which Police station are you going to? (1 => Grønland, 2 => Sandvika)" | |
echo " -c, --cache-file A path for a cache file to store the date" | |
echo " -q, --quiet Only print available date" | |
echo "" | |
echo "Example: $0 --number-of-people 2 --section-id 1 --cache-file last-known-date.txt" | |
exit 1 | |
} | |
welcome | |
# parse params | |
while [[ "$#" > 0 ]]; do case $1 in | |
-n|--number-of-people) NUMBER_OF_PEOPLE="$2"; shift;shift;; | |
-s|--section-id) SECTION_ID="$2";shift;shift;; | |
-c|--cache-file) CACHED_DATE_FILE="$2";shift;shift;; | |
-q|--quiet) QUIET=1;shift;; | |
*) usage "Unknown parameter passed: $1"; shift; shift;; | |
esac; done | |
# verify params | |
if [ -z "$NUMBER_OF_PEOPLE" ]; then usage "Number of people is not set"; fi; | |
if [ -z "$SECTION_ID" ]; then usage "Section id is not set."; fi; | |
if [ -z "$CACHED_DATE_FILE" ]; then usage "Cache file for storing the last known date is not set. Needed in order to compare and be certain that a booking has been cancelled."; fi; | |
function output_start() { | |
if [ -z "$QUIET" ]; then echo -ne "\r${GREY}[~] $@...${CLEAR}"; fi | |
} | |
function output_success() { | |
if [ -z "$QUIET" ]; then echo -e "\r${GREEN}[+] $@ (SUCCESS)${CLEAR}"; fi | |
} | |
function output_failed() { | |
echo -e "\r${RED}[!] $@ (FAILED)${CLEAR}"; | |
exit 1; | |
} | |
clean_up() { | |
rm $COOKIE_FILE 2> /dev/null | |
rm $TEMP_FILE 2> /dev/null | |
} | |
fatal_error() { | |
clean_up; | |
echo $1; | |
exit 1; | |
} | |
echo_request_id() { | |
cat $1 | grep '<input name="__RequestVerificationToken" type="hidden" value="' | sed -n -e 's/.*__RequestVerificationToken" type="hidden" value="//p' | sed -n -e 's/".*//p' | head -1 | |
} | |
assert_valid_req_id() { | |
if [ ${#1} = "172" ] ; then | |
output_success "$2" | |
else | |
output_failed "$2" | |
fi | |
} | |
first_date_is_earlier() { | |
y1=$(date -jf '%d.%m.%y%y' "$1" +'%Y' 2> /dev/null) | |
y2=$(date -jf '%d.%m.%y%y' "$2" +'%Y' 2> /dev/null) | |
m1=$(date -jf '%d.%m.%y%y' "$1" +'%m' 2> /dev/null) | |
m2=$(date -jf '%d.%m.%y%y' "$2" +'%m' 2> /dev/null) | |
d1=$(date -jf '%d.%m.%y%y' "$1" +'%d' 2> /dev/null) | |
d2=$(date -jf '%d.%m.%y%y' "$2" +'%d' 2> /dev/null) | |
if [ $y2 -gt $y1 ] ; then | |
return 0 | |
fi | |
if [ $m2 -gt $m1 ] ; then | |
return 0 | |
fi | |
if [ $d2 -gt $d1 ] ; then | |
return 0 | |
fi | |
return 1 | |
} | |
partial_curl() { | |
curl \ | |
-H 'Origin: https://pass.oslo.politi.no' \ | |
-H 'Host: pass.oslo.politi.no' \ | |
-H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8' \ | |
-H 'Accept-Language: nb-no' \ | |
-H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/604.5.6 (KHTML, like Gecko) Version/11.0.3 Safari/604.5.6' \ | |
-H 'Referer: https://pass.oslo.politi.no/Booking/Booking/Index/Pass' \ | |
--compressed 'https://pass.oslo.politi.no/Booking/Booking/Index/Pass' \ | |
--silent -L \ | |
-c $COOKIE_FILE -b $COOKIE_FILE \ | |
-o $TEMP_FILE \ | |
$@ | |
} | |
COOKIE_FILE=$(mktemp) | |
TEMP_FILE=$(mktemp) | |
output_start "Start session" | |
partial_curl | |
REQ_ID=`echo_request_id $TEMP_FILE` | |
assert_valid_req_id $REQ_ID "Start session" | |
output_start "Start booking" | |
rm $TEMP_FILE 2> /dev/null | |
partial_curl \ | |
--data "__RequestVerificationToken=$REQ_ID&FormId=1&ServiceGroupId=1&NextButtonID20=Booke+time" \ | |
--compressed 'https://pass.oslo.politi.no/Booking/Booking/Next/Pass' \ | |
-o $TEMP_FILE | |
REQ_ID=`echo_request_id $TEMP_FILE` | |
assert_valid_req_id $REQ_ID "Start booking" | |
output_start "Accept terms of agreement" | |
rm $TEMP_FILE 2> /dev/null | |
partial_curl \ | |
--data "__RequestVerificationToken=$REQ_ID&AgreementText=Samtykke+for+lagring+av+personopplysninger.%0D%0A%0D%0AFor+%C3%A5+kunne+bestille+elektronisk+time+m%C3%A5+du+samtykke+til+at+navn%2C%0D%0Ae-postadresse+og+mobiltelefonnummer+lagres.%0D%0ADine+opplysninger+blir+automatisk+slettet+dagen+etter+at+du+har+v%C3%A6rt+hos+oss.%0D%0A%0D%0AJeg+samtykker+til+at+navn%2C+e-postadresse+og+mobiltelefonnummer+lagres.%0D%0A%0D%0AVelg+antall+personer+%28ogs%C3%A5+barn%29+som+bestillingen+gjelder+%28hvor+mange+som+skal+ha+pass%29.&AcceptInformationStorage=true&AcceptInformationStorage=false&NumberOfPeople=$NUMBER_OF_PEOPLE&Next=Neste" \ | |
--compressed 'https://pass.oslo.politi.no/Booking/Booking/Next/Pass' \ | |
-o $TEMP_FILE | |
REQ_ID=`echo_request_id $TEMP_FILE` | |
assert_valid_req_id $REQ_ID "Accept terms of agreement" | |
output_start "Fetch dates" | |
rm $TEMP_FILE 2> /dev/null | |
TODAY=`date +%d.%m.%Y` | |
partial_curl \ | |
--data "__RequestVerificationToken=$REQ_ID&FormId=1&RegionId=0&SectionId=$SECTION_ID&ServiceTypeId=1&FromDateString=$TODAY&TimeSearchFirstAvailableButton=Vis+f%C3%B8rste+ledige+tidspunkt" \ | |
--compressed 'https://pass.oslo.politi.no/Booking/Booking/Next/Pass' \ | |
-o $TEMP_FILE | |
REQ_ID=`echo_request_id $TEMP_FILE` | |
assert_valid_req_id $REQ_ID "Fetch dates" | |
FIRST_DATE=`cat $TEMP_FILE | grep '<th id="' | sed -n -e 's/ <th id="//p' | sed -n -e 's/">//p' | head -1 | sed -n -e 's/[^0-9.]//gp'` | |
CACHED_DATE=`cat $CACHED_DATE_FILE 2> /dev/null` | |
echo "$FIRST_DATE" > $CACHED_DATE_FILE | |
if [ -z "$QUIET" ]; then | |
echo "" | |
if [ ${#CACHED_DATE} = "0" ] ; then | |
echo -e "First available date is $FIRST_DATE (cache is empty)" | |
else | |
echo -e "First available date is $FIRST_DATE (cached $CACHED_DATE)" | |
fi | |
if first_date_is_earlier $FIRST_DATE $CACHED_DATE; then | |
echo -e "${GREEN}🎉 Congrats! We got a new date available! 🎉${CLEAR}" | |
else | |
echo "Current available date is not earlier than cached..." | |
fi | |
else | |
echo "$FIRST_DATE" | |
fi | |
clean_up | |
if first_date_is_earlier $FIRST_DATE $CACHED_DATE; then | |
output_start "Send SMS" | |
curl 'https://api.twilio.com/2010-04-01/Accounts/ACf60817ef55bfc9f6f650363a2269c581/Messages.json' -X POST \ | |
--data-urlencode 'To=+123123' \ | |
--data-urlencode 'From=+123123' \ | |
--data-urlencode "Body=Nyeste dato er $FIRST_DATE" \ | |
-u 123:312 \ | |
--silent -L -f > /dev/null \ | |
&& output_success "Send SMS" \ | |
|| output_failed "Send SMS" | |
exit 0; | |
else | |
exit 1; | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment