File size: 2,770 Bytes
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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)