Ejemplo n.º 1
0
def test1():
    """Franchised, Series, 1 Season, 1 Ep"""
    p = Producer(name='BBC Productions')
    p.save()

    d = Distributor(name='PBS Distribution')
    d.save()

    ep1 = SeriesEpisode(name="Wolf Hall Ep 1")
    ep1.save()

    se = Season(name="1", ordinal="1")
    se.save()

    s = Series(name='Wolf Hall')
    s.orgs = [p, d]
    s.save()

    sa = SeasonAssociation(season=se)
    sa.series = s
    sa.episode = ep1
    sa.season = se
    sa.save()

    f = Franchise(name='Masterpiece')
    f.serieses.append(s)
    f.save()
Ejemplo n.º 2
0
def test4():
    """Franchised series with 1 oto"""
    p = Producer(name='Ken Burns')
    p.save()

    d = Distributor(name='PBS Distribution')
    d.save()

    ep1 = SeriesEpisode(name="The Scripture of Nature (1851-1890)")
    ep1.save()

    se = Season(name='Season 2009', ordinal=2009)
    se.save()

    s = Series(name='The National Parks')
    s.orgs = [p, d]
    s.save()

    sa = SeasonAssociation(season=se)
    sa.series = s
    sa.episode = ep1
    sa.season = se
    sa.save()

    f = Franchise(name='Ken Burns')
    f.serieses = [s]
    f.save()
Ejemplo n.º 3
0
def test3():
    """Franchised, Series, Seasoned, with 1 OTO"""
    p = Producer(name='BBC Productions')
    p.save()

    d = Distributor(name='PBS Distribution')
    d.save()

    ep1 = SeriesEpisode(name="Scandal in Belgravia")
    ep1.save()

    se = Season(name="Season 2", ordinal="2")
    se.save()

    s = Series(name='Sherlock')
    s.orgs = [p, d]
    s.save()

    sa = SeasonAssociation(season=se)
    sa.series = s
    sa.episode = ep1
    sa.season = se
    sa.save()

    # get the previous
    f = session.query(Franchise).filter(Franchise.name=="Masterpiece").all()[0]
    f.serieses.append(s)
    f.save()
Ejemplo n.º 4
0
def _json_to_season(data):

	league = load_league(data[SEASON_LEAGUENAME_LABEL])

	schedule = data[SEASON_SCHEDULE_LABEL]
	for week in range(len(schedule)):
		for game in range(len(schedule[week])):
			schedule[week][game][2] = tuple(schedule[week][game][2])
			schedule[week][game] = tuple(schedule[week][game])

	week = data[SEASON_WEEK_LABEL]
	season = Season(league, schedule, week)

	season.standings = data[SEASON_STANDINGS_LABEL]

	results = {}
	for key,jsonresult in data[SEASON_RESULTS_LABEL].items():
		results[_to_tuple(key)] = _json_to_game_result(jsonresult)
	season.results = results

	postseason = None
	if(data[SEASON_POSTSEASON_LABEL] != None):
		postseason = _json_to_postseason(data[SEASON_POSTSEASON_LABEL])
	season.postseason = postseason

	return season
Ejemplo n.º 5
0
def test1112():
    """Series with 1 ep (in a season) and 1 OTO (seasonless)"""
    p = Producer(name='Universal')
    p.save()

    d = Distributor(name='PBS Distribution')
    d.save()

    se = Season(name='3', ordinal=3)
    se.save()

    e1 = SeriesEpisode(name="Go West Young Monkey")
    e1.seasons = [se,]
    e1.save()

    e2 = OneTimeOnlyEpisode(name="Curious George XMAS")
    e2.save()

    s = Series(name="Curious George")
    s.onetimeonlys = [e2,]
    s.save()

    sa = SeasonAssociation(season=se)
    sa.series = s
    sa.episode = e1
    sa.save()
Ejemplo n.º 6
0
def test6():
    """Series with OTO"""
    p = Producer(name='WGBH Productions')
    p.save()

    d = Distributor(name='PBS Distribution')
    d.save()

    se = Season(name='38', ordinal=38)
    se.save()

    e1 = OneTimeOnlyEpisode(name="Surviving Ebola")
    e1.seasons = [se,]
    e1.save()

    # get the previous
    s = session.query(Series).filter(Series.name=="NOVA").all()[0]
    s.seasons = [se,]
    s.episodes = [e1,]
    s.save()

    sa = SeasonAssociation(season=se)
    sa.series = s
    sa.episode = e1
    sa.save()
Ejemplo n.º 7
0
def test_tag():
    p = Producer(name='BBC Productions')
    p.save()

    d = Distributor(name='PBS Distribution')
    d.save()

    ep1 = SeriesEpisode(name="Wolf Hall Ep 1")
    ep1.save()

    se = Season(name="1", ordinal="1")
    se.save()

    s = Series(name='Wolf Hall')
    s.orgs = [p, d]
    s.save()

    s2 = Series(name='Wolf Hall Subseries')
    s2.orgs = [p, d]
    s2.save()

    #sept = SeriesEpisodeTag(series=s, episode=s2, tag='subseries')
    #sept.save()

    sept = SeriesRelationTag(parent=s, child=s2, tag='subseries')
    sept.save()

    sa = SeasonAssociation(season=se)
    sa.series = s
    sa.episode = ep1
    sa.season = se
    sa.save()
Ejemplo n.º 8
0
    def test_get_season(self):

        # create two seasons
        classification = Classification(label='test mens')
        classification.save()
        competition = Competition(
            name='div 1',
            mode='l',
            classification=classification
        )
        competition.save()
        season_1 = Season(label='s1',
            start_date=datetime.date.today(),
            end_date=datetime.date.today() + datetime.timedelta(365),
            competition=competition,
            published=True
        )
        season_2 = Season(label='s2',
            start_date=datetime.date.today() + datetime.timedelta(365),
            end_date=datetime.date.today() + datetime.timedelta(730),
            competition=competition
        )
        season_1.save()
        season_2.save()

        self.assertIn(season_1,
            Season.get_current_season_by_slugs('test-mens', 'div-1'))
        self.assertNotIn(season_2,
            Season.get_current_season_by_slugs('test-mens', 'div-1'))
Ejemplo n.º 9
0
    def create_test_data(self):
        user = User.get_or_create(123)
        user.admin = True
        User.get_or_create(12345)
        db.session.commit()

        team1 = Team.create(user, 'EnvyUs', 'EnvyUs', 'fr', 'nv',
                            ['76561198053858673'])
        team2 = Team.create(user, 'Fnatic', 'Fnatic', 'se', 'fntc',
                            ['76561198053858673'])
        server = GameServer.create(user, 'myserver1', '127.0.0.1', '27015',
                                   'password', False)
        server.in_use = True

        GameServer.create(user, 'myserver2', '127.0.0.1', '27016', 'password',
                          True)
        db.session.commit()
        season = Season.create(
            user, 'Season One Test', datetime.datetime.utcnow(),
            datetime.datetime.utcnow() + datetime.timedelta(days=1))
        db.session.commit()

        Match.create(user, team1.id, team2.id, '', '', 1, False,
                     'Map {MAPNUMBER}', ['de_dust2', 'de_cache', 'de_mirage'],
                     season.id, 'always_knife', 'CT', server.id)
        db.session.commit()

        vetoBan = Veto.create(1, 'EnvyUs', 'de_dust2', 'ban')
        vetoPick = Veto.create(1, 'EnvyUs', 'de_overpass', 'pick')
        db.session.commit()
Ejemplo n.º 10
0
def season_create():
    if not g.user:
        return redirect('/login')

    form = SeasonForm(request.form)

    if request.method == 'POST':
        num_seasons = g.user.seasons.count()
        max_seasons = config_setting('USER_MAX_SEASONS')
        if max_seasons >= 0 and num_seasons >= max_seasons and not (util.is_admin(g.user) or util.is_super_admin(g.user)):
            flash('You already have the maximum number of seasons ({}) created'.format(
                num_seasons))

        elif form.validate():
            season = Season.create(
                g.user, form.data['season_title'],
                form.data['start_date'], form.data['end_date'])

            db.session.commit()
            app.logger.info('User {} created season {}'
                            .format(g.user.id, season.id))

            return redirect('/myseasons')

        else:
            get5.flash_errors(form)

    return render_template(
        'season_create.html', form=form, user=g.user)
Ejemplo n.º 11
0
def create_season(classification, competition, season_label, start, end):

    competition = Competition(
        name=competition,
        mode='l',
        classification=classification
    )
    competition.save()
    season = Season(label=season_label,
        start_date=start,
        end_date=end,
        competition=competition
    )
    season.save()

    return season
Ejemplo n.º 12
0
Archivo: tests.py Proyecto: myang97/idb
 def test_get_seasons2(self):
     with app.app_context():
         result = Season.lookup('28')
         self.assertEqual(result['nfc_champion'], "CAR")
         self.assertEqual(result['year'], 2015)
         self.assertEqual(result['afc_champion'], 'DEN')
         self.assertEqual(result['start_date'], '2015-09-10')
         self.assertEqual(result['end_date'], '2016-01-03')
Ejemplo n.º 13
0
def create_season_and_stat_objects():
    for player in data:
        player_search = Player.query.filter_by(name=player['Player']).first()
        team_search = Team.query.filter_by(abbreviation=player['Team']).first()
        stat_line = Statistic(end_yr_team=player['Team'], games_played=int(player['GP']), time_on_ice=round(float(player['TOI']),2), goals=int(player['G']), assists=int(player['A']), points=int(player['P']), primary_points=int(player['P1']), pts_per_60=round(float(player['P/60']),2), p_pts_per_60=round(float(player['P1/60']),2), cf=int(player['CF']), ca=int(player['CA']), corsi_plus_minus=int(player['C+/-']), cf_percentage=round(float(player['CF%']),2), rel_cf=round(float(player['Rel CF%']),2), gf=int(player['GF']), ga=int(player['GA']), plus_minus=int(player['G+/-']), pdo=round(float(player['PDO']),2), zsr=round(float(player['ZSR']),2), weighted_cf=create_weighted_cf(player), weighted_ca=create_weighted_ca(player), weighted_corsi_percentage=create_weighted_corsi_percent(player))
        season = Season(year=player['Season'], player=player_search, team=team_search, statistic=stat_line)
        db.session.add(season)
    db.session.commit()
Ejemplo n.º 14
0
Archivo: tests.py Proyecto: myang97/idb
 def test_get_seasons1(self):
     with app.app_context():
         result = Season.lookup('29')
         self.assertEqual(result['nfc_champion'], "ATL")
         self.assertEqual(result['year'], 2016)
         self.assertEqual(result['afc_champion'], 'NE')
         self.assertEqual(result['start_date'], '2016-09-08')
         self.assertEqual(result['end_date'], '2017-01-01')
Ejemplo n.º 15
0
def _initialize():
    try:
        system("clear")
        season = datatool.load_season(sys.argv[1])
        return SeasonShell(season)
    except (IOError):
        league = datatool.load_league(sys.argv[1])
        season_schedule = schedule.make_schedule(
            [team.get_team_name() for team in league.get_all_teams()])
        season = Season(league, season_schedule, 0)
        return SeasonShell(season)
Ejemplo n.º 16
0
def test7():
    """Series with Subseries and 1 Ep"""
    p = Producer(name='WGBH Productions')
    p.save()

    d = Distributor(name='PBS Distribution')
    d.save()

    se = Season(name='17', ordinal=17)
    se.save()

    e1 = SeriesEpisode(name="Antiques Roadshow Seattle, Ep1")
    e1.seasons = [se,]
    e1.save()

    sss = Season(name='1', ordinal=1)
    sss.save()

    ss1 = SubSeries(name="Antiques Roadshow in Seattle")
    ss1.save()

    s = Series(name="Antiques Roadshow")
    s.subseries = [ss1]
    s.seasons = [se,]
    s.save()

    sa = SeasonAssociation(season=se)
    sa.series = s
    sa.episode = e1
    sa.save()

    sa2 = SeasonAssociation(season=sss)
    sa2.series = ss1
    sa2.episode = e1
    sa2.save()
Ejemplo n.º 17
0
def test8():
    """Series with EP"""

    p = Producer(name='WETA')
    p.save()

    d = Distributor(name='PBS Distribution')
    d.save()

    ep1 = SeriesEpisode(name="Monday, July 15th, 2014")
    ep1.save()

    s = Series(name='PBS NewsHour')
    s.orgs = [p, d]
    s.save()

    se = Season(name='29', ordinal=29)
    se.save()

    sa2 = SeasonAssociation(season=se)
    sa2.series = s
    sa2.episode = ep1
    sa2.save()
