Created
June 21, 2022 08:02
-
-
Save guillett/932aba9231a19a16e5d5eff1b92ac1e1 to your computer and use it in GitHub Desktop.
Script de correspondance semiautomatisée de SIREN pour les CAF
This file contains hidden or 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
/* | |
Script de correspondance semiautomatisée de SIREN pour les CAF | |
Il génère un simili CSV pour réaliser une vérification manuelle | |
*/ | |
const benefits = require("../data/all") | |
const axios = require('axios') | |
const Promise = require('bluebird') | |
/*console.log(Object.keys(benefits)) | |
console.log(benefits.institutionsMap) | |
*/ | |
const cafs = Object.values(benefits.institutionsMap).filter(i => i.type == "caf" && !i.code_siren) | |
Promise.map(cafs.map(c => c).slice(0, 100), caf => { | |
const n = caf.label.slice(3) | |
const slug = caf.slug | |
return new Promise((a, r) => { | |
setTimeout(() => { | |
a() | |
}, 2000 * Math.random()) | |
}).then(e => { | |
const request = encodeURI("https://recherche-entreprises.api.gouv.fr/search?q=caisse allocation " + n + "&page=1&per_page=10") | |
return axios(request) | |
}).then(e => { | |
return e.data | |
}).then(e => { | |
return e.results | |
}).then(e => { | |
return e.filter(v => v.etat_administratif == "A" && v.activite_principale == '84.30C') | |
}).then(list => { | |
if (list.length == 0) { | |
return [[ | |
slug, | |
]] | |
} | |
return list.map(e => { | |
return [ | |
slug, | |
e.siren, e.nom_raison_sociale, | |
e.nom_complet, | |
e.etat_administratif, | |
`https://annuaire-entreprises.data.gouv.fr/entreprise/${e.siren}` | |
].join(';') | |
}) | |
}) | |
.catch(e => { | |
console.log(e) | |
return [[slug, "ERROR"].join(";")] | |
}) | |
}, { concurrency: 3 }) | |
.then(r => { | |
r.map(p => { | |
console.log(p.join('\n')) | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment