From eeec6fd46f94ebbe8e0cd8ef5ffc88b6c5d03513 Mon Sep 17 00:00:00 2001 From: Emmy D'Anello Date: Sat, 1 Oct 2022 23:42:33 +0200 Subject: [PATCH] =?UTF-8?q?Infographie=20de=20r=C3=A9partition=20des=20vot?= =?UTF-8?q?es=20par=20groupe?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Emmy D'Anello --- procrasturgence/procrasturgence.py | 117 ++++++++++++++++++++++++++--- 1 file changed, 107 insertions(+), 10 deletions(-) diff --git a/procrasturgence/procrasturgence.py b/procrasturgence/procrasturgence.py index 4380bb9..5ddebf2 100755 --- a/procrasturgence/procrasturgence.py +++ b/procrasturgence/procrasturgence.py @@ -1,3 +1,4 @@ +from collections import OrderedDict import datetime import hashlib from io import BytesIO @@ -8,14 +9,32 @@ import requests import subprocess from zipfile import ZipFile +from bs4 import BeautifulSoup from tweepy import API, Client, OAuth1UserHandler from . import config -LEG = 16 BASE_PATH = os.path.dirname(os.path.dirname(__file__)) +LEG = 16 +DEPUTES = {} +GROUPES_ID = [ + "PO800490", + "PO800502", + "PO800526", + "PO800496", + "PO800538", + "PO800484", + "PO800514", + "PO800532", + "PO800508", + "PO800520", + "PO793087", +] +GROUPES = OrderedDict(**{k: None for k in GROUPES_ID}) + + def update_scrutins(): url = f"https://data.assemblee-nationale.fr/static/openData/repository/{LEG}/loi/scrutins/Scrutins.json.zip" md5 = requests.get(f"{url}.md5").content.decode().strip() @@ -100,28 +119,85 @@ def publish_amendement(obj, api, client): print(date, delta) syn = scrutin['syntheseVote'] + for g in scrutin['ventilationVotes']['organe']['groupes']['groupe']: + print(g['organeRef']) + + print(scrutin['ventilationVotes']['organe']['groupes']['groupe'][-1]) + print(sum(len((g['vote']['decompteNominatif']['pours'] or {'votant': []})['votant']) + len((g['vote']['decompteNominatif']['contres'] or {'votant': []})['votant']) for g in scrutin['ventilationVotes']['organe']['groupes']['groupe'])) + with open('/tmp/img.html', 'w') as f: f.write(content) subprocess.run(['wkhtmltoimage', '/tmp/img.html', '/tmp/img.jpg']) text_media = api.media_upload("/tmp/img.jpg") - api.create_media_metadata(text_media.media_id, content[:1000]) + api.create_media_metadata(text_media.media_id, BeautifulSoup(content.replace('

', '

\n\n'), features="lxml").get_text()[:1000]) + + vote_groupes = OrderedDict(**{k: None for k in GROUPES_ID}) + for g in scrutin['ventilationVotes']['organe']['groupes']['groupe']: + vote_groupes[g['organeRef']] = g['vote'] # FIXME Faites mieux - syn_text = f"

Pour : {syn['decompte']['pour']}

\n" + \ - f"

Contre : {syn['decompte']['contre']}

\n" + \ - f"

Abstentions : {syn['decompte']['abstentions']}

\n" + \ - f"

Exprimés : {syn['suffragesExprimes']}" + syn_html = '\n\n\n\n\n' + syn_html += '\n\n' + syn_html += f"\n" + syn_html += '\n\n' + for i, (gid, voix) in enumerate(vote_groupes.items()): + if i % 4 == 0: + if i != 0: + syn_html += "\n" + syn_html += "\n" + syn_html += f"\n" + + syn_html += "\n" + syn_html += "\n" + syn_html += "\n" + syn_html += f"\n\n\n\n\n" + syn_html += "

{scrutin['titre']}

\n" + syn_html += f"{GROUPES[gid]['libelleAbrege']}
\n" + syn_html += "" + syn_html += int(voix['decompteVoix']['pour']) * " ●" + syn_html += "" + syn_html += "" + syn_html += int(voix['decompteVoix']['contre']) * " ●" + syn_html += "" + syn_html += "" + syn_html += int(voix['decompteVoix']['abstentions']) * " ●" + syn_html += "" + syn_html += "" + syn_html += (len(GROUPES[gid]['deputes']) - int(voix['decompteVoix']['pour']) - int(voix['decompteVoix']['contre']) - int(voix['decompteVoix']['abstentions'])) * " ●" + syn_html += "\n
\n" + syn_html += f"Pour : {voix['decompteVoix']['pour']}
Contre : {voix['decompteVoix']['contre']}
Abstentions : {voix['decompteVoix']['abstentions']}\n" + syn_html += "
\n" + syn_html += "Total\n" + syn_html += "
\n" + syn_html += f"Pour : {syn['decompte']['pour']}
\n" + syn_html += f"Contre : {syn['decompte']['contre']}
\n" + syn_html += f"Abstentions : {syn['decompte']['abstentions']}
\n" + if scrutin['sort']['code'] == 'rejeté': + syn_html += "" + else: + syn_html += "" + syn_html += scrutin['sort']['libelle'] + syn_html += "\n" + syn_html += "
\nScrutin n°{scrutin['numero']}, réalisé le {datetime.date.fromisoformat(scrutin['dateScrutin']):%d/%m/%Y}
\n\n" + with open('/tmp/img.html', 'w') as f: - f.write(syn_text) + f.write(syn_html) + + alt_text = "Tableau présentant les votes de l'amendement\n\n" + \ + f"Exprimés : {syn['suffragesExprimes']}\n" + \ + f"Pour : {syn['decompte']['pour']}\n" + \ + f"Contre : {syn['decompte']['contre']}\n" + \ + f"Abstentions : {syn['decompte']['abstentions']}\n\n" + \ + scrutin['sort']['libelle'] subprocess.run(['wkhtmltoimage', '/tmp/img.html', '/tmp/img.jpg']) result_media = api.media_upload("/tmp/img.jpg") - api.create_media_metadata(result_media.media_id, syn_text[:1000]) + api.create_media_metadata(result_media.media_id, alt_text) - message = obj['message'].format(date=date, nb_jours = delta) + message = obj['message'].format(date=date, nb_jours=delta) - client.create_tweet(text=message, media_ids=[result_media.media_id, text_media.media_id])) + client.create_tweet(text=message, media_ids=[result_media.media_id, text_media.media_id]) def main(): @@ -147,5 +223,26 @@ def main(): with open(os.path.join(BASE_PATH, 'data', 'votes.json')) as f: data = json.load(f) + for groupe_id in GROUPES_ID: + with open(os.path.join(BASE_PATH, 'data', str(LEG), 'Organes', f"{groupe_id}.json")) as f: + groupe = json.load(f)['organe'] + groupe['deputes'] = set() + GROUPES[groupe_id] = groupe + + for filename in os.listdir(os.path.join(BASE_PATH, 'data', str(LEG), 'Acteurs')): + with open(os.path.join(os.path.join(BASE_PATH, 'data', str(LEG), 'Acteurs', filename))) as f: + acteur = json.load(f)['acteur'] + for mandat in acteur['mandats']['mandat']: + organes = mandat['organes']['organeRef'] + if not isinstance(organes, list): + organes = [organes] + for organe in organes: + if organe in GROUPES: + groupe = GROUPES[organe] + groupe['deputes'].add(acteur['uid']['#text']) + + for g in GROUPES.values(): + print(g['libelle'], len(g['deputes'])) + obj = random.choice(data) publish_amendement(obj, api, client)