-
Notifications
You must be signed in to change notification settings - Fork 235
/
Copy pathretrieve-csv-report.py
executable file
·66 lines (57 loc) · 1.77 KB
/
retrieve-csv-report.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
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env python3
#
# Responses can be binary ZIP streams instead of the expected JSON,
# based on query parameters. A prime example is 'RetrieveReport':
#
# https://www.kraken.com/features/api#get-history-export
#
# This script shows how to "unpack" such a response.
#
# Run unbuffered to see output as in happens:
# python -u export-csv-export.py
###################################
## Library imports ##
###################################
from json import JSONDecodeError
from pprint import pprint
from time import sleep
import krakenex
###################################
## Key setup ##
###################################
kraken = krakenex.API()
# NOTE: key must have the "Export data" permission.
kraken.load_key('/path/to/export-data.key')
###################################
## Report ##
###################################
# Request report.
response = kraken.query_private(
'AddExport',
{
'description': 'reporting',
'report': 'ledgers',
'format': 'CSV',
},
)
report_id = response['result']['id']
# "Wait" for the report to be created.
print('Waiting for export to be ready...', end='')
while True:
sleep(10)
response = kraken.query_private('ExportStatus', {'report': 'ledgers'})
if len(response['error']) > 0 and response['error'][0] == 'EExport:Not ready':
print('.', end='')
else:
print('')
break
# Expecting streamed binary response when query successful.
try:
response = kraken.query_private('RetrieveExport', {'id': report_id})
except JSONDecodeError:
export_bytes = kraken.response.content
with open('ledgers.csv.zip', 'wb') as fd:
fd.write(export_bytes)
print('Done! Written as: ledgers.csv.zip')
else:
pprint(response)