-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi-to-csv.py
53 lines (29 loc) · 1.12 KB
/
api-to-csv.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
# %%
import os
import requests
import json
import csv
import logging
# %%
logging.basicConfig(filename='api-to-csv.log', encoding='utf-8', format='%(asctime)s - %(levelname)s - %(message)s', level=logging.DEBUG)
output_filename = 'data.csv'
with open(output_filename, 'w', newline='') as csv_file:
limit = 500
offset = 0
while(1):
try:
url = 'https://connection.keboola.com/v2/storage/files?limit='+ str(limit) +'&offset=' + str(offset) + '&showExpired=true'
r = requests.get(url, headers={"X-StorageApi-Token":os.environ['X-Storage-Token']})
data = json.loads(r.text)
logging.info(offset)
if len(data) > 0:
offset += limit
if not csv_file.tell():
csv_writer = csv.DictWriter(csv_file, fieldnames=data[0].keys())
csv_writer.writeheader()
for row in data:
csv_writer.writerow(row)
else:
break
except Exception as e:
logging.error('Loop:\t' + str(e))