Ejemplo n.º 1
0
def log_db_status(competition_ids=settings.COMPS):
    status = {}
    with session_scope() as session:
        competitions = queries.get_competitions_by_id(session, competition_ids)
        for competition in competitions:
            n_fixtures_in_db = queries.count_fixtures_for_competition(
                session, competition.api_id)
            n_fixtures_in_db_after_today = len(
                queries.get_fixtures_by_date_and_comp(
                    session, utils.time.today()+dt.timedelta(days=1), competition.api_id,
                    utils.time.today()+dt.timedelta(days=1000)))
            n_fixtures_in_db_before_today = len(
                queries.get_fixtures_by_date_and_comp(
                    session, utils.time.today()-dt.timedelta(days=1000), competition.api_id,
                    utils.time.today()))
            n_fixtures_in_season = competition.games_in_season
            status['n_db_fixtures'] = n_fixtures_in_db
            status['n_db_fixtures_after_today'] = n_fixtures_in_db_after_today
            status['n_db_fixtures_before_today'] = n_fixtures_in_db_before_today
            status['comp_name'] = competition.name
            status['n_season_fixtures'] = n_fixtures_in_season
            status['underline'] = '-' * len(competition.name)
            if n_fixtures_in_season:
                status['completeness'] = n_fixtures_in_db / n_fixtures_in_season * 100
            else:
                status['completeness'] = 0
            print(log_template.format(**status))
Ejemplo n.º 2
0
def past_results(comp_id, month_index=TODAY.month):
    comp_ids = settings.COMPS if comp_id == 'all' else [comp_id]

    start_day = dt.date(year=THIS_YEAR, month=int(month_index), day=1)
    end_day = (
        dt.date(year=THIS_YEAR, month=int(month_index) % 12 + 1, day=1) -
        dt.timedelta(days=1))

    today = utils.time.today()
    with db.session_scope() as session:
        fixtures_today = db_to_web.get_comp_grouped_fixtures(
            session, today, COMPS_FOR_PAGE)
        comps_with_games = [f['name'] for f in fixtures_today if f['fixtures']]
        all_comps = db_to_web.get_competitions_by_id(session, COMPS_FOR_PAGE)
        fixtures = db_to_web.get_date_grouped_fixtures(session, start_day,
                                                       comp_ids, end_day)
        if comp_id == 'all':
            selected_comp = 'All Competitions'
        else:
            selected_comp = db_to_web.get_competition_by_id(
                session, int(comp_id)).name

        past_games = games_template('fixtures_results.html', 'results',
                                    all_comps, fixtures, utils.time.today(),
                                    selected_comp + ' - Results / Fixtures',
                                    comp_id, comps_with_games)
    return past_games
Ejemplo n.º 3
0
 def run(self):
     active_fixtures = False
     with db.session_scope() as session:
         fixtures_today = self.refresh_and_get_todays_fixtures(session)
         while not active_fixtures:
             active_fixtures = [f for f in fixtures_today if f.is_active()]
             fixtures_soon = [
                 f for f in fixtures_today
                 if f.kicks_off_within(settings.PRE_GAME_PREP_PERIOD)
             ]
             needs_lineups = [
                 f for f in fixtures_soon if not f.has_lineups()
             ]
             if needs_lineups:
                 self.update_fixtures_lineups(session, needs_lineups)
                 logger.info('Prep state pausing for %d seconds',
                             settings.PREP_STATE_PAUSE)
                 time.sleep(settings.PREP_STATE_PAUSE)
             else:
                 time_to_next_game = self.time_to_next_kickoff(
                     fixtures_soon)
                 log_time_util_next_fixture(time_to_next_game,
                                            time_to_next_game)
                 time.sleep(time_to_next_game)
                 return _ActiveState
     return _ActiveState
Ejemplo n.º 4
0
def match_details(fixture_id):
    today = utils.time.today()
    with db.session_scope() as session:
        all_comps = db_to_web.get_competitions_by_id(session, COMPS_FOR_PAGE)
        fixture = db_to_web.get_fixture_by_id(session, fixture_id)
        grouped_fixtures = [
            {
                'name': fixture.competition.name,
                'fixtures': (fixture, )
            },
        ]
        comps_with_games = [
            f['name'] for f in grouped_fixtures if f['fixtures']
        ]
        web_date = utils.time.custom_strftime(settings.WEB_DATEFORMAT_SHORT,
                                              today)
        fixture.lineups = db_to_web.determine_substitutions(
            fixture.lineups, fixture.events)
        todays_games_with_details = games_template(
            'details.html',
            'details',
            all_comps,
            grouped_fixtures,
            utils.time.today(),
            'Live Scores - ' + web_date,
            competitions_with_games_today=comps_with_games,
        )
    return todays_games_with_details
Ejemplo n.º 5
0
def save_teams():
    api = FootballData()
    api_teams = api.get_competition_teams()
    with db.session_scope() as session:
        for team in api_teams:
            db_team = Team(**team)
            session.add(db_team)
            print('{} saved in db'.format(team['team_name']))
Ejemplo n.º 6
0
def fixture_updates():
    today = utils.time.today()
    with db.session_scope() as session:
        fixtures = db_to_web.get_fixtures_by_dates_and_comps(
            session, today, COMPS_FOR_PAGE)
        id_keyed_fixtures = {
            fixture.api_fixture_id: fixture.to_python()
            for fixture in fixtures
        }
    return jsonify(id_keyed_fixtures)
Ejemplo n.º 7
0
def update_db_competitions(updated_comps):
    with session_scope() as session:
        db_comps = queries.get_competitions(session)
        for dbc in db_comps:
            try:
                api_comp = next(api_comps_id_match(updated_comps, dbc.api_id))
                print('Vars updated on {}'.format(dbc))
                for api_key, db_column in API_DB_KEY_COL_MAP.items():
                    setattr(dbc, db_column, api_comp.get(api_key))
                    print('\t{} on {} set to {}'.format(
                        db_column, dbc, api_comp.get(api_key)))
            except StopIteration:
                pass
Ejemplo n.º 8
0
def save_competitions():
    fapi_comps = FootballAPI().get_competitions()
    fdata_comps = FootballData().get_competitions()
    competitions = response_merges.merge_two_lists(
        fapi_comps,
        fdata_comps,
        id_map=competition_map['football-api_to_football-data'],
        consistent_keys=True)
    with db.session_scope() as session:
        for comp in competitions:
            if not row_exists(session, Competition, Competition.api_id,
                              comp['api_id']):
                db_comp = Competition(**comp)
                session.add(db_comp)
Ejemplo n.º 9
0
 def run(self):
     with db.session_scope() as session:
         fixtures_today = self.refresh_and_get_todays_fixtures(session)
         fixtures_active = [f for f in fixtures_today if f.is_active()]
         fixtures_soon = [
             f for f in fixtures_today
             if f.kicks_off_within(settings.PRE_GAME_PREP_PERIOD)
         ]
         logger.info(
             '%d fixtures today, %d active, %d in prep state (KO within %.0f minutes)',
             len(fixtures_today), len(fixtures_active), len(fixtures_soon),
             settings.PRE_GAME_PREP_PERIOD / 60)
     if fixtures_active or fixtures_soon:
         return _ActiveState
     else:
         return _MaintenanceState
Ejemplo n.º 10
0
def todays_fixtures():
    today = utils.time.today()
    with db.session_scope() as session:
        all_comps = db_to_web.get_competitions_by_id(session, COMPS_FOR_PAGE)
        fixtures = db_to_web.get_comp_grouped_fixtures(session, today,
                                                       COMPS_FOR_PAGE)
        comps_with_games = [f['name'] for f in fixtures if f['fixtures']]
        web_date = utils.time.custom_strftime(settings.WEB_DATEFORMAT_SHORT,
                                              today)
        todays_games = games_template(
            'scores.html',
            'scores',
            all_comps,
            fixtures,
            utils.time.today(),
            'Live Scores - ' + web_date,
            competitions_with_games_today=comps_with_games,
        )
    return todays_games
Ejemplo n.º 11
0
def update_all_fixtures(competition_ids=settings.COMPS):
    """ Request fixtures for current date plus/minus 6 months.

    Fixtures to be requested by competition and for a two month date
    range."""

    with session_scope() as session:
        api = FootballAPI()

        start_date = dt.datetime.today() - dt.timedelta(days=200)
        end_date = dt.datetime.today() + dt.timedelta(days=200)
        start_dates = [start_date + dt.timedelta(days=60 * i) for i in range(6)]
        end_dates = [date + dt.timedelta(days=60) for date in start_dates]
        date_chunks = zip(start_dates, end_dates)

        for start_date, end_date in date_chunks:
            fixtures = api.get_fixtures_for_date(
                start_date, competition_ids, end_date,
                split_requests=True)
            api_to_db.save_fixtures(session, *fixtures)
Ejemplo n.º 12
0
 def run(self):
     active_fixtures, needs_lineups = True, True
     while active_fixtures or needs_lineups:
         with db.session_scope() as session:
             fixtures = self.refresh_and_get_todays_fixtures(session)
             active_fixtures = [f for f in fixtures if f.is_active()]
             fixtures_soon = [
                 f for f in fixtures
                 if f.kicks_off_within(settings.PRE_GAME_PREP_PERIOD)
             ]
             needs_lineups = [
                 f for f in fixtures_soon + active_fixtures
                 if not f.has_lineups()
             ]
             if needs_lineups:
                 self.update_fixtures_lineups(session, needs_lineups)
         logger.info('Active state pausing for %d seconds',
                     settings.ACTIVE_STATE_PAUSE)
         time.sleep(settings.ACTIVE_STATE_PAUSE)
     return _IdleState
Ejemplo n.º 13
0
 def run(self):
     with db.session_scope() as session:
         fixtures_today = db.queries.get_fixtures_by_date(
             session, utils.time.today())
         future_fixtures = [f for f in fixtures_today if f.status != 'FT']
         if not future_fixtures:
             logger.info(
                 'No games today, sleeping for %d seconds (time is %s)',
                 settings.NO_GAMES_SLEEP, utils.time.now())
             time.sleep(settings.NO_GAMES_SLEEP)
             logger.info('Awoken')
             return _IdleState
         else:
             log_list(fixtures_today, logger, intro='Todays fixtures:')
             time_to_next_game = self.time_to_next_kickoff(future_fixtures)
             if time_to_next_game < settings.PRE_GAME_ACTIVE_PERIOD:
                 return _ActiveState
             elif time_to_next_game < settings.PRE_GAME_PREP_PERIOD:
                 return _PreparationState
             else:
                 sleeptime = time_to_next_game - settings.PRE_GAME_PREP_PERIOD
                 log_time_util_next_fixture(time_to_next_game, sleeptime)
                 time.sleep(sleeptime)
                 return _PreparationState