-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcoin_rank_analyzer.py
342 lines (296 loc) · 12.3 KB
/
coin_rank_analyzer.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Mon Feb 19 22:21:12 2018
@author: [email protected]
"""
import MySQLdb as mdb
import json, time, gspread, requests
from datetime import datetime
from oauth2client.service_account import ServiceAccountCredentials
def data_updater(ri, clist, l, c):
scope = ['https://spreadsheets.google.com/feeds','https://www.googleapis.com/auth/drive']
creds = ServiceAccountCredentials.from_json_keyfile_name('cryptoweigh.json', scope)
client = gspread.authorize(creds)
sh = client.open('Coin Rank Analysis')
wk = sh.worksheet('API-CoinRank')
for i in [0,1]: # two times updates
cell_list = wk.range(3,ri+1+i+c,l+2,ri+1+i+c) #(start,end)
for cell in range(len(cell_list)):
cell_list[cell].value = ''
wk.update_cells(cell_list)
for cell in range(len(cell_list)):
if i == 1:
if clist[cell][i] == '':
cell_list[cell].value = clist[cell][i]
if clist[cell][i] != '':
cell_list[cell].value = int(clist[cell][i])
if i == 0:
cell_list[cell].value = clist[cell][i]
wk.update_cells(cell_list)
def add_new_coin_compare(new_coins):
scope = ['https://spreadsheets.google.com/feeds', 'https://www.googleapis.com/auth/drive']
creds = ServiceAccountCredentials.from_json_keyfile_name('cryptoweigh.json', scope)
client = gspread.authorize(creds)
sh = client.open('Coin Rank Analysis')
wk = sh.worksheet('API-Coincompare')
nrow = wk.col_values(1)
nrow = [i for i in nrow if i != '']
nrow = len(nrow)
cell_list = wk.range(nrow + 1, 1, nrow + len(new_coins) + 1, 1)
print (cell_list)
# exit(1)
print (new_coins)
for i in range(len(new_coins)):
print ('Added {} in Data Analysis sheet'.format(new_coins[i]))
cell_list[i].value = new_coins[i]
wk.update_cells(cell_list)
def add_new_coin(new_coins):
scope = ['https://spreadsheets.google.com/feeds','https://www.googleapis.com/auth/drive']
creds = ServiceAccountCredentials.from_json_keyfile_name('cryptoweigh.json', scope)
client = gspread.authorize(creds)
sh = client.open('Coin Rank Analysis')
wk = sh.worksheet('Data Analysis')
nrow = wk.col_values(1)
nrow = [i for i in nrow if i != '']
nrow = len(nrow)
cell_list = wk.range(nrow+1,1,nrow+len(new_coins)+1,1)
print (cell_list)
# exit(1)
print (new_coins)
for i in range(len(new_coins)):
print ('Added {} in Data Analysis sheet' .format(new_coins[i]))
cell_list[i].value = new_coins[i]
wk.update_cells(cell_list)
add_new_coin_compare(new_coins)
def coin_compare_data_updater(ri, clist, l):
scope = ['https://spreadsheets.google.com/feeds','https://www.googleapis.com/auth/drive']
creds = ServiceAccountCredentials.from_json_keyfile_name('cryptoweigh.json', scope)
client = gspread.authorize(creds)
sh1 = client.open('Coin Rank Analysis')
wk1 = sh1.worksheet('API-Coincompare')
cell_list = wk1.range(2,2,1000,2) #'[ row col start, row col end]' B2:B1000
for cell in range(len(cell_list)):
cell_list[cell].value = ''
wk1.update_cells(cell_list)
cell_list = wk1.range(2,2,l+1,2) #'[ row col start, row col end]' B2:B1000
for cell in range(len(cell_list)):
cell_list[cell].value = clist[cell][0]
wk1.update_cells(cell_list)
time.sleep(6)
cmpr_result = wk1.col_values(3)[1:]
new_coins = []
for i in range(len(cmpr_result)):
if cmpr_result[i] == 'No Match Found':
new_coins.append(clist[i][0])
add_new_coin(new_coins)
def dashboard_data_updater(ri):
scope = ['https://spreadsheets.google.com/feeds','https://www.googleapis.com/auth/drive']
creds = ServiceAccountCredentials.from_json_keyfile_name('cryptoweigh.json', scope)
client = gspread.authorize(creds)
sh3 = client.open('Coin Rank Analysis')
wk3 = sh3.worksheet('Dashboard')
time.sleep(4)
values_list = wk3.row_values(1)[2:13]
values_list[0]=datetime.today().strftime("%d-%m-%Y %H:%M:%S")
values_list.insert(0,'Successful')
cell_list = wk3.range(4+ri,2,4+ri,13)
print (ri)
print (cell_list)
print (values_list)
# print len(cell_list)
# print len(values_list)
for cell in range(len(values_list)):
cell_list[cell].value = values_list[cell]
wk3.update_cells(cell_list)
def data_migrate(x):
scope = ['https://spreadsheets.google.com/feeds','https://www.googleapis.com/auth/drive']
creds = ServiceAccountCredentials.from_json_keyfile_name('cryptoweigh.json', scope)
client = gspread.authorize(creds)
sh = client.open('Coin Rank Analysis')
# exit(1)
wk = sh.worksheet('API-CoinRank')
c = 0
for ri in range(0,30): #0,30
c+=1
for i in [0,1]: # two times updates
ccol = wk.col_values(ri+i+1+c)[2:]
l = len(ccol)+2
print (3,ri+1+i+c,l,ri+1+i+c)
cell_list1 = wk.range(3,ri+1+i+c,l,ri+1+i+c) #(start,end)
for cell in range(len(cell_list1)):
cell_list1[cell].value = ''
wk.update_cells(cell_list1)
cell_list2 = wk.col_values(ri+1+i+c+2)[2:]
# cell_list2.append('')
# cell_list2.append('')
if len(cell_list2) > len(cell_list1):
d = len(cell_list2) - len(cell_list1)
l1 = len(cell_list1) + d + 2
cell_list1 = wk.range(3,ri+1+i+c,l1,ri+1+i+c) #(start,end)
elif len(cell_list1) > len(cell_list2):
d = len(cell_list1) - len(cell_list2) + 2
cell_list2 = cell_list2 + ['']*d
print (len(cell_list1))
print (len(cell_list2))
for cell in range(len(cell_list1)):
if i == 1:
if cell_list2[cell] != '':
cell_list1[cell].value = int(cell_list2[cell])
if cell_list2[cell] == '':
cell_list1[cell].value = cell_list2[cell]
else:
cell_list1[cell].value = cell_list2[cell]
wk.update_cells(cell_list1)
ri = 30; c = 31 #30,31
for i in [0,1]:
cell_list1 = wk.range(3,ri+1+i+c,l,ri+1+i+c) #(start,end)
for cell in range(len(cell_list1)):
cell_list1[cell].value = ''
wk.update_cells(cell_list1)
last_column_data(x)
def last_column_data(x):
data = requests.get('https://api.coinmarketcap.com/v1/ticker/?limit=3000')
data = json.loads(data.text)
clist = []
for stat in data:
if stat['market_cap_usd'] and stat['24h_volume_usd'] and float(stat['24h_volume_usd']) > 50000.0000 :
clist.append([stat['name'],int(stat['rank']), stat['percent_change_24h']])
l = len(clist)
ri = 30 #30
c= 31 #31
coin_compare_data_updater(ri, clist,l)
time.sleep(8)
print ('migrated data update')
data_updater(ri, clist, l, c)
dc = ri
time.sleep(8)
print (x)
print ('migrated dashboard')
dashboard_data_updater(dc+x)
time.sleep(4)
get_top_coins(clist, dc+x)
def get_top_coins(clist,ri):
scope = ['https://spreadsheets.google.com/feeds','https://www.googleapis.com/auth/drive']
creds = ServiceAccountCredentials.from_json_keyfile_name('cryptoweigh.json', scope)
client = gspread.authorize(creds)
sh = client.open('Coin Rank Analysis')
wk = sh.worksheet('Data Analysis')
abscoindata = []; relcoindata =[]
coinname = wk.col_values(1)[1:]
abscoinscore = wk.col_values(10)[1:]
relcoinscore = wk.col_values(11)[1:]
print abscoinscore
abscoinscore = [float(c.replace(',','')) for c in abscoinscore]
relcoinscore = [float(c.replace(',','')) for c in relcoinscore]
for i in range(len(abscoinscore)):
abscoindata.append((coinname[i],abscoinscore[i]))
print abscoindata
for i in range(len(relcoinscore)):
relcoindata.append((coinname[i],relcoinscore[i]))
abscoindata = sorted(abscoindata, key=lambda x: x[1])
# print coinname, coinscore
relcoindata = sorted(relcoindata, key=lambda x: x[1])
abscoindata = abscoindata[-10:][::-1]
relcoindata = relcoindata[-10:][::-1]
if abscoindata != [] and relcoindata!= []:
abscoindata = [c[0] for c in abscoindata]
relcoindata = [c[0] for c in relcoindata]
abs_top_10(abscoindata,ri)
rel_top_10(relcoindata,ri)
#news_accumulator(relcoindata,ri)
# exit(1)
def abs_top_10(abscoindata,ri):
print '===='
print abscoindata
scope = ['https://spreadsheets.google.com/feeds','https://www.googleapis.com/auth/drive']
creds = ServiceAccountCredentials.from_json_keyfile_name('cryptoweigh.json', scope)
client = gspread.authorize(creds)
sh = client.open('Coin Rank Analysis')
wk = sh.worksheet('Absolute Top 10')
if abscoindata != []:
cell_list = wk.range(2+ri,3,2+ri,12)
print cell_list
for cell in range(len(abscoindata)):
cell_list[cell].value = abscoindata[cell]
wk.update_cells(cell_list)
def rel_top_10(relcoindata,ri):
scope = ['https://spreadsheets.google.com/feeds','https://www.googleapis.com/auth/drive']
creds = ServiceAccountCredentials.from_json_keyfile_name('cryptoweigh.json', scope)
client = gspread.authorize(creds)
sh = client.open('Coin Rank Analysis')
wk = sh.worksheet('Relative Top 10')
if relcoindata != []:
cell_list = wk.range(2+ri,3,2+ri,12)
for cell in range(len(relcoindata)):
cell_list[cell].value = relcoindata[cell]
wk.update_cells(cell_list)
def news_accumulator(relcoindata,ri):
scope = ['https://spreadsheets.google.com/feeds','https://www.googleapis.com/auth/drive']
creds = ServiceAccountCredentials.from_json_keyfile_name('cryptoweigh.json', scope)
client = gspread.authorize(creds)
sh = client.open('Coin Rank Analysis')
wk = sh.worksheet('News')
conn = mdb.connect("localhost", "root","admin@123","cryptov1")
with conn:
cursor = conn.cursor() # prepare the cursor object
if relcoindata != []:
cell_list= wk.range(2+ri,3,2+ri,12)
for cell in range(len(relcoindata)):
cursor.execute("SELECT * FROM filtered_tweets where coin_name = %s;",[relcoindata[cell]])
ls = cursor.fetchall()
if ls == ():
cell_list[cell].value = relcoindata[cell]+' ||| No Data in DB'
# news = get_news(relcoindata[cell])
else:
cell_list[cell].value = relcoindata[cell]+' ||| '+''.join(ls[-1][4])
# cell_list[cell].value = news
wk.update_cells(cell_list)
def data_timer():
print ('data analysis update started')
c = 0
for ri in range(0,31): #0,31
c+=1
ini_data_run(ri,c)
final_data_run()
def ini_data_run(ri,c):
try:
clist = []
data = requests.get('https://api.coinmarketcap.com/v1/ticker/?limit=1000')
#print (data.text)
data = json.loads(data.text)
for stat in data:
# print stat['name'],stat['market_cap_usd'] ,stat['24h_volume_usd'] ,float(stat['24h_volume_usd'])
if stat['market_cap_usd'] and stat['24h_volume_usd'] and float(stat['24h_volume_usd']) > 50000.0 :
clist.append([stat['name'],int(stat['rank']), stat['percent_change_24h']])
l = len(clist)
# print (l)
# print clist
print ('run %s' %str(ri))
print ('-> coincompare started')
coin_compare_data_updater(ri, clist, l)
time.sleep(5)
data_updater(ri, clist, l, c)
time.sleep(5)
print ('-> dashboard started')
dashboard_data_updater(ri)
print ('==Waiting===')
time.sleep(5)
get_top_coins(clist,ri)
time.sleep(650)
except:
ini_data_run(ri,c)
#
def final_data_run():
try:
x = 0
while True:
x +=1
data_migrate(x)
print ('========')
print ('data migrated')
print ('===Waiting====')
time.sleep(500)
except:
data_migrate(x)
data_timer()