-
Notifications
You must be signed in to change notification settings - Fork 13
/
bot.py
297 lines (230 loc) · 9.72 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
293
294
295
296
297
import steem
import time
import datetime
import math
import signal
import copy
import requests
import sys
import traceback
from steem import Steem
from datetime import timedelta
nodes = ['https://api.steemit.com',
'https://rpc.buildteam.io',
'https://steemd.minnowsupportproject.org']
acc_name = 'therising' #Replace therising with your steem account name.
s = Steem(nodes, keys=['Your_Private_Posting_Key', 'Your_Private_Active_Key'])
firstrun= True
rndlimit= False #Set to True if you want to enable round fill limit
trxlist=[]
votelist=[]
total=0
errcnt=0
curr_round=[]
last_round=[]
#%store curr_round # Uncomment all %store statements if you want to enable api
#%store last_round
def node_failover():
global nodes,s
nodes = nodes[1:] + nodes[:1]
print ("Switching to the next node: ",nodes)
s = Steem(nodes)
return
def get_vote_value(account_name):
total_vests=float(s.get_account(account_name)['vesting_shares'].split(" ")[0]) + float(s.get_account(account_name)['received_vesting_shares'].split(" ")[0])
vests_steem= total_vests*(10**6)*0.02*float(s.get_reward_fund('post')['reward_balance'].split(" ")[0])/float(s.get_reward_fund('post')['recent_claims'])
vests_sbd = vests_steem*float(s.get_current_median_history_price()['base'].split(" ")[0])/float(s.get_current_median_history_price()['quote'].split(" ")[0])
vote_value = (vests_sbd/(2*float(requests.get('https://api.coinmarketcap.com/v1/ticker/steem-dollars/').json()[0]['price_usd'])))+(vests_sbd/2)
vote_value = round(vote_value,2)
return (vote_value)
def convt(amt,curr):
r1 = requests.get('https://api.coinmarketcap.com/v1/ticker/steem-dollars/')
r2 = requests.get('https://api.coinmarketcap.com/v1/ticker/steem/')
tkr1=r1.json()[0]['price_usd']
tkr2=r2.json()[0]['price_usd']
conv=float(tkr2)/float(tkr1)
print ("Converting", amt, curr)
famt = amt*conv
fcurr = 'SBD'
print ("Converted to",famt,fcurr)
return (famt,fcurr)
def refund (bidder,amt,curr,msg):
global firstrun,errcnt
if ((0.01<amt<100.0) and (not firstrun)):
memo = 'Refund for invalid bid: ' + msg
for i in range(16):
try:
s.commit.transfer(bidder,amt,curr,memo,acc_name)
break
except:
print ("Refund error: ", sys.exc_info()[0])
print (traceback.format_exc())
errcnt+=1
if (errcnt>=5):
node_failover()
errcnt=0
time.sleep(3)
print (memo)
elif (firstrun):
print ("No refund: First Run")
else:
print ("No refund: Amt not eligible")
return
def upvote (votelist,total):
global last_round,curr_round,errcnt
last_round=copy.deepcopy(curr_round)
curr_round = []
ind=0
errcnt=0
for j in votelist:
wgt=round(j[0]*100/(total),2)
link = j[2][j[2].find('@'):]
while True:
try:
s.commit.vote(link,wgt,acc_name)
break
except:
print ("Voting error: ", sys.exc_info()[0])
print (traceback.format_exc())
errcnt+=1
if (errcnt>=5):
node_failover()
errcnt=0
time.sleep(4)
last_round[ind]['weight']=int(wgt*100)
ind+=1
time.sleep(5)
print ("Upvoted, weight:",link,wgt)
# %store last_round
# %store curr_round
#Comment
for k in votelist:
wgt=round(k[0]*100/(total),2)
link = k[2][k[2].find('@'):]
comment = 'You just rose by ' + str(wgt) + '% upvote from @' + acc_name + ' courtesy of @' + k[3]
while True:
try:
s.commit.post(title='',author=acc_name,body=comment,reply_identifier=link)
break
except:
print ("Commenting error: ", sys.exc_info()[0])
print (traceback.format_exc())
errcnt+=1
if (errcnt>=5):
node_failover()
errcnt=0
time.sleep(3)
time.sleep(22)
print ("Upvoted & commented:",link)
return
def validate (bidder,amt,curr,memo):
global votelist,curr_round,total,rndlimit
### Validation: Min Bid Amt (1.0 SBD)
if (amt<1.0):
refund(bidder,amt,curr,'Min Bid amount is 1 SBD')
return ("Invalid")
### Validation: Round Fill Limit
if (rndlimit):
try:
vote_value=get_vote_value(acc_name)
curr_vote_value=round(0.75*vote_value*1.0,3)
print ("Vote Value: ",curr_vote_value)
if (curr == 'STEEM'):
namt,ncurr = convt(amt,curr)
if ((total+namt)>curr_vote_value):
refund(bidder,amt,curr,'Our bot guarantees profit or atleast breakeven for our bot-users. Please bid in the next round as current round is already around breakeven.')
return ("Invalid")
if ((total+amt)>curr_vote_value):
print ("Total round amt:")
refund(bidder,amt,curr,'Our bot guarantees profit or atleast breakeven for our bot-users. Please bid in the next round as current round is already around breakeven.')
return ("Invalid")
except:
print ("Round Limit Error: ",sys.exc_info()[0])
print (traceback.format_exc())
### Validation: Valid URL, Post Age, Voted or Not?
pl = memo[memo.find('@'):]
perm = pl[pl.find('/')+1:]
auth = pl[1:pl.find('/')]
urlapi = memo[memo.find('.com/')+4:]
memos = [x[2][x[2].find('@'):] for x in votelist]
d = timedelta(days=3.5)
try:
post = steem.post.Post(pl,s)
votl = [x['voter'] for x in s.get_active_votes(auth,perm)]
if (post.is_main_post()):
if (post.time_elapsed()<d):
if (acc_name not in votl):
if (pl not in memos):
curr_round.append({"amount":amt,"currency":curr,"sender":bidder,"author":auth,"permlink":perm,"url":urlapi})
#%store curr_round
return ("Valid")
else:
curr_round[memos.index(pl)]['amount']+=round(amt,3)
#%store curr_round
if (curr == 'STEEM'):
amt,curr = convt(amt,curr)
votelist[memos.index(pl)][0]+=round(amt,3)
total+=amt
return("Already Present in Votelist")
else:
refund (bidder,amt,curr,'Post is already upvoted')
return ("Invalid")
else:
refund (bidder,amt,curr,'Max Post Age exceeded')
return ("Invalid")
else:
refund (bidder,amt,curr,'Invalid URL')
return ("Invalid")
except:
refund (bidder,amt,curr,'Invalid URL')
print ("Validation error: ", sys.exc_info()[0])
print (traceback.format_exc())
return ("Invalid")
while True:
try:
ac=steem.account.Account(acc_name,s)
gen1= ac.get_account_history(-1,500,filter_by=['transfer'])
gen2= ac.get_account_history(-1,500,filter_by=['vote'])
for k in gen2:
if (k['voter'] == acc_name):
prev_time = datetime.datetime.strptime(k['timestamp'], "%Y-%m-%dT%H:%M:%S")
break
tt = timedelta(seconds=30+(10000-s.get_account(acc_name)['voting_power'])*43.2)
print ("Current Time: "+ str(datetime.datetime.utcnow()) +"|Prev vote: " + str(prev_time) + "|Next Vote: " + str(prev_time + tt))
for i in gen1:
if (i['trx_id'] in trxlist):
print ("Breaking at TrxID: ",i['trx_id'])
break
if (i['to']==acc_name):
bidder = i['from']
memo = i['memo']
amt,curr = i['amount'].split(" ")
amt = float (amt)
trxlist.append(i['trx_id'])
print ("Trxlist after append=",trxlist)
if (validate(bidder,amt,curr,memo)=="Valid"):
if (curr == 'STEEM'):
amt,curr = convt(amt,curr)
votelist.append([round(amt,3),curr,memo,bidder])
total = total + amt
print ("Votelist , total after append",votelist,total)
#print ("Votelist: ",votelist,total)
if ( (datetime.datetime.utcnow() - prev_time) > tt):
print ("Upvoting: votelist,total=",votelist,total)
upvote(votelist,total)
votelist = []
total = 0
trxlist = trxlist[0:5] + trxlist[-5:]
firstrun=False
time.sleep(10)
except KeyboardInterrupt:
print ("Interrupted")
break
except:
print ("Unexpected error: ", sys.exc_info()[0])
print (traceback.format_exc())
errcnt+=1
if (errcnt>=5):
node_failover()
errcnt=0
time.sleep(10)