Ejemplo n.º 18
0
def convert_season(data: SeasonData) -> Season:
    contestants = dict()
    teams = []

    next_contestant_number = 0
    for team_data in data.teams:
        team = []
        for contestant in sorted(team_data):
            contestant_number = contestants.setdefault(contestant,
                                                       next_contestant_number)
            if contestant_number == next_contestant_number:
                next_contestant_number += 1

            team.append(contestant_number)
        teams.append(team)

    weeks = []
    week_number = 0
    for week_data in data.weeks:
        booths = []
        for booth_data in week_data.booths:
            first = contestants[booth_data.pairing[0]]
            second = contestants[booth_data.pairing[1]]
            booth = TruthBooth(pairing=(first, second),
                               outcome=booth_data.outcome)
            booths.append(booth)

        pairings = set()
        # each contestant may only be in one pairing
        contestants_copy = dict(contestants)
        for pairing_data in week_data.pairings:
            first = contestants_copy.pop(pairing_data[0])
            second = contestants_copy.pop(pairing_data[1])
            pairings.add((first, second))

        week = Week(id=week_number,
                    pairings=pairings,
                    beams=week_data.beams,
                    booths=booths)

        weeks.append(week)
        week_number += 1

    sorted_contestants = sorted(contestants.items(), key=lambda x: x[1])
    season = Season(contestants=[k for k, v in sorted_contestants],
                    teams=teams,
                    weeks=weeks)

    return season
Ejemplo n.º 19
0
def connect_match_data_season_1617():
    df = pd.read_csv("./data/season_files/season1617.csv").drop(["Unnamed: 0"],
                                                                axis=1)
    n_rels = 0
    n_nodes = 0
    for _, row in df.iterrows():
        # NODES
        # Hometeam
        ht = Team.nodes.get(name=row["Home/Neutral"])
        # Visitor
        vt = Team.nodes.get(name=row["Visitor/Neutral"])
        # HomeScore
        hs = Score(score=row["PTS.1"]).save()
        # VisitorScore
        vs = Score(score=row["PTS"]).save()
        # Arena
        a = ht.arena.get()
        # Season
        s = Season.get_or_create({"name": "2016/2017"})[0]
        # Date
        d = Date.get_or_create({
            "datetime":
            datetime.strptime(row["Date"] + " " + row["Start (ET)"],
                              "%Y-%m-%d %I:%M %p")
        })[0]
        # Game
        g = Game(game_name=row["game_name"],
                 game_type=row["game_type"],
                 ot=row["OT"]).save()
        n_nodes += 1

        # RELATIONSHIPS
        # Team -> Score
        ht.scored.connect(hs)
        vt.scored.connect(vs)
        # Score -> Game
        hs.in_game.connect(g)
        vs.in_game.connect(g)
        # Game -> Season
        g.season.connect(s)
        # Game -> Date
        g.date.connect(d)
        # Game -> Arena
        g.arena.connect(a)
        n_rels += 7
    print("created (approx:) {} nodes and {} relationships".format(
        n_nodes, n_rels))
Ejemplo n.º 20
0
def test9():
    """WEIRD ONE: Series and Subseries of different seasons sharing the same Ep"""
    p = Producer(name='WETA Productions')
    p.save()

    d = Distributor(name='PBS Distribution')
    d.save()

    se = Season(name='41', ordinal=41)
    se.save()

    se2 = Season(name='2012', ordinal=2012)
    se2.save()

    e1 = SeriesEpisode(name="Democratic National Convention September 5, 2015 Part 1")
    e1.save()

    ss1 = SubSeries(name="Elections")
    #ss1.episodes = [e1,]
    ss1.seasons = [se2]
    ss1.save()

    s = session.query(Series).filter(Series.name=="PBS NewsHour").all()[0]
    #s = Series(name="PBS NewsHour")
    s.subseries = [ss1]
    s.seasons = [se,]
    s.save()

    sa = SeasonAssociation(season=se)
    sa.series = s
    sa.episode = e1
    sa.save()

    sa2 = SeasonAssociation(season=se2)
    sa2.series = ss1
    sa2.episode = e1
    sa2.save()
Ejemplo n.º 21
0
def test5():
    """Series with Ep in one season AND Subseries with Ep in a different season"""
    p = Producer(name='WGBH Productions')
    p.save()

    d = Distributor(name='PBS Distribution')
    d.save()

    se = Season(name='38', ordinal=38)
    se.save()

    se1 = Season(name='1', ordinal=1)
    se1.save()

    e1 = SeriesEpisode(name="Making Stuff Similar Episode")
    e1.save()

    e2 = SeriesEpisode(name="Making Stuff Similar Episode2")
    e2.save()

    ss1 = SubSeries(name="Making Stuff")
    ss1.save()

    s = Series(name="NOVA")
    s.subseries = [ss1]
    s.save()

    sa = SeasonAssociation(season=se)
    sa.series = s
    sa.episode = e1
    sa.save()

    sa2 = SeasonAssociation(season=se1)
    sa2.series = ss1
    sa2.episode = e2
    sa2.save()
Ejemplo n.º 22
0
def _make_season(serie_id, episodes):
    return Season(serie_id, episodes[0].season,
                  sorted(episodes, key=lambda x: natural_sort_key(x.episode)))
Ejemplo n.º 23
0
def _make_season(serie_id, episodes):
    return Season(serie_id, episodes[0].season, episodes)
Ejemplo n.º 24
0
def import_schedule(season=None):

    # We want to convert from Eastern Time in the schedule to UTC
    import pytz
    est = pytz.timezone('US/Eastern')

    if not season:
        season = Season.current()

    # Make a map of all the teams. Map by both name and place because the
    # input data uses either one, sort of arbitrarily.
    teams = Team.all().fetch(32)
    team_map = dict((t.name, t) for t in teams)
    team_map.update(dict((t.place, t) for t in teams))

    # We'll build up the weeks and games we're importing in these structures.
    week_map = {}
    games = []

    csvlines = open('data/examples/2011-2012-schedule.csv').readlines()[1:]
    reader = csv.reader(csvlines)
    for row in reader:
        # Figure out kickoff
        kickoff = '%s %s' % itemgetter(0, 7)(row)
        kickoff = datetime.datetime.strptime(kickoff, '%m/%d/%Y %I:%M %p')
        kickoff = est.localize(kickoff).astimezone(pytz.utc)

        # Look up home and away teams by team name in the team map
        team_names = [_.strip() for _ in row[-2:]]
        home, away = itemgetter(*team_names)(team_map)

        # Figure out what week this game belongs to. The data in the CSV is a
        # string like this:
        # 'NFL Week 8:    Chargers @ Chiefs          [TV: ESPN]'
        info = row[2]
        week_num = int(info.split()[2][:-1])

        if week_num not in week_map:
            key = db.Key.from_path('Week', week_num, parent=season.key())
            week_map[week_num] = Week(key=key, name='Week %s' % week_num)
        week = week_map[week_num]

        key_name = '%s@%s' % (away.slug, home.slug)

        game = dict(parent=week,
                    key_name=key_name,
                    home_team=home,
                    away_team=away,
                    teams=[home.key(), away.key()],
                    start=kickoff)
        games.append(game)

    games.sort(key=itemgetter('parent'))

    # Figure out each week's end date based on the latest kickoff of its games
    for week, week_games in groupby(games, itemgetter('parent')):
        week_games = list(week_games)
        start = min(game['start'] for game in week_games)
        end = max(game['start'] for game in week_games)
        week.start = start
        week.end = end + datetime.timedelta(hours=5)

    # Store the weeks, so they have "complete" keys, and can therefore be used
    # as the parents to the games we're about to create.
    week_keys = db.put(week_map.values())

    # Create actual game entities from the kwargs we gathered, and store them.
    games = [Game(**kwargs) for kwargs in games]
    game_keys = db.put(games)

    return week_keys + game_keys
Ejemplo n.º 25
0
def get_gamedates(years):
    """
    Load the dates and games played on them for the given years supplied

    :param years: the years to load info for
    :type  years: list[str]
    """

    # For each specified year, look at the dates and games played on them
    for year in years:
        season_entry = Season()
        season_entry.year = year

        # Get the first day of October as the first possible default date
        first_date = '{}-10-01'.format(year[:4])

        # Iterate until finding first day of regular season
        while True:
            print("Looking at {} for first day of season".format(first_date))
            gameday = query_nba_api(scoreboardv2.ScoreboardV2,
                                    game_date=first_date)
            game_ids = gameday.available.get_data_frame()['GAME_ID']
            # If there were games this day, and it is regular season
            if len(game_ids) > 0 and game_ids[0][2] == '2':
                season_entry.first_date = first_date
                break
            else:
                first_date = (datetime.date.fromisoformat(first_date) +
                              timedelta(1)).isoformat()

        # Begin loading into mongo the game dates
        date = first_date
        while True:

            gamedate_entry = GameDate.objects(date=date)

            # Game date already exists in database
            if gamedate_entry:
                print('{} is already in the database'.format(date))
            # Else game date is not already in database
            else:
                gameday = query_nba_api(scoreboardv2.ScoreboardV2,
                                        game_date=date)
                game_ids = (
                    gameday.available.get_data_frame()['GAME_ID'].to_list())

                # If all star game, skip
                if len(game_ids) > 0 and game_ids[0][2] == '3':
                    game_ids = []
                # If playoff game, stop and mark previous date as last day
                if len(game_ids) > 0 and game_ids[0][2] == '4':
                    last_date = (datetime.date.fromisoformat(date) -
                                 timedelta(1)).isoformat()
                    season_entry.last_date = last_date
                    if not Season.objects(year=year):
                        season_entry.save()
                    break

                # Create gameday entry for this day
                gamedate_entry = GameDate()
                gamedate_entry.date = date
                gamedate_entry.year = year
                if '0021201214' in game_ids:  # Remove not played game
                    game_ids.remove('0021201214')
                gamedate_entry.games = game_ids
                gamedate_entry.save()
                print('Adding {} to database with {} games played on '
                      'this day'.format(date, len(game_ids)))

            date = (datetime.date.fromisoformat(date) +
                    timedelta(1)).isoformat()
Ejemplo n.º 26
0
def get_games(years):
    """
    Load the info and statistics for the game played in the specified years.
    Must have already loaded the gamedates for these years in order to fetch
    the games themselves.

    :param years: the years to load info for
    :type  years: list[str]
    """

    # For each specified year, look at the dates and games played on them
    for year in years:

        # Load season
        season = Season.objects(year=year)
        if not season:
            print('Season and GameDates not yet loaded for {}'.format(year))
            continue
        season = season[0]
        first_date = season.first_date
        last_date = season.last_date

        # For each day in the season
        days = (datetime.date.fromisoformat(last_date) -
                datetime.date.fromisoformat(first_date)).days + 1
        for date in (datetime.date.fromisoformat(first_date) + timedelta(n)
                     for n in range(days)):

            print('\n{}     Loading date: {}     {}\n'.format(
                '#' * 20, date, '#' * 20))
            date = date.isoformat()

            # Fetch GameDate, if it doesn't exist then the season didn't
            # properly load and should re-load it
            game_date = GameDate.objects(date=date)
            if not game_date:
                print('GameDate not loaded for {}, you should re-load this '
                      'season {} in full to get the full season before '
                      'proceeding.'.format(date, year))
                break
            game_date = game_date[0]

            # For each game on this day
            invalid_game_ids = []
            for game_id in game_date.games:

                # Fetch Game, if it exists already, skip it
                game = Game.objects(game_id=game_id)
                if game:
                    continue
                game = Game(game_id=game_id)
                game.date = date
                game.year = year

                # Fetch Box Score Summary
                try:
                    box_score_summary = query_nba_api(
                        boxscoresummaryv2.BoxScoreSummaryV2, game_id=game_id)
                except JSONDecodeError:
                    invalid_game_ids.append(game_id)
                    # The purpose of this except block is because in 2019-20, covid led
                    # to games being cancelled. F**k COVID.
                    if year == '2019-20':
                        print('F**k COVID. This game was cancelled.')
                        continue
                    else:
                        raise Exception("Game wasn't found.".format(game_id))

                # Store inactive players
                game.inactives = [
                    str(inactive_player)
                    for inactive_player in box_score_summary.inactive_players.
                    get_data_frame()['PLAYER_ID'].to_list()
                ]

                # Store officials for this game (create Official if needed)
                officials_df = clean_boxscore_df(
                    box_score_summary.officials.get_data_frame(),
                    index='OFFICIAL_ID',
                    str_keys=['OFFICIAL_ID'])
                officials = []
                game.officials = officials
                for official_id, official in officials_df.iterrows():
                    official_name = '{} {}'.format(official['FIRST_NAME'],
                                                   official['LAST_NAME'])
                    official_entry = add_entry_to_db(document_type=Official,
                                                     unique_id=official_id,
                                                     name=official_name,
                                                     year=year,
                                                     game_id=game_id)
                    officials.append(official_id)

                # Store home team id and road team id
                game_summary = box_score_summary.game_summary.get_data_frame()
                home_team_id = str(game_summary['HOME_TEAM_ID'][0])
                road_team_id = str(game_summary['VISITOR_TEAM_ID'][0])

                # Fetch various relevant box scores to use
                # Traditional box score
                box_score_traditional = query_nba_api(
                    boxscoretraditionalv2.BoxScoreTraditionalV2,
                    game_id=game_id)
                players_traditional = clean_boxscore_df(
                    box_score_traditional.player_stats.get_data_frame(),
                    index='PLAYER_ID')
                teams_traditional = clean_boxscore_df(
                    box_score_traditional.team_stats.get_data_frame(),
                    index='TEAM_ID')
                # Advanced box score
                box_score_advanced = query_nba_api(
                    boxscoreadvancedv2.BoxScoreAdvancedV2, game_id=game_id)
                players_advanced = clean_boxscore_df(
                    box_score_advanced.player_stats.get_data_frame(),
                    index='PLAYER_ID')
                teams_advanced = clean_boxscore_df(
                    box_score_advanced.team_stats.get_data_frame(),
                    index='TEAM_ID')
                # Usage box score
                box_score_usage = query_nba_api(
                    boxscoreusagev2.BoxScoreUsageV2, game_id=game_id)
                players_usage = clean_boxscore_df(
                    box_score_usage.sql_players_usage.get_data_frame(),
                    index='PLAYER_ID')

                # Log the current game
                team_names = [
                    '{} {}'.format(team['TEAM_CITY'], team['TEAM_NAME'])
                    for _, team in teams_traditional.iterrows()
                ]
                print('\n{}     Loading game: {} vs. {}     {}'.format(
                    '#' * 10, team_names[0], team_names[1], '#' * 10))

                # Create each PlayerGame and map them to player_id
                player_games = {}
                game.player_games = player_games
                for player_id, player in players_traditional.iterrows():

                    # Gather player info and add to db for this year if not already stored
                    player_name = player['PLAYER_NAME']
                    print("Player: {}  (id: {})".format(
                        player_name, player_id))
                    add_entry_to_db(document_type=Player,
                                    unique_id=player_id,
                                    name=player_name,
                                    year=year,
                                    game_id=game_id)

                    # Create PlayerGame entry to add to this game
                    player_game = PlayerGame(player_id=player_id)
                    player_games[player_id] = player_game

                    # Store basic data about PlayerGame
                    if player['TEAM_ID'] == home_team_id:
                        player_game.home = True
                        player_game.team_id = home_team_id
                        player_game.opposing_team_id = road_team_id
                    else:
                        player_game.home = False
                        player_game.team_id = road_team_id
                        player_game.opposing_team_id = home_team_id

                    # Create traditional stats entry for this player
                    traditional_player_entry = GameTraditionalStats()
                    player_game.traditional_stats = traditional_player_entry
                    assign_all_values(traditional_player_entry, player)

                    # Create advanced stats entry for this player
                    advanced_player_entry = GameAdvancedStats()
                    player_game.advanced_stats = advanced_player_entry
                    assign_all_values(advanced_player_entry,
                                      players_advanced.loc[player_id])

                    # Create usage stats entry for this player
                    usage_player_entry = GameUsageStats()
                    player_game.usage_stats = usage_player_entry
                    assign_all_values(usage_player_entry,
                                      players_usage.loc[player_id])

                # Create each TeamGame and map them to team_id
                team_games = {}
                game.team_games = team_games
                for team_id, team in teams_traditional.iterrows():

                    # Gather team info and add to db for this year if not already stored
                    team_name = '{} {}'.format(team['TEAM_CITY'],
                                               team['TEAM_NAME'])
                    print("Team: {}  (id: {})".format(team_name, team_id))
                    add_entry_to_db(document_type=Team,
                                    unique_id=team_id,
                                    name=team_name,
                                    year=year,
                                    game_id=game_id)

                    # Create TeamGame entry to add to this game
                    team_game = TeamGame(team_id=team_id)
                    team_games[team_id] = team_game

                    # Store basic data about TeamGame
                    team_game.date = date
                    if team_id == home_team_id:
                        team_game.home = True
                        team_game.opposing_team_id = road_team_id
                    else:
                        team_game.home = False
                        team_game.opposing_team_id = home_team_id

                    # Create traditional stats entry for this team
                    traditional_team_entry = GameTraditionalStats()
                    team_game.traditional_stats = traditional_team_entry
                    assign_all_values(traditional_team_entry, team)

                    # Create advanced stats entry for this team
                    advanced_team_entry = GameAdvancedStats()
                    team_game.advanced_stats = advanced_team_entry
                    assign_all_values(advanced_team_entry,
                                      teams_advanced.loc[team_id])

                # Save game
                game.save()
                print("")

            # Remove game_id of games that were cancelled (covid) from game dates for
            # future iterations
            game_date.games = [
                game_id for game_id in game_date.games
                if game_id not in invalid_game_ids
            ]
            game_date.save()
Ejemplo n.º 27
0
def import_schedule(season=None):

    # We want to convert from Eastern Time in the schedule to UTC
    import pytz

    est = pytz.timezone("US/Eastern")

    if not season:
        season = Season.current()

    # Make a map of all the teams. Map by both name and place because the
    # input data uses either one, sort of arbitrarily.
    teams = Team.all().fetch(32)
    team_map = dict((t.name, t) for t in teams)
    team_map.update(dict((t.place, t) for t in teams))

    # We'll build up the weeks and games we're importing in these structures.
    week_map = {}
    games = []

    csvlines = open("data/examples/2011-2012-schedule.csv").readlines()[1:]
    reader = csv.reader(csvlines)
    for row in reader:
        # Figure out kickoff
        kickoff = "%s %s" % itemgetter(0, 7)(row)
        kickoff = datetime.datetime.strptime(kickoff, "%m/%d/%Y %I:%M %p")
        kickoff = est.localize(kickoff).astimezone(pytz.utc)

        # Look up home and away teams by team name in the team map
        team_names = [_.strip() for _ in row[-2:]]
        home, away = itemgetter(*team_names)(team_map)

        # Figure out what week this game belongs to. The data in the CSV is a
        # string like this:
        # 'NFL Week 8:    Chargers @ Chiefs          [TV: ESPN]'
        info = row[2]
        week_num = int(info.split()[2][:-1])

        if week_num not in week_map:
            key = db.Key.from_path("Week", week_num, parent=season.key())
            week_map[week_num] = Week(key=key, name="Week %s" % week_num)
        week = week_map[week_num]

        key_name = "%s@%s" % (away.slug, home.slug)

        game = dict(
            parent=week,
            key_name=key_name,
            home_team=home,
            away_team=away,
            teams=[home.key(), away.key()],
            start=kickoff,
        )
        games.append(game)

    games.sort(key=itemgetter("parent"))

    # Figure out each week's end date based on the latest kickoff of its games
    for week, week_games in groupby(games, itemgetter("parent")):
        week_games = list(week_games)
        start = min(game["start"] for game in week_games)
        end = max(game["start"] for game in week_games)
        week.start = start
        week.end = end + datetime.timedelta(hours=5)

    # Store the weeks, so they have "complete" keys, and can therefore be used
    # as the parents to the games we're about to create.
    week_keys = db.put(week_map.values())

    # Create actual game entities from the kwargs we gathered, and store them.
    games = [Game(**kwargs) for kwargs in games]
    game_keys = db.put(games)

    return week_keys + game_keys
Ejemplo n.º 28
0
def seasonIndex():
    pageNum = int(request.args.get('page', 1))
    order = OrderBy.OrderBySeason(request.args.get('order', None)).value()
    filters = FilterBy.SeasonFilter(request.args.get('filter', None)).value()
    seasons = Season.list(order, filters, pageNum)
    return json.dumps(seasons)