-
Notifications
You must be signed in to change notification settings - Fork 0
/
uploader.py
70 lines (58 loc) · 2.07 KB
/
uploader.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
import os, json
from datetime import datetime, timedelta
from re import X
import pymongo
uploaded = 0
totalFile = 0
notUploaded = 0
inputPath = "Output"
outputPath = "uploadLog"
def log(*args, end='\n'):
global logger
print(*args, end=end)
logger.write("".join(args)+end)
def checkMarketPresentByName(name):
return db.tradeNew.find({"trade.info.marketInfo.marketName": name})
def uploadInDb(trade):
global uploaded
db.tradeNew.insert_one(trade)
uploaded = uploaded+1
log(f'\t[>] Upload: {marketName}')
def checkByHash(found, trade):
global notUploaded
find = False
for x in found:
if x['trade'] == trade['trade']:
find = True
notUploaded = notUploaded+1
break
if find:
log(f'\t\t[x] Skip: {marketName}')
else:
uploadInDb(trade)
time = datetime.now().strftime('%m_%d_%Y_%H_%M_%S')
logger = open(os.path.join(outputPath, f'{time}_logs.txt'), 'w', encoding='utf-8')
client = pymongo.MongoClient(
"mongodb+srv://marco:4Nr1fD8mAOSypUur@cluster1.fzsll.mongodb.net/bf_historical?retryWrites=true&w=majority")
db = client.bf_historical
for dirpath, dirnames, filenames in os.walk(inputPath):
for filename in [f for f in filenames if f.endswith(".json")]:
try:
totalFile = totalFile+1
name = os.path.join(dirpath, filename)
log(f'[-] File: {name}')
file = open(os.path.join(dirpath, filename))
data = json.load(file)
date = data['trade']['info']['date']
marketName = data['trade']['info']['marketInfo']['marketName']
find = checkMarketPresentByName(marketName)
if find == None:
uploadInDb(data)
else:
checkByHash(find, data)
except Exception as e:
log(f"[-] Unable to read Book: {name}, Error: {e}")
continue
log(f"[v] Total File: {totalFile}")
log(f"[v] Uploaded File: {uploaded} ({round(uploaded / totalFile, 2)})")
log(f"[x] Skipper File: {notUploaded} ({round(notUploaded / totalFile, 2)})")