Created
July 2, 2017 16:52
-
-
Save TheEssem/46e207aee2195d4eec1a5386cbf7db1a to your computer and use it in GitHub Desktop.
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
#!/usr/bin/python | |
import praw | |
import pdb | |
import re | |
import os | |
# Create the Reddit instance | |
reddit = praw.Reddit('bot1') | |
# and login | |
reddit.login(REDDIT_USERNAME, REDDIT_PASSWORD) | |
# Have we run this code before? If not, create an empty list | |
if not os.path.isfile("posts_replied_to.txt"): | |
posts_replied_to = [] | |
# If we have run the code before, load the list of posts we have replied to | |
else: | |
# Read the file into a list and remove any empty values | |
with open("posts_replied_to.txt", "r") as f: | |
posts_replied_to = f.read() | |
posts_replied_to = posts_replied_to.split("\n") | |
posts_replied_to = list(filter(None, posts_replied_to)) | |
# Get values from our subreddit | |
subreddit = reddit.subreddit('me_irl') | |
for submission in subreddit: | |
#print(submission.title) | |
# If we haven't replied to this post before | |
if submission.id not in posts_replied_to: | |
# Do a case insensitive search | |
if re.search("r/bonehurtingjuice", submission.title, re.IGNORECASE): | |
# Reply to the post | |
submission.reply("oof ow ouch ^^I am a bot") | |
print("Bot replying to : ", submission.title) | |
# Store the current id into our list | |
posts_replied_to.append(submission.id) | |
# Write our updated list back to the file | |
with open("posts_replied_to.txt", "w") as f: | |
for post_id in posts_replied_to: | |
f.write(post_id + "\n") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment