Created
October 27, 2020 13:20
-
-
Save mdamien/17fd273bbf8a311bb4cc606578602e3a to your computer and use it in GitHub Desktop.
la fabrique de la loi vers mediawiki
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import glob | |
import os | |
import sys | |
from pathlib import Path | |
from lys import L, raw, render | |
from mwclient import Site | |
from tlfp.tools.common import open_json | |
def read_text(path): | |
# TODO: format tables | |
try: | |
articles = open_json(os.path.dirname(path), os.path.basename(path))["articles"] | |
except FileNotFoundError: | |
return "" | |
texte = "" | |
for art in articles: | |
texte += "= Article " + art.get("newtitre", art["titre"]) + " =\n\n" | |
for key in sorted(art["alineas"].keys()): | |
if art["alineas"][key] != "": | |
texte += art["alineas"][key] + "\n\n" | |
return texte | |
site = Site('wiki-lois.dam.io', scheme='http', path='/') | |
site.login('user', 'password') | |
pages = [] | |
for procedure_file in sorted(glob.glob(sys.argv[1], recursive=True)): | |
procedure = open_json(procedure_file) | |
project_dir = os.path.dirname(os.path.dirname(procedure_file)) | |
page = site.pages[procedure['id']] | |
try: | |
page.delete() | |
except: | |
pass | |
print(procedure['id']) | |
revisions = [] | |
for i, step in enumerate(procedure["steps"]): | |
if step.get("step") == "depot": | |
msg = "Dépot du texte" | |
elif step.get("step") == "commission": | |
msg = "Travaux en commission" | |
elif step.get("step") == "hemicycle": | |
msg = "Travaux en hemicycle" | |
elif step.get("stage") == "promulgation": | |
msg = "Promulgation" | |
elif step.get("stage") == "constitutionnalité": | |
msg = "Constitutionnalité" | |
else: | |
raise Exception("Unknown step: %s" % (str(step))) | |
instit = step.get("institution") | |
author_depot = step.get("auteur_depot") | |
if instit == "gouvernement" or author_depot == "Gouvernement": | |
author = "Gouvernement" | |
elif instit == "assemblee": | |
author = author_depot or "Assemblée nationale" | |
elif instit == "senat": | |
author = author_depot or "Sénat" | |
elif instit == "CMP": | |
author = "Commission mixte paritaire" | |
elif instit == "conseil constitutionnel": | |
author = "Conseil Constitutionnel" | |
elif instit == "congrès": | |
author = "Congrès" | |
else: | |
raise Exception("Unknown author: %s" % instit) | |
date = step.get("date") | |
if "directory" not in step or step.get("debats_order") is None: | |
continue | |
texte_path = os.path.join( | |
project_dir, "procedure", step.get("directory"), "texte/texte.json" | |
) | |
texte = read_text(texte_path) | |
if texte: | |
try: | |
page.edit(texte, msg + ' - ' + author) | |
except: | |
print("failed to upload page") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment