Advertisement
Guest User

Untitled

a guest
Nov 25th, 2017
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.20 KB | None | 0 0
  1. import praw
  2. import config
  3. import os
  4. import urllib
  5. import bs4 as bs
  6.  
  7.  
  8. # Logs in to reddit api. config.xxxx just grabs vars from cinfig.py file associated with this program
  9. def bot_login():
  10. r = praw.Reddit(username = config.username,
  11. password = config.password,
  12. client_id = config.client_id,
  13. client_secret = config.client_secret,
  14. user_agent = "SbfuTestBot v0.1")
  15. print("Logged in!")
  16.  
  17. return r
  18.  
  19. # Function webscrapes the wiki for dict(keyword, http link)
  20. def grab_wiki():
  21. #vars for BS4 soup
  22. sauce = urllib.urlopen('https://www.spacebuckets.com/wiki/Special:AllPages')
  23. soup = bs.BeautifulSoup(sauce, 'lxml')
  24. b = {}
  25. for link in soup.find_all('a'):
  26. aKey = link.text.lower()
  27. aLink = link.get('href')
  28. for char in ' ?.!/;:)(-':
  29. aKey = aKey.replace(char, '')
  30. b.update({aKey:aLink})
  31. print("WebScrape Complete!")
  32. return(b)
  33.  
  34. #Function to run the reddit bot
  35. def run_bot(r, comments_replied_to):
  36. 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
  37. print("Obtaining 25 comments")
  38. for comment in r.subreddit('sbfutest').comments(limit=25): #gets the most recent 25 comments from our selected subreddit
  39. comment_text = comment.body.lower() # makes the content of the comment lowercase for parsing
  40. 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
  41.  
  42. 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
  43. print ("Bot called by " + comment.id)
  44. print("Searching SpaceBuckets Wiki")
  45. wiki_scrape = grab_wiki() # calls function that stored webscraped contents of spacebucket wiki page https://www.spacebuckets.com/wiki/Special:AllPages
  46. helplist = str(wiki_scrape.keys()) # creates a lit of all the keys from our webscraped dictionary
  47. " ".join(helplist) # joins our list into one large string for iteration
  48. title_found = set(comment_text) # makes our reddit comment into a set
  49. 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
  50. for k in wiki_scrape.keys(): # for each keyword in our SB wiki scrape
  51. if k in matches: # if keyword is in our match
  52. match_envelope += "[" + k + "]" + "(" + "https://www.spacebuckets.com/" + wiki_scrape[k] + ")" + " "+ "\n" #reddit syntax response, catches all keywords and stores reddit response
  53.  
  54. if match_envelope: #if there is data in match_envelope, then we have a comment to respond to, and need basic response template
  55. comment.reply("Here are the requested articles! " + "\n" + match_envelope + "\n" + "To see list of bot keywords, call !SbFuBot in a comment")
  56.  
  57. if not match_envelope: #If match envelope is empty, then someone didnt know the correct keyword, or is requesting what keywords are available
  58. 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.")
  59.  
  60.  
  61. print("replied to comment " + comment.id) # Stores comments we have already replied to, so we dont reply multiple times.
  62. comments_replied_to.append(comment.id)
  63. with open ("comments_replied_to.txt", "a") as f:
  64. f.write(comment.id + " ")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement