forked from holgern/steemrewarding
-
Notifications
You must be signed in to change notification settings - Fork 0
/
apply_vote_rules.py
173 lines (153 loc) · 6.69 KB
/
apply_vote_rules.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
from beem.utils import formatTimeString, resolve_authorperm, construct_authorperm, addTzInfo
from beem.nodelist import NodeList
from beem.comment import Comment
from beem import Steem
from datetime import datetime, timedelta
from beem.instance import set_shared_steem_instance
from beem.blockchain import Blockchain
import time
import json
import os
import math
import dataset
import random
from datetime import date, datetime, timedelta
from dateutil.parser import parse
from beem.constants import STEEM_100_PERCENT
from steemrewarding.post_storage import PostsTrx
from steemrewarding.command_storage import CommandsTrx
from steemrewarding.vote_rule_storage import VoteRulesTrx
from steemrewarding.pending_vote_storage import PendingVotesTrx
from steemrewarding.config_storage import ConfigurationDB
from steemrewarding.vote_storage import VotesTrx
from steemrewarding.utils import isfloat, tags_included, tags_excluded, string_included, string_excluded
from steemrewarding.version import version as rewardingversion
import dataset
if __name__ == "__main__":
config_file = 'config.json'
if not os.path.isfile(config_file):
raise Exception("config.json is missing!")
else:
with open(config_file) as json_data_file:
config_data = json.load(json_data_file)
# print(config_data)
databaseConnector = config_data["databaseConnector"]
start_prep_time = time.time()
db = dataset.connect(databaseConnector)
# Create keyStorage
print("Start apply_vote_rules.py")
nobroadcast = False
# nobroadcast = True
postTrx = PostsTrx(db)
voteRulesTrx = VoteRulesTrx(db)
confStorage = ConfigurationDB(db)
pendingVotesTrx = PendingVotesTrx(db)
conf_setup = confStorage.get()
last_processed_timestamp = conf_setup["last_processed_timestamp"]
if last_processed_timestamp is None:
last_processed_timestamp = datetime(1970,1,1,0,0,0)
if True:
max_batch_size = 50
threading = False
wss = False
https = True
normal = False
appbase = True
elif False:
max_batch_size = None
threading = True
wss = True
https = False
normal = True
appbase = True
else:
max_batch_size = None
threading = False
wss = True
https = True
normal = True
appbase = True
nodes = NodeList()
# nodes.update_nodes(weights={"block": 1})
try:
nodes.update_nodes()
except:
print("could not update nodes")
node_list = nodes.get_nodes(normal=normal, appbase=appbase, wss=wss, https=https)
if "https://api.steemit.com" in node_list:
node_list.remove("https://api.steemit.com")
stm = Steem(node=node_list, num_retries=5, call_num_retries=3, timeout=15, nobroadcast=nobroadcast)
pendingVotesTrx.delete_old_votes(6.4)
for post in postTrx.get_posts_list(last_processed_timestamp):
authorperm = post["authorperm"]
author = post["author"]
main_post = post["main_post"]
last_processed_timestamp = post["created"]
rules = voteRulesTrx.get_rules(author, main_post)
if len(rules) == 0:
continue
fitting_rules = []
for rule in rules:
# print(rule)
if not tags_included(rule["include_tags"], post["tags"]):
continue
if not tags_excluded(rule["exclude_tags"], post["tags"]):
continue
if post["word_count"] < rule["minimum_word_count"]:
continue
if rule["exclude_declined_payout"] and post["decline_payout"]:
continue
app = post["app"]
if app is not None and app.find("/") > -1:
app = app.split("/")[0]
if not string_included(rule["include_apps"], app):
continue
if not string_excluded(rule["exclude_apps"], app):
continue
fitting_rules.append(rule)
if len(fitting_rules) == 0:
continue
cnt = 0
c = None
while c is None and cnt < 5:
cnt += 1
try:
c = Comment(authorperm, use_tags_api=False, steem_instance=stm)
c.refresh()
except:
nodelist = NodeList()
nodelist.update_nodes()
stm = Steem(node=nodelist.get_nodes(), num_retries=5, call_num_retries=3, timeout=15, nobroadcast=nobroadcast)
time.sleep(1)
if cnt == 5:
print("Could not read %s" % (authorperm))
continue
voters = []
#for v in c.get_votes():
# voters.append(v["voter"])
not_processed_rules = []
for r in fitting_rules:
voter = r["voter"]
if voter in voters:
continue
not_processed_rules.append(r)
if len(not_processed_rules) == 0:
continue
print("vote %s - rules %d" % (authorperm, len(not_processed_rules)))
for rule in not_processed_rules:
if rule["include_text"] is not None and rule["include_text"] != "":
if c.body.find(rule["include_text"]) == -1:
continue
if rule["exclude_text"] is not None and rule["exclude_text"] != "":
if c.body.find(rule["exclude_text"]) > -1:
continue
if rule["enabled"]:
pending_vote = {"authorperm": authorperm, "voter": rule["voter"], "vote_weight": rule["vote_weight"], "comment_timestamp": c["created"].replace(tzinfo=None),
"vote_delay_min": rule["vote_delay_min"], "created": datetime.utcnow(), "min_vp": rule["min_vp"], "vote_when_vp_reached": rule["vote_when_vp_reached"],
"vp_reached_order": rule["vp_reached_order"], "max_net_votes": rule["max_net_votes"], "max_pending_payout": rule["max_pending_payout"],
"max_votes_per_day": rule["max_votes_per_day"], "max_votes_per_week": rule["max_votes_per_week"], "vp_scaler": rule["vp_scaler"], "leave_comment": rule["leave_comment"],
"maximum_vote_delay_min": rule["maximum_vote_delay_min"], "vote_sbd": rule["vote_sbd"],
"main_post": rule["main_post"], "pending_vote_timestamp": c["created"].replace(tzinfo=None) + timedelta(seconds=rule["vote_delay_min"]/60)}
pendingVotesTrx.add(pending_vote)
confStorage.update({"last_processed_timestamp": last_processed_timestamp})
print("check posts script run %.2f s" % (time.time() - start_prep_time))