import http.client import json conn = http.client.HTTPSConnection("api.aides-entreprises.fr") payload = '' headers = { 'X-Aidesentreprises-Id': 'jb4nMj67', 'X-Aidesentreprises-Key': 'waMF2TjO', } profils = "10,14" # 10: ETI et grande entreprise, 14: Industrie territoire = "103" def request(limit=20, offset=0): print(f"Requesting {limit} subventions from offset {offset}") conn.request("GET", f"/v1.1/aides?profils={profils}&territoire={territoire}&limit={limit}&offset={offset}", payload, headers) res = conn.getresponse() data = res.read() return json.loads(data.decode("utf-8"))['data'] def get_final_type(types): type_mapping = { "16": 3, # Exonération de charges sociales "15": 1, # Prêt d'honneur "12": 4, # Prix "3": 2, # Subvention "5": 1, # Prêt "4": 1, # Avance remboursable "7": 1, # Garantie "8": 3, # Allègement fiscal "6": 4, # Bonification d'intérêt "9": 4, # Participation en capital "14": 4, # Appel à projet "11": 4 # Crédit-bail } 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["id_typ"] in type_mapping: return final_type_mapping[type_mapping[t["id_typ"]]] return None # Return None if no matching type is found def getAide(aide): conn.request("GET", f"/v1.1/aides/{aide['id_aid']}", payload, headers) res = conn.getresponse() aide = json.loads(res.read().decode("utf-8"))[0] # if 'contacts' in aide: # del aide['contacts'] # if 'contact' in aide: # del aide['contact'] # if 'profils' in aide: # del aide['profils'] # if 'projets' in aide: # del aide['projets'] # if 'cache_indexation' in aide: # del aide['cache_indexation'] print(aide["prets"]) aide["metadata"] = { "type_aide": get_final_type(aide["prets"]), "lien": f"https://aides-entreprises.fr/aide/{aide['id_aid']}", "Source": f"https://aides-entreprises.fr" } return aide # Set to 400 to get all subventions, pagination doesn't work !!! take = 400 skip = 0 subventions = [] while True: responses = request(take, skip) subventions += responses print(f"Got {len(responses)} subventions") if len(responses) < take: break skip += take for i in range(len(subventions)): subventions[i] = getAide(subventions[i]) # print(subventions) with open('data/aides_entreprises.json', 'w', encoding='utf-8') as f: json.dump(subventions, f, ensure_ascii=False, indent=4)