-
Notifications
You must be signed in to change notification settings - Fork 4
/
get_data.py
executable file
·188 lines (162 loc) · 5.3 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
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#! /usr/bin/python3
import time
import requests
import json
import csv
import os
import multiprocessing
import load_data
# Gets past data from poloniex api
base_url = "https://poloniex.com/public?command=returnChartData¤cyPair=BTC_"
data_folder_base = "data_"
period = 3600*24*30*20
step = 3600*24*30*2
def make_dir(path):
"""
Creates folder if it doesn't exist
Args:
path: place where the folder must be created
"""
if(not os.path.isdir(path)):
os.mkdir(path)
def get_missing_period(name,spacing):
"""
Finds the period of time between the last update of a csv and the current time.
Args:
name (string): name of the csv to consider
Returns:
(int): starting time of the missing period
(int): end time of the missing period
"""
data_folder = data_folder_base + str(spacing)
end_time = int(time.time())
start_time = end_time - period
filename = data_folder + os.sep + name + '.csv'
exists = os.path.exists(filename)
if not exists:
create_csv(name,spacing)
time_ = get_last_timestamp(name,spacing)
if time_ != -1:
start_time = time_
return start_time,end_time
def get_last_timestamp(name,spacing):
"""
Finds the last timestamp of a given csv
Args:
name (string): name of the csv to consider
Returns:
(int): last timestamp of the csv
"""
data_folder = data_folder_base + str(spacing)
filename = data_folder + os.sep + name + '.csv'
tp = -1
with open(filename, 'r') as csvfile:
reader = csv.reader(csvfile)
first = True
for row in reader:
if first:
first = False
else:
tp = int(row[0])
return tp
print("error opening file ",filename)
def create_csv(name,spacing):
"""
Create csv files with headers for every given currency.
Args:
name (string): name of the csv to consider
"""
data_folder = data_folder_base + str(spacing)
filename = data_folder + os.sep + name + '.csv'
#permission = input('This operation will delete all previous data,\nAre you sure you want to continue ?\
# \nType delete to continue\n')
#if(permission != "delete"):
# return
make_dir(data_folder)
with open(filename, 'w') as csvfile:
columns = ['date','high','low','open', 'close', 'volume', 'quoteVolume', 'weightedAverage']
wr = csv.writer(csvfile,lineterminator = '\n' )
wr.writerow(columns)
print("Data has been deleted")
def get_pair(name,spacing):
"""
Appends missing data until today to a csv by finding the missing period
Args:
name (string): name of the csv to consider
"""
print("******* Getting ", name)
start_time,end_time = get_missing_period(name,spacing)
while(start_time <= end_time):
print("start time = ", start_time)
if(not(get_part_pair(name, start_time+1,start_time + step,spacing))):
return
start_time += step
def get_part_pair(name, start_time, end_time,spacing):
"""
Appends data from missing period to a csv
Args:
name (string): name of the csv to consider
start_time (int): beginning of the missing period
end_time (int): end of the missing period
Returns:
(bool) status of the update. False if the update couldn't be made. True otherwise.
"""
print("start time = ",start_time)
print("end time = ",end_time)
url = base_url + name
#url = "https://poloniex.com/public?command=returnChartData¤cyPair=USDT_BTC"
url += "&start=" + str(start_time)
url += "&end=" + str(end_time)
url += "&period=" + str(spacing)
r = requests.get(url)
worked = True
if(r.status_code != 200):
worked = False
else:
trades_data = json.loads(r.text)
try:
error = trades_data['error']
print("error : ", error)
worked = False
except:
pass
if(worked):
data = json.loads(r.text)
write_data(data, name,spacing)
return True
else:
print("impossible to get ", name)
return False
def write_data(data, name,spacing):
"""
Write the data from the dictionary to the concerned csv files
Args:
data: dictionary associating currencies' acronyms to the relevant data
name: name of the csv to consider
"""
data_folder = data_folder_base + str(spacing)
filename = data_folder + os.sep + name + '.csv'
with open(filename, 'a') as csvfile:
wr = csv.writer(csvfile,lineterminator = '\n' )
for x in data:
if x['date'] != 0:
L=[]
L.append(x['date'])
L.append(x['high'])
L.append(x['low'])
L.append(x['open'])
L.append(x['close'])
L.append(x['volume'])
L.append(x['quoteVolume'])
L.append(x['weightedAverage'])
wr.writerow(L)
def get_data(spacing,moneys):
p_list = []
for name in moneys:
# get_pair(name,spacing)
p = multiprocessing.Process(target=get_pair,args=(name,spacing))
p_list.append(p)
p.start()
for name in moneys:
p.join()
#get_data(300,params.moneys)