Skip to content

Instantly share code, notes, and snippets.

@cclloyd
Created October 6, 2018 04:03
Show Gist options
  • Select an option

  • Save cclloyd/55ab9ae7b99d3c9c5e71c4a8fd92c1e3 to your computer and use it in GitHub Desktop.

Select an option

Save cclloyd/55ab9ae7b99d3c9c5e71c4a8fd92c1e3 to your computer and use it in GitHub Desktop.
SnafuBot
import praw
import re
import json
def lambda_handler(event, context):
# TODO implement
reddit_handler()
return {
"statusCode": 200,
"body": json.dumps('Hello from Lambda!')
}
def reddit_handler():
reddit = praw.Reddit(client_id='OMITTED',
client_secret='OMITTED',
user_agent='python',
username='SnafuBot', password='OMITTED')
coaxedintoasnafu = reddit.subreddit('coaxedintoasnafu')
regex = r'(r\/[a-zA-Z0-9]+)'
post_template = "Links to subreddits in flair: " \
"\n\n \n %s ***** \n" \
"^^^^I ^^^^am ^^^^a ^^^^bot. ^^^^PM ^^^^me " \
"^^^^if ^^^^you ^^^^have ^^^^any ^^^^issues ^^^^or ^^^^suggestions."
for submission in coaxedintoasnafu.new(limit=40):
flair = submission.link_flair_text
subs = []
poststr = ""
if flair is not None:
matches = re.findall(regex, flair)
if matches is not None:
for match in matches:
subs.append(match)
if len(subs) > 0:
for sub in subs:
poststr += "\n%s\n\n \n" % sub
#print(poststr)
# Check if already commented
commented = False
for comment in submission.comments:
if comment.author == "SnafuBot":
commented = True
break
if not commented:
print("======================")
print(post_template % poststr)
print("======================")
submission.reply(post_template % poststr)
break
#exit(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment