Skip to content

Instantly share code, notes, and snippets.

@vadimkantorov
Last active January 16, 2024 20:26
Show Gist options
  • Save vadimkantorov/c6942cc702cceec7ffb30a990c72df5e to your computer and use it in GitHub Desktop.
Save vadimkantorov/c6942cc702cceec7ffb30a990c72df5e to your computer and use it in GitHub Desktop.
A Bash script to monitor naturalisation rendez-vous availability at Prefectures Bobigny, Nanterre. Consider renting one or several EC2 micro instances at Paris AWS data center to avoid bans.
# Usage:
# 1. Put in your email/SendGrid credentials or phone/Twilio credentials
# 2. Run as: "bash rdv_naturalisation.sh nanterre" or "bash rdv_naturalisation.sh bobigny" or "bash rdv_naturalisation.sh cre"
# 3. Get your RDV! The script will dump the HTML of the page and will continue execution even after the first RDV is found.
SENDGRID_BEARER_TOKEN='' # should start with "SG."
SENDGRID_EMAIL='' # put your e-mail here
TWILIO_SID='' # should start with "AC"
TWILIO_AUTH_TOKEN=''
TWILIO_FROM_NUMBER='' # put your Twilio Trial Phone Number here, should start with +
TWILIO_TO_NUMBER='' # put your cell phone number here, should start with +
TIMEOUT=300 # timeout between checks in seconds
set -e
#URL='http://www.val-de-marne.gouv.fr/booking/create/4963/0' # DOES NOT WORK, NEEDS SOME FIXES
if [[ $1 == bobigny ]]; then
URL='http://www.seine-saint-denis.gouv.fr/booking/create/1194/0'
elif [[ $1 == nanterre ]]; then
URL='http://www.hauts-de-seine.gouv.fr/booking/create/13525/0'
elif [[ $1 == cre ]]; then
URL='http://pprdv.interieur.gouv.fr/booking/create/953/0'
else
echo "Exiting. Not supported [$1]"
exit 1
fi
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'
while true; do
OUTPUT=$(curl -sS $URL -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 "$ERROR1")
if [[ "$OUTPUT" == *"$GETCOOKIE"* ]]; then
echo "$(date) Need new cookie"
COOKIE=$(curl -sS --head $URL --connect-timeout $TIMEOUT --max-time $TIMEOUT | grep -i Set-Cookie | cut -d':' -f2 | cut -d';' -f1 | sed 's/[[:space:]]*$//g' | sed 's/^[[:space:]]*//')
echo "$(date) Set cookie: $COOKIE"
elif [[ "$OUTPUT" == *"$NORDV"* ]]; then
echo "$(date) No rendez-vous available"
elif [[ "$OUTPUT" == *"$ERROR1"* ]] || [[ "$OUTPUT" == *"$ERROR2"* ]] || [[ "$OUTPUT" == *"$ERROR3"* ]] || [[ "$OUTPUT" == *"$ERROR4"* ]] || [[ "$OUTPUT" == *"$ERROR5"* ]] || [[ "$OUTPUT" == *"$ERROR6"* ]]; then
echo "$(date) Error"
echo "$OUTPUT" > "$0.error.html"
COOKIE=
sleep $(($TIMEOUT * 2))
else
echo "$(date) YHOO! RDV ON INTERWEBZ"
echo "$OUTPUT" > "$0.$(date +%s%N).html"
if [[ "$SENDGRID_BEARER_TOKEN" ]]; then
curl -sS -X POST https://api.sendgrid.com/api/mail.send.json -H "Authorization: Bearer $SENDGRID_BEARER_TOKEN" -d "to=$SENDGRID_EMAIL" -d "from=$SENDGRID_EMAIL" -d "subject=RDV available" -d"text=$URL"
fi
if [[ "$TWILIO_SID" ]]; then
curl -sS -X POST https://api.twilio.com/2010-04-01/Accounts/${TWILIO_SID}/Messages.json -u "${TWILIO_SID}:${TWILIO_AUTH_TOKEN}" --data-urlencode "To=${TWILIO_TO_NUMBER}" --data-urlencode "From=${TWILIO_FROM_NUMBER}" --data-urlencode "Body=$URL"
fi
# exit 0 # uncommment to exit after the first RDV is found
fi
sleep $TIMEOUT
done
@dbatay
Copy link

dbatay commented Nov 7, 2020

PlEASE I didn't get what is : SENDGRID_BEARER_TOKEN='' # should start with "SG."

How can I get it ?

Thank you so much for helping

You either should have sendrid token or twilio. For Twilio, you can have a trial account from here: https://www.twilio.com/

@dbatay
Copy link

dbatay commented Nov 7, 2020

The urls should be https not http. In case, you set it http, the script doesn't function correctly.

@Lecrapouille
Copy link

For one who needs I just right now made a bash script based on this GIST to check when planning for titre de séjours are ready. This is a little complex since there is an intermediate page with different guichets. https://gist.github.com/Lecrapouille/f11d541f435dbbba9ccccde86ab2d0dd Use with care since I have just written it and not yet fully tested. Beware also I do not know if we can be banned but contrary to this current script I sleep random 20 - 30 minutes before retrying. You have to set your phone number in the script, you'll receive up to 1 SMS by day thanks to https://textbelt.com when plannings are opened.

@vadimkantorov
Copy link
Author

vadimkantorov commented May 31, 2021

For avoiding banning, I recommend spinning several Amazon EC2 Paris microinstances

@Lecrapouille
Copy link

@vadimkantorov why looping the main loop every 300 s ? This makes their servers slow down, not nice for other people

@vadimkantorov
Copy link
Author

Because you must be this fast for booking an appointment for naturalisation. Otherwise someone else takes it.

@Lecrapouille
Copy link

@vadimkantorov have you succeeded to do the "click" on the planning to reserve a day ? Look like not to me

@vadimkantorov
Copy link
Author

No, I haven't tried doing that, that would be the best, getting these rdv to do any testing was almost impossible

@vadimkantorov
Copy link
Author

So if you do that, that's a big improvement!

@Lecrapouille
Copy link

Depuis le covid, les prises de rendez-vous pour déposer les papiers sont plus humaines : les plannings sont ouverts pour les 2 semaines suivantes. Donc je n'ai pas eu de soucis. Par contre, pour récupérer le titre ça semble à nouveau être la misère d'où mon script. Dès que j'aurais le rendez-vous, je pourrais tester plus facilement.

@gleek77
Copy link

gleek77 commented Aug 29, 2022

Salut lecrapouille. j'aurai besoin d'adapter ce script pour la préfecture de melun. Suffirait il de ne modifier que l'url pour la faire correspondre a celle de melun ?

@Lecrapouille
Copy link

@gleek77 tu n'es pas sur mon gist, le mien est en bash. Et je ne suis pas sûr de pouvoir t'aider, car 1/ je n'ai pas retesté depuis et je sais qu'il y avait des bugs (sms), les liens ont dû totalement changés 2/ la pref me donne des boutons donc ce n'est pas une partie de plaisir que de coder ce genre de truc les soirs 3/ je ne suis pas webdev donc pas mon domaine. Ce qu'il faut faire c'est d'expérimenter le soir après 20h avec un browser ouvert en mode debug (F12) et voir les trames qui passent.

@gleek77
Copy link

gleek77 commented Aug 31, 2022 via email

@vadimkantorov
Copy link
Author

vadimkantorov commented Aug 31, 2022

@gleek77 T'es au fait sur mon gist (qui est aussi en bash) :) Pour le reste, je suis d'accord avec @Lecrapouille, étudie dans Dev Console -> Network des paquets et ajoute des modifs si nécessaire. En plus, mon script ne sait pas "choisir" le service (qui est obligatoire pour Créteil, par exemple) ou prendre le rdv en autonomie complète.

@gleek77
Copy link

gleek77 commented Sep 18, 2022

Re. je vais voir si j'arrive à quelque chose . jsuis en terre inconnue

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