is-anybody-here-bot/main.py

32 lines
861 B
Python
Raw Normal View History

2017-08-31 10:12:53 -06:00
#!./env/bin/python
# main.py
import praw, pdb, re, os
# Setup reddit and subs
reddit = praw.Reddit('DEFAULT')
rall = reddit.subreddit('all')
sub = reddit.subreddit('IsAnybodyHere')
2017-08-31 12:53:54 -06:00
print("Started IsAnybodyHereBot...")
2017-08-31 10:12:53 -06:00
# Search latest comments in r/all
for comment in rall.stream.comments():
# Match with regex
2017-08-31 12:19:19 -06:00
match = re.search("(does|has|is)(n't)? (any one|anyone|someone|anybody) (here)? (.*?)\?", comment.body, re.IGNORECASE)
2017-08-31 10:12:53 -06:00
if match:
# Extract a description of the needed person
2017-08-31 10:31:52 -06:00
needed = match.group(3) + ' who ' + match.group(1) + ' ' + match.group(5)
print(match.group(),'\n',needed.title(),'\n')
2017-08-31 10:12:53 -06:00
# Post to our sub
2017-08-31 12:01:16 -06:00
try:
post = sub.submit(needed.title(), url='https://www.reddit.com'+comment.permalink(), resubmit=False)
except:
print("Failed to post to our sub"); raise
else:
print("Posted to sub:",post.shortlink)