-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
73 lines (62 loc) · 2.37 KB
/
main.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
import requests, json, datetime,logging
from ConfigParser import SafeConfigParser
from pymongo import MongoClient
client = MongoClient('mongodb://localhost:27017/')
db = client.lisk_pool
parser = SafeConfigParser()
parser.read('config.ini')
logging.basicConfig(format='[%(asctime)s] %(message)s', filename='logger.log', level=logging.INFO)
host = parser.get('Node','protocol')+parser.get('Node','ip')+parser.get('Node','port')
endpoint = parser.get('Node','endpoint')
params = parser.get('Node','params')
pub_key = parser.get('Node','pub_key')
def check_time_diff(last_update, now):
"""
Return True if more than 24 hours has passed from last update
:param last_update:
:param now:
:return: boolean
"""
return now - last_update > datetime.timedelta(1)
r = requests.get(host + endpoint + params + pub_key)
voters = json.loads(r.text)['accounts']
voters_already_in_pool = db.voters
for v in voters:
voter = voters.find_one({'address': v['address']})
if voter:
if not check_time_diff(voter['updated_at'], datetime.datetime.now()):
db.voters.update(
{'address': v['address']},
{'$set': {
'updated_at': datetime.datetime.now(),
'address': v['address'],
'username': v['username'],
'publicKey': v['publicKey'],
'balance': v['balance']
}
})
info_str = "{} day in pool not updated".format(v['username'])
else:
db.voters.update(
{'address': v['address']},
{'$inc': {'day_in_pool': 1},
'$set': {
'updated_at': datetime.datetime.now(),
'address': v['address'],
'username': v['username'],
'publicKey': v['publicKey'],
'balance': v['balance']
}
})
info_str = "{} day in pool updated".format(v['username'])
else:
db.voters.insert_one({
'address': v['address'],
'day_in_pool': 1,
'updated_at': datetime.datetime.now(),
'username': v['username'],
'publicKey': v['publicKey'],
'balance': v['balance']
})
info_str = "{} welcome".format(v['username'])
logging.info(info_str)