Skip to content

Commit b9cb96d

Browse files
committed
Update bot.py
updated with some exception handling and the ability to cycle reddit accounts when rumbled.
1 parent 7782d24 commit b9cb96d

File tree

1 file changed

+78
-49
lines changed

1 file changed

+78
-49
lines changed

bot.py

+78-49
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,45 @@
88
import os
99
import random
1010

11-
USERNAME = '' #add in login details for bot..
12-
PASSWORD = ''
11+
PASSWORD = 'password' #the password for the throwaway accounts..
1312

14-
downvoters = [] #usernames to downvote and upvote..
15-
upvoters = []
13+
14+
accounts = ['random_throwaway1', 'random_throwaway2', 'etc' ]
15+
downvoters = ['some','user','to','downvote']
16+
upvoters = ['some','user','to','upvote']
1617

1718
n_upvotes = 0
1819
n_downvotes = 0
1920

2021
already_comment_id = []
2122

23+
def r_login():
24+
while True:
25+
try:
26+
attempt = accounts[random.randint(0,len(accounts)-1)]
27+
print 'attempt login with account:', attempt
28+
r.login(attempt, PASSWORD, disable_warning=True)
29+
log(time.strftime("%Y-%m-%d %H:%M:%S")+' login success: '+attempt+'\n')
30+
return
31+
except praw.errors.InvalidUserPass as e:
32+
print 'login failed - praw.errors.InvalidUserPass', attempt
33+
log(time.strftime("%Y-%m-%d %H:%M:%S")+' '+str(type(e))+attempt+'\n')
34+
accounts.remove(attempt)
35+
time.sleep(15)
36+
continue
37+
except Exception as e:
38+
print 'login failed', type(e), e.args, e
39+
quickstart.ErrorSendEmail()
40+
exit()
41+
42+
43+
2244
def log_work():
2345
global n_upvotes, n_downvotes
2446
print "Emailing 24 hourly log file.."
2547
print 'Upvotes ',str(n_upvotes),' Downvotes ', str(n_downvotes)
2648
quickstart.CredsSendLog(n_upvotes, n_downvotes)
27-
os.rename('log.txt','log.old'+str(random.randint(0,65535)))
49+
os.rename('./log/log.txt','./log/log.old'+str(random.randint(0,6553500)))
2850
n_upvotes = 0
2951
n_downvotes = 0
3052
print "Saving old log file, creating new file.."
@@ -33,74 +55,81 @@ def log_work():
3355
return
3456

3557
def log(string_data):
36-
with open("log.txt", "a") as myfile:
58+
with open("./log/log.txt", "a") as myfile:
3759
myfile.write(string_data)
3860
return
3961

4062

41-
r = praw.Reddit(user_agent='Something random') #change this to a unique string..
4263

43-
try:
44-
r.login(USERNAME, PASSWORD, disable_warning=True)
45-
except Exception as e:
46-
print 'login failure', type(e), e.args, e
47-
exit()
64+
r = praw.Reddit(user_agent='Some unique text not containing bot')
65+
66+
r_login()
4867

49-
subreddit = r.get_subreddit('test') #lazy so no need to exception raise.
68+
subreddit = r.get_subreddit('any subreddit you want') #lazy so no need to error check..
5069

5170
print '>>>Reddit /r/bitcoin bot..<<<'
5271

5372
print 'Downvoting:', downvoters
5473
print 'Upvoting:', upvoters
5574

56-
print '>>>active - logging to /log.txt<<<'
75+
print '>>>active - logging to ./log/log.txt<<<'
5776

58-
schedule.every().day.at("07:00").do(log_work) #scheduled log maintenance and emailing..
77+
schedule.every().day.at("07:00").do(log_work)
5978
schedule.every().day.at("19:00").do(log_work)
6079

6180
while True:
62-
try:
6381

64-
subreddit_comments = subreddit.get_comments() #get the new comments..
82+
try:
83+
subreddit_comments = subreddit.get_comments()
6584

85+
for comment in subreddit_comments:
6686

67-
for comment in subreddit_comments: #make sure they havent been seen already..
68-
69-
if comment.id in already_comment_id:
70-
pass
71-
72-
else:
73-
already_comment_id.append(comment.id)
74-
75-
chance = random.randint(1,4) #only vote manipulate 75% of time..
76-
if chance == 4:
87+
if comment.id in already_comment_id:
7788
pass
78-
79-
elif chance < 4 and comment.author.name in downvoters:
80-
print "downvoted: ", comment.author.name, comment.id
81-
comment.downvote()
82-
n_downvotes+=1
83-
logstr = time.strftime("%Y-%m-%d %H:%M:%S")+' downvoted '+comment.author.name+' '+comment.id+'\n'
84-
log(logstr)
85-
86-
elif chance < 4 and comment.author.name in upvoters:
87-
print 'match', comment.author.name, comment.id
88-
logstr = time.strftime("%Y-%m-%d %H:%M:%S")+' upvoted '+comment.author.name+' '+comment.id+'\n'
89-
log(logstr)
90-
comment.upvote()
91-
n_upvotes+=1
92-
93-
sys.stdout.write('.')
94-
sys.stdout.flush()
95-
schedule.run_pending()
96-
time.sleep(random.randint(0,20)) #randomly wait before scanning subreddit again..
97-
89+
90+
else:
91+
already_comment_id.append(comment.id)
92+
93+
if comment.author.name in downvoters:
94+
#comment.reply('test')
95+
print "downvoted: ", comment.author.name, comment.id
96+
comment.downvote()
97+
n_downvotes+=1
98+
logstr = time.strftime("%Y-%m-%d %H:%M:%S")+' downvoted '+comment.author.name+' '+comment.id+'\n'
99+
log(logstr)
100+
101+
102+
if comment.author.name in upvoters:
103+
print 'match', comment.author.name, comment.id
104+
logstr = time.strftime("%Y-%m-%d %H:%M:%S")+' upvoted '+comment.author.name+' '+comment.id+'\n'
105+
log(logstr)
106+
comment.upvote()
107+
n_upvotes+=1
108+
109+
110+
sys.stdout.write('.')
111+
sys.stdout.flush()
112+
schedule.run_pending()
113+
time.sleep(random.randint(0,20))
114+
115+
except praw.errors.InvalidUserPass as e:
116+
print 'login failed - praw.errors.InvalidUserPass'
117+
log(time.strftime("%Y-%m-%d %H:%M:%S")+' '+str(type(e))+'\n')
118+
time.sleep(15)
119+
r_login()
120+
continue
121+
except praw.errors.LoginRequired as e:
122+
print 'logged out - praw.errors.LoginRequired'
123+
log(time.strftime("%Y-%m-%d %H:%M:%S")+' '+str(type(e))+'\n')
124+
time.sleep(15)
125+
r_login()
126+
continue
98127
except Exception as e:
99128
print "Error", type(e), e.args, e
100-
log("Error"+type(e)+e.args)
129+
log(time.strftime("%Y-%m-%d %H:%M:%S")+" Error"+str(e)+"\n")
101130
time.sleep(20)
102131
continue
103-
132+
104133
except KeyboardInterrupt:
105134
print 'Exiting..upvotes: '+str(n_upvotes), ' downvotes: '+str(n_downvotes)
106135
log(time.strftime("%Y-%m-%d %H:%M:%S")+' manual shutdown CTRL-C..\n')

0 commit comments

Comments
 (0)