Skip to content

Instantly share code, notes, and snippets.

@rhanka
Last active June 13, 2019 21:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rhanka/38ababbed8812e53d823a5c2762abb96 to your computer and use it in GitHub Desktop.
Save rhanka/38ababbed8812e53d823a5c2762abb96 to your computer and use it in GitHub Desktop.
download BRPP INSEE data
#!/bin/bash
#login et mdp doivent être url encoded (uriComponent et pas simplement uri)
set -e
set echo off
first_year=1970
login=MYBRPPLOGIN
password=MYURLENCODEDPASSWORD
# étape 1: création d'un jeton de session (stockage dans cookie1)
curl -s -c cookie1 https://echanges.insee.fr/ihm/download/brpp-deces -o session.html && echo initialisation de session || echo initialisation de session en échec
# étape 2: login => renvoie un autre jeton de session, valant authent (stockage dans cookie2)
curl -s -b cookie1 -c cookie2 'https://echanges.insee.fr/ihm/download/brpp-deces' \
-H 'User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:66.0) Gecko/20100101 Firefox/66.0'\
-H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8' \
-H 'Accept-Language: fr,fr-FR;q=0.8,en-US;q=0.5,en;q=0.3' \
--compressed -H 'Referer: https://echanges.insee.fr/ihm/download/brpp-deces'\
-H 'Content-Type: application/x-www-form-urlencoded' -H 'DNT: 1' -H 'Connection: keep-alive' \
-H 'Upgrade-Insecure-Requests: 1' -H 'Pragma: no-cache' -H 'Cache-Control: no-cache' \
--data "userContext.aliasOperationId=fourniture+des+fichiers+d%C3%A9c%C3%A8s&userContext.userId=${login}&userContext.password=${password}&action%3AdownloadInit=+++Retirer+des+fichiers+++&userContext.pageOrigine=download&userContext.operationId=brpp-deces"\
-o login.html && echo login || echo login failed
# étape 3: récupération des fichiers
# commande de listing des fichiers (paginée)
curl -s --compressed -b cookie2 https://echanges.insee.fr/ihm/downloadListeFichiers -o test.html && echo login ok
# 3.1: récupération des années précédentes
last_year=`date --date="1 year ago" +'%Y'`
echo récupération des années ${first_year} à ${last_year}
for y in `seq ${first_year} ${last_year}`;\
do \
[ ! -f Deces_${y}.txt ] && \
echo "récupération année ${y}" &&\
(curl -s --compressed -b cookie2 "https://echanges.insee.fr/ihm/download_Deces_${y}.txt.html?nomFichier=Deces_${y}.txt" > Deces_${y}.txt) &&\
echo "${y}: ok";\
done
# 3.2: récupération des mois précédents
last_month=`date --date="1 month ago" +'%m'`
y=`date +'%Y'`
m=`date --date '1 month ago' +'%m'`
echo récupération des mois de l\'année en cours
for m in `seq 1 ${m}`;\
do \
m=$(printf %02d $m);\
echo $m;\
[ ! -f Deces_${y}_M${m}.txt ] &&\
echo "récupération année ${y} mois ${m}" &&\
(curl -s --compressed -b cookie2 "https://echanges.insee.fr/ihm/download_Deces_${y}_M${m}.txt.html?nomFichier=Deces_${y}_M${m}.txt" > Deces_${y}_M${m}.txt) &&\
echo "${y}: ok";\
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment