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)
            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)
        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:
        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.')

        rhists = {}
        rhists['goko'] = ru.get_rating_history_stub(rs.goko, 'goko')
        rhists['isotropish'] = ru.get_rating_history_stub(rs.isotropish,
                                                        'isotropish')
        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
                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' %
            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:
        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()
        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)