Last active
February 12, 2018 21:56
-
-
Save awhit012/13ccc026089c2c48d50c to your computer and use it in GitHub Desktop.
BotMother: A Reddit QuoteBot template using Praw
This file contains 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
# UPDATED 7/15 | |
# Thanks to kootenpv for the code review! | |
# This makes it easy to create a reddit bot that | |
# A. Grabs comments from subreddits of your choice | |
# B. Searches for a keyword | |
# C. Replies to comments with that keyword from a list of responses on a CSV file | |
# Follow correct etiquette: | |
# https://www.reddit.com/r/Bottiquette | |
# STEP 1: Sign up to Reddit using the name you want your bot to have e.g. "mckenna_quotebot" | |
# STEP 2: in terminal | |
# $ touch bot.py | |
# $ touch quotes.csv | |
# STEP 3: Copy the file below into bot.py | |
# STEP 4: Copy desired responses into quotes.csv | |
# This should be in the following format: | |
# "the response", | |
# "the second response", | |
# "the third response", ... | |
# STEP 5: Enter your info in below. | |
# STEP 6: In terminal: | |
# $ python bot.py | |
# And you're up and running! | |
import praw, time, csv, random, itertools, requests | |
BOT = "your_bot_username_here" | |
PASSWORD = "your_bots_password_here" | |
KEY_WORD = "what you are searching comments for" | |
SUBREDDITS = ['test'] # Use /r/test until you get it going. Then put your desired /r/'s here. | |
SLEEP_MESSAGE = "Sleeping for 5 minutes..." # Leave this for now, you can change it later. | |
BY_LINE = " -Your Byline" | |
USER_AGENT = "Brief description of your bot so Reddit knows what it is" | |
class Bot: | |
def __init__(self): | |
print("Initializing Bot") | |
self.quotes = [] | |
self.cache = [] | |
self.logged_in = False | |
self.r = praw.Reddit(user_agent = USER_AGENT) | |
self.parse_quotes() | |
self.parse_cache() | |
self.log_in() | |
while True: | |
self.run() | |
def run(self): | |
try: | |
self.get_subs() | |
except praw.errors.RateLimitExceeded as error: | |
print('\tSleeping for %d seconds' % error.sleep_time) | |
time.sleep(error.sleep_time) | |
except requests.exceptions.ConnectionError as error: | |
self.wait(error) | |
except KeyboardInterrupt: | |
raise | |
except: | |
self.wait(error) | |
self.save_cache() | |
self.wait("All Subreddits checked.") | |
def parse_quotes(self): | |
with open('quotes.csv') as csvfile: | |
for row in csvfile: | |
self.quotes.append(row.decode('utf-8')) | |
def parse_cache(self): | |
f = open('cache.csv') | |
for row in csv.reader(f): | |
self.cache.append(row) | |
from itertools import chain | |
chain = itertools.chain.from_iterable(self.cache) | |
self.cache = list(chain) | |
def wait(self, tag): | |
print(tag + ' ' + SLEEP_MESSAGE) | |
time.sleep(300) | |
def log_in(self): | |
while not self.logged_in: | |
try: | |
self.r.login(BOT, PASSWORD) | |
self.logged_in = True | |
print("logging in...") | |
except requests.exceptions.ConnectionError: | |
tag = 'No web connection.' | |
self.wait(tag) | |
def get_subs(self): | |
for subreddit in SUBREDDITS: | |
sub = self.r.get_subreddit(subreddit) | |
print("getting subreddit " + str(subreddit.title())) | |
self.get_comments_for_sub(sub) | |
def get_comments_for_sub(self, sub): | |
comments = sub.get_comments(limit=199) | |
print("getting comments...") | |
for comment in comments: | |
comment_text = comment.body.lower() | |
if self.has_keyword(comment_text) and self.not_bot(comment) and self.not_in_cache(comment): | |
self.reply_to_comment(sub, comment) | |
def has_keyword(self, comment_text): | |
if KEY_WORD in comment_text: | |
return True | |
def not_bot(self, comment): | |
if comment.author and comment.author.name != BOT: | |
return True | |
def not_in_cache(self, comment): | |
if comment.id not in self.cache: | |
return True | |
def reply_to_comment(self, sub, comment): | |
quote = random.choice(self.quotes) | |
if len(quote) > 1: | |
comment.reply(quote[:-2] + BY_LINE) | |
print "replying to comment in " + sub.title + " with " + quote[:-2]) | |
self.cache.append(comment.id) | |
self.save_cache() | |
def save_cache(self): | |
myfile = open('cache.csv', 'wb') | |
wr = csv.writer(myfile, quoting=csv.QUOTE_ALL) | |
wr.writerow(self.cache) | |
if __name__ == '__main__': | |
a_bot = Bot() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment