tools/assemblee_nationale/scrap_memberships.py

53 lines
1.9 KiB
Python

# encoding: utf-8
"""
Tool used to upload representatives from French National Assembly.
"""
import csv
from datetime import datetime
import json
import os
import sys
from slugify import slugify
data_root = os.environ.get("POLITIKORAMA_DATA_ROOT", "../tmp")
target_root = os.path.join(data_root, "assemblee_nationale")
# Extract representatives
data_source = os.path.join(data_root, "json/acteur")
data_target = os.path.join(target_root, "assemblee_nationale_memberships.csv")
with open(data_target, "w", encoding="utf-8", newline="") as csvfile:
writer = csv.writer(csvfile, delimiter=",", quotechar='"', quoting=csv.QUOTE_MINIMAL)
writer.writerow(["representative_slug", "role_code", "entity_code", "start", "end"])
for filename in os.listdir(data_source):
print(".", end="")
sys.stdout.flush()
# Loading informations
with open(os.path.join(data_source, filename)) as file_handler:
acteur = json.load(file_handler)["acteur"]
identity = acteur["etatCivil"]["ident"]
representative_slug = slugify(f"{identity['prenom']} {identity['nom']}")
mandats = acteur["mandats"]["mandat"]
if isinstance(mandats, dict):
mandats = [mandats]
for mandat in mandats:
print(".", end="")
sys.stdout.flush()
role_code = mandat["infosQualite"].get("codeQualite", "")
start = mandat.get("dateDebut", None)
end = mandat.get("dateFin", None)
organes = mandat["organes"]["organeRef"]
if isinstance(organes, str):
organes = [organes]
for entity_code in organes:
# CSV line
writer.writerow([
representative_slug,
role_code,
entity_code,
start,
end,
])