Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import praw
- import config
- import os
- import urllib
- import bs4 as bs
- # Logs in to reddit api. config.xxxx just grabs vars from cinfig.py file associated with this program
- def bot_login():
- r = praw.Reddit(username = config.username,
- password = config.password,
- client_id = config.client_id,
- client_secret = config.client_secret,
- user_agent = "SbfuTestBot v0.1")
- print("Logged in!")
- return r
- # Function webscrapes the wiki for dict(keyword, http link)
- def grab_wiki():
- #vars for BS4 soup
- sauce = urllib.urlopen('https://www.spacebuckets.com/wiki/Special:AllPages')
- soup = bs.BeautifulSoup(sauce, 'lxml')
- b = {}
- for link in soup.find_all('a'):
- aKey = link.text.lower()
- aLink = link.get('href')
- for char in ' ?.!/;:)(-':
- aKey = aKey.replace(char, '')
- b.update({aKey:aLink})
- print("WebScrape Complete!")
- return(b)
- #Function to run the reddit bot
- def run_bot(r, comments_replied_to):
- match_envelope = ("") #The var that holds the matches from title_found and wiki_scrape, intitilaized as empty so we can reply to empty calls with a helpful response containing our keywords
- print("Obtaining 25 comments")
- for comment in r.subreddit('sbfutest').comments(limit=25): #gets the most recent 25 comments from our selected subreddit
- comment_text = comment.body.lower() # makes the content of the comment lowercase for parsing
- comment_text = comment_text.split() #splits our comment into an array of strings, seperated by spaces, so each word in comment is a string in array
- if "!SbFuBot" in comment.body and comment.id not in comments_replied_to and comment.author != r.user.me(): #If call word in comment body, and not a comment we have replied to, and not a comment made by the bot
- print ("Bot called by " + comment.id)
- print("Searching SpaceBuckets Wiki")
- wiki_scrape = grab_wiki() # calls function that stored webscraped contents of spacebucket wiki page https://www.spacebuckets.com/wiki/Special:AllPages
- helplist = str(wiki_scrape.keys()) # creates a lit of all the keys from our webscraped dictionary
- " ".join(helplist) # joins our list into one large string for iteration
- title_found = set(comment_text) # makes our reddit comment into a set
- matches = list(set(title_found) & set(wiki_scrape)) # makes matches intoaa list of items in both title_found from reddit comment, and wiki_scrape, from spacebucket wiki
- for k in wiki_scrape.keys(): # for each keyword in our SB wiki scrape
- if k in matches: # if keyword is in our match
- match_envelope += "[" + k + "]" + "(" + "https://www.spacebuckets.com/" + wiki_scrape[k] + ")" + " "+ "\n" #reddit syntax response, catches all keywords and stores reddit response
- if match_envelope: #if there is data in match_envelope, then we have a comment to respond to, and need basic response template
- comment.reply("Here are the requested articles! " + "\n" + match_envelope + "\n" + "To see list of bot keywords, call !SbFuBot in a comment")
- if not match_envelope: #If match envelope is empty, then someone didnt know the correct keyword, or is requesting what keywords are available
- comment.reply("To suggest a wiki article to someone on /r/SpaceBuckets just comment !SbFuBot (Case sensitive) followed by one or more of the following keywords: " + "\n" + " " + "\n" + " " + "**" + helplist + "**" + " " + "\n" + " " + "\n" + "Created by /u/SpaceBucketFu.")
- print("replied to comment " + comment.id) # Stores comments we have already replied to, so we dont reply multiple times.
- comments_replied_to.append(comment.id)
- with open ("comments_replied_to.txt", "a") as f:
- f.write(comment.id + " ")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement