-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInsee-siret-enrichment.py
54 lines (42 loc) · 1.9 KB
/
Insee-siret-enrichment.py
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
from google.cloud import bigquery
import requests
import json
def query_pa():
#Select query for SIRET list
client = bigquery.Client()
query_job = client.query("""
SELECT *
FROM `billing-module-qa.partners_applications.partners_applications`
LIMIT 10000""")
results = query_job.result() # Waits for job to complete.
# Parse results
for row in results:
print("{}".format(row.SIRET))
#INSEE SIREN API CALL
url = 'https://api.insee.fr/entreprises/sirene/V3/siret/%s?champs=trancheEffectifsEtablissement&date=2020-03-24' % (row.SIRET)
#print("{}".format(url))
response = requests.get(url, headers = {'Authorization': 'Bearer c0678066-bcb0-35f4-8cc5-eecc4ca58afd'})
if format(response) != "<Response [200]>" : print("Erreur : {}".format(response))
else:
jsonResponse = response.json()
tranche = jsonResponse.get('etablissement')
effectif = tranche.get('trancheEffectifsEtablissement')
if effectif is None : effectif = '00'
print "Value : %s" % effectif
#SQL INSERT QUERY
client = bigquery.Client()
query = """
UPDATE `billing-module-qa.partners_applications.partners_applications`
SET effectif = @effectif where SIRET = @siret
"""
job_config = bigquery.QueryJobConfig(
query_parameters=[
bigquery.ScalarQueryParameter("effectif", "STRING", effectif),
bigquery.ScalarQueryParameter("siret", "INT64", row.SIRET),
]
)
query_job2 = client.query(query, job_config=job_config)
results2 = query_job2.result() # Waits for job to complete.
if __name__ == '__main__':
query_pa()
#Token : c0678066-bcb0-35f4-8cc5-eecc4ca58afd