daily pastebin goal
20%
SHARE
TWEET

Garfield Bot

a guest May 31st, 2017 69 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import praw
  2. import lasagnaconfig
  3. import time
  4. import os
  5. import random
  6. import traceback
  7.  
  8. #runs login
  9. def bot_login():
  10.     print('logging in')
  11.  
  12.     r = praw.Reddit(username=lasagnaconfig.username,
  13.                     password=lasagnaconfig.password,
  14.                     client_id=lasagnaconfig.client_id,
  15.                     client_secret=lasagnaconfig.client_secret,
  16.                     user_agent="Posts a garfield comic any time lasagna is mentioned in the top 25000 comments in r/all")
  17.     print('logged in ' + lasagnaconfig.username)
  18.     return r
  19.  
  20. #searches for comments with 'lasagna' mentioned.
  21. def run_bot(r, comments_replied_to):
  22.     print('obtaining 25000 comments')
  23.     try:
  24.         for comment in r.subreddit('all').comments(limit=25000):
  25.             if "lasagna" in comment.body.lower() and comment.id not in comments_replied_to_lasagna and comment.author != r.user.me():
  26.                 year = random.randint(1988, 2016)
  27.                 month = random.randint(1, 12)
  28.                 day = random.randint(1, 28)
  29.                 print("String with lasagna found in comment " + comment.id)
  30.                 comment.reply(
  31.                     'Did somebody say ' + '[lasagna?](http://www.gocomics.com/garfield/%s/%s/%s)' % (year, month, day,))
  32.                 print('Replied to comment ' + comment.id)
  33.  
  34.                 comments_replied_to_lasagna.append(comment.id)
  35.                 with open("comments_replied_to_lasagna.txt", "a") as f:
  36.                     f.write(comment.id + "\n")
  37.     except:
  38.         traceback.print_exc()
  39.         print('Resuming in 10')
  40.         time.sleep(10)
  41.  
  42.     print("Sleeping for 1 min")
  43.     time.sleep(60)
  44.  
  45.  
  46. def get_saved_comments():
  47.     if not os.path.isfile("comments_replied_to.txt"):
  48.         comments_replied_to = []
  49.     else:
  50.         with open("comments_replied_to.txt", "r") as f:
  51.             comments_replied_to = f.read()
  52.             comments_replied_to = comments_replied_to.split("\n")
  53.     return comments_replied_to
  54.  
  55.  
  56. comments_replied_to_lasagna = get_saved_comments()
  57. r = bot_login()
  58.  
  59. while True:
  60.     run_bot(r, comments_replied_to_lasagna)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top