Skip to content

Instantly share code, notes, and snippets.

@Watchful1
Created August 1, 2018 22:36
Show Gist options
  • Save Watchful1/ee58e256e33c8e77b3bd85d989d2c32d to your computer and use it in GitHub Desktop.
Save Watchful1/ee58e256e33c8e77b3bd85d989d2c32d to your computer and use it in GitHub Desktop.
def bot_login():
print "Loggin in..."
r = praw.Reddit(username = config.username,
password = config.password,
client_id = config.client_id,
client_secret = config.client_secret,
user_agent = "phylohelper v0.1")
print "Logged in!"
return r
def run_bot(r, comments_replied_to):
print "Obtaining 10 comments..."
for comment in r.subreddit('whatsthissnaketest').comments(limit=10):
# we still want all these checks other than the one checking if the keyword is in the comment
if comment.author.name.lower() in list_of_names and comment.id not in comments_replied_to and comment.author != r.user.me() and datetime.utcfromtimestamp(comment.created_utc) > startTime:
# now if it's a good comment, let's loop over all of our words
for species in specieslist:
# check if this word is in the comment
if "*"+species+"*" in comment.body:
print "String with \"+ species""\" found in comment " + comment.id
# assuming your text file is the species name
with open(species + ".txt", "r") as f:
comment_reply = f.read()
comment.reply(comment_reply + "\n\n" + sig)
print "Replied to comment " + comment.id
comments_replied_to.append(comment.id)
with open ("comments_replied_to.txt", "a") as f:
f.write(comment.id + "\n")
# assuming you only want to reply once to each comment, even if it has multiple keywords, so let's break out of the loop
#break
# checking if there's brackets in the title with text in them is a little bit tricky to do normally, so we're going to use something called a regex
# this is basically a fancy way of finding a certain string inside another string and it lets us do all kinds of fancy stuff like use wildcards
for submission in r.subreddit('whatsthissnaketest').new(limit=10):
# we're doing a couple things here. First we're calling the function re.findall, which is our fancy search function. We pass in our regex and the submission title
# The regex is '\[.+\]'. We want to first find [, but that already has a special meaning in regex, so we need to escape it with the '\'. Then '.' means any character at all
# and we modify it with '+', which means at least one, but then any number. Then we close it with another '\]' bracket. So something like '[test]' matches
# but '[]' doesn't. And '[test' doesn't. And no brackets at all doesn't either. This function returns a list of the matches, so we can check the length of the list.
# If that length is 0, there are no matches, and we should reply with our comment.
if len(re.findall('\[.+\]', submission.title)) == 0:
submission.reply("Please provide a geographic location")
print "Sleeping for 30 seconds..."
#Sleep for 30 seconds...
time.sleep(30)
def get_saved_comments():
if not os.path.isfile("comments_replied_to.txt"):
comments_replied_to = []
else:
with open("comments_replied_to.txt", "r") as f:
comments_replied_to = f.read()
comments_replied_to = comments_replied_to.split("\n")
comments_replied_to = filter(None, comments_replied_to)
return comments_replied_to
r = bot_login()
comments_replied_to = get_saved_comments()
print comments_replied_to
while True:
run_bot(r, comments_replied_to)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment