Browse Source

Making the string searching more unified.

master
Michael Tan 6 years ago
parent
commit
29d6c04af5
  1. 59
      linkmakerbot.py

59
linkmakerbot.py

@ -5,6 +5,22 @@ import re
import os import os
import time import time
def check_for_string(searchString,comment):
temp = re.findall(searchString, comment, flags=re.IGNORECASE)
res = list(temp)
myreply=""
if len(res)!=0:
if len(res)>1:
myreply="Let me make those links for you:"
else:
myreply="Let me make that link for you:"
for linkNum in res:
temp = linkNum.split()
temp = temp[-1]
temp = int(temp)
myreply+="\n\nhttps://xkcd.com/%d" %temp
return myreply
# Create the Reddit instance # Create the Reddit instance
reddit = praw.Reddit('bot1') reddit = praw.Reddit('bot1')
@ -24,28 +40,17 @@ else:
posts_replied_to = list(filter(None, posts_replied_to)) posts_replied_to = list(filter(None, posts_replied_to))
# Get the top 5 values from our subreddit # Get the top 5 values from our subreddit
subreddit = reddit.subreddit('xkcd') subreddit = reddit.subreddit('pythonforengineers')
for submission in subreddit.hot(limit=1): for submission in subreddit.hot(limit=5):
#time.sleep(2) #time.sleep(2)
# If we haven't replied to this post before # If we haven't replied to this post before
if submission.id not in posts_replied_to: if submission.id not in posts_replied_to:
# Do a case insensitive search # Do a case insensitive search
temp = re.findall(r'xkcd \d+', submission.title, flags=re.IGNORECASE) myreply = check_for_string(r'xkcd \d+',submission.title)
res = list(temp) if myreply:
if len(res)!=0: print(myreply)
myreply=""
if len(res)>1:
myreply="Let me make those links for you:"
else:
myreply="Let me make that link for you:"
for linkNum in res:
temp = linkNum.split()
temp = temp[-1]
temp = int(temp)
myreply+="\n\nhttps://xkcd.com/%d" %temp
#print(myreply)
# Reply to the post # Reply to the post
submission.reply(myreply) #submission.reply(myreply)
tempMessage="Bot replying to: %s with ID: %s" %(submission.title,submission.id) tempMessage="Bot replying to: %s with ID: %s" %(submission.title,submission.id)
print(tempMessage) print(tempMessage)
#print("Waiting so we don't exceed rate limit...") #print("Waiting so we don't exceed rate limit...")
@ -55,22 +60,12 @@ for submission in subreddit.hot(limit=1):
for thisComment in submission.comments: for thisComment in submission.comments:
#time.sleep(2) #time.sleep(2)
if thisComment.id not in posts_replied_to: if thisComment.id not in posts_replied_to:
temp = re.findall(r'xkcd \d+', thisComment.body, flags=re.IGNORECASE)
res = list(temp) myreply = check_for_string(r'xkcd \d+',thisComment.body)
if len(res)!=0: if myreply:
myreply="" print(myreply)
if len(res)>1:
myreply="Let me make those links for you:"
else:
myreply="Let me make that link for you:"
for linkNum in res:
temp = linkNum.split()
temp = temp[-1]
temp = int(temp)
myreply+="\n\nhttps://xkcd.com/%d" %temp
#print(myreply)
# Reply to the comment # Reply to the comment
thisComment.reply(myreply) #thisComment.reply(myreply)
tempMessage="Bot replying to a comment in: %s with ID: %s" %(submission.title,thisComment.id) tempMessage="Bot replying to a comment in: %s with ID: %s" %(submission.title,thisComment.id)
print(tempMessage) print(tempMessage)
#print("Waiting so we don't exceed rate limit...") #print("Waiting so we don't exceed rate limit...")

Loading…
Cancel
Save