From 81e5aa05b58d8ddf0eed230ad0b95c397c9fa6e1 Mon Sep 17 00:00:00 2001 From: Thomas Gratier Date: Wed, 28 Aug 2024 10:55:34 +0200 Subject: [PATCH 1/2] Update script to generate locaux stats by commune from MAJIC data We remove automatic push to data gouv as it's an yearly update and automatic management may erase previous year data. It's needed to push data manually but it takes less than 5 min yearly but it's safer. We added MAJIC_YEAR to set the name of output files (export the environnement variable or set it in .env file with 2024 or another year). For previous years, if you use the script, before running it, you need to change temporarily the dependency in root package.json @etalab/decoupage-administratif version e.g https://github.com/datagouv/decoupage-administratif?tab=readme-ov-file#mill%C3%A9simes-et-versions-de-package otherwise your stats will not match the official COG list at this time. --- scripts/build-communes-locaux-adresses.cjs | 26 +++++++++++++--------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/scripts/build-communes-locaux-adresses.cjs b/scripts/build-communes-locaux-adresses.cjs index 65a60717..7f7b509e 100644 --- a/scripts/build-communes-locaux-adresses.cjs +++ b/scripts/build-communes-locaux-adresses.cjs @@ -1,12 +1,21 @@ #!/usr/bin/env node require('dotenv').config() +const fs = require('fs'); +const fsPromises = fs.promises; const bluebird = require('bluebird') const {chain, compact, snakeCase, mapKeys} = require('lodash') const Papa = require('papaparse') const {getCommuneData} = require('@etalab/majic') const {getCommunes} = require('../lib/util/cog.cjs') -const {replaceResourceFile} = require('../lib/util/datagouv.cjs') +function getEnv(name) { + let val = process.env[name]; + if ((val === undefined) || (val === null)) { + throw Error("missing env var for " + name); + } + return val; +} +const YEAR_MAJIC = getEnv('YEAR_MAJIC') const ACCEPTED_CATEGORIES_LOCAUX = new Set([ 'maison', @@ -68,19 +77,16 @@ async function main() { const communesLocauxCompact = compact(communesLocaux) - const datasetId = '5fda75d3084b5fa14f89cd2f' + // Update manually https://www.data.gouv.fr/fr/datasets/nombre-de-locaux-adressables-par-communes/ + // with below files - await replaceResourceFile( - datasetId, - '9a4a5188-8142-4c9d-b3e6-f54594848509', - 'communes-locaux-adresses.json', + await fsPromises.writeFile( + `communes-locaux-adresses-${year}.json`, JSON.stringify(communesLocauxCompact) ) - await replaceResourceFile( - datasetId, - '9854b368-abce-479e-80f1-cfdbedaa0232', - 'communes-locaux-adresses.csv', + await fsPromises.writeFile( + `communes-locaux-adresses-${year}.csv`, Papa.unparse(communesLocauxCompact.map(c => mapKeys(c, (v, k) => snakeCase(k)))) ) } From 29c8ee2b3f979139cdb155ef5bc3fcd9b3870364 Mon Sep 17 00:00:00 2001 From: Thomas Gratier Date: Wed, 11 Sep 2024 10:08:21 +0200 Subject: [PATCH 2/2] Change variable due to wrong reference --- scripts/build-communes-locaux-adresses.cjs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/build-communes-locaux-adresses.cjs b/scripts/build-communes-locaux-adresses.cjs index 7f7b509e..a75cb2bf 100644 --- a/scripts/build-communes-locaux-adresses.cjs +++ b/scripts/build-communes-locaux-adresses.cjs @@ -81,12 +81,12 @@ async function main() { // with below files await fsPromises.writeFile( - `communes-locaux-adresses-${year}.json`, + `communes-locaux-adresses-${YEAR_MAJIC}.json`, JSON.stringify(communesLocauxCompact) ) await fsPromises.writeFile( - `communes-locaux-adresses-${year}.csv`, + `communes-locaux-adresses-${YEAR_MAJIC}.csv`, Papa.unparse(communesLocauxCompact.map(c => mapKeys(c, (v, k) => snakeCase(k)))) ) }