Spaces:
Running
Running
File size: 2,230 Bytes
6236000 76f1467 6236000 76f1467 6236000 76f1467 6236000 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
import http.client
import json
conn = http.client.HTTPSConnection("aides-territoires.beta.gouv.fr")
headersConnexion = {
'X-AUTH-TOKEN': 'eeb481e42950f1dbfc46dc348e6e32a0c631cc5b94dd7ab874a30c027f9de87c',
}
perimeter_id = '71054-yvelines'
organization_type_slugs = 'private-sector'
def connexion():
conn.request("POST", "/api/connexion/", '', headersConnexion)
data = conn.getresponse().read()
return json.loads(data.decode("utf-8"))['token']
def get_final_type(types):
type_mapping = {
"Subvention": 2,
"Prêt": 1,
"Avance récupérable": 1,
"Certificat d'économie d'énergie (CEE)": 4,
"Autre aide financière": 4,
"Ingénierie technique": 4,
"Ingénierie financière": 4,
"Ingénierie Juridique / administrative": 4
}
final_type_mapping = {
1: "Avance − Prêts − Garanties",
2: "Subvention",
3: "Prise en charge des coûts et allègement des charges",
4: "Autres"
}
for t in types:
if t in type_mapping:
return final_type_mapping[type_mapping[t]]
return None # Return None if no matching type is found
def request():
subventions = []
page = 1
while True:
conn.request("GET", f"/api/aids/?page={page}&organization_type_slugs={organization_type_slugs}&perimeter_id={perimeter_id}", '', headersRequest)
res = conn.getresponse().read()
resData = res.decode("utf-8")
data = json.loads(resData)
subventions += data['results']
if data['next'] is None:
break
page += 1
print(f"Total subventions : {len(data['results'])}")
return subventions
token = connexion()
headersRequest = {
'Authorization': 'Bearer ' + token,
}
aides = request()
print(f"Nb aides : {len(aides)}")
for aide in aides:
aide["metadata"] = {
"type_aide": get_final_type(aide["aid_types"]),
"lien": f"https://aides-territoires.beta.gouv.fr/aides/{aide['slug']}",
"Source": f"https://aides-territoires.beta.gouv.fr"
}
# print(subventions)
with open('data/aides_territoires.json', 'w', encoding='utf-8') as f:
json.dump(aides, f, ensure_ascii=False, indent=4) |