Exemple #1
0
def main_loop():
    # try to learn every hour
    if time.time() - main_loop.last_learned > 60*60:
        logger.info("Learning from today's top posts")
        grow_from_top(timeperiod="day", limit=50)
        main_loop.last_learned = time.time()
    # Start writing comments
    logger.debug("Finding comments to reply to")
    comments_to_insert = []
    for c in r.get_all_comments(limit=200):
        # Keep from processing the same thing twice
        if db.last_seen(c) is not None:
            continue
        # learn it
        comments_to_insert.append(c)
        # Don't reply to yourself
        if c.author.name == config.username:
            continue
        response = scorer.get_best_response(c)
        if response is None:
            continue
        logger.info("Responding to a user")
        try:
            c.reply(rewriter.prepare_for_post(response, c))
        except HTTPError as err:
            if err.response.status_code == 403:
                logger.warning("HTTP 403. This probably means you were banned "
                               "from a subreddit.")
            else:
                logger.exception("Error responding to user")
    db.insert_comments(*comments_to_insert, fast=True)
def main_loop():
    # try to learn every hour
    if time.time() - main_loop.last_learned > 60*60:
        print("Learning from today's top posts")
        grow_from_top(timeperiod="day", limit=50)
        main_loop.last_learned = time.time()
    # Start writing comments
    print("Fetching more comments")
    comments_to_insert = []
    for c in r.get_all_comments(limit=200):
        # Keep from processing the same thing twice
        if db.last_seen(c) is not None:
            continue
        # learn it
        comments_to_insert.append(c)
        # Don't reply to yourself
        if c.author.name == config.username:
            continue
        response = get_best_response(c)
        if response is None:
            continue
        print("Someone said:\n    %s\nSo I should say:\n    %s" %
              (c.body, response.body))
        try:
            c.reply(rewriter.prepare_for_post(response, c))
        except:
            traceback.print_exc()
    db.insert_comments(*comments_to_insert, fast=True)