def main():
    util.bot_stdout_print("Script started.")

    # Parse config file
    parser = SafeConfigParser()
    with codecs.open("bot.ini", "r", encoding="utf-8") as f:
        parser.readfp(f)
    url = parser.get("General", "url")
    summary = parser.get("General", "summary")
    username = parser.get("General", "reddit_username")
    password = parser.get("General", "reddit_password")
    triggers = tuple(parser.get("General", "subreddit_triggers").split(","))
    subreddits = tuple(parser.get("General", "subreddits").split(","))
    user_agent = "%s %s" % (summary, url)

    # Setup reddit api object and login
    praw_reddit = praw.Reddit(user_agent=user_agent)
    praw_reddit.login(username, password)

    # Setup the subreddit watcher handler
    watcher_handler = SubredditWatcherHandler(praw_reddit, username, triggers,
                                              subreddits)

    # Setup the mentions watcher
    mentions_watcher = MentionsWatcher(praw_reddit, username)

    # Main loop
    while True:
        util.bot_stdout_print("Main loop started.")
        watcher_handler.process_subreddits()
        mentions_watcher.watch()
        util.bot_stdout_print("Sleeping for 3 seconds.")
        time.sleep(3)
Beispiel #2
0
 def watch(self):
     subreddit = self.praw_reddit.get_subreddit(self.name)
     util.bot_stdout_print("Getting comments for subreddit: %s" % (self.name))
     for submission in subreddit.get_comments():
         if submission.id not in self.already_done:
             try:
                 msg = submission.body
                 successes,failures = self.msg_parser.parse(msg)
                 self.already_done.append(submission.id)
                 reply = self.get_reply(submission, successes, failures)
                 if not reply is None:
                     util.bot_stdout_print("Reply to %s: %s\n" % (submission.id, reply))
                     util.handle_ratelimit(submission.reply, reply)
             except:
                 util.bot_stdout_print("Unknown exception: %s" % sys.exc_info()[0])
                 print traceback.format_exc()
                 self.already_done.append(submission.id)
     self.cleanup_already_done()
 def watch(self):
     now = int(time.time())
     if self.last_run is None or now - self.last_run > self.DELAY:
         util.bot_stdout_print("Getting mentions for user: %s" % (self.username))
         mentions = self.praw_reddit.get_mentions()
         self.last_run = int(time.time())
         try:
             for mention in mentions:
                 if mention.new == True and mention.id not in self.already_done:
                     msg = mention.body
                     successes,failures = self.msg_parser.parse(msg)
                     reply = self.get_reply(successes, failures)
                     self.already_done.append(mention.id)
                     mention.mark_as_read()
                     if not reply is None:
                         util.bot_stdout_print("Reply to %s: %s\n" % (mention.id, reply))
                         util.handle_ratelimit(mention.reply, reply)
         except:
             util.bot_stdout_print("Unknown exception: %s" % sys.exc_info()[0])
             print traceback.format_exc()
             self.already_done.append(mention.id)
         self.cleanup_already_done()