scripts
/
analyse-votes-AN
/
2 contributor
#!/bin/bash
groupe=${1:-LREM}
nom=$2
groupe_ref=${3:-GDR}
archive_scrutins=$HOME/archive_scrutins
declare -A groupes cible_votes ref_votes
groupes[LREM]='Groupe La République en Marche'
groupes[LR]='Groupe Les Républicains'
groupes[MODEM]='Groupe du Mouvement Démocrate et apparentés'
groupes[PS]='Groupe Socialistes et apparentés'
groupes[UDI]='Groupe UDI, Agir et Indépendants'
groupes[NG]='Groupe Nouvelle Gauche'
groupes[FI]='Groupe La France insoumise'
groupes[GDR]='Groupe de la Gauche démocrate et républicaine'
groupes[LT]='Groupe Libertés et Territoires'
groupes[NI]='Non inscrits'
for g in ${!groupes[@]}; do
if test "$nom" = $g; then
groupe_ref=$nom
unset nom
break
fi
done
if test -n "${groupes[$groupe]}" -a -n "${groupes[$groupe_ref]}"; then
groupes[$groupe]='/<p class="nomgroupe">'${groupes[$groupe]}' <span class="block topmargin">/,/<div class="TTgroupe topmargin-lg">/'
if test $groupe != $groupe_ref; then
groupes[$groupe_ref]='/<p class="nomgroupe">'${groupes[$groupe_ref]}' <span class="block topmargin">/,/<div class="TTgroupe topmargin-lg">/'
fi
else
echo "groupe \"$groupe\" inconnu:"
for g in ${!groupes[@]}; do
echo "$g -> ${groupes[$g]}"
done
exit 1
fi
tempfile="/dev/shm/scrutin"
id_cols="Scrutin Date Titre Adoption"
typevotes="Pour Contre Abstention Non-votants"
nb_cols=$(wc -w <<< "$id_cols $typevotes $typevotes")
if test -n "$nom"; then
cible="$nom ($groupe)"
else
cible=$groupe
fi
result="comparaisons $cible avec $groupe_ref"
mkdir -p "/dev/shm/$result/META-INF"
cat > "/dev/shm/$result/META-INF/manifest.xml" << EOmetainf
<?xml version="1.0" encoding="UTF-8"?>
<manifest:manifest xmlns:manifest="urn:oasis:names:tc:opendocument:xmlns:manifest:1.0" manifest:version="1.2">
<manifest:file-entry manifest:full-path="/" manifest:version="1.2" manifest:media-type="application/vnd.oasis.opendocument.spreadsheet"/>
<manifest:file-entry manifest:full-path="content.xml" manifest:media-type="text/xml"/>
</manifest:manifest>
EOmetainf
printf 'application/vnd.oasis.opendocument.spreadsheet' > "/dev/shm/$result/mimetype"
content="/dev/shm/$result/content.xml"
cat > "$content" << EOcontent
<?xml version="1.0" encoding="UTF-8"?>
<office:document-content xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0" xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0" xmlns:draw="urn:oasis:names:tc:opendocument:xmlns:drawing:1.0" xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0" xmlns:number="urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0" xmlns:presentation="urn:oasis:names:tc:opendocument:xmlns:presentation:1.0" xmlns:svg="urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0" xmlns:chart="urn:oasis:names:tc:opendocument:xmlns:chart:1.0" xmlns:dr3d="urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0" xmlns:math="http://www.w3.org/1998/Math/MathML" xmlns:form="urn:oasis:names:tc:opendocument:xmlns:form:1.0" xmlns:script="urn:oasis:names:tc:opendocument:xmlns:script:1.0" xmlns:ooo="http://openoffice.org/2004/office" xmlns:ooow="http://openoffice.org/2004/writer" xmlns:oooc="http://openoffice.org/2004/calc" xmlns:dom="http://www.w3.org/2001/xml-events" xmlns:xforms="http://www.w3.org/2002/xforms" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:rpt="http://openoffice.org/2005/report" xmlns:of="urn:oasis:names:tc:opendocument:xmlns:of:1.2" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:grddl="http://www.w3.org/2003/g/data-view#" xmlns:tableooo="http://openoffice.org/2009/table" xmlns:drawooo="http://openoffice.org/2010/draw" xmlns:calcext="urn:org:documentfoundation:names:experimental:calc:xmlns:calcext:1.0" xmlns:loext="urn:org:documentfoundation:names:experimental:office:xmlns:loext:1.0" xmlns:field="urn:openoffice:names:experimental:ooo-ms-interop:xmlns:field:1.0" xmlns:formx="urn:openoffice:names:experimental:ooxml-odf-interop:xmlns:form:1.0" xmlns:css3t="http://www.w3.org/TR/css3-text/" office:version="1.2">
<office:scripts/>
<office:font-face-decls>
<style:font-face style:name="Liberation Sans" svg:font-family="'Liberation Sans'" style:font-family-generic="swiss" style:font-pitch="variable"/>
<style:font-face style:name="DejaVu Sans" svg:font-family="'DejaVu Sans'" style:font-family-generic="system" style:font-pitch="variable"/>
<style:font-face style:name="FreeSans" svg:font-family="FreeSans" style:font-family-generic="system" style:font-pitch="variable"/>
</office:font-face-decls>
<office:automatic-styles>
EOcontent
for i in $(seq $nb_cols); do
cat >> "$content" << EOcontent
<style:style style:name="co$i" style:family="table-column">
<style:table-column-properties fo:break-before="auto" style:column-width="30.00mm"/>
</style:style>
EOcontent
done
cat >> "$content" << EOcontent
<style:style style:name="ro1" style:family="table-row">
<style:table-row-properties style:row-height="4.52mm" fo:break-before="auto" style:use-optimal-row-height="true"/>
</style:style>
<style:style style:name="ta1" style:family="table" style:master-page-name="Default">
<style:table-properties table:display="true" style:writing-mode="lr-tb"/>
</style:style>
</office:automatic-styles>
<office:body>
<office:spreadsheet>
<table:calculation-settings table:automatic-find-labels="false"/>
<table:table table:name="$result" table:style-name="ta1">
<office:forms form:automatic-focus="false" form:apply-design-mode="false"/>
EOcontent
for i in $(seq $nb_cols); do
echo "<table:table-column table:style-name=\"co$i\" table:default-cell-style-name=\"Default\"/>" >> "$content"
done
echo '<table:table-row table:style-name="ro1">' >> "$content"
for colonne in $id_cols; do
cat >> "$content" << EOcontent
<table:table-cell office:value-type="string" calcext:value-type="string">
<text:p>$colonne</text:p>
</table:table-cell>
EOcontent
done
for typevote in $typevotes; do
for g in "$cible" $groupe_ref; do
cat >> "$content" << EOcontent
<table:table-cell office:value-type="string" calcext:value-type="string">
<text:p>$typevote - $g</text:p>
</table:table-cell>
EOcontent
done
done
echo '</table:table-row>' >> "$content"
last=$(wget -qO- 'http://www2.assemblee-nationale.fr/scrutins/liste/(legislature)/15/(type)/TOUS/(idDossier)/TOUS' | sed -rn 's,^.*<td class="denom">(.+)</td>.*$,\1,p' | head -1)
test -r "$archive_scrutins" && first=$(tar tf "$archive_scrutins" | tail -1) || first=0
if test $first -lt $last; then
echo "récupération des scrutins n°$((first+1)) à n°$last dans "$archive_scrutins" (à conserver autant que possible)" >&2
for scrutin in $(seq $((first+1)) $last); do
wget -qO- "http://www2.assemblee-nationale.fr/scrutins/detail/(legislature)/15/(num)/$scrutin" \
| sed -r '0,/< *div class="titre-bandeau-bleu +to-print" *>/d; /< *script +type="text\/javascript" *>/,$d' > /dev/shm/$scrutin \
&& tar -C /dev/shm -rf "$archive_scrutins" $scrutin \
&& rm -f /dev/shm/$scrutin \
&& echo -n '.' >&2
done
echo
fi
for scrutin in $(seq $last); do
printf "."
tar -C /dev/shm -Oxf "$archive_scrutins" $scrutin > $tempfile
title=$(sed -rn '/<h1 class="">Analyse du scrutin n° '$scrutin'/n; s,^.*<h3 class="president-title">(.+).</h3>,\1,p' $tempfile | sed "s/;//g; s/[ \t][ \t]+/ /g; s/^Scrutin public sur *//; s/^l[ae'] *//")
date=$(sed -rn 's,^.*<h1 class="">Analyse du scrutin n° '$scrutin'<br/>(.+) </h1>,\1,p' $tempfile)
adoption=$(sed -rn 's,^.*<p class="annonce"><span class="annoncevote">(.+).</span></p>.*$,\1,p' $tempfile)
cat >> "$content" << EOcontent
<table:table-row table:style-name="ro1">
<table:table-cell office:value-type="float" office:value="$scrutin" calcext:value-type="float">
<text:p>$scrutin</text:p>
</table:table-cell>
<table:table-cell office:value-type="string" calcext:value-type="string">
<text:p>$date</text:p>
</table:table-cell>
<table:table-cell office:value-type="string" calcext:value-type="string">
<text:p>${title//\'/'}</text:p>
</table:table-cell>
<table:table-cell office:value-type="string" calcext:value-type="string">
<text:p>${adoption//\'/'}</text:p>
</table:table-cell>
EOcontent
unset cible_votes[*] ref_votes[*]
for typevote in $typevotes; do
cible_votes[$typevote]=$(sed -rn "${groupes[$groupe]}p" $tempfile | sed -rn '/<p class="typevote">'${typevote}':/,/<.div>/p' | sed 's,</li>,\n,g' | grep -ic " <b>${nom:+${nom}</br>}")
ref_votes[$typevote]=$(sed -rn "${groupes[$groupe_ref]}p" $tempfile | sed -rn '/<p class="typevote">'${typevote}':/,/<.div>/p' | sed 's,</li>,\n,g' | grep -c ' <b>')
cat >> "$content" << EOcontent
<table:table-cell office:value-type="float" office:value="${cible_votes[$typevote]}" calcext:value-type="float">
<text:p>${cible_votes[$typevote]}</text:p>
</table:table-cell>
<table:table-cell office:value-type="float" office:value="${ref_votes[$typevote]}" calcext:value-type="float">
<text:p>${ref_votes[$typevote]}</text:p>
</table:table-cell>
EOcontent
done
echo '</table:table-row>' >> "$content"
done
echo
rm $tempfile
cat >> "$content" << EOcontent
</table:table>
<table:named-expressions/>
<table:database-ranges>
<table:database-range table:name="__Anonymous_Sheet_DB__0" table:target-range-address="'$result'.A1:'$result'.$(printf "\\$(printf '%03o' $((64+$nb_cols)))")$(($last+1))" table:display-filter-buttons="true"/>
</table:database-ranges>
</office:spreadsheet>
</office:body>
</office:document-content>
EOcontent
( cd "/dev/shm/$result" && zip -r ../"$result" * > /dev/null 2>&1 && cd .. && rm -fr "$result" )
mv -f "/dev/shm/$result.zip" "$result.ods"
echo "$result.ods"