Пример #1
0
def random_reply():
    log.info('making random reply')
    # Choose a random submission from /r/all that is currently hot
    submission = random.choice(list(api.subreddit('all').hot()))
    submission.comments.replace_more(
        limit=0
    )  # Replace the "MoreReplies" with all of the submission replies

    sub_name = submission.subreddit.display_name
    brain = "{}/{}.db".format(DB_DIR, sub_name)
    if not glob.glob(brain):
        learn(sub_name)

    reply_brain = bot.Brain(brain)

    try:
        if prob(.35):  #There's a larger chance that we'll reply to a comment.
            log.info('replying to a comment')
            comment = random.choice(submission.comments.list())
            response = reply_brain.reply(comment.body)
            reply = comment.reply(response)
            log.info('Replied to comment: {}'.fomrat(comment))
            log.info('Replied with: {}'.format(reply))
        else:
            log.info('replying to a submission')
            # Pass the users comment to chatbrain asking for a reply
            response = reply_brain.reply(submission.title)
            submission.reply(response)
            log.info('Replied to Title: {}'.format(submission.title))
            log.info('Replied with: {}'.format(response))

    except Exception as e:
        log.error(e, exc_info=False)
Пример #2
0
def random_reply():
    log.info('making random reply')
    # Choose a random submission from /r/all that is currently hot
    submission = random.choice(list(api.subreddit('all').hot()))

    sub_name = submission.subreddit.display_name
    brain = "{}/{}.db".format(DB_DIR, sub_name)
    if not glob.glob(brain):
        learn(sub_name)

    reply_brain = bot.Brain(brain)
    # Replace the "MoreReplies" with all of the submission replies
    submission.comments.replace_more(limit=0)

    # Choose a random top level comment
    comment = random.choice(submission.comments.list())

    try:
        # Pass the users comment to chatbrain asking for a reply
        response = reply_brain.reply(comment.body)
    except Exception as e:
        log.error(e, exc_info=False)

    try:
        # Reply tp the same users comment with chatbrains reply
        reply = comment.reply(response)
    except Exception as e:
        log.error(e, exc_info=False)
Пример #3
0
def random_reply():
    log.info("making random reply")
    # Choose a random submission from /r/all that is currently hot
    if SUBREDDIT_LIST:
        subreddit = random.choice(SUBREDDIT_LIST)
        submission = random.choice(list(api.subreddit(subreddit).hot()))
    else:
        submission = random.choice(list(api.subreddit("all").hot()))

    submission.comments.replace_more(
        limit=0
    )  # Replace the "MoreReplies" with all of the submission replies

    sub_name = submission.subreddit.display_name
    brain = "{}/{}.db".format(DB_DIR, sub_name)
    log.info(brain)
    if not glob.glob(brain):
        learn(sub_name)

    reply_brain = bot.Brain(brain)

    try:
        #         if prob(.1): # small chance we advertise
        #           content = share()
        #           comment = random.choice(submission.comments.list())
        #           log.info('sharing - thanks for helping out!')
        #           sharing = '{} {}'.format(content['comment'], content['url'])
        #           reply = comment.reply(sharing)
        #           log.info("Replied to comment: {}".format(comment.body))
        #           log.info("Replied with: {}".format(reply))
        #           return
        if prob(.35):  # There's a larger chance that we'll reply to a comment.
            log.info("replying to a comment")
            comment = random.choice(submission.comments.list())
            response = reply_brain.reply(comment.body)

            # We might not be able to learn enough from the subreddit to reply
            # If we don't, then pull a reply from the general database.
            if "I don't know enough to answer you yet!" in response:
                log.info(
                    "I don't know enough from {}, using main brain db to reply"
                    .format(sub_name))
                brain = "{}/{}.db".format(DB_DIR, "brain")
                reply_brain = bot.Brain(brain)
                response = reply_brain.reply(comment.body)

            reply = comment.reply(response)
            log.info("Replied to comment: {}".format(comment.body))
            log.info("Replied with: {}".format(response))
        else:
            log.info("replying to a submission")
            # Pass the users comment to chatbrain asking for a reply
            response = reply_brain.reply(submission.title)

            # same as above. nobody will ever see this so it's fine.
            if "I don't know enough to answer you yet!" in response:
                log.info(
                    "I don't know enough from {}, using main brain db to reply"
                    .format(sub_name))
                brain = "{}/{}.db".format(DB_DIR, "brain")
                reply_brain = bot.Brain(brain)
                response = reply_brain.reply(submission.title)

            submission.reply(response)
            log.info("Replied to Title: {}".format(submission.title))
            log.info("Replied with: {}".format(response))
    except praw.exceptions.APIException as e:
        raise e
    except Exception as e:
        log.error(e, exc_info=False)