-
Notifications
You must be signed in to change notification settings - Fork 46
/
get_data.py
70 lines (55 loc) · 2.4 KB
/
get_data.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
67
68
69
70
# -*- coding: utf-8 -*-
"""Get bitcoin historic data.
Works with python 3
"""
from datetime import timedelta, datetime
import csv
import requests
import settings
# Get and write data
def get(path_file='data/datas.csv', period='6-hour', market='bitstampUSD'):
print("Loading.....")
header = ["Timestamp", "Open", "High", "Low", "Close", "Volume_BTC",
"Volume_Currency", "Weighted_Price"]
with open(path_file, 'w') as f:
writer = csv.writer(f, delimiter=',')
writer.writerow(header)
period_to_call = False
# 1 API CALL
if period == 'Weekly' or period == 'Daily' or period == '12-hour':
url = settings.URL_DATA_BASE + 'm='+ market + \
'&i=' + period + '&c=1' + '&s=' + settings.DATE_START.isoformat()+ \
'&e=' + settings.DATE_END.isoformat()
# print url
data = requests.get(url).json()
for d in data:
writer.writerow(d)
elif period == '6-hour' or period == '2-hour':
period_to_call = 365 # 1 API CALL per year
elif period == 'Hourly' or period == '30-min' or period == '15-min':
period_to_call = 30 # 1 API CALL per month
elif period == '5-min':
period_to_call = 7 # 1 API CALL per week
else:
period_to_call = 1 # 1 API CALL per day
if period_to_call:
delta = settings.DATE_END - settings.DATE_START
i = 0
while i <= delta.days:
try:
date_start = settings.DATE_START + timedelta(days=i)
date_end = settings.DATE_START + timedelta(days=i+period_to_call)
url = settings.URL_DATA_BASE + 'm='+ market + \
'&i=' + period + '&c=1' + '&s=' + date_start.isoformat() + \
'&e=' + date_end.isoformat()
# print url
data = requests.get(url).json()
for d in data:
writer.writerow(d)
except:
print('Url not available (date): ' + url)
i += period_to_call + 1
print(str(i) + ' of ' + str(delta.days+1) + ' days loaded...')
print("Last Timestamp: " + \
datetime.fromtimestamp(int(data[-1][0])).strftime('%Y-%m-%d %H:%M:%S'))
get('data/datas-Hourly.csv', period=settings.PERIOD, market=settings.MARKET)