# encoding: utf-8 """ Tool used to upload representatives from French National Assembly. """ import csv from datetime import datetime import json import os from slugify import slugify # Extract representatives print("Scraping memberships") with open("../tmp/assemblee_nationale_memberships.csv", "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("../tmp/json/acteur"): # Loading informations with open(os.path.join("../tmp/json/acteur", 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: 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, ])