Skip to content

Commit

Permalink
Gestion des batchs de telechargement de la source LD Cadastre
Browse files Browse the repository at this point in the history
  • Loading branch information
vdct committed Feb 21, 2024
1 parent a303199 commit 03a7d87
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions bano/sources/cadastre_ld.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from ..constants import DEPARTEMENTS
from .. import db
from .. import helpers as hp
from .. import batch as b


def process(departements, **kwargs):
Expand All @@ -23,10 +24,11 @@ def process(departements, **kwargs):
for dept in sorted(departements):
print(f"Processing {dept}")
status = download(dept)
# if status:
import_to_pg(dept)
if status:
import_to_pg(dept)

def download(departement):
id_batch = b.batch_start_log("download source", "LD CADASTRE", departement)
destination = get_destination(departement)
headers = {}
if destination.exists():
Expand All @@ -41,10 +43,18 @@ def download(departement):
f.write(resp.content)
mtime = parsedate_to_datetime(resp.headers["Last-Modified"]).timestamp()
os.utime(destination, (mtime, mtime))
b.batch_stop_log(id_batch, True)
return True
if resp.status_code == 304:
b.batch_stop_log(id_batch, True)
return False

print(f"Code de téléchargement : {resp.status_code}")
b.batch_stop_log(id_batch, False)
return False

def import_to_pg(departement, **kwargs):
id_batch = b.batch_start_log("import source", "LD CADASTRE", departement)
fichier_source = get_destination(departement)
with gzip.open(fichier_source, mode="rt") as f:
json_source = json.load(f)
Expand All @@ -62,9 +72,11 @@ def import_to_pg(departement, **kwargs):
)
if a_values:
cur_insert.execute(str_query + ",".join(a_values) + ";COMMIT;")
b.batch_stop_log(id_batch, True)
except psycopg2.DataError as e:
print(e)
db.bano_db.reset()
b.batch_stop_log(id_batch, False)

def get_destination(departement):
try:
Expand Down

0 comments on commit 03a7d87

Please sign in to comment.