64 lines
2.1 KiB
Python
64 lines
2.1 KiB
Python
# encoding: utf-8
|
|
"""
|
|
Tool used to upload representatives from French National Assembly.
|
|
"""
|
|
|
|
import csv
|
|
import json
|
|
import os
|
|
import sys
|
|
|
|
# Extract representatives
|
|
with open("../tmp/assemblee_nationale_representatives.csv", "w", encoding="utf-8", newline="") as csvfile:
|
|
writer = csv.writer(csvfile, delimiter=",", quotechar='"', quoting=csv.QUOTE_MINIMAL)
|
|
writer.writerow(["code", "name", "picture", "nationality", "sex", "birth_date", "birth_place", "job"])
|
|
|
|
for filename in os.listdir("../tmp/json/acteur"):
|
|
print(".", end="")
|
|
sys.stdout.flush()
|
|
# Loading informations
|
|
with open(os.path.join("../tmp/json/acteur", filename)) as file_handler:
|
|
acteur = json.load(file_handler)["acteur"]
|
|
uid = f"AN_{acteur['uid']['#text'][2:]}"
|
|
# Identity
|
|
identity = acteur["etatCivil"]["ident"]
|
|
if identity["civ"] == "M.":
|
|
sex = "M"
|
|
else:
|
|
sex = "F"
|
|
fullname = f"{identity['prenom']} {identity['nom']}"
|
|
birth = acteur["etatCivil"]["infoNaissance"]
|
|
birth_date = birth["dateNais"]
|
|
if isinstance(birth["villeNais"], dict):
|
|
birth_city = None
|
|
else:
|
|
birth_city = birth["villeNais"]
|
|
if isinstance(acteur["profession"]["libelleCourant"], dict):
|
|
job = None
|
|
else:
|
|
job = acteur["profession"]["libelleCourant"]
|
|
# Picture
|
|
legislatures = []
|
|
mandats = acteur["mandats"]["mandat"]
|
|
if isinstance(mandats, dict):
|
|
mandats = [mandats]
|
|
for mandat in mandats:
|
|
if mandat["legislature"] is not None:
|
|
legislatures.append(int(mandat["legislature"]))
|
|
part_1 = "https://www2.assemblee-nationale.fr/static/tribun/"
|
|
if len(legislatures) == 0:
|
|
picture = f""
|
|
else:
|
|
picture = f"{part_1}{max(legislatures)}/photos/{uid[3:]}.jpg"
|
|
# CSV line
|
|
writer.writerow([
|
|
uid,
|
|
fullname,
|
|
picture,
|
|
"FR",
|
|
sex,
|
|
birth_date,
|
|
birth_city,
|
|
job,
|
|
])
|