Created
November 15, 2019 03:20
-
-
Save mia-ktlk/d7510091b42266fb6cc4bfa496a6f3ac to your computer and use it in GitHub Desktop.
Reddit bot that blesses sneezes.
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
#Special thanks to Shantnu "shantnu" Tiwari for his tutorial series "Build a Reddit Bot" on pythonforengineers.com | |
#!/usr/bin/python | |
import praw | |
import pdb | |
import re | |
import os | |
# Create the Reddit instance | |
reddit = praw.Reddit('bot1') | |
def idTracker(): | |
# 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)) | |
return posts_replied_to | |
# Select your desired subreddit | |
subreddit = reddit.subreddit('SUBREDDIT') | |
def main(posts_replied_to): | |
for submission in subreddit.hot(limit=10): | |
# If we haven't replied to this post before | |
if submission.id not in posts_replied_to: | |
# Search for cases representing a sneeze | |
if re.search("sneeze", submission.title, re.IGNORECASE) or re.search("achoo", submission.title, re.IGNORECASE): | |
# Reply to the post | |
response = "Bless you u/" + str(submission.author) + "/n | |
submission.reply(response) | |
print("Bot replying to : ", submission.id) | |
# 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", "a") as f: | |
f.write(submission.id + "\n") | |
#Steps are the same for this but focusing on comments in the subreddit rather than post titles | |
for comment in subreddit.comments(limit=500): | |
if comment.id not in posts_replied_to: | |
if re.search("sneeze", comment.body, re.IGNORECASE) or re.search("achoo", comment.body, re.IGNORECASE): | |
response = "Bless you u/" + str(comment.author) | |
comment.reply(response) | |
print("Bot replying to : ", comment.id) | |
posts_replied_to.append(comment.id) | |
with open ("posts_replied_to.txt", "a") as f: | |
f.write(comment.id + "\n") | |
posts_replied_to = idTracker() | |
print posts_replied_to | |
while True: | |
main(posts_replied_to) |
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
astroid==1.6.6 | |
attrs==19.1.0 | |
backports-abc==0.5 | |
backports.functools-lru-cache==1.5 | |
backports.shutil-get-terminal-size==1.0.0 | |
backports.shutil-which==3.5.2 | |
bleach==3.1.0 | |
certifi==2019.6.16 | |
chardet==3.0.4 | |
colorama==0.4.1 | |
configparser==3.8.1 | |
decorator==4.4.0 | |
defusedxml==0.6.0 | |
entrypoints==0.3 | |
enum34==1.1.6 | |
functools32==3.2.3.post2 | |
futures==3.3.0 | |
idna==2.8 | |
ipaddress==1.0.22 | |
ipykernel==4.10.1 | |
ipython==5.8.0 | |
ipython-genutils==0.2.0 | |
ipywidgets==7.5.1 | |
isort==4.3.21 | |
Jinja2==2.10.1 | |
jsonschema==3.0.2 | |
jupyter==1.0.0 | |
jupyter-client==5.3.1 | |
jupyter-console==5.2.0 | |
jupyter-core==4.5.0 | |
lazy-object-proxy==1.4.2 | |
MarkupSafe==1.1.1 | |
mccabe==0.6.1 | |
mistune==0.8.4 | |
nbconvert==5.6.0 | |
nbformat==4.4.0 | |
notebook==5.7.8 | |
pandocfilters==1.4.2 | |
pathlib2==2.3.4 | |
pickleshare==0.7.5 | |
praw==6.3.1 | |
prawcore==1.0.1 | |
prometheus-client==0.7.1 | |
prompt-toolkit==1.0.16 | |
Pygments==2.4.2 | |
pylint==1.9.5 | |
pyrsistent==0.15.4 | |
python-dateutil==2.8.0 | |
pywinpty==0.5.5 | |
pyzmq==18.1.0 | |
qtconsole==4.5.5 | |
requests==2.22.0 | |
scandir==1.10.0 | |
Send2Trash==1.5.0 | |
simplegeneric==0.8.1 | |
singledispatch==3.4.0.3 | |
six==1.12.0 | |
terminado==0.8.2 | |
testpath==0.4.2 | |
tornado==5.1.1 | |
traitlets==4.3.2 | |
update-checker==0.16 | |
urllib3==1.25.3 | |
wcwidth==0.1.7 | |
webencodings==0.5.1 | |
websocket-client==0.56.0 | |
widgetsnbextension==3.5.1 | |
win-unicode-console==0.5 | |
wrapt==1.11.2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment