tools/assemblee_nationale/load_datas.py

40 lines
1.0 KiB
Python
Raw Normal View History

2021-07-23 17:21:38 +02:00
# encoding: utf-8
"""
Tool used to upload representatives from French National Assembly.
"""
import os
import shutil
import requests
url = "https://data.assemblee-nationale.fr/static/openData/repository/15/amo/tous_acteurs_mandats_organes_xi_legislature/AMO30_tous_acteurs_tous_mandats_tous_organes_historique.json.zip"
2022-07-08 13:51:54 +02:00
data_root = os.environ.get("POLITIKORAMA_DATA_ROOT", "../tmp")
data_source = os.path.join(data_root, "assemblee_nationale.zip")
data_target = os.path.join(data_root, "json")
2021-07-23 17:21:38 +02:00
# Cleaning old data
try:
2022-07-08 13:51:54 +02:00
os.remove(data_source)
2021-07-23 17:21:38 +02:00
except FileNotFoundError:
# No file to remove
pass
try:
2022-07-08 13:51:54 +02:00
shutil.rmtree(data_target)
2021-07-23 17:21:38 +02:00
except FileNotFoundError:
# No folder to remove
pass
# Download and extract data
print("Downloading archive")
with requests.get(url, stream=True) as result:
result.raise_for_status()
2022-07-08 13:51:54 +02:00
with open(data_source, "wb") as f:
2021-07-23 17:21:38 +02:00
for chunk in result.iter_content(chunk_size=8192):
f.write(chunk)
print("Unpacking archive")
2022-07-08 13:51:54 +02:00
shutil.unpack_archive(data_source, data_root)
2021-07-23 17:21:38 +02:00
2022-07-08 13:51:54 +02:00
os.remove(data_source)