Created
June 22, 2015 19:27
-
-
Save kootenpv/681e011958329c06073a to your computer and use it in GitHub Desktop.
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
# Code review of McKenna/Alex https://gist.github.com/awhit012/13ccc026089c2c48d50c by kootenpv | |
# 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 | |
# This bot attempt to reply with a relevant response by searching for the words in the comment in the responses. | |
# I plan to improve upon this feature | |
# 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 for BOT through USER_AGENT | |
# STEP 6: In terminal: | |
# $ python bot.py | |
import praw, time, csv, itertools | |
import 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. The 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 | |
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) | |
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, comment_text) | |
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, comment_text): | |
quote = self.find_quote_based_on_comment_text(comment_text) | |
if quote: | |
comment.reply(quote[:-2] + BY_LINE) | |
print("replying to comment in " + sub.title + " with " + quote[:-2]) | |
self.cache.append(comment.id) | |
def find_quote_based_on_comment_text(self, comment_text): | |
from random import shuffle | |
shuffle(self.quotes) | |
comment_words = comment_text.split() | |
comment_words.sort(key = len) | |
comment_words.reverse() | |
for word in comment_words: | |
self.get_good_quote(word) | |
if self.quotes: | |
return self.quotes[0] | |
return '' | |
def get_good_quote(self, word): | |
for quote in self.quotes: | |
if word in quote: | |
quote_to_use = quote | |
return quote_to_use | |
def save_cache(self): | |
myfile = open('cache.csv', 'wb') | |
wr = csv.writer(myfile, quoting=csv.QUOTE_ALL) | |
wr.writerow(self.cache) | |
if __name__ == '__main__': | |
mckenna = Bot() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment