2021-07-23 17:21:38 +02:00
|
|
|
# encoding: utf-8
|
|
|
|
"""
|
|
|
|
Tool used to upload representatives from French National Assembly.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import csv
|
|
|
|
from datetime import datetime
|
|
|
|
import json
|
|
|
|
import os
|
2022-07-08 13:51:54 +02:00
|
|
|
import sys
|
2021-07-23 17:21:38 +02:00
|
|
|
|
|
|
|
from slugify import slugify
|
|
|
|
|
2022-07-08 13:51:54 +02:00
|
|
|
data_root = os.environ.get("POLITIKORAMA_DATA_ROOT", "../tmp")
|
|
|
|
target_root = os.path.join(data_root, "assemblee_nationale")
|
|
|
|
|
2021-07-23 17:21:38 +02:00
|
|
|
# Extract representatives
|
2022-07-08 13:51:54 +02:00
|
|
|
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:
|
2021-07-23 17:21:38 +02:00
|
|
|
writer = csv.writer(csvfile, delimiter=",", quotechar='"', quoting=csv.QUOTE_MINIMAL)
|
|
|
|
writer.writerow(["representative_slug", "role_code", "entity_code", "start", "end"])
|
|
|
|
|
2022-07-08 13:51:54 +02:00
|
|
|
for filename in os.listdir(data_source):
|
|
|
|
print(".", end="")
|
|
|
|
sys.stdout.flush()
|
2021-07-23 17:21:38 +02:00
|
|
|
# Loading informations
|
2022-07-08 13:51:54 +02:00
|
|
|
with open(os.path.join(data_source, filename)) as file_handler:
|
2021-07-23 17:21:38 +02:00
|
|
|
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:
|
2022-07-08 13:51:54 +02:00
|
|
|
print(".", end="")
|
|
|
|
sys.stdout.flush()
|
2021-07-23 17:21:38 +02:00
|
|
|
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,
|
|
|
|
])
|