Advertisement
Guest User

Untitled

a guest
Oct 1st, 2018
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.46 KB | None | 0 0
  1. #!/usr/bin/python
  2. import praw
  3. import pdb
  4. import re
  5. import os
  6. import time
  7. dir_path = os.path.dirname(os.path.realpath(__file__))
  8.  
  9. # Create the Reddit instance
  10. def bot_login():
  11. print ("Logging in...")
  12. reddit = praw.Reddit(client_id=os.environ['CLIENTID'],
  13. client_secret=os.environ['CLIENTSECRET'],
  14. password=os.environ['PASSWORD'],
  15. user_agent='LokiBot v0.1',
  16. username='LokiBot')
  17. print ("Logged in.")
  18. return reddit
  19.  
  20. def bot_run(reddit):
  21. #reddit.login(REDDIT_USERNAME, REDDIT_PASS)
  22. # Have we run this code before? If not, create an empty list
  23. if not os.path.isfile(dir_path + "\posts_replied_to.txt"):
  24. posts_replied_to = []
  25. # If we have run the code before, load the list of posts we have replied to
  26. else:
  27. # Read the file into a list and remove any empty values
  28. with open(dir_path + "\posts_replied_to.txt", "r") as f:
  29. posts_replied_to = f.read()
  30. posts_replied_to = posts_replied_to.split("\n")
  31. posts_replied_to = list(filter(None, posts_replied_to))
  32. # Get the top 25 values from our subreddit
  33. print ("Getting 25 submissions")
  34. subreddit = reddit.subreddit('smite')
  35. for submission in subreddit.new(limit=25):
  36. print(submission.title)
  37. # If we haven't replied to this post before
  38. print ("Checking if we have replied to " + submission.title)
  39. if submission.id not in posts_replied_to:
  40. search = submission.title.lower() + submission.selftext.lower()
  41. if ('loki' in search and 'rework' in search) or ('loki' in search and 'broken' in search) or ('loki' in search and 'overpowered' in search) or ('loki' in search and 'unfun' in search):
  42. reply = open("reply.txt", "r")
  43. submission.reply(reply.read())
  44. print("Bot replying to : ", submission.title.lower())
  45. # Store the current id into our list
  46. print ("Storing " + submission.id + " in posts_replied_to.txt")
  47. posts_replied_to.append(submission.id)
  48. print (posts_replied_to)
  49. print ("Writing list back to file")
  50. with open(dir_path + "\posts_replied_to.txt", "w") as f:
  51. for post_id in posts_replied_to:
  52. f.write(post_id + "\n")
  53. print ("Sleeping for 10 seconds...")
  54. time.sleep(10)
  55.  
  56. reddit = bot_login()
  57. while True:
  58. bot_run(reddit)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement