1 | #!/usr/bin/perl |
---|
2 | |
---|
3 | $file = shift; |
---|
4 | use HTML::TokeParser; |
---|
5 | use File::stat; |
---|
6 | use Date::Format; |
---|
7 | require ("../common/common.pm"); |
---|
8 | |
---|
9 | my %amdmt; |
---|
10 | |
---|
11 | $source = $file; |
---|
12 | $source =~ s/^.*(http.*)$/\1/i; |
---|
13 | $source =~ s/_/\//g; |
---|
14 | |
---|
15 | # Récupération des informations identifiantes à partir de l'url plus sure : |
---|
16 | if ($source =~ /(\d{2})\/amendements\/(\d{4})\/(\d{4})(\d|[A-Z])(\d{4})\./i) { |
---|
17 | $amdmt{'legislature'} = $1; |
---|
18 | if ($2-$3 == 0) { |
---|
19 | $amdmt{'loi'} = $3+0; |
---|
20 | } |
---|
21 | $num = $5+0; |
---|
22 | $lettre = $4; |
---|
23 | if ($lettre =~ /[a-z]/i) { |
---|
24 | $amdmt{'numero'} = $num.uc($lettre); |
---|
25 | } else { |
---|
26 | $amdmt{'numero'} = (10000*$lettre+$num); |
---|
27 | } |
---|
28 | } elsif ($source =~ /(\d{2})\/amendements\/(\d{4})\/(\d+)\./i) { |
---|
29 | $amdmt{'legislature'} = $1; |
---|
30 | $amdmt{'loi'} = $2+0; |
---|
31 | $num = $3+0; |
---|
32 | $amdmt{'numero'} = $num; |
---|
33 | } |
---|
34 | |
---|
35 | open(FILE, $file) ; |
---|
36 | @string = <FILE>; |
---|
37 | $string = "@string"; |
---|
38 | utf8::decode($string) if ($string =~ /charset=UTF-?8/i); |
---|
39 | $string =~ s/(\<p class="presente".*)\s*\<br[\/]?\>\s*[\n]?\s*(.*)/\1, \2/g; |
---|
40 | $string =~ s/\<br\>.*\n//g; |
---|
41 | $string =~ s/(’|’)/'/g; |
---|
42 | $string =~ s/œ/oe/g; |
---|
43 | $string =~ s/–/-/g; |
---|
44 | $string =~ s/É/É/g; |
---|
45 | $string =~ s/é/é/g; |
---|
46 | $string =~ s/È/È/g; |
---|
47 | $string =~ s/è/è/g; |
---|
48 | $string =~ s/À/À/g; |
---|
49 | $string =~ s/à/à/g; |
---|
50 | $string =~ s/\\//g; |
---|
51 | close FILE; |
---|
52 | |
---|
53 | my $presente = 0; |
---|
54 | my $texte = 0; |
---|
55 | my $identiques = 0; |
---|
56 | my $num_ident = -1; |
---|
57 | |
---|
58 | sub numero { |
---|
59 | $line =~ s/^.*content="//; |
---|
60 | $line =~ s/".*$//; |
---|
61 | $line =~ s/[\(\)]//g; |
---|
62 | if ($line =~ /^\s*(\d+)\s+([1-9a-zA-Z].*)$/i) { |
---|
63 | # $amdmt{'numero'} = $1; |
---|
64 | $suite = $2; |
---|
65 | if (!$suite =~ /rect/i) { |
---|
66 | $amdmt{'rectif'} = 0; |
---|
67 | } else { |
---|
68 | if ($suite =~ /(\d+)/) { |
---|
69 | $amdmt{'rectif'} = $1; |
---|
70 | } else { |
---|
71 | $amdmt{'rectif'} = 1; |
---|
72 | } |
---|
73 | if ($suite =~ /bis/i) { |
---|
74 | $amdmt{'rectif'}++; |
---|
75 | } |
---|
76 | } |
---|
77 | } else { |
---|
78 | $line =~ /(\d+)/; |
---|
79 | # $amdmt{'numero'} = $1; |
---|
80 | $amdmt{'rectif'} = 0; |
---|
81 | } |
---|
82 | } |
---|
83 | |
---|
84 | sub auteurs { |
---|
85 | $line =~ s/\<br\/\>/, /g; |
---|
86 | $line =~ s/\s*\<\/?[^\>]+\>//g; |
---|
87 | $line =~ s/\s*présenté\s*par\s*//g; |
---|
88 | $line =~ s/\s+e{1,2}t\s+/, /g; |
---|
89 | $line =~ s/^et\s+/, /g; |
---|
90 | $line =~ s/\s*EXPOSÉ SOMMAIRE\s*//g; |
---|
91 | $line =~ s/\s+,/,/g; |
---|
92 | $line =~ s/^de M/M/; |
---|
93 | if (!$line =~ /^$/ && (!$amdmt{'auteurs'} || $amdmt{'auteurs'} !~ /, $line/)) { |
---|
94 | $amdmt{'auteurs'} = $amdmt{'auteurs'}.", ".$line; |
---|
95 | } |
---|
96 | } |
---|
97 | |
---|
98 | sub texte { |
---|
99 | $line2 = $line; |
---|
100 | $line2 =~ s/\s*\<\/?[^\>]+\>//g; |
---|
101 | $line2 =~ s/^[ \s]+(\w)/\1/; |
---|
102 | $line2 =~ s/[\s ]+$//; |
---|
103 | if ($line2 !~ /^$/ && !($line2 =~ /\s*Adt\s+n°\s*$/)) { |
---|
104 | $output = 'texte'; |
---|
105 | if ($texte == 2) { |
---|
106 | return if $line2 =~ /^\s*\d+\s*$/; |
---|
107 | $output = 'expose'; |
---|
108 | } |
---|
109 | $line2 =~ s/"\s*([^"]*)\s*"/« \1 »/g; |
---|
110 | if (!$amdmt{$output}) { $amdmt{$output} = "<p>".$line2."</p>"; } |
---|
111 | else { $amdmt{$output} = $amdmt{$output}."<p>".$line2."</p>"; } |
---|
112 | } |
---|
113 | } |
---|
114 | |
---|
115 | sub sortseance { |
---|
116 | if ($line =~ /irrecevable/i) { |
---|
117 | $amdmt{'sort'} = 'Irrecevable'; |
---|
118 | } elsif ($line =~ /retir.+s.+ance/i) { |
---|
119 | $amdmt{'sort'} = 'Retiré avant séance'; |
---|
120 | } elsif ($line =~ /retiré/i) { |
---|
121 | $amdmt{'sort'} = 'Retiré'; |
---|
122 | } elsif ($line =~ /non.*(soutenu|défendu)/i) { |
---|
123 | $amdmt{'sort'} = 'Non soutenu'; |
---|
124 | } elsif ($line =~ /tomb/i) { |
---|
125 | $amdmt{'sort'} = 'Tombe'; |
---|
126 | } elsif ($line =~ /rejet/i) { |
---|
127 | $amdmt{'sort'} = 'Rejeté'; |
---|
128 | } elsif ($line =~ /adopt/i) { |
---|
129 | $amdmt{'sort'} = 'Adopté'; |
---|
130 | } elsif ($line =~ /Re/) { |
---|
131 | $amdmt{'sort'} = 'Retiré'; |
---|
132 | } |
---|
133 | } |
---|
134 | |
---|
135 | sub identiques { |
---|
136 | if ($line =~ /\<div\>\s*(de)?\s*(M[\s\.Mml].*)\s*\<\/div\>/) { |
---|
137 | $line = $2; |
---|
138 | if ($amdmt{'numero'} != $num_ident) { |
---|
139 | auteurs(); |
---|
140 | } |
---|
141 | } else { |
---|
142 | $line =~ s/\s*\<\/?[^\>]+\>//g; |
---|
143 | if ($line =~ /^.*Adt\s+n°\s+(\d+).*\s+de\s+(.*)\s*$/) { |
---|
144 | $num = $1; |
---|
145 | $line = $2; |
---|
146 | if ($num_ident == -1) { |
---|
147 | $num_ident = $num; |
---|
148 | $amdmt{'serie'} = $num_ident."-"; |
---|
149 | } |
---|
150 | if ($amdmt{'numero'} != $num) { |
---|
151 | auteurs(); |
---|
152 | } |
---|
153 | if ($num > $num_ident + 1) { |
---|
154 | $amdmt{'serie'} = $amdmt{'serie'}.$num_ident.",".$num."-"; |
---|
155 | } |
---|
156 | $num_ident = $num; |
---|
157 | } elsif ($line =~ /^\s*(\d+)\s*$/) { |
---|
158 | $num = $1; |
---|
159 | if ($num_ident == -1) { |
---|
160 | $num_ident = $num; |
---|
161 | $amdmt{'serie'} = $num_ident."-"; |
---|
162 | } |
---|
163 | if ($num > $num_ident + 1) { |
---|
164 | $amdmt{'serie'} = $amdmt{'serie'}.$num_ident.",".$num."-"; |
---|
165 | } |
---|
166 | $num_ident = $num; |
---|
167 | } elsif ($line =~ /^\s*de\s+(M.*)\s*$/) { |
---|
168 | $line = $1; |
---|
169 | if ($amdmt{'numero'} != $num) { |
---|
170 | auteurs(); |
---|
171 | } |
---|
172 | $num_ident = $num; |
---|
173 | } |
---|
174 | } |
---|
175 | } |
---|
176 | |
---|
177 | |
---|
178 | |
---|
179 | $string =~ s/\r//g; |
---|
180 | $string =~ s/\t+/ /g; |
---|
181 | $string =~ s/ +\n+/\n/g; |
---|
182 | $string =~ s/\n+ +/\n/g; |
---|
183 | $string =~ s/ / /g; |
---|
184 | $string =~ s/\|(\W+)\|/$1/g; |
---|
185 | $string =~ s/([^\n>]+)\n/\1 /g; |
---|
186 | $string =~ s/>\n([^\n<]+)/> \1/g; |
---|
187 | foreach $line (split /\n/, $string) |
---|
188 | { |
---|
189 | #print "TEST: $presente / $texte / $line\n"; |
---|
190 | if ($line =~ /meta.*content=/) { |
---|
191 | if ($line =~ /name="DATE_BADAGE"/i) { |
---|
192 | $line =~ s/^.*content="//i; |
---|
193 | $line =~ s/".*$//; |
---|
194 | if ($line =~ /(\d{1,2})\/(\d{2})\/(\d{4})/) { |
---|
195 | $amdmt{'date'} = $3.'-'.$2.'-'.sprintf('%02d', $1); |
---|
196 | } |
---|
197 | } elsif ($line =~ /name="DESIGNATION_ARTICLE"/i && !$amdmt{'sujet'}) { |
---|
198 | $line =~ s/^.*content="//i; |
---|
199 | $line =~ s/"\s*(name|\>).*$//; |
---|
200 | $amdmt{'sujet'} = $line; |
---|
201 | } elsif ($line =~ /name="SORT_EN_SEANCE"/i) { |
---|
202 | $line =~ s/^.*content="//i; |
---|
203 | $line =~ s/".*$//; |
---|
204 | sortseance(); |
---|
205 | } elsif ($line =~ /name="NUM_AM(TXT|ENDG?)"/i) { |
---|
206 | numero(); |
---|
207 | } |
---|
208 | } elsif ($line =~ /date_?amend.*([0-9]+e?r? \S+ [0-9]+)\D/i && !$amdmt{'date'}) { |
---|
209 | $amdmt{'date'} = join '/', reverse datize($1); |
---|
210 | } elsif ($line =~ /class="amddispotitre"/i && !$amdmt{'sujet'}) { |
---|
211 | $line =~ s/<[^>]+>//g; |
---|
212 | $amdmt{'sujet'} = $line; |
---|
213 | } elsif (($line =~ /class="presente"/i || $line =~ /<div>\s*(de )?M[Mmel.s]+ /) && $line !~ /utilisateurs/) { |
---|
214 | $texte = 0 if ($texte == 2); |
---|
215 | if ($presente != 1) { |
---|
216 | $presente = 1; |
---|
217 | } elsif ($texte >= 1 && $line =~ /font-style: italic/i) { |
---|
218 | texte(); |
---|
219 | } |
---|
220 | } elsif ($presente == 1 && $line =~ /class="tirets"/i) { |
---|
221 | $presente = 2; |
---|
222 | } |
---|
223 | if ($line =~ /(NOEXTRACT|EXPOSE)/i) { |
---|
224 | if (!$amdmt{'numero'} && ($line =~ /class="numamendement"/i || $line =~ /class="titreamend".*num_partie/i)) { |
---|
225 | if ($line =~ /\<num_amend\>\s*(.*)\s*\<\/num_amend\>/i) { |
---|
226 | $line = $1; |
---|
227 | numero(); |
---|
228 | } elsif ($line =~ /\<\/num_partie\>\s*-\s*(.*)\<\/p\>/i) { |
---|
229 | $line = $1; |
---|
230 | numero(); |
---|
231 | } |
---|
232 | } elsif (!$amdmt{'serie'} && ($line =~ /class="numamendement".*à\s+(\d+)\W/i)) { |
---|
233 | $num_ident = $1; |
---|
234 | $amdmt{'serie'} = ($amdmt{'numero'}+1).'-'; |
---|
235 | } elsif ($line =~ /class="presente"/i) { |
---|
236 | if ($line =~ /amendement/) { |
---|
237 | $line =~ /(\d+)/; |
---|
238 | $amdmt{'parent'} = $1; |
---|
239 | } elsif ($presente == 1) { |
---|
240 | auteurs(); |
---|
241 | } else { |
---|
242 | texte(); |
---|
243 | } |
---|
244 | } elsif ($line =~ /class="amddispotitre"/i) { |
---|
245 | $texte = 1; |
---|
246 | if ($line =~ /amendement.*[\s°](\d+)[\s\<]/i) { |
---|
247 | $amdmt{'parent'} = $1; |
---|
248 | } |
---|
249 | } elsif ($line =~ /class="amddispotexte"/i) { |
---|
250 | texte(); |
---|
251 | } elsif ($line =~ /class="amdexpotitre"/i) { |
---|
252 | if ($amdmt{'texte'} || !$line =~ /article/i) { |
---|
253 | $texte = 2; |
---|
254 | } |
---|
255 | } elsif ($line =~ /class="amdexpotexte"/i) { |
---|
256 | texte(); |
---|
257 | } elsif ($line =~ /amendements\s*identiques/i) { |
---|
258 | $identiques = 1; |
---|
259 | } elsif ($line =~ /\<div.*\>.*M[\.Mml]/ && !($line =~ /EXPOSE SOMMAIRE/i)) { |
---|
260 | if ($identiques == 1) { |
---|
261 | identiques(); |
---|
262 | } elsif ($presente == 1) { |
---|
263 | auteurs(); |
---|
264 | } |
---|
265 | } elsif ($identiques == 1 && $line =~ /\<div\>.*(\d+).*\<\/div\>/) { |
---|
266 | identiques(); |
---|
267 | } elsif ($texte >= 1 && $line =~ /\<div\>(.*)\<\/div\>/) { |
---|
268 | $line = $1; |
---|
269 | texte(); |
---|
270 | } |
---|
271 | } elsif ($line =~ /class="amddispotitre"/i) { |
---|
272 | $texte = 1; |
---|
273 | if ($line =~ /amendement.*[\s°](\d+)[\s\<]/i) { |
---|
274 | $amdmt{'parent'} = $1; |
---|
275 | } |
---|
276 | } elsif ($line =~ /class="amd(expo|dispo)texte"/i) { |
---|
277 | texte(); |
---|
278 | } elsif ($line =~ /class="amdexpotitre"/i) { |
---|
279 | if ($amdmt{'texte'} || !$line =~ /article/i) { |
---|
280 | $texte = 2; |
---|
281 | } |
---|
282 | } elsif ((!$amdt{'sort'} || $amdt{'sort'} == "") && ($line =~ /\<div.*id="sort"/i || $line =~ /retir.+ avant s.+ance/i)) { |
---|
283 | sortseance(); |
---|
284 | } elsif ($identiques == 1 && $line =~ /\<p style=".*text-align:.*\>.*M[\.Mml]/i) { |
---|
285 | identiques(); |
---|
286 | } elsif ($line =~ /class="presente"/i) { |
---|
287 | if ($line =~ /amendement/) { |
---|
288 | $line =~ /(\d+)/; |
---|
289 | $amdmt{'parent'} = $1; |
---|
290 | } elsif ($texte < 1) { |
---|
291 | auteurs() if ($line !~ /par<\/p>/); |
---|
292 | } else { |
---|
293 | texte(); |
---|
294 | } |
---|
295 | } elsif ($presente == 1 && $line =~ /<(p style=".*text-indent:.*|td align="center"[^>]*)>.*(M[\.Mml]|Le gouvern)/i) { |
---|
296 | auteurs(); |
---|
297 | } elsif ($line =~ /\<p style=".*text-indent:/i) { |
---|
298 | if ($line =~ /amendement.*(irrecevable|retir)/i) { |
---|
299 | if (!$amdmt{'sort'}) { |
---|
300 | if ($line =~ /irrecevable/i) { |
---|
301 | $amdmt{'sort'} = "Irrecevable"; |
---|
302 | } elsif ($line =~ /retir/i) { |
---|
303 | $amdmt{'sort'} = "Retiré avant séance"; |
---|
304 | } |
---|
305 | } |
---|
306 | } |
---|
307 | texte(); |
---|
308 | } elsif ($line =~ /\<p[^\>]*\>(.*)\<\/p\>/i && $texte >= 1) { |
---|
309 | $line = $1; |
---|
310 | texte(); |
---|
311 | } |
---|
312 | } |
---|
313 | |
---|
314 | if ($num_ident > 0) { |
---|
315 | $amdmt{'serie'} = $amdmt{'serie'}.$num_ident; |
---|
316 | } |
---|
317 | |
---|
318 | $amdmt{'auteurs'} =~ s/\s+Mme,\s*/ Mme /g; |
---|
319 | $amdmt{'auteurs'} =~ s/([a-z])\s+(M[\.Mml])/\1, \2/g; |
---|
320 | $amdmt{'auteurs'} =~ s/,\s*M[\s\.mle]+\s*,/,/g; |
---|
321 | $amdmt{'auteurs'} =~ s/\s*[,]?\s*les\s+[cC]ommissaires.*$//g; |
---|
322 | $amdmt{'auteurs'} =~ s/\s*[,]?\s*[rR]apporteur[\s,a-zéèêà\-']*M(.*)/, M\1/g; |
---|
323 | $amdmt{'auteurs'} =~ s/\s*[,]?\s*[rR]apporteur[\s,a-zéèêà\-']*//g; |
---|
324 | $amdmt{'auteurs'} =~ s/(,\s*,|,+)/,/g; |
---|
325 | $amdmt{'auteurs'} =~ s/,+/,/g; |
---|
326 | $amdmt{'auteurs'} =~ s/^\s*,\s*//g; |
---|
327 | $amdmt{'auteurs'} =~ s/\s*,\s*$//g; |
---|
328 | $amdmt{'auteurs'} =~ s/ et(M[mle\.\s])/, \1/g; |
---|
329 | $amdmt{'auteurs'} =~ s/ et(\W)/\1/g; |
---|
330 | $amdmt{'auteurs'} =~ s/([^,\s])\s*(les\s*membres.*groupe.*)$/\1, \2/i; |
---|
331 | |
---|
332 | if (!$amdmt{'date'}) { |
---|
333 | $time = stat($file)->mtime; |
---|
334 | $amdmt{'date'} = time2str("%Y-%m-%d", $time); |
---|
335 | } |
---|
336 | if (!$amdmt{'sort'} && $amdmt{'texte'} =~ /amendement irrecevable/i) { |
---|
337 | $amdmt{'sort'} = 'Irrecevable'; |
---|
338 | } |
---|
339 | |
---|
340 | print '{"source": "'.$source.'", "legislature": "'.$amdmt{'legislature'}.'", "loi": "'.$amdmt{'loi'}.'", "numero": "'.$amdmt{'numero'}.'", "serie": "'.$amdmt{'serie'}.'", "rectif": "'.$amdmt{'rectif'}.'", "parent": "'.$amdmt{'parent'}.'", "date": "'.$amdmt{'date'}.'", "auteurs": "'.$amdmt{'auteurs'}.'", "sort": "'.$amdmt{'sort'}.'", "sujet": "'.$amdmt{'sujet'}.'", "texte": "'.$amdmt{'texte'}.'", "expose": "'.$amdmt{'expose'}.'" } '."\n"; |
---|