Advertisement
Guest User

kemonomimi_bot.py

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