Skip to content

Instantly share code, notes, and snippets.

@PythonCoderAS
Last active September 29, 2019 02:26
Show Gist options
  • Select an option

  • Save PythonCoderAS/4d1a917c5999bd368a60344b7b6aff27 to your computer and use it in GitHub Desktop.

Select an option

Save PythonCoderAS/4d1a917c5999bd368a60344b7b6aff27 to your computer and use it in GitHub Desktop.
#!usr/local/bin/python
# -*- coding: utf-8 -*-
"""This program will scan through a subreddit, and check for new posts. It will check for a code combination of
xxxxx-xxxxx-xxxxx-xxxxx-xxxxx, where x is a number from 0-9. It will make a comment with the post code."""
import praw # Reddit API
import re # Regex API
# Credit to u/admirelurk (https://www.reddit.com/user/admirelurk/)
# Comment: (https://www.reddit.com/r/RequestABot/comments/damalg/a_bot_that_recognizes_a_character_in_a_post_body/f1rijhz?utm_source=share&utm_medium=web2x)
regex = r"(?:\w{5}\-)+\w{5}" # The regex code to use in order to find the code.
sub = "test" # The subreddit to scan
username = "example" # The username of the bot
password = "password" # The password of the bot
# These must be generated at https://old.reddit.com/perfs/apps
client_id = "id" # The client ID of the bot
client_secret = "secret" # The client secret of the bot.
reddit = praw.Reddit(username=username, password=password, client_id=client_id, client_secret=client_secret, user_agent = "Bot to post found codes")
subreddit = reddit.subreddit(sub)
for submission in subreddit.stream.submissions():
if submission.is_self:
body = submission.selftext
match = regex.search(body)
if match:
code = match.group()
code.replace("-", "")
r = submission.reply("Code: `%s\nCheck replies to this comment for a mobile-compatible copy`," % code)
r.distinguish(how='yes', sticky=True) # only if the bot is a mod of the subreddit
r.reply(str(code))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment