Skip to content

Instantly share code, notes, and snippets.

@awhit012
Created July 16, 2015 01:31
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save awhit012/089a54b09a53ee389827 to your computer and use it in GitHub Desktop.
Save awhit012/089a54b09a53ee389827 to your computer and use it in GitHub Desktop.
spirit_of_mckenna.py
import praw, time, csv, random, itertools, requests
BOT = 'spirit_of_mckenna'
PASSWORD = "" #not actual password
KEYWORD = "mckenna"
SLEEP_MESSAGE = "Sleeping for 12 hours..."
BY_LINE = " -Terence McKenna"
USER_AGENT = "When people reference McKenna, I provide a quote of his, /u/spirit_of_mckenna"
SUBREDDITS = [ 'test',
'psychonaut',
'rationalpsychonaut',
'conspiro',
'joerogan',
'heavymind',
'woahdude',
'news',
'worldnews',
'quotesporn',
'shamanism',
'luciddreaming',
'interestingasfuck',
'holofractal',
'futurology',
'futureporn',
'learnprogramming'
]
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('')
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
print SLEEP_MESSAGE
time.sleep(43200)
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 KEYWORD 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__':
mckenna = Bot()
@epetakov
Copy link

epetakov commented Sep 1, 2015

This is awesome! Where can we see McKenna Bot in action?

@awhit012
Copy link
Author

@epetakov Thank you! I had it running on a raspberry pi and it commented on the subreddits in the SUBREDDITS array, but I haven't ran it in a couple years. I'll set it back up one of these days. Feel free to copy it and run it yourself it you want!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment