#!/bin/bash for tool in sqlite3 getopt md5sum mktemp; do which $tool > /dev/null 2>&1 || { echo missing tool $tool exit 1 } done function create_database () { declare -A groupes if test -z "${groupes[@]}"; then echo "unable to find groupes in $config_file" >&2 exit 1 fi sqlite3 "$in_ram_database" <<< "create table if not exists votes (id integer primary key, nom text)" sqlite3 "$in_ram_database" <<< "create table if not exists url (id integer primary key autoincrement, url text)" sqlite3 "$in_ram_database" <<< "create table if not exists députés (id integer primary key autoincrement, nom text, groupe integer)" sqlite3 "$in_ram_database" <<< "create table if not exists groupes (id integer primary key autoincrement, nom text, nom_court text)" sqlite3 "$in_ram_database" <<< "create table if not exists scrutins (num integer primary key, date text not null, intitulé text non null, adoption boolean, url integer)" sqlite3 "$in_ram_database" <<< "create table if not exists dépouillement (député integer not null, scrutin integer not null, groupe integer not null, vote integer not null)" v_id=0 for v in Pour Contre Abstention Non-votant; do if test -z $(sqlite3 "$in_ram_database" <<< "select nom from votes where id is $v_id"); then sqlite3 "$in_ram_database" <<< "insert into votes values ($v_id, '$v')" else test -z $(sqlite3 "$in_ram_database" <<< "select nom from votes where id is $v_id and nom is '$v'") \ && sqlite3 "$in_ram_database" <<< "update votes set nom = '$v' where id is $v_id)" fi let v_id++ done unset v_id v for g in ${!groupes[@]}; do test -z $(sqlite3 "$in_ram_database" <<< "select id from groupes where nom is '${groupes[$g]}' and nom_court is '$g'") \ && sqlite3 "$in_ram_database" <<< "insert into groupes (nom, nom_court) values ('${groupes[$g]}', '$g')" done unset g groupes test -z $(sqlite3 "$in_ram_database" <<< "select id from url where id = 0") \ && sqlite3 "$in_ram_database" <<< "insert into url values (0, '')" } function update_database () { test "$no_db_update" = $true_flag && return tempfile="/dev/shm/scrutin.$$" progress=0 if test ${first:-0} -lt $last; then echo "récupération des scrutins n°$((${first:-0}+1)) à n°$last dans "$database" (à conserver autant que possible)" >&2 url_database=/dev/shm/url_database : > "$url_database" test $((last % 100)) -ne 0 && last_offset=0 for offset in $(seq $((last - 100)) -100 ${first:-0} ) $last_offset; do wget -qO- "http://www2.assemblee-nationale.fr/scrutins/liste/(offset)/$offset/(legislature)/15/(type)/TOUS/(idDossier)/TOUS" \ | awk ' /
(.+).
.*$,\1,p' $tempfile) test -n "$title" -a -n "$date" -a -n "$adoption" || { echo "erreur dans la récupération du scrutin $scrutin" exit 1 } grep -q 'e a a' <<< "$adoption" && adoption=1 || adoption=0 url=$(awk -F'|' "/^$scrutin\|/{print \$2}" "$url_database") id_url=$(sqlite3 "$in_ram_database" <<< "select id from url where url is '$url'") if test -z "$id_url"; then sqlite3 "$in_ram_database" <<< "insert into url (url) values ('$url')" id_url=$(sqlite3 "$in_ram_database" <<< "select id from url where url is '$url'") fi sqlite3 "$in_ram_database" <<< "insert into scrutins values ($scrutin, '$date', \"${title//\"}\", $adoption, ${id_url:-0})" for v in $(sqlite3 "$in_ram_database" <<< "select * from votes"); do for g in $(sqlite3 "$in_ram_database" <<< "select id,nom from groupes"); do for d in $(sed -rn '/'${g#*|}' /,/ '${v#*|}':/,/<.div>/p' \
| sed 's,,\n,g' \
| sed -rn '/ /d; s,^\s*(.+) .*$,\1,p' \
| head -1)
test -z "$first" && first=$(sqlite3 "$in_ram_database" <<< "select count(num) from scrutins")
fi
if test "$liste_dossiers" = $true_flag; then
sqlite3 "$in_ram_database" <<< "select printf('%s - %s', id, url) from url" | sed 's,https*://.*/dossiers/,,; s/_/ /g; s/.asp$//'
exit
fi
if test "$db_update_only" = $true_flag; then
unset first last
update_database
exit
fi
if test "$liste_deputes" = $true_flag; then
if test -n "$liste_deputes_value"; then
sqlite3 "$in_ram_database" <<< "select printf('%s - %s', députés.nom, groupes.nom_court) from députés inner join groupes on groupes.id = députés.groupe where groupes.nom_court is '$liste_deputes_value'"
else
sqlite3 "$in_ram_database" <<< "select printf('%s - %s', députés.nom, groupes.nom_court) from députés inner join groupes on groupes.id = députés.groupe order by groupes.nom_court asc"
fi
exit
fi
if test "$depute" = $true_flag; then
if test -n "$nom"; then
match=$(sqlite3 "$in_ram_database" <<< "select count(députés.id) from députés inner join groupes on groupes.id = députés.groupe where députés.nom like '%$nom%' and groupes.nom_court is '$groupe' collate nocase")
if test $match -ne 1; then
if test $match -eq 0; then
echo "pas de député correspondant dans le groupe $groupe"
else
echo "plusieurs députés correspondent:"
sqlite3 "$in_ram_database" <<< "select députés.nom from députés inner join groupes on groupes.id = députés.groupe where députés.nom like '%$nom%' and groupes.nom_court is '$groupe' collate nocase"
fi
exit 1
else
nom=$(sqlite3 "$in_ram_database" <<< "select députés.id,députés.nom from députés inner join groupes on groupes.id = députés.groupe where députés.nom like '%$nom%' and groupes.nom_court is '$groupe' collate nocase")
cible="${nom#*|} ($groupe)"
fi
fi
fi
if test "$dossier" = $true_flag; then
if test -z "$dossier_value"; then
IFS_=$IFS
IFS=$'\n'
select dossier in $(sqlite3 "$in_ram_database" <<< "select url from url" | sed 's,^.*/dossiers/,,; s/_/ /g; s/.asp$//'); do
if test -n "$dossier"; then
seq="sqlite3 \"$in_ram_database\" <<< \"select num from scrutins inner join url on url.id = scrutins.url where url.url like '%/dossiers/${dossier// /_}%' order by num asc\""
qty=$(sqlite3 "$in_ram_database" <<< "select count(num) from scrutins inner join url on url.id = scrutins.url where url.url like '%/dossiers/${dossier// /_}%' order by num asc")
break
fi
done
IFS=$IFS_
else
seq="sqlite3 \"$in_ram_database\" <<< \"select num from scrutins inner join url on url.id = scrutins.url where url.id is $dossier_value order by num asc\""
qty=$(sqlite3 "$in_ram_database" <<< "select count(num) from scrutins inner join url on url.id = scrutins.url where url.id is $dossier_value order by num asc")
dossier=$(sqlite3 "$in_ram_database" <<< "select url from url where id is $dossier_value" | sed 's,^.*/dossiers/,,; s/_/ /g; s/.asp$//')
fi
fi
update_database
write_comparaison