Skip to content

Instantly share code, notes, and snippets.

@fanbyprinciple
Last active January 19, 2022 18:25
Show Gist options
  • Save fanbyprinciple/f6aa60105a6f854510c8b3d8d8d0726a to your computer and use it in GitHub Desktop.
Save fanbyprinciple/f6aa60105a6f854510c8b3d8d8d0726a to your computer and use it in GitHub Desktop.
creating a reddit bot.py
## importing all the required libraries
import praw
import os
import re
## initialising praw
reddit = praw.Reddit('brevity_bot')
subreddit = reddit.subreddit("botjungle")
## creating a file posts_replied_to.txt to check what are the posts your bot has replied to
if not os.path.isfile("posts_replied_to.txt"):
posts_replied_to = []
else:
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))
## to iterate ove the subbredit post and then reply on selected posts
for submission in subreddit.hot(limit=5):
if submission.id not in posts_replied_to:
if re.search("colab.research.google.com", submission.selftext, re.IGNORECASE) or re.search("google-colab.com", submission.selftext, re.IGNORECASE):
submission.reply("jupter_bot: Thank you for sharing this Notebook.")
print("Bot replying to : ", submission.title)
posts_replied_to.append(submission.id)
## writing posts already replied to in a text file posts_replied_to.txt
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