Last active
November 27, 2015 01:59
-
-
Save markhc/89114288172523a2466b to your computer and use it in GitHub Desktop.
Dank bot
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import sys | |
import praw | |
import time | |
import datetime | |
#default value | |
COUNTER = 2276 | |
FILENAME_COMMENTS = 'done_comments'; | |
FILENAME_COUNTER = 'counter'; | |
REDDIT_USERNAME = 'bot_username' | |
REDDIT_PASSWORD = 'bot_password' | |
#you get these from https://www.reddit.com/prefs/apps/ | |
BOT_CLIENTID = 'app_id' | |
BOT_CLIENTSECRET = 'dank_secret_wolololo' | |
def main(): | |
global COUNTER | |
#Read the ids of already parsed comments | |
with open(FILENAME_COMMENTS, 'r+') as dbfile: | |
already_done = dbfile.readlines() | |
already_done = [x.strip('\n') for x in already_done] | |
#Get the latest counter | |
with open(FILENAME_COUNTER, 'r') as file: | |
COUNTER = int(file.readline()) | |
user_agent = "HelloImSnith for /r/Dota2 1.0 by /u/ThatNotSoRandomGuy" | |
r = praw.Reddit(user_agent=user_agent) | |
#Set OAuth info | |
r.set_oauth_app_info( | |
client_id = BOT_CLIENTID, | |
client_secret = BOT_CLIENTSECRET, | |
redirect_uri = 'http://127.0.0.1:65010/' | |
'authorize_callback') | |
#Login to reddit | |
r.login(REDDIT_USERNAME, REDDIT_PASSWORD) | |
#Sends me a message when the bot is (re)started | |
r.send_message('ThatNotSoRandomGuy', 'Bot restarted', 'Shit happened') | |
subreddit = r.get_subreddit('Dota2') | |
sub_wait = {} # rate limit wait times for subreddits | |
running = True | |
while running: | |
try: | |
mentions = r.get_unread() | |
#Check new mentions | |
for m in mentions: | |
if m.subject != 'username mention': | |
m.mark_as_read(); | |
continue | |
#Do we have a cooldown on this sub? | |
if check_sub_wait(m.subreddit, sub_wait): | |
continue | |
#Process the message | |
if m.id not in already_done: | |
print('Replying to mention %s\n' % m.id) | |
do_mention(m, already_done) | |
#Get the newest 100 comments | |
dota2_comments = subreddit.get_comments(limit=100) | |
for comment in dota2_comments: | |
parse_comment(comment, already_done) | |
#Sleep for 2 seconds | |
time.sleep(2) | |
except KeyboardInterrupt: | |
running = False | |
except praw.errors.RateLimitExceeded as pe: | |
print(now.strftime("%m-%d-%Y %H:%M")) | |
print('ERROR:', pe) | |
add_sub_wait(pe.sleep_time, current_sub, sub_wait) | |
continue | |
except Exception as e: | |
print('ERROR:', e) | |
print('Going to sleep for %i seconds...\n' % (15)) | |
time.sleep(15) | |
continue | |
def parse_comment(comment, already_done): | |
#Scan comments for these words | |
matches = [ | |
['snith'], | |
['chen', 'jungle'] | |
] | |
#Dont reply to own comment | |
if comment.author.name == REDDIT_USERNAME: | |
already_done.append(m.id) | |
if comment.id not in already_done: | |
for match in matches: | |
i = 0 | |
for term in match: | |
if term in comment.body: | |
i = i + 1 | |
if i == len(match): | |
print('Replying to comment %s' % comment.id) | |
do_comment(comment, already_done) | |
def do_comment(comment, already_done): | |
#Build reply and send | |
replyto(comment, already_done) | |
def do_mention(m, already_done): | |
#Build reply and send | |
replyto(m, already_done) | |
#Mark as read | |
m.mark_as_read(); | |
def to_unicode(n): | |
chars = { | |
'0': '0', | |
'1': '1', | |
'2': '2', | |
'3': '3', | |
'4': '4', | |
'5': '5', | |
'6': '6', | |
'7': '7', | |
'8': '8', | |
'9': '9' | |
} | |
string = str(n) | |
for key, value in chars.items(): | |
if key in string: | |
string=string.replace(key,value) | |
return string | |
def replyto(c, done): | |
now = datetime.datetime.now() | |
global COUNTER | |
COUNTER = COUNTER + 1 | |
text = 'Hello I'm Snith, playing my %sth game on Chen. Even though it's the only hero I play I still suck at it. As a compensation for my lack of skill I am going to buy all support items and gank mid at lvl 2 and fail. Thanks for listening' % (to_unicode(COUNTER)) | |
c.reply(text) | |
#print text | |
done.append(c.id) | |
with open(FILENAME_COMMENTS, 'w') as dbfile: | |
for item in done: | |
dbfile.write("%s\n" % item) | |
with open(FILENAME_COUNTER, 'w') as file: | |
file.write("%s" % COUNTER) | |
def check_sub_wait(subreddit, subs): | |
s = subreddit.display_name | |
if s in subs: | |
now = time.time() | |
wait_time = subs[s] | |
print('%i < %i?' % (now, wait_time)) | |
if now >= wait_time: | |
del subs[s] | |
else: | |
return True | |
return False | |
def add_sub_wait(sleep_time, subreddit, subs): | |
now = time.time() | |
s = subreddit.display_name | |
subs[s] = now+sleep_time | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment