Skip to content

Instantly share code, notes, and snippets.

@Lecrapouille
Last active November 3, 2022 11:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Lecrapouille/f11d541f435dbbba9ccccde86ab2d0dd to your computer and use it in GitHub Desktop.
Save Lecrapouille/f11d541f435dbbba9ccccde86ab2d0dd to your computer and use it in GitHub Desktop.
A personal bash script I'm using to monitor when planning for "remises de titres de séjours" are ready at Bobigny prefecture. To avoid ban, spinning several few Amazon EC2 Paris microinstances is recommended
#!/bin/bash -e
###
### This script is a continuation of vadimkantorov/rdv.sh
### https://gist.github.com/vadimkantorov/c6942cc702cceec7ffb30a990c72df5e
### This script is a helper to notify you when a planning for inscription
### to the prefecture to get back your card.
###
# Set your phone number to get SMS (ie +336...)
PHONE=""
if [ "$PHONE" == "" ]; then
echo "Please fill your phone number to get a SMS giving you which guichet is opened"
exit 1
fi
# https://www.seine-saint-denis.gouv.fr/Prendre-un-rendez-vous
# Remise de titre - section délivrance Bobigny
# Note: identifier such as "/0" will be appended to the URL
URL='https://www.seine-saint-denis.gouv.fr/booking/create/16105'
# Possible response from the server
NORDV='existe plus de plage horaire libre pour votre demande'
MAYBERDV='Description de la nature du rendez-vous'
GETCOOKIE='Effectuer une demande de rendez-vous'
ERROR1='Guru Meditation'
ERROR2='Bad Gateway'
ERROR3='Bad request'
ERROR4='Forbidden'
ERROR5='Service Unavailable'
ERROR6='Gateway Time-out'
# Timeout to requests
TIMEOUT=300
# Bash token separator
OLD_IFS=$IFS
IFS='
'
while true;
do
# Get the cookie
COOKIE=$(curl -sS --head "$URL/0" --connect-timeout $TIMEOUT --max-time $TIMEOUT | grep -i Set-Cookie | cut -d':' -f2 | cut -d';' -f1 | sed 's/[[:space:]]*$//g' | sed 's/^[[:space:]]*//' || echo "$ERROR1")
if [[ "$COOKIE" == "" ]] || [[ "$COOKIE" == "$ERROR1" ]]; then
echo "Failed while getting the cookie"
exit 1
fi
echo "Cookie: $COOKIE"
# Get the HTML page holding the list of guichets
OUTPUT=$(curl -sS "$URL/0" -H 'Content-Type: application/x-www-form-urlencoded' -H "Cookie: $COOKIE" --data 'condition=on&nextButton=Effectuer+une+demande+de+rendez-vous' -L --connect-timeout $TIMEOUT --max-time $TIMEOUT)
echo "$OUTPUT" > F1.txt
# Extract all guichets from returned html file
RAW_GUICHETS=`echo "$OUTPUT" | grep "Remise de titre - Section délivrance Bobigny - Guichet"`
# For each guichet present in the html file ...
for LINE in `echo "$RAW_GUICHETS"`;
do
IFS=$OLD_IFS
PATTERN='.*value=\"([0-9]+)\".*Guichet ([A-Z]).*'
if [[ "$LINE" =~ $PATTERN ]];
then
# Extract guichet identifier and guichet name
GUICHET_VALUE="${BASH_REMATCH[1]}"
GUICHET_NAME="${BASH_REMATCH[2]}"
echo "I'll check the planning for the guichet $GUICHET_NAME (id=$GUICHET_VALUE)"
# Try to access to the planning.
# Note: the POST data is obtained with your browser in debug mode (F12).
# For example in Firefox: widget 'Network' then subwidget 'Request'
POST="planning=""$GUICHET_VALUE""&nextButton=Etape+suivante"
GUICHET_OUTPUT=$(curl -sS "$URL/1" -H 'Content-Type: application/x-www-form-urlencoded' -H "Cookie: $COOKIE" --data "$POST" -L --connect-timeout $TIMEOUT --max-time $TIMEOUT)
echo "$GUICHET_OUTPUT" > "GUICHET_""$GUICHET_NAME"".txt"
# Check planning state
if [[ "$GUICHET_OUTPUT" == *"$NORDV"* ]]; then
# Planning not yet opened
echo "FAILED: $NORDV"
# Click on the button return to try back next guichet
RES=$(curl -sS "$URL/2" -H 'Content-Type: application/x-www-form-urlencoded' -H "Cookie: $COOKIE" --data 'finishButton=Terminer' -L --connect-timeout $TIMEOUT --max-time $TIMEOUT)
elif [[ "$GUICHET_OUTPUT" == *"$ERROR1"* ]] ||
[[ "$GUICHET_OUTPUT" == *"$ERROR2"* ]] ||
[[ "$GUICHET_OUTPUT" == *"$ERROR3"* ]] ||
[[ "$GUICHET_OUTPUT" == *"$ERROR4"* ]] ||
[[ "$GUICHET_OUTPUT" == *"$ERROR5"* ]] ||
[[ "$GUICHET_OUTPUT" == *"$ERROR6"* ]]; then
echo "FAILED: $GUICHET_OUTPUT"
else # FIXME look for a matcher
echo "*** SUCESS => GUICHET $GUICHET_NAME ***"
# Send a texto
curl -X POST https://textbelt.com/text --data-urlencode phone='$PHONE' \
--data-urlencode message='Pref guichet $GUICHET_NAME' -d key=textbelt
exit 0
fi
else
echo "FAILED regexp: no guichets found"
fi
IFS='
'
done
DURATION=$((RANDOM % 10 + 20))
echo "Sleep $DURATION minutes"
sleep "$DURATION""m"
done
@Lecrapouille
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment