-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongoDB.py
494 lines (416 loc) · 17.6 KB
/
mongoDB.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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
#:: By GrepBlock.com developers // pr0logas, mrNemo
#:: Modified date: 2019-12-03 - 2020-03-04
#:: Description: This file contains all MongoDB core methods - find / insert / update / check / assigning mongodb indexes.
import pymongo
import json, ast, time, sys
from time import gmtime, strftime
from pymongo.errors import AutoReconnect
from pymongo import errors as mongoerrors
mongoAuth = {
"host" : "mongoHostIP",
"port" : ":27017",
}
class mongoConnection():
def __init__ (self, mongoAuth, database, collection):
self.mongoConn = pymongo.MongoClient(mongoAuth['host'] + mongoAuth['port'])
self.mongoDB = self.mongoConn[database]
def autoreconnect_retry(fn, retries=10):
def db_op_wrapper(*args, **kwargs):
tries = 0
while tries < retries:
try:
return fn(*args, **kwargs)
except AutoReconnect:
tries += 1
raise Exception("MongoDB not responding. No luck even after %d retries" % retries)
return db_op_wrapper
@autoreconnect_retry
def findByBlock(self, fromCollection, blockNum):
searchBlock = list(self.mongoDB[fromCollection].find({'block' : blockNum}))
return searchBlock[0]
@autoreconnect_retry
def findByTx(self, fromCollection, blockTx):
searchTx = list(self.mongoDB[fromCollection].find({'tx' : blockTx}))
return searchTx
@autoreconnect_retry
def findLastBlock(self, fromCollection):
searchLastBlock = list(self.mongoDB[fromCollection].find({},{ "_id": 0, "block": 1}).sort([( '$natural', -1 )] ).limit(1))
lastBlock = searchLastBlock[0]['block']
return int(lastBlock)
@autoreconnect_retry
def findWallet(self, fromCollection, wallet):
searchWlt = list(self.mongoDB[fromCollection].find({'wallet' : wallet}))
return searchWlt
@autoreconnect_retry
def findLastTxidProgress(self, fromCollection):
searchLastTxidProg = list(self.mongoDB[fromCollection].find({},{ "_id": 0, "lastblock": 1}).sort([( '$natural', -1 )] ).limit(1))
lastTxidProgress = searchLastTxidProg[0]['lastblock']
return int(lastTxidProgress)
@autoreconnect_retry
def findLastHistoricalPrices(self, fromCollection):
s = list(self.mongoDB[fromCollection].find({},{ "_id": 0, "unix_time": 1}).sort([( '$natural', -1 )] ).limit(1))
r = s[0]['unix_time']
return int(r)
@autoreconnect_retry
def findLastPriceDataUnixTime(self, fromCollection):
s = list(self.mongoDB[fromCollection].find({},{ "_id": 0, "unix_time": 1}).sort([( '$natural', -1 )] ).limit(1))
r = s[0]['unix_time']
return int(r)
@autoreconnect_retry
def findLastPriceGtThan(self, fromCollection, unixTime):
s = list(self.mongoDB[fromCollection].find({'unix_time': {"$gt": int(unixTime)}}).sort([( '$natural', 1 )] ).limit(1))
if s == []:
return "Empty"
else:
r = s[0]['unix_time']
return int(r)
@autoreconnect_retry
def findLastPrice(self, fromCollection, unixTime):
s = list(self.mongoDB[fromCollection].find({'unix_time' : unixTime}).limit(1))
r = ''
if 'market_data' in s[0]:
r = s[0]['market_data']['current_price']['usd']
else:
return float(0.0)
if r is None:
print('WARNING there are no historical price for unixTime: ' + str(unixTime))
return float(0.0)
else:
return float(r)
@autoreconnect_retry
def findLastMarketCap(self, fromCollection, unixTime):
s = list(self.mongoDB[fromCollection].find({'unix_time' : unixTime}).limit(1))
r = ''
if 'market_data' in s[0]:
r = s[0]['market_data']['market_cap']['usd']
else:
return float(0.0)
if r is None:
print('WARNING there are no historical price for unixTime: ' + str(unixTime))
return float(0.0)
else:
return float(r)
@autoreconnect_retry
def findLastVolume(self, fromCollection, unixTime):
s = list(self.mongoDB[fromCollection].find({'unix_time' : unixTime}).limit(1))
r = ''
if 'market_data' in s[0]:
r = s[0]['market_data']['total_volume']['usd']
else:
return float(0.0)
if r is None:
print('WARNING there are no historical price for unixTime: ' + str(unixTime))
return float(0.0)
else:
return float(r)
@autoreconnect_retry
def findLastMarketCapQuick(self, fromCollection, unixTime):
s = list(self.mongoDB[fromCollection].find({'unix_time' : unixTime}).limit(1))
try:
r = s[0]['market_cap']
return float(r)
except KeyError:
return 'KeyError'
@autoreconnect_retry
def findLastVolumeQuick(self, fromCollection, unixTime):
s = list(self.mongoDB[fromCollection].find({'unix_time' : unixTime}).limit(1))
try:
r = s[0]['total_volume']
return float(r)
except KeyError:
return 'KeyError'
@autoreconnect_retry
def findLastPriceQuick(self, fromCollection, unixTime):
s = list(self.mongoDB[fromCollection].find({'unix_time' : unixTime}).limit(1))
try:
r = s[0]['current_price']
return float(r)
except KeyError:
return 'KeyError'
@autoreconnect_retry
def findLastBlockTime(self, fromCollection, unixTime):
s = list(self.mongoDB[fromCollection].find({'time' : int(unixTime)}))
r = s[0]['block']
return int(r)
@autoreconnect_retry
def findLastBlockMedianTime(self, fromCollection, unixTime):
s = list(self.mongoDB[fromCollection].find({'mediantime' : int(unixTime)}))
r = s[0]['block']
return int(r)
@autoreconnect_retry
def findLastDiffbyTime(self, fromCollection, unixTime):
s = list(self.mongoDB[fromCollection].find({'time' : int(unixTime)}))
r = s[0]['time']
return int(r)
@autoreconnect_retry
def findLastActiveWalletsbyTime(self, fromCollection, unixTime):
s = list(self.mongoDB[fromCollection].find({'time' : int(unixTime)}))
r = s[0]['time']
return int(r)
@autoreconnect_retry
def findLastActiveWalletsbyTimeReturnBlock(self, fromCollection, unixTime):
s = list(self.mongoDB[fromCollection].find({'time' : int(unixTime)}))
r = s[0]['block']
return int(r)
@autoreconnect_retry
def findDiffGtThan(self, fromCollection, unixTime):
s = list(self.mongoDB[fromCollection].find({'time': {"$gt": int(unixTime)}}).sort([( '$natural', 1 )] ).limit(1))
if s == []:
return "Empty"
else:
r = s[0]['difficulty']
return float(r)
@autoreconnect_retry
def findDiffGtThanReturnTime(self, fromCollection, unixTime):
s = list(self.mongoDB[fromCollection].find({'time': {"$gt": int(unixTime)}}).sort([( '$natural', 1 )] ).limit(1))
r = s[0]['time']
return int(r)
@autoreconnect_retry
def findActiveWalletsGtThan(self, fromCollection, unixTime):
s = list(self.mongoDB[fromCollection].find({'time': {"$gt": int(unixTime)}}).sort([( '$natural', 1 )] ).limit(1))
if s == []:
return "Empty"
else:
r = s[0]['time']
return float(r)
@autoreconnect_retry
def findActiveWalletsGtThanReturnBlock(self, fromCollection, unixTime):
s = list(self.mongoDB[fromCollection].find({'time': {"$gt": int(unixTime)}}).sort([('$natural', 1)]).limit(1))
if s == []:
return int(10000000000) # Send 10 Billions in order to stop //
else:
r = s[0]['block']
return int(r)
@autoreconnect_retry
def findActiveWalletsGtThanCalc1(self, fromCollection, unixTime):
ut = int(unixTime)
s = (self.mongoDB[fromCollection].count_documents({'walletTime': {"$lt": ut}}))
return int(s)
@autoreconnect_retry
def findActiveWalletsGtThanCalc2(self, fromCollection, unixTime):
ut = int(unixTime)
s = (self.mongoDB[fromCollection].count_documents({'walletTime': {"$lt": ut}}))
return int(s)
@autoreconnect_retry
def findActiveWalletsGtThanReturnTime(self, fromCollection, unixTime):
s = list(self.mongoDB[fromCollection].find({'time': {"$gt": int(unixTime)}}).sort([( '$natural', 1 )] ).limit(1))
r = s[0]['time']
return int(r)
@autoreconnect_retry
def findBasicInfo(self, fromCollection, whatToFind):
check = list(self.mongoDB[fromCollection].find({whatToFind: {"$exists": "true"}},{ "_id": 0, whatToFind : 1})).limit(1)
if check == []:
print("Collumn is empty!")
sys.exit(1)
else:
return str(check)
@autoreconnect_retry
def updateLastTxidProgressPlusOne(self, toCollection, lastTxidProgress):
increasing = int(lastTxidProgress)
decresing = int(lastTxidProgress -1)
current = { "lastblock": decresing }
new = { "$set": { "lastblock": increasing } }
self.mongoDB[toCollection].update_one(current, new)
@autoreconnect_retry
def updateLastTxidProgressMinusOne(self, toCollection, lastTxidProgress):
decrease = int(lastTxidProgress - 1)
current = { "lastblock": lastTxidProgress }
decreasing = { "$set": { "lastblock": decrease } }
self.mongoDB[toCollection].update_one(current, decreasing)
@autoreconnect_retry
def upsertUniqueWallets(self, toCollection, jsonOfWallets):
data = eval(jsonOfWallets)
#print(mongoerrors.__dict__.keys())
try:
self.mongoDB[toCollection].insert(data)
return str(data['wallet'])
except pymongo.errors.DuplicateKeyError:
timeSet = strftime("%Y-%m-%d %H:%M:%S", gmtime())
print timeSet + " MongoDB dup wlt, skip"
pass
@autoreconnect_retry
def insertInitValueForBlocks(self, toCollection):
data = '{ "block" : 0 }'
setData = ast.literal_eval(data)
self.mongoDB[toCollection].insert(setData)
print "**Creating required indexes for MongoDB**"
self.mongoDB[toCollection].create_index([('block', pymongo.ASCENDING)], unique=True)
self.mongoDB[toCollection].create_index([('tx', pymongo.ASCENDING)])
self.mongoDB[toCollection].create_index([('hash', pymongo.ASCENDING)])
self.mongoDB[toCollection].create_index([('merkleroot', pymongo.ASCENDING)])
self.mongoDB[toCollection].create_index([('time', pymongo.DESCENDING)])
self.mongoDB[toCollection].create_index([('mediantime', pymongo.DESCENDING)])
@autoreconnect_retry
def insertInitValueForWalletsProgress(self, toCollection):
data = '{ "lastblock" : 0 }'
setData = ast.literal_eval(data)
print "**Creating required indexes for MongoDB**"
self.mongoDB[toCollection].insert(setData)
self.mongoDB[toCollection].create_index([('lastblock', pymongo.ASCENDING)], unique=True)
self.mongoDB['wallets'].create_index([('walletTime', pymongo.ASCENDING)])
self.mongoDB['wallets'].create_index([('block', pymongo.ASCENDING)])
self.mongoDB['wallets'].create_index([('wallet', pymongo.ASCENDING)], unique=True)
@autoreconnect_retry
def insertInitValueForHP(self, toCollection, time):
data = '{ "unix_time" : ' + str(time) + ' }'
setData = ast.literal_eval(data)
self.mongoDB[toCollection].insert(setData)
print "**Creating required indexes for MongoDB**"
self.mongoDB[toCollection].create_index([('unix_time', pymongo.ASCENDING)], unique=True)
self.mongoDB['priceDataUSD'].create_index([('unix_time', pymongo.ASCENDING)], unique=True)
@autoreconnect_retry
def insertBlocksData(self, toCollection, aggregatedBlockData):
data = eval(aggregatedBlockData)
#data = ast.literal_eval(aggregatedBlockData)
try:
self.mongoDB[toCollection].insert(data)
return str(data['block'])
except pymongo.errors.DuplicateKeyError:
timeSet = strftime("%Y-%m-%d %H:%M:%S", gmtime())
print timeSet + " MongoDB found a duplicate block: " + str(data['block']) + ', skipping...'
pass
@autoreconnect_retry
def insertPricesData(self, toCollection, aggregatedPricesData):
data = eval(aggregatedPricesData)
#data = ast.literal_eval(aggregatedBlockData)
try:
self.mongoDB[toCollection].insert(data)
return str(data['current_price'])
except:
timeSet = strftime("%Y-%m-%d %H:%M:%S", gmtime())
print(timeSet + " MongoDB failed to insert Price Data!")
print(data)
sys.exit(1)
@autoreconnect_retry
def insertHistoricalPricesData(self, toCollection, aggregatedHPData):
null = None
data = eval(aggregatedHPData)
#data = ast.literal_eval(aggregatedBlockData)
try:
self.mongoDB[toCollection].insert(data)
return str(data['market_data']['current_price']['usd'])
except:
timeSet = strftime("%Y-%m-%d %H:%M:%S", gmtime())
print(timeSet + " MongoDB failed to insert Historical Price Data!")
print(data)
sys.exit(1)
@autoreconnect_retry
def checkIfBlocksColEmpty(self, fromCollection):
check = list(self.mongoDB[fromCollection].find({},{ "_id": 0, "block": 1}).sort([( '$natural', -1 )] ).limit(1))
if check == []:
return "Empty"
@autoreconnect_retry
def checkIfTxidProgressColEmpty(self, fromCollection):
check = list(self.mongoDB[fromCollection].find({},{ "_id": 0, "lastblock": 1}).sort([( '$natural', -1 )] ).limit(1))
if check == []:
return "Empty"
@autoreconnect_retry
def checkIfHistoricalPricesColEmpty(self, fromCollection):
check = list(self.mongoDB[fromCollection].find({},{ "_id": 0, "unix_time": 1}).sort([( '$natural', -1 )] ).limit(1))
if check == []:
return "Empty"
########################### Basic asset info methods #############################
@autoreconnect_retry
def findAssetName(self, fromCollection):
res = list(self.mongoDB[fromCollection].find({"assetName" : {"$exists":True}},{ "_id": 0, "assetName" : 1}).limit(1))
return res[0]
@autoreconnect_retry
def findAssetType(self, fromCollection):
res = list(self.mongoDB[fromCollection].find({"assetType" : {"$exists":True}},{ "_id": 0, "assetType" : 1}).limit(1))
return res[0]
@autoreconnect_retry
def findAssetTicker(self, fromCollection):
res = list(self.mongoDB[fromCollection].find({"assetTicker" : {"$exists":True}},{ "_id": 0, "assetTicker" : 1}).limit(1))
return res[0]
@autoreconnect_retry
def findAssetMineable(self, fromCollection):
res = list(self.mongoDB[fromCollection].find({"mineable" : {"$exists":True}},{ "_id": 0, "mineable" : 1}).limit(1))
return res[0]
@autoreconnect_retry
def findAssetMasternode(self, fromCollection):
res = list(self.mongoDB[fromCollection].find({"masternode" : {"$exists":True}},{ "_id": 0, "masternode" : 1}).limit(1))
return res[0]
@autoreconnect_retry
def findAssetSourceCode(self, fromCollection):
res = list(self.mongoDB[fromCollection].find({"sourceCode" : {"$exists":True}},{ "_id": 0, "sourceCode" : 1}).limit(1))
return res[0]
@autoreconnect_retry
def findAssetBlockTime(self, fromCollection):
res = list(self.mongoDB[fromCollection].find({"blockTime" : {"$exists":True}},{ "_id": 0, "blockTime" : 1}).limit(1))
return res[0]
@autoreconnect_retry
def findAssetMaxSupply(self, fromCollection):
res = list(self.mongoDB[fromCollection].find({"maxSupply" : {"$exists":True}},{ "_id": 0, "maxSupply" : 1}).limit(1))
return res[0]
@autoreconnect_retry
def findAssetBlockSize(self, fromCollection):
res = list(self.mongoDB[fromCollection].find({"blockSize" : {"$exists":True}},{ "_id": 0, "blockSize" : 1}).limit(1))
return res[0]
@autoreconnect_retry
def findAssetWalletPrefix(self, fromCollection):
res = list(self.mongoDB[fromCollection].find({"walletPrefix" : {"$exists":True}},{ "_id": 0, "walletPrefix" : 1}).limit(1))
return res[0]
@autoreconnect_retry
def findAssetExplorer(self, fromCollection):
res = list(self.mongoDB[fromCollection].find({"explorer" : {"$exists":True}},{ "_id": 0, "explorer" : 1}).limit(1))
return res[0]
@autoreconnect_retry
def findAssetExplorer2(self, fromCollection):
res = list(self.mongoDB[fromCollection].find({"explorer2" : {"$exists":True}},{ "_id": 0, "explorer2" : 1}).limit(1))
return res[0]
@autoreconnect_retry
def findAssetExplorer3(self, fromCollection):
res = list(self.mongoDB[fromCollection].find({"explorer3" : {"$exists":True}},{ "_id": 0, "explorer3" : 1}).limit(1))
return res[0]
@autoreconnect_retry
def findAssetWebsite(self, fromCollection):
res = list(self.mongoDB[fromCollection].find({"website" : {"$exists":True}},{ "_id": 0, "website" : 1}).limit(1))
return res[0]
@autoreconnect_retry
def findAssetRpcPort(self, fromCollection):
res = list(self.mongoDB[fromCollection].find({"rpcPort" : {"$exists":True}},{ "_id": 0, "rpcPort" : 1}).limit(1))
return res[0]
@autoreconnect_retry
def findAssetNetworkPort(self, fromCollection):
res = list(self.mongoDB[fromCollection].find({"networkPort" : {"$exists":True}},{ "_id": 0, "networkPort" : 1}).limit(1))
return res[0]
@autoreconnect_retry
def findAssetBitcointalkThread(self, fromCollection):
res = list(self.mongoDB[fromCollection].find({"bitcointalkThread" : {"$exists":True}},{ "_id": 0, "bitcointalkThread" : 1}).limit(1))
return res[0]
@autoreconnect_retry
def findAssetTelegram(self, fromCollection):
res = list(self.mongoDB[fromCollection].find({"telegram" : {"$exists":True}},{ "_id": 0, "telegram" : 1}).limit(1))
return res[0]
@autoreconnect_retry
def findAssetReddit(self, fromCollection):
res = list(self.mongoDB[fromCollection].find({"reddit" : {"$exists":True}},{ "_id": 0, "reddit" : 1}).limit(1))
return res[0]
@autoreconnect_retry
def findAssetTwitter(self, fromCollection):
res = list(self.mongoDB[fromCollection].find({"twitter" : {"$exists":True}},{ "_id": 0, "twitter" : 1}).limit(1))
return res[0]
@autoreconnect_retry
def findAssetChat(self, fromCollection):
res = list(self.mongoDB[fromCollection].find({"chat" : {"$exists":True}},{ "_id": 0, "chat" : 1}).limit(1))
return res[0]
@autoreconnect_retry
def findAssetAbout(self, fromCollection):
res = list(self.mongoDB[fromCollection].find({"about" : {"$exists":True}},{ "_id": 0, "about" : 1}).limit(1))
return res[0]
@autoreconnect_retry
def findAssetAlgorithm(self, fromCollection):
res = list(self.mongoDB[fromCollection].find({"algorithm" : {"$exists":True}},{ "_id": 0, "algorithm" : 1}).limit(1))
return res[0]
@autoreconnect_retry
def findAssetDeveloperFee(self, fromCollection):
res = list(self.mongoDB[fromCollection].find({"developerFee" : {"$exists":True}},{ "_id": 0, "developerFee" : 1}).limit(1))
return res[0]
@autoreconnect_retry
def findAssetWhitepaper(self, fromCollection):
res = list(self.mongoDB[fromCollection].find({"whitepaper" : {"$exists":True}},{ "_id": 0, "whitepaper" : 1}).limit(1))
return res[0]
@autoreconnect_retry
def findAssetFirstBlock(self, fromCollection):
res = list(self.mongoDB[fromCollection].find({"firstBlock" : {"$exists":True}},{ "_id": 0, "firstBlock" : 1}).limit(1))
return res[0]