scripts / analyse-votes-AN /
6da0b1e 6 years ago
2 contributor
201 lines | 10.05kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202#!/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="&apos;Liberation Sans&apos;" style:font-family-generic="swiss" style:font-pitch="variable"/>
<style:font-face style:name="DejaVu Sans" svg:font-family="&apos;DejaVu Sans&apos;" 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//\'/&apos;}</text:p>
</table:table-cell>
<table:table-cell office:value-type="string" calcext:value-type="string">
<text:p>${adoption//\'/&apos;}</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 "&nbsp;<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 '&nbsp;<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="&apos;$result&apos;.A1:&apos;$result&apos;.$(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"