Advertisement
Guest User

bot.py

a guest
Sep 3rd, 2017
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.35 KB | None | 0 0
  1. import praw
  2. import config
  3. import random
  4. import os
  5. import time
  6.  
  7. def bot_login():
  8.     print "Logging in..."
  9.     r = praw.Reddit(username = config.username,
  10.             password = config.password,
  11.             client_id = config.client_id,
  12.             client_secret = config.client_secret,
  13.             user_agent = "kemonomimi bot v0.1")
  14.     print "Logged in!"
  15.  
  16.     return r
  17.  
  18. def run_bot(r, comments_replied_to, catgirls):
  19.     print catgirls
  20.     print "Obtaining 25 comments..."
  21.  
  22.     for comment in r.subreddit('KemonomimiCheerUpBot').comments(limit=25):
  23.         if ("Im sad" in comment.body and comment.id not in comments_replied_to
  24.                 and comment.author != r.user.me()):
  25.  
  26.             comment.reply("[Here](" + random.choice(catgirls) + ") is a"
  27.                           "picture of a catgirl! Hopefully this will cheer you"
  28.                           "up!"
  29.                           "\n\n"
  30.                           "---"
  31.                           "\n\n"
  32.                           "I am a bot. For more info on me and how to use me,"
  33.                           "see r/KemonomimiCheerUpBot")
  34.  
  35.             comments_replied_to.append(comment.id)
  36.  
  37.             with open("comments_replied_to.txt", "a") as f:
  38.                 f.write(comment.id + "\n")
  39.  
  40.     time.sleep(10)
  41.  
  42. # Load catgirls from a file (defaults to catgirls.txt)
  43. def get_catgirls(fname='catgirls.txt'):
  44.  
  45.     if not os.path.isfile(fname):
  46.         raise FileNotFoundError("Can't get catgirls from " + fname)
  47.  
  48.     catgirls = []
  49.  
  50.     with open(fname) as catgirls_file:
  51.         catgirls_contents = catgirls_file.read()
  52.  
  53.     catgirls = catgirls_contents.split("\n")
  54.     catgirls = filter(None, catgirls)
  55.  
  56.     catgirls = [catgirl.strip() for catgirl in catgirls]
  57.  
  58.     return catgirls
  59.  
  60. def get_saved_comments():
  61.  
  62.     if not os.path.isfile("comments_replied_to.txt"):
  63.         comments_replied_to = []
  64.     else:
  65.         with open("comments_replied_to.txt", "r") as f:
  66.             comments_replied_to = f.read()
  67.             comments_replied_to = comments_replied_to.split("\n")
  68.             comments_replied_to = filter(None, comments_replied_to)
  69.  
  70.     return comments_replied_to
  71.  
  72.  
  73. if __name__ == '__main__':
  74.  
  75.     r = bot_login()
  76.     catgirls = get_catgirls()
  77.     comments_replied_to = get_saved_comments()
  78.  
  79.     while True:
  80.         run_bot(r, comments_replied_to, catgirls)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement