/
bot.py
292 lines (227 loc) · 9.41 KB
/
bot.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
# Original Source Code from https://github.com/emre/tagbot
# Run and maintain by team @dpet on steemit
import argparse
import json
import logging
import random
import time
from datetime import datetime, timedelta
from dateutil.parser import parse
from steem.account import Account
from steem.post import Post
from steem.amount import Amount
import requests
from utils import get_steem_conn, get_current_vp, url, reputation, tokenize
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
logging.basicConfig()
class TagBot:
def __init__(self, config, steemd_instance):
self.config = config
self.steemd_instance = steemd_instance
self.target_tags = config.get("TAGS") or [config.get("TAG"), ]
def check_vp(self):
current_vp = get_current_vp(
self.config["BOT_ACCOUNT"],
self.steemd_instance)
if current_vp < self.config["MINIMUM_VP_TO_START"]:
raise ValueError(
"Current voting power is not enough to start the round.",
round(current_vp, 2)
)
else:
logger.info("VP is enough, let's start the round!")
def last_voted_accounts_and_posts(self):
voted_accounts = set()
voted_posts = set()
account = Account(self.config.get("BOT_ACCOUNT"))
vote_interval = self.config.get("VOTE_INTERVAL_IN_DAYS", 1)
for vote in account.history_reverse(filter_by=["vote"]):
created_at = parse(vote["timestamp"])
if created_at < (datetime.utcnow() - timedelta(days=vote_interval)):
break
voted_accounts.add(vote["author"])
voted_posts.add("@%s/%s" % (vote["author"], vote["permlink"]))
return voted_accounts, voted_posts
def conforms_minimum_word_count(self, body):
# @todo: consider removing noise. (html, markdown etc.)
if self.config.get("MINIMUM_WORD_COUNT"):
word_count = len(tokenize(body))
if word_count < self.config.get("MINIMUM_WORD_COUNT"):
return False
return True
def reputation_is_enough(self, author_rep):
return reputation(author_rep) > self.config["MINIMUM_AUTHOR_REP"]
def fetch_tag(self, tag, start_author=None, start_permlink=None, posts=[], scanned_pages=0):
logger.info("Fetching tag: #%s", tag)
query = {
"limit": 100,
"tag": tag,
}
if start_author:
query.update({
"start_author": start_author,
"start_permlink": start_permlink,
})
post_list = list(self.steemd_instance.get_discussions_by_created(query))
for post in post_list:
created_at = parse(post["created"])
if (datetime.utcnow() - created_at).days > 5:
return posts
# Check if post age is older than MINIMUM_POST_AGE (IN MINUTE!)
elapsed_minutes = (datetime.utcnow() - created_at).total_seconds() // 60
if self.config.get("MINIMUM_POST_AGE_MIN"):
if self.config.get("MINIMUM_POST_AGE_MIN") > elapsed_minutes:
continue
if self.config.get("MAXIMUM_POST_REWARDS"):
pending_payout = Amount(post.get("pending_payout_value"))
if pending_payout.amount > self.config.get("MAXIMUM_POST_REWARDS"):
continue
posts.append(post)
if scanned_pages > 100 or len(posts) > 50:
logger.info("%s posts found at #%s tag.", len(posts), tag)
return posts
if not len(post_list):
logger.info("No article Found with this tag")
return posts
return self.fetch_tag(
tag,
start_author=post["author"],
start_permlink=post["permlink"],
posts=posts,
scanned_pages=scanned_pages + 1,
)
def in_global_blacklist(self, author):
'''
@dev Check if author is in global blacklist
@param author The username that we want to check
@return bool True if the user is black listed globally
'''
url = "http://blacklist.usesteem.com/user/" + author
response = requests.get(url).json()
return bool(len(response["blacklisted"]))
def start_voting_round(self):
'''
@dev the main voting round script
'''
posts = []
for tag in self.target_tags:
posts += self.fetch_tag(tag)
logger.info("%s posts found.", len(posts))
already_voted_accounts, already_voted_posts = self.last_voted_accounts_and_posts()
blacklist = self.config.get("BLACKLIST", [])
app_whitelist = self.config.get("APP_WHITELIST", [])
filtered_posts = []
for post in posts:
post_instance = Post(post, steemd_instance=self.steemd_instance)
if not self.reputation_is_enough(post["author_reputation"]):
continue
if "@%s/%s" % (post.get("author"), post.get("permlink")) in already_voted_posts:
continue
if post.get("author") in blacklist:
continue
if post.get("author") in already_voted_accounts:
continue
tag_blacklist = self.config.get("TAG_BLACKLIST", [])
if set(tag_blacklist).intersection(post_instance.get("tags")):
continue
if app_whitelist:
app = post_instance.get("json_metadata").get("app")
if not app:
continue
app_without_version = app.split("/")[0]
if app_without_version not in app_whitelist:
continue
if not self.conforms_minimum_word_count(post_instance.get("body")):
continue
# Check if the post is downvoted by any trusted account
if self.config.get("TRUSTED_ACCOUNTS"):
found_downvote = False
for vote in post.get("active_votes", []):
rshares = vote["rshares"]
if not isinstance(rshares, int):
rshares = int(rshares)
if vote["voter"] in self.config.get("TRUSTED_ACCOUNTS") and rshares < 0:
found_downvote = True
break
if found_downvote:
logger.info("Found a downvote of trusted accounts. Skipping. %s", post.identifier)
continue
filtered_posts.append(post)
logger.info("%s posts left after the filters." % len(filtered_posts))
# Shuffle the list to make it random
random.shuffle(filtered_posts)
# for post in filtered_posts[0:self.config["VOTE_COUNT"]]:
for post in filtered_posts:
if self.config.get("DEBUG"):
print(post)
continue
# check cheetah commented on it
replies = self.steemd_instance.get_content_replies(
post.get("author"), post.get("permlink")
)
found_cheetah = False
for reply in replies:
if reply["author"] in ["cheetah"]:
found_cheetah = True
break
if found_cheetah:
logger.info(
"Post: %s is skipped since it's commented by cheetah.",
post.identifier
)
continue
if self.in_global_blacklist(post.get("author")):
logger.info(
"Post is skipped since @{} is globally blacklisted".format(post.get("author"))
)
continue
self.upvote(
Post(post, steemd_instance=self.steemd_instance),
self.config["VOTE_WEIGHT"],
)
def upvote(self, post, weight, retry_count=None, reply=True):
if not retry_count:
retry_count = 0
if self.config["BOT_ACCOUNT"] in [
v["voter"] for v in post.get("active_votes", [])]:
logger.error(
"Already voted on this. Skipping... {}".format(post.identifier))
return
try:
self.steemd_instance.commit.vote(
post.identifier,
weight, account=self.config["BOT_ACCOUNT"])
logger.info(
"Casted vote on: %s with weight: %s", url(post), weight)
time.sleep(3)
except Exception as error:
logger.error(error)
return self.upvote(post, weight, retry_count + 1)
if reply:
self.reply(post)
def reply(self, post):
if self.config.get("POST_REPLY_TEMPLATE"):
reply_template = open(self.config.get("POST_REPLY_TEMPLATE")).read()
reply_body = reply_template.format(
author=post.get("author")
)
post.reply(reply_body, author=self.config["BOT_ACCOUNT"])
time.sleep(20)
else:
logger.info(" No comment specified, skipped ")
def run(self):
self.check_vp()
self.start_voting_round()
def main():
parser = argparse.ArgumentParser()
parser.add_argument("config", help="Config file in JSON format")
args = parser.parse_args()
config = json.loads(open(args.config).read())
upvote_bot = TagBot(
config,
get_steem_conn(config["NODES"])
)
upvote_bot.run()
if __name__ == '__main__':
main()