Advertisement
Guest User

Untitled

a guest
Jul 24th, 2018
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.40 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=5):
  23.         if ("!Im sad" in comment.body and comment.id not in comments_replied_to 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 up!"
  27.                           "\n\n"
  28.                           "---"
  29.                           "\n\n"
  30.                           "I am a bot. For more info on me and how to use me, see r/KemonomimiCheerUpBot")
  31.  
  32.             print "replied to comment " + comment.id
  33.  
  34.             comments_replied_to.append(comment.id)
  35.  
  36.             with open("comments_replied_to.txt", "a") as f:
  37.                 f.write(comment.id + "\n")
  38.    
  39.     print "sleeping for 60 seconds"
  40.  
  41.     time.sleep(60)
  42.  
  43. # Load catgirls from a file (defaults to catgirls.txt)
  44. def get_catgirls(fname='catgirls.txt'):
  45.  
  46.     if not os.path.isfile(fname):
  47.         raise FileNotFoundError("Can't get catgirls from " + fname)
  48.  
  49.     catgirls = []
  50.  
  51.     with open(fname) as catgirls_file:
  52.         catgirls_contents = catgirls_file.read()
  53.  
  54.     catgirls = catgirls_contents.split("\n")
  55.     catgirls = filter(None, catgirls)
  56.  
  57.     catgirls = [catgirl.strip() for catgirl in catgirls]
  58.  
  59.     return catgirls
  60.  
  61. def get_saved_comments():
  62.  
  63.     if not os.path.isfile("comments_replied_to.txt"):
  64.         comments_replied_to = []
  65.     else:
  66.         with open("comments_replied_to.txt", "r") as f:
  67.             comments_replied_to = f.read()
  68.             comments_replied_to = comments_replied_to.split("\n")
  69.             comments_replied_to = filter(None, comments_replied_to)
  70.  
  71.     return comments_replied_to
  72.  
  73.  
  74. if __name__ == '__main__':
  75.  
  76.     r = bot_login()
  77.     catgirls = get_catgirls()
  78.     comments_replied_to = get_saved_comments()
  79.  
  80.     while True:
  81.         run_bot(r, comments_replied_to, catgirls)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement