ajout script d'analyse des v...
|
1 |
#!/bin/bash |
2 | ||
complète re-écriture
|
3 |
for tool in sqlite3 getopt md5sum mktemp; do |
4 |
which $tool > /dev/null 2>&1 || { |
|
5 |
echo missing tool $tool |
|
6 |
exit 1 |
|
7 |
} |
|
plus de souplesse dans les p...
|
8 |
done |
9 | ||
complète re-écriture
|
10 |
function create_database () { |
11 |
declare -A groupes |
|
12 |
if test -z "${groupes[@]}"; then |
|
13 |
echo "unable to find groupes in $config_file" >&2 |
|
14 |
exit 1 |
|
fixe pb quand groupe cible e...
|
15 |
fi |
complète re-écriture
|
16 | |
17 |
sqlite3 "$in_ram_database" <<< "create table if not exists votes (id integer primary key, nom text)" |
|
18 |
sqlite3 "$in_ram_database" <<< "create table if not exists url (id integer primary key autoincrement, url text)" |
|
19 |
sqlite3 "$in_ram_database" <<< "create table if not exists députés (id integer primary key autoincrement, nom text, groupe integer)" |
|
20 |
sqlite3 "$in_ram_database" <<< "create table if not exists groupes (id integer primary key autoincrement, nom text, nom_court text)" |
|
21 |
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)" |
|
22 |
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)" |
|
23 | ||
24 |
v_id=0 |
|
25 |
for v in Pour Contre Abstention Non-votant; do |
|
26 |
if test -z $(sqlite3 "$in_ram_database" <<< "select nom from votes where id is $v_id"); then |
|
27 |
sqlite3 "$in_ram_database" <<< "insert into votes values ($v_id, '$v')" |
|
28 |
else |
|
29 |
test -z $(sqlite3 "$in_ram_database" <<< "select nom from votes where id is $v_id and nom is '$v'") \ |
|
30 |
&& sqlite3 "$in_ram_database" <<< "update votes set nom = '$v' where id is $v_id)" |
|
31 |
fi |
|
32 |
let v_id++ |
|
33 |
done |
|
34 |
unset v_id v |
|
35 | ||
ajout script d'analyse des v...
|
36 |
for g in ${!groupes[@]}; do |
complète re-écriture
|
37 |
test -z $(sqlite3 "$in_ram_database" <<< "select id from groupes where nom is '${groupes[$g]}' and nom_court is '$g'") \ |
38 |
&& sqlite3 "$in_ram_database" <<< "insert into groupes (nom, nom_court) values ('${groupes[$g]}', '$g')" |
|
ajout script d'analyse des v...
|
39 |
done |
complète re-écriture
|
40 |
unset g groupes |
ajout script d'analyse des v...
|
41 | |
complète re-écriture
|
42 |
test -z $(sqlite3 "$in_ram_database" <<< "select id from url where id = 0") \ |
43 |
&& sqlite3 "$in_ram_database" <<< "insert into url values (0, '')" |
|
44 |
} |
|
ajout script d'analyse des v...
|
45 | |
complète re-écriture
|
46 |
function update_database () { |
47 |
test "$no_db_update" = $true_flag && return |
|
48 |
tempfile="/dev/shm/scrutin.$$" |
|
49 |
progress=0 |
|
50 |
if test ${first:-0} -lt $last; then |
|
51 |
echo "récupération des scrutins n°$((${first:-0}+1)) à n°$last dans "$database" (à conserver autant que possible)" >&2 |
|
ajout script d'analyse des v...
|
52 | |
complète re-écriture
|
53 |
url_database=/dev/shm/url_database |
54 |
: > "$url_database" |
|
55 |
test $((last % 100)) -ne 0 && last_offset=0 |
|
56 |
for offset in $(seq $((last - 100)) -100 ${first:-0} ) $last_offset; do |
|
57 |
wget -qO- "http://www2.assemblee-nationale.fr/scrutins/liste/(offset)/$offset/(legislature)/15/(type)/TOUS/(idDossier)/TOUS" \ |
|
58 |
| awk ' |
|
59 |
/<td class="denom">/ { |
|
60 |
scrutin = gensub(/^.+denom.>([[:digit:]]+).*<.td./,"\\1","1",$0) |
|
61 |
} |
|
62 |
/<td class="desc">.+dossier<.a/ { |
|
63 |
a[scrutin] = gensub(/^.+.<a href="(.+)">dossier<.a>.*$/,"\\1","1",$0) |
|
64 |
} |
|
65 |
END { |
|
66 |
for (i in a) |
|
67 |
print gensub("*","","1",i) "|" a[i] |
|
68 |
}' >> "$url_database" |
|
69 |
done |
|
70 |
sort -u "$url_database" > "${url_database}.sorted" |
|
71 |
mv -f "${url_database}.sorted" "$url_database" |
|
72 | ||
73 |
IFS=$'\n' |
|
74 |
begin=$(date +%s) |
|
75 |
for scrutin in $(seq $((${first:-0}+1)) $last); do |
|
76 |
wget -qO- "http://www2.assemblee-nationale.fr/scrutins/detail/(legislature)/15/(num)/$scrutin" \ |
|
77 |
| sed -r '0,/< *div class="titre-bandeau-bleu +to-print" *>/d; /< *script +type="text\/javascript" *>/,$d' > $tempfile |
|
78 | ||
79 |
unset title date adoption url id_url |
|
80 | ||
81 |
title=$(sed -rn '/<h1 class="">Analyse du scrutin n° '$scrutin'/n; s,^.*<h3 class="president-title">(.+).</h3>,\1,p' $tempfile \ |
|
82 |
| sed "s/;//g; s/[ \t][ \t]+/ /g; s/^Scrutin public sur *//; s/^l[ae']s* *//") |
|
83 |
date=$(sed -rn 's,^.*<h1 class="">Analyse du scrutin n° '$scrutin'<br/>(.+) </h1>,\1,p' $tempfile) |
|
84 |
adoption=$(sed -rn 's,^.*<p class="annonce"><span class="annoncevote">(.+).</span></p>.*$,\1,p' $tempfile) |
|
85 |
test -n "$title" -a -n "$date" -a -n "$adoption" || { |
|
86 |
echo "erreur dans la récupération du scrutin $scrutin" |
|
87 |
exit 1 |
|
88 |
} |
|
89 |
grep -q 'e a a' <<< "$adoption" && adoption=1 || adoption=0 |
|
petite simplification du cod...
|
90 | |
complète re-écriture
|
91 |
url=$(awk -F'|' "/^$scrutin\|/{print \$2}" "$url_database") |
92 |
id_url=$(sqlite3 "$in_ram_database" <<< "select id from url where url is '$url'") |
|
93 |
if test -z "$id_url"; then |
|
94 |
sqlite3 "$in_ram_database" <<< "insert into url (url) values ('$url')" |
|
95 |
id_url=$(sqlite3 "$in_ram_database" <<< "select id from url where url is '$url'") |
|
96 |
fi |
|
écriture directe en feuille ...
|
97 | |
complète re-écriture
|
98 |
sqlite3 "$in_ram_database" <<< "insert into scrutins values ($scrutin, '$date', \"${title//\"}\", $adoption, ${id_url:-0})" |
99 | ||
100 |
for v in $(sqlite3 "$in_ram_database" <<< "select * from votes"); do |
|
101 |
for g in $(sqlite3 "$in_ram_database" <<< "select id,nom from groupes"); do |
|
102 |
for d in $(sed -rn '/<p class="nomgroupe">'${g#*|}' <span class="block topmargin">/,/<div class="TTgroupe topmargin-lg">/p' $tempfile \ |
|
103 |
| sed -rn '/<p class="typevote">'${v#*|}':/,/<.div>/p' \ |
|
104 |
| sed 's,</li>,\n,g' \ |
|
105 |
| sed -rn '/<p class="typevote">/d; s,^\s*<li>\s*,,; s, , ,g; s/^\s*//; s/M(me|\.) //; s/ \(.*$//; s,<b>,,; s,</b>,,p'); do |
|
106 |
d_id=$(sqlite3 "$in_ram_database" <<< "select id from députés where nom is \"$d\" and groupe is ${g%|*}") |
|
107 |
if test -z "$d_id"; then |
|
108 |
sqlite3 "$in_ram_database" <<< "insert into députés (nom, groupe) values (\"$d\", ${g%|*})" |
|
109 |
d_id=$(sqlite3 "$in_ram_database" <<< "select id from députés where nom is \"$d\" and groupe is ${g%|*}") |
|
110 |
fi |
|
111 |
sqlite3 "$in_ram_database" <<< "insert into dépouillement values ($d_id, $scrutin, ${g%|*}, ${v%|*})" |
|
112 |
done |
|
113 |
done |
|
114 |
done |
|
115 | ||
116 |
if test $((($scrutin*100)/$last)) -ne $progress; then |
|
117 |
progress=$((($scrutin*100)/$last)) |
|
118 |
if test $(($progress % ${update_progress:-1})) -eq 0; then |
|
119 |
now=$(date +%s) |
|
120 |
delta=$(( $now - $begin )) |
|
121 |
echo $progress%, ETA: $(date +%H:%M:%S -d "$(($delta * ($last - $scrutin) / $scrutin)) seconds") |
|
122 |
fi |
|
123 |
fi |
|
124 |
done |
|
125 |
rm -f "$url_database" "$tempfile" |
|
126 |
fi |
|
127 |
} |
|
128 | ||
129 |
function write_comparaison () { |
|
130 |
result="comparaisons $cible avec $groupe_ref${dossier:+ - ${dossier}}" |
|
131 |
content="/dev/shm/$result/content.xml" |
|
132 |
id_cols="Scrutin Date Titre Adoption" |
|
133 |
typevotes=$(sqlite3 "$in_ram_database" <<< "select nom from votes") |
|
134 |
nb_cols=$(wc -w <<< "$id_cols $typevotes $typevotes") |
|
135 | ||
136 |
echo "génération du fichier $result" |
|
137 | ||
138 |
mkdir -p "/dev/shm/$result/META-INF" |
|
139 | ||
140 |
cat > "/dev/shm/$result/META-INF/manifest.xml" << EOmetainf |
|
écriture directe en feuille ...
|
141 |
<?xml version="1.0" encoding="UTF-8"?> |
142 |
<manifest:manifest xmlns:manifest="urn:oasis:names:tc:opendocument:xmlns:manifest:1.0" manifest:version="1.2"> |
|
143 |
<manifest:file-entry manifest:full-path="/" manifest:version="1.2" manifest:media-type="application/vnd.oasis.opendocument.spreadsheet"/> |
|
144 |
<manifest:file-entry manifest:full-path="content.xml" manifest:media-type="text/xml"/> |
|
145 |
</manifest:manifest> |
|
146 |
EOmetainf |
|
147 | ||
complète re-écriture
|
148 |
printf 'application/vnd.oasis.opendocument.spreadsheet' > "/dev/shm/$result/mimetype" |
écriture directe en feuille ...
|
149 | |
complète re-écriture
|
150 |
echo '<?xml version="1.0" encoding="UTF-8"?>' > "$content" |
écriture directe en feuille ...
|
151 | |
complète re-écriture
|
152 |
cat >> "$content" << EOcontent |
153 |
<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"> |
|
154 |
<office:scripts/> |
|
155 |
<office:font-face-decls> |
|
156 |
<style:font-face style:name="Liberation Sans" svg:font-family="'Liberation Sans'" style:font-family-generic="swiss" style:font-pitch="variable"/> |
|
157 |
<style:font-face style:name="DejaVu Sans" svg:font-family="'DejaVu Sans'" style:font-family-generic="system" style:font-pitch="variable"/> |
|
158 |
<style:font-face style:name="FreeSans" svg:font-family="FreeSans" style:font-family-generic="system" style:font-pitch="variable"/> |
|
159 |
</office:font-face-decls> |
|
160 |
<office:automatic-styles> |
|
161 |
EOcontent |
|
162 | ||
163 |
for i in $(seq $nb_cols); do |
|
164 |
cat >> "$content" << EOcontent |
|
165 |
<style:style style:name="co$i" style:family="table-column"> |
|
166 |
<style:table-column-properties fo:break-before="auto" style:column-width="30.00mm"/> |
|
167 |
</style:style> |
|
écriture directe en feuille ...
|
168 |
EOcontent |
complète re-écriture
|
169 |
done |
écriture directe en feuille ...
|
170 | |
171 |
cat >> "$content" << EOcontent |
|
complète re-écriture
|
172 |
<style:style style:name="ro1" style:family="table-row"> |
173 |
<style:table-row-properties style:row-height="4.52mm" fo:break-before="auto" style:use-optimal-row-height="true"/> |
|
174 |
</style:style> |
|
175 |
<style:style style:name="ta1" style:family="table" style:master-page-name="Default"> |
|
176 |
<style:table-properties table:display="true" style:writing-mode="lr-tb"/> |
|
177 |
</style:style> |
|
178 |
<style:style style:name="ce1" style:family="table-cell" style:parent-style-name="Default"> |
|
179 |
<style:table-cell-properties fo:background-color="#cccccc"/> |
|
180 |
</style:style> |
|
181 |
</office:automatic-styles> |
|
182 |
<office:body> |
|
183 |
<office:spreadsheet> |
|
184 |
<table:calculation-settings table:automatic-find-labels="false"/> |
|
185 |
<table:table table:name="$result" table:style-name="ta1"> |
|
186 |
<office:forms form:automatic-focus="false" form:apply-design-mode="false"/> |
|
187 |
<table:table-column table:style-name="co1" table:number-columns-repeated="$(wc -w <<< $id_cols)" table:default-cell-style-name="Default"/> |
|
écriture directe en feuille ...
|
188 |
EOcontent |
189 | ||
complète re-écriture
|
190 |
for i in $(seq $(wc -w <<< $typevotes)); do |
191 |
cat >> "$content" << EOcontent |
|
192 |
<table:table-column table:style-name="co1" table:default-cell-style-name="ce1"/> |
|
193 |
<table:table-column table:style-name="co1" table:default-cell-style-name="Default"/> |
|
écriture directe en feuille ...
|
194 |
EOcontent |
complète re-écriture
|
195 |
done |
196 |
echo '<table:table-row table:style-name="ro1">' >> "$content" |
|
écriture directe en feuille ...
|
197 | |
complète re-écriture
|
198 |
for colonne in $id_cols; do |
199 |
cat >> "$content" << EOcontent |
|
200 |
<table:table-cell office:value-type="string" calcext:value-type="string"> |
|
201 |
<text:p>$colonne</text:p> |
|
202 |
</table:table-cell> |
|
203 |
EOcontent |
|
204 |
done |
|
écriture directe en feuille ...
|
205 | |
complète re-écriture
|
206 |
for typevote in $typevotes; do |
207 |
for g in "$cible" $groupe_ref; do |
|
208 |
cat >> "$content" << EOcontent |
|
209 |
<table:table-cell office:value-type="string" calcext:value-type="string"> |
|
210 |
<text:p>$typevote - $g</text:p> |
|
211 |
</table:table-cell> |
|
écriture directe en feuille ...
|
212 |
EOcontent |
complète re-écriture
|
213 |
done |
214 |
done |
|
215 | ||
216 |
echo '</table:table-row>' >> "$content" |
|
217 | ||
218 |
progress=0 |
|
219 |
begin=$(date +%s) |
|
220 |
line=1 |
|
221 |
for scrutin in $(eval ${seq:-seq $first $last}); do |
|
222 | ||
223 |
data=$(sqlite3 "$in_ram_database" <<< "select date,intitulé,adoption,url.url from scrutins inner join url on scrutins.url = url.id where num is $scrutin") |
|
224 |
date=$(cut -d'|' -sf 1 <<< $data) |
|
225 |
title=$(cut -d'|' -sf 2 <<< $data) |
|
226 |
adoption=$(cut -d'|' -sf 3 <<< $data) |
|
227 |
url=$(cut -d'|' -sf 4 <<< $data) |
|
228 |
test $adoption -eq 1 && adoption='oui' || adoption='non' |
|
groupe de référence: GDR
|
229 | |
écriture directe en feuille ...
|
230 |
cat >> "$content" << EOcontent |
complète re-écriture
|
231 |
<table:table-row table:style-name="ro1"> |
232 |
EOcontent |
|
233 |
if test -n "$url"; then |
|
234 |
cat >> "$content" << EOcontent |
|
235 |
<table:table-cell office:value-type="string" calcext:value-type="string"> |
|
236 |
<text:p><text:a xlink:href="$url" xlink:type="simple">$scrutin</text:a></text:p> |
|
237 |
</table:table-cell> |
|
écriture directe en feuille ...
|
238 |
EOcontent |
complète re-écriture
|
239 |
else |
240 |
cat >> "$content" << EOcontent |
|
241 |
<table:table-cell office:value-type="float" office:value="$scrutin" calcext:value-type="float"> |
|
242 |
<text:p>$scrutin</text:p> |
|
243 |
</table:table-cell> |
|
244 |
EOcontent |
|
245 |
fi |
|
246 |
cat >> "$content" << EOcontent |
|
247 |
<table:table-cell office:value-type="string" calcext:value-type="string"> |
|
248 |
<text:p>$date</text:p> |
|
249 |
</table:table-cell> |
|
250 |
<table:table-cell office:value-type="string" calcext:value-type="string"> |
|
251 |
<text:p>${title//\'/'}</text:p> |
|
252 |
</table:table-cell> |
|
253 |
<table:table-cell office:value-type="string" calcext:value-type="string"> |
|
254 |
<text:p>${adoption}</text:p> |
|
255 |
</table:table-cell> |
|
256 |
EOcontent |
|
257 |
for typevote in 0 1 2 3; do |
|
258 |
cible_votes=$(sqlite3 "$in_ram_database" <<< "select |
|
259 |
count(député) |
|
260 |
from |
|
261 |
dépouillement |
|
262 |
where |
|
263 |
scrutin is $scrutin |
|
264 |
and |
|
265 |
vote is $typevote |
|
266 |
and |
|
267 |
groupe is $groupe_id ${nom:+ and député is ${nom%|*}}") |
|
268 |
ref_votes=$(sqlite3 "$in_ram_database" <<< "select |
|
269 |
count(député) |
|
270 |
from |
|
271 |
dépouillement |
|
272 |
where |
|
273 |
scrutin is $scrutin |
|
274 |
and |
|
275 |
vote is $typevote |
|
276 |
and |
|
277 |
groupe is $groupe_ref_id") |
|
278 |
cat >> "$content" << EOcontent |
|
279 |
<table:table-cell office:value-type="float" office:value="$cible_votes" calcext:value-type="float"> |
|
280 |
<text:p>$cible_votes</text:p> |
|
281 |
</table:table-cell> |
|
282 |
<table:table-cell office:value-type="float" office:value="$ref_votes" calcext:value-type="float"> |
|
283 |
<text:p>$ref_votes</text:p> |
|
284 |
</table:table-cell> |
|
285 |
EOcontent |
|
286 |
done |
|
287 |
echo '</table:table-row>' >> "$content" |
|
288 | ||
meilleur calcul progression
|
289 |
if test $(( ($line * 100) / ${qty:-$last} )) -ne $progress; then |
290 |
progress=$(( ($line * 100) / ${qty:-$last} )) |
|
291 |
if test $(( $progress % ${generation_progress:-5} )) -eq 0; then |
|
complète re-écriture
|
292 |
now=$(date +%s) |
293 |
delta=$(( $now - $begin )) |
|
meilleur calcul progression
|
294 |
echo $progress%, ETA: $(date +%H:%M:%S -d "$(( $delta * (${qty:-$last} - $line) / $line )) seconds") |
complète re-écriture
|
295 |
fi |
296 |
fi |
|
meilleur calcul progression
|
297 | |
298 |
let line++ |
|
299 | ||
ajout script d'analyse des v...
|
300 |
done |
complète re-écriture
|
301 |
echo |
302 | ||
303 |
cat >> "$content" << EOcontent |
|
304 |
</table:table> |
|
305 |
<table:named-expressions/> |
|
306 |
<table:database-ranges> |
|
307 |
<table:database-range table:name="__Anonymous_Sheet_DB__0" table:target-range-address="'$result'.D1:'$result'.$(printf "\\$(printf '%03o' $((64+$nb_cols)))")$line" table:display-filter-buttons="true"/> |
|
308 |
</table:database-ranges> |
|
309 |
</office:spreadsheet> |
|
310 |
</office:body> |
|
311 |
</office:document-content> |
|
312 |
EOcontent |
|
313 | ||
314 |
( cd "/dev/shm/$result" && zip -r ../"$result" * > /dev/null 2>&1 && cd .. && rm -fr "$result" ) |
|
315 | ||
316 |
mv -f "/dev/shm/$result.zip" "$result.ods" |
|
317 | ||
318 |
echo "$result.ods" |
|
319 |
} |
|
320 | ||
321 |
function save_database () { |
|
322 |
if test -r "$database" && md5sum $in_ram_database | sed "s,$in_ram_database,$database," | md5sum --status -c -; then |
|
323 |
rm -f $in_ram_database |
|
324 |
elif test -w "$database"; then |
|
325 |
mv -f $in_ram_database "$database" |
|
326 |
elif ! test -e "$database"; then |
|
327 |
mv $in_ram_database "$database" |
|
328 |
else |
|
329 |
rm -f $in_ram_database |
|
330 |
fi |
|
331 |
} |
|
332 | ||
333 |
trap save_database EXIT |
|
334 | ||
335 |
true_flag=$(mktemp --dry-run XXXXX) |
|
336 | ||
337 |
OPTS=$( getopt -l no-db-update,\ |
|
338 |
db-update-only,\ |
|
339 |
cible:,\ |
|
340 |
ref:,\ |
|
341 |
député:,\ |
|
342 |
premier-scrutin:,\ |
|
343 |
dernier-scrutin:,\ |
|
344 |
période:,\ |
|
345 |
liste-dossiers,\ |
|
346 |
liste-députés,\ |
|
347 |
dossiers,\ |
|
348 |
dossier:,\ |
|
349 |
conf:,\ |
|
350 |
database:,\ |
|
351 |
progrès-génération:\ |
|
352 |
progrès-update: \ |
|
353 |
-- "$@" ) |
|
354 | ||
355 |
eval set --$OPTS |
|
356 | ||
357 |
while [[ $# -gt 0 ]]; do |
|
358 |
case "$1" in |
|
359 |
"--no-db-update") |
|
360 |
no_db_update=$true_flag;; |
|
361 |
"--db-update-only") |
|
362 |
db_update_only=$true_flag;; |
|
363 |
"--cible") |
|
364 |
groupe="${2^^}" |
|
365 |
shift;; |
|
366 |
"--ref") |
|
fix typo
|
367 |
groupe_ref="${2^^}" |
complète re-écriture
|
368 |
shift;; |
369 |
"--député") |
|
370 |
depute=$true_flag |
|
371 |
nom="$2" |
|
372 |
shift;; |
|
373 |
"--premier-scrutin") |
|
374 |
no_db_update=$true_flag |
|
375 |
first="$2" |
|
376 |
shift;; |
|
377 |
"--dernier-scrutin") |
|
378 |
no_db_update=$true_flag |
|
379 |
last="$2" |
|
380 |
shift;; |
|
381 |
"--période") |
|
382 |
periode=$true_flag |
|
383 |
no_db_update=$true_flag |
|
384 |
periode_value="$2" |
|
385 |
shift;; |
|
386 |
"--liste-députés-du-groupe") |
|
387 |
liste_deputes=$true_flag |
|
388 |
liste_deputes_value="${2^^}" |
|
389 |
shift;; |
|
390 |
"--liste-députés") |
|
391 |
liste_deputes=$true_flag;; |
|
392 |
"--liste-dossiers") |
|
393 |
liste_dossiers=$true_flag;; |
|
394 |
"--dossier") |
|
395 |
dossier=$true_flag |
|
396 |
dossier_value="$2" |
|
397 |
shift;; |
|
398 |
"--dossiers") |
|
399 |
dossier=$true_flag;; |
|
400 |
"--conf") |
|
401 |
test -r "$2" || { |
|
402 |
echo "config introuvable $2" >&2 |
|
403 |
options_error=$true_flag |
|
404 |
} |
|
405 |
config_file="$2" |
|
406 |
shift;; |
|
407 |
"--database") |
|
408 |
test -r "$2" && file -b "$2" | grep -q '^SQLite 3.x database' || { |
|
409 |
echo "erreur sur option database: fichier '$2' introuvable ou pas une base SQLite 3" >&2 |
|
410 |
options_error=$true_flag |
|
411 |
} |
|
412 |
database="$2" |
|
413 |
shift;; |
|
414 |
"--progrès-génération") |
|
415 |
generation_progress="$2" |
|
416 |
shift;; |
|
417 |
"--progrès-update") |
|
418 |
update_progress="$2" |
|
419 |
shift;; |
|
420 |
esac |
|
421 |
shift |
|
ajout script d'analyse des v...
|
422 |
done |
écriture directe en feuille ...
|
423 | |
complète re-écriture
|
424 |
test "$options_error" = $true_flag && exit 1 |
ajout script d'analyse des v...
|
425 | |
complète re-écriture
|
426 |
test -z "$database" && database="${0}.db" |
utilsation d'une archive com...
|
427 | |
complète re-écriture
|
428 |
if test -n "$config_file"; then |
429 |
source "$config_file" |
|
430 |
else |
|
431 |
config_file="${0}.conf" |
|
432 |
if test -r "$config_file"; then |
|
433 |
source "$config_file" |
|
434 |
fi |
|
utilsation d'une archive com...
|
435 |
fi |
436 | ||
complète re-écriture
|
437 |
groupe=${groupe:-LREM} |
438 |
groupe_ref=${groupe_ref:-GDR} |
|
439 |
cible=$groupe |
|
ajout script d'analyse des v...
|
440 | |
complète re-écriture
|
441 |
in_ram_database=$(mktemp --dry-run /dev/shm/XXXXXXXXXXXX) |
442 |
if test -r "$database"; then |
|
443 |
cp "$database" "$in_ram_database" |
|
444 |
else |
|
445 |
create_database |
|
446 |
fi |
|
ajout script d'analyse des v...
|
447 | |
complète re-écriture
|
448 |
for g in groupe groupe_ref; do |
449 |
eval "${g}_id=$(sqlite3 "$in_ram_database" <<< "select id from groupes where nom_court is '${!g}'")" |
|
450 |
if eval "test -z \$${g}_id"; then |
|
451 |
echo "groupe ${!g} inconnu" >&2 |
|
452 |
exit 1 |
|
453 |
fi |
|
454 |
done |
|
ajout script d'analyse des v...
|
455 | |
complète re-écriture
|
456 |
if test "$periode" = $true_flag; then |
457 |
first=$(sqlite3 "$in_ram_database" <<< "select num from scrutins where date like '% du ${periode_value%:*}' order by num asc" | head -1) |
|
458 |
last=$(sqlite3 "$in_ram_database" <<< "select num from scrutins where date like '% du ${periode_value#*:}' order by num asc" | tail -1) |
|
459 |
test -z "$first" && echo "date de début inconnue: ${periode_value#*:}" >&2 && rm -f $in_ram_database && exit 1 |
|
460 |
test -z "$last" && echo "date de fin inconnue: ${periode_value%:*}" >&2 && rm -f $in_ram_database && exit 1 |
|
461 |
else |
|
462 |
test -z "$last" && last=$(wget -qO- 'http://www2.assemblee-nationale.fr/scrutins/liste/(legislature)/15/(type)/TOUS/(idDossier)/TOUS' \ |
|
463 |
| sed -rn 's,^.*<td class="denom">(.+)</td>.*$,\1,p' \ |
|
464 |
| head -1) |
|
écriture directe en feuille ...
|
465 | |
complète re-écriture
|
466 |
test -z "$first" && first=$(sqlite3 "$in_ram_database" <<< "select count(num) from scrutins") |
467 |
fi |
|
affiche nom du fichier des r...
|
468 | |
complète re-écriture
|
469 |
if test "$liste_dossiers" = $true_flag; then |
470 |
sqlite3 "$in_ram_database" <<< "select printf('%s - %s', id, url) from url" | sed 's,https*://.*/dossiers/,,; s/_/ /g; s/.asp$//' |
|
471 |
exit |
|
472 |
fi |
|
473 | ||
474 |
if test "$db_update_only" = $true_flag; then |
|
475 |
unset first last |
|
476 |
update_database |
|
477 |
exit |
|
478 |
fi |
|
479 | ||
480 |
if test "$liste_deputes" = $true_flag; then |
|
481 |
if test -n "$liste_deputes_value"; then |
|
fix erreur de syntaxe
|
482 |
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'" |
complète re-écriture
|
483 |
else |
fix erreur de syntaxe
|
484 |
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" |
complète re-écriture
|
485 |
fi |
486 |
exit |
|
487 |
fi |
|
écriture directe en feuille ...
|
488 | |
complète re-écriture
|
489 |
if test "$depute" = $true_flag; then |
490 |
if test -n "$nom"; then |
|
491 |
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") |
|
492 |
if test $match -ne 1; then |
|
493 |
if test $match -eq 0; then |
|
494 |
echo "pas de député correspondant dans le groupe $groupe" |
|
495 |
else |
|
496 |
echo "plusieurs députés correspondent:" |
|
497 |
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" |
|
498 |
fi |
|
499 |
exit 1 |
|
500 |
else |
|
501 |
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") |
|
502 |
cible="${nom#*|} ($groupe)" |
|
503 |
fi |
|
504 |
fi |
|
505 |
fi |
|
506 | ||
507 |
if test "$dossier" = $true_flag; then |
|
508 |
if test -z "$dossier_value"; then |
|
509 |
IFS_=$IFS |
|
510 |
IFS=$'\n' |
|
511 |
select dossier in $(sqlite3 "$in_ram_database" <<< "select url from url" | sed 's,^.*/dossiers/,,; s/_/ /g; s/.asp$//'); do |
|
512 |
if test -n "$dossier"; then |
|
513 |
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\"" |
|
meilleur calcul progression
|
514 |
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") |
complète re-écriture
|
515 |
break |
516 |
fi |
|
517 |
done |
|
518 |
IFS=$IFS_ |
|
519 |
else |
|
520 |
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\"" |
|
meilleur calcul progression
|
521 |
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") |
complète re-écriture
|
522 |
dossier=$(sqlite3 "$in_ram_database" <<< "select url from url where id is $dossier_value" | sed 's,^.*/dossiers/,,; s/_/ /g; s/.asp$//') |
523 |
fi |
|
524 |
fi |
|
écriture directe en feuille ...
|
525 | |
complète re-écriture
|
526 |
update_database |
527 |
write_comparaison |