tools/assemblee_nationale/load_datas.py

36 lines
975 B
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"
# Cleaning old data
try:
os.remove("../tmp/assemblee_nationale.zip")
except FileNotFoundError:
# No file to remove
pass
try:
shutil.rmtree("../tmp/json")
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()
with open("../tmp/assemblee_nationale.zip", "wb") as f:
for chunk in result.iter_content(chunk_size=8192):
f.write(chunk)
print("Unpacking archive")
shutil.unpack_archive("../tmp/assemblee_nationale.zip", "../tmp/")
os.remove("../tmp/assemblee_nationale.zip")