def normal_usage():
    logger.info('Now watching for new logs to be posted to MF logserver.')
    logger.info('Games played before 12:00 AM today will not be handled.')

    rsys = rs.TrueSkillSystem(RATING_SYSTEM_NAME,
                              mu=25,
                              sigma=25,
                              beta=25,
                              tau=0.25,
                              draw_probability=0.05,
                              k=3)

    rhist = ru.get_rating_history_stub(rsys, RATING_SYSTEM_NAME)
    next_day = datetime.datetime.now()
    last_time = datetime.datetime.now()
    while True:
        # This ensures that we don't miss the very last logs posted before
        # the date ticks over at midnight.  We're guaranteed to search for
        # "yesterday's" logs at least once "today."
        #
        # NOTE we can still lose logs if the local system's time is ahead
        # of Goko's server time.  It might be better to have a 10 minute
        # window (12:00-12:10AM) in which we search for both yesterday's
        # and today's logs.
        #
        today = datetime.datetime.now()
        yesterday = today - timedelta(days=1)
        parsecount = 0
        last_time = datetime.datetime.now()
        try:
            download_new_logs(yesterday)
            parsecount += parse_new_logs(yesterday)
            download_new_logs(today)
            parsecount += parse_new_logs(today)
        except:
            # TODO: Do something about these errors
            logger.error(sys.exc_info()[1])
            logger.error(sys.exc_info()[2])
            pass

        logger.info('Rating new games')
        rate_new_games(rhist, allow_guests=False, min_turns=2, only_2p=True)
        logger.info('Finished.  Recording new ratings')
        ru.record_ratings(rhist, RATING_SYSTEM_NAME)
        logger.info('Finished.')

        sec_delta = (datetime.datetime.now() - last_time).seconds
        logger.info('Found %d new logs. Checking again in %d seconds.' %
                    (parsecount, 300 - sec_delta))
        if sec_delta < 300:
            time.sleep(300 - sec_delta)
def normal_usage():
    logger.info('Now watching for new logs to be posted to MF logserver.')
    logger.info('Games played before 12:00 AM today will not be handled.')

    rsys = rs.TrueSkillSystem(RATING_SYSTEM_NAME, mu=25, sigma=25,
                              beta=25, tau=0.25,
                              draw_probability=0.05, k=3) 

    rhist = ru.get_rating_history_stub(rsys, RATING_SYSTEM_NAME)
    next_day = datetime.datetime.now()
    last_time = datetime.datetime.now()
    while True:
        # This ensures that we don't miss the very last logs posted before
        # the date ticks over at midnight.  We're guaranteed to search for
        # "yesterday's" logs at least once "today."
        #
        # NOTE we can still lose logs if the local system's time is ahead
        # of Goko's server time.  It might be better to have a 10 minute
        # window (12:00-12:10AM) in which we search for both yesterday's
        # and today's logs.
        #
        next_day = datetime.datetime.now()
        parsecount = 0
        last_time = datetime.datetime.now()
        try:
            download_new_logs(next_day)
            parsecount = parse_new_logs(next_day)
        except:
            # TODO: Do something about these errors
            logger.error(sys.exc_info()[1])
            logger.error(sys.exc_info()[2])
            pass

        logger.info('Rating new games')
        rate_new_games(rhist, allow_guests=False, min_turns=2, only_2p=True)
        logger.info('Finished.  Recording new ratings')
        ru.record_ratings(rhist, RATING_SYSTEM_NAME)
        logger.info('Finished.')

        sec_delta = (datetime.datetime.now() - last_time).seconds
        logger.info('Found %d new logs. Checking again in %d seconds.'
                    % (parsecount, 30 - sec_delta))
        if sec_delta < 30:
            time.sleep(30 - sec_delta)
                            % (date.strftime('%Y-%m-%d')))
                download_new_logs(date)
                parse_new_logs(date)
                date += datetime.timedelta(days=1)
            normal_usage()
        else:
            logger.error('Check usage.')

    elif len(sys.argv) == 2 and sys.argv[1] == 'regen':
        while True:
            rsys = rs.TrueSkillSystem('iso_regen', mu=25, sigma=25,
                                      beta=25, tau=0.25,
                                      draw_probability=0.05, k=3) 
            rhist = ru.get_rating_history_stub(rsys, 'iso_regen')
            rate_new_games(rhist, allow_guests=False, min_turns=2, only_2p=True)
            ru.record_ratings(rhist, 'iso_regen')

    elif len(sys.argv) == 2:
        start_dir = sys.argv[1]
        logger.info('Parsing ALL previous downloaded logs after %s. '
                    + 'This may take a while.' + sys.argv[1])
        for log_dir in sorted(os.listdir(LOG_DIR)):
            if log_dir > start_dir:
                if re.match('\d{8}', log_dir):
                    log_day = datetime.datetime.strptime(log_dir, '%Y%m%d')
                    logger.info('Parsing logs from %s'
                                % log_day.strftime('%Y-%m-%d'))
                    parse_new_logs(log_day)

    else:
        normal_usage()
        next_day = datetime.datetime.now()
        while True:
            # This ensures that we don't miss the very last logs posted before
            # the date ticks over at midnight.  We're guaranteed to search for
            # "yesterday's" logs at least once "today."
            #
            # NOTE we can still lose logs if the local system's time is ahead
            # of Goko's server time.  It might be better to have a 10 minute
            # window (12:00-12:10AM) in which we search for both yesterday's
            # and today's logs.
            #
            next_day = datetime.datetime.now()
            parsecount = 0
            try:
                download_new_logs(next_day)
                parsecount = parse_new_logs(next_day)
            except:
                # TODO: Do something about these errors
                logger.error(sys.exc_info()[1])
                logger.error(sys.exc_info()[2])
                pass

            for sysname in rhists:
                print(sysname)
                rate_new_games(rhists[sysname])
                ru.record_ratings(rhists[sysname], sysname)

            logger.info('Found %d new logs. Checking again in 5 seconds.'
                        % parsecount)
            time.sleep(5)
    elif len(sys.argv) == 2 and sys.argv[1] == 'regen':
        while True:
            rsys = rs.TrueSkillSystem('iso_regen',
                                      mu=25,
                                      sigma=25,
                                      beta=25,
                                      tau=0.25,
                                      draw_probability=0.05,
                                      k=3)
            rhist = ru.get_rating_history_stub(rsys, 'iso_regen')
            rate_new_games(rhist,
                           allow_guests=False,
                           min_turns=2,
                           only_2p=True)
            ru.record_ratings(rhist, 'iso_regen')

    elif len(sys.argv) == 2:
        start_dir = sys.argv[1]
        logger.info('Parsing ALL previous downloaded logs after %s. ' +
                    'This may take a while.' + sys.argv[1])
        for log_dir in sorted(os.listdir(LOG_DIR)):
            if log_dir > start_dir:
                if re.match('\d{8}', log_dir):
                    log_day = datetime.datetime.strptime(log_dir, '%Y%m%d')
                    logger.info('Parsing logs from %s' %
                                log_day.strftime('%Y-%m-%d'))
                    parse_new_logs(log_day)

    else:
        normal_usage()
            logger.info("Downloading/parsing logs starting on %s.  Not rating." % sys.argv[1])
            date = datetime.datetime.strptime(sys.argv[1], "%Y%m%d")
            while date < datetime.datetime.now():
                logger.info("Downloading logs for %s" % (date.strftime("%Y-%m-%d")))
                download_new_logs(date)
                parse_new_logs(date)
                date += datetime.timedelta(days=1)
            normal_usage()
        else:
            logger.error("Check usage.")

    elif len(sys.argv) == 2 and sys.argv[1] == "regen":
        while True:
            rsys = rs.TrueSkillSystem("iso_regen", mu=25, sigma=25, beta=25, tau=0.25, draw_probability=0.05, k=3)
            rhist = ru.get_rating_history_stub(rsys, "iso_regen")
            rate_new_games(rhist, allow_guests=False, min_turns=2, only_2p=True)
            ru.record_ratings(rhist, "iso_regen")

    elif len(sys.argv) == 2:
        start_dir = sys.argv[1]
        logger.info("Parsing ALL previous downloaded logs after %s. " + "This may take a while." + sys.argv[1])
        for log_dir in sorted(os.listdir(LOG_DIR)):
            if log_dir > start_dir:
                if re.match("\d{8}", log_dir):
                    log_day = datetime.datetime.strptime(log_dir, "%Y%m%d")
                    logger.info("Parsing logs from %s" % log_day.strftime("%Y-%m-%d"))
                    parse_new_logs(log_day)

    else:
        normal_usage()
        while True:
            # This ensures that we don't miss the very last logs posted before
            # the date ticks over at midnight.  We're guaranteed to search for
            # "yesterday's" logs at least once "today."
            #
            # NOTE we can still lose logs if the local system's time is ahead
            # of Goko's server time.  It might be better to have a 10 minute
            # window (12:00-12:10AM) in which we search for both yesterday's
            # and today's logs.
            #
            next_day = datetime.datetime.now()
            parsecount = 0
            try:
                download_new_logs(next_day)
                parsecount = parse_new_logs(next_day)
            except:
                # TODO: Do something about these errors
                logger.error(sys.exc_info()[1])
                logger.error(sys.exc_info()[2])
                pass

            logger.info('Rating new games')
            rate_new_games(rhist, allow_guests=False, min_turns=2, only_2p=True)
            logger.info('Finished.  Recording new ratings')
            ru.record_ratings(rhist, RATING_SYSTEM_NAME)
            logger.info('Finished.')

            logger.info('Found %d new logs. Checking again in 5 seconds.'
                        % parsecount)
            time.sleep(2)