Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import praw
- import lasagnaconfig
- import time
- import os
- import random
- import traceback
- #runs login
- def bot_login():
- print('logging in')
- r = praw.Reddit(username=lasagnaconfig.username,
- password=lasagnaconfig.password,
- client_id=lasagnaconfig.client_id,
- client_secret=lasagnaconfig.client_secret,
- user_agent="Posts a garfield comic any time lasagna is mentioned in the top 25000 comments in r/all")
- print('logged in ' + lasagnaconfig.username)
- return r
- #searches for comments with 'lasagna' mentioned.
- def run_bot(r, comments_replied_to):
- print('obtaining 25000 comments')
- try:
- for comment in r.subreddit('all').comments(limit=25000):
- if "lasagna" in comment.body.lower() and comment.id not in comments_replied_to_lasagna and comment.author != r.user.me():
- year = random.randint(1988, 2016)
- month = random.randint(1, 12)
- day = random.randint(1, 28)
- print("String with lasagna found in comment " + comment.id)
- comment.reply(
- 'Did somebody say ' + '[lasagna?](http://www.gocomics.com/garfield/%s/%s/%s)' % (year, month, day,))
- print('Replied to comment ' + comment.id)
- comments_replied_to_lasagna.append(comment.id)
- with open("comments_replied_to_lasagna.txt", "a") as f:
- f.write(comment.id + "\n")
- except:
- traceback.print_exc()
- print('Resuming in 10')
- time.sleep(10)
- print("Sleeping for 1 min")
- time.sleep(60)
- def get_saved_comments():
- if not os.path.isfile("comments_replied_to.txt"):
- comments_replied_to = []
- else:
- with open("comments_replied_to.txt", "r") as f:
- comments_replied_to = f.read()
- comments_replied_to = comments_replied_to.split("\n")
- return comments_replied_to
- comments_replied_to_lasagna = get_saved_comments()
- r = bot_login()
- while True:
- run_bot(r, comments_replied_to_lasagna)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement