Ejemplo n.º 1
0
 def players(self):
     if self._players:
         return self._players
     self._players = [
         Player.from_response(p.__dict__, self.team.league)
         for p in get_value(self._raw['players']).player
     ]
     return self._players
Ejemplo n.º 2
0
 def get_stats(self, week_num=None):
     """ Get this player's stats for a given week or the whole season """
     data = self._fetch_stats(week_num)
     stats_data = data['fantasy_content']['league']['players']['player'][
         'player_stats']
     return [
         Stat.from_value(s, self.league.game_code)
         for s in get_value(stats_data).stats.stat
     ]
Ejemplo n.º 3
0
    def test_get_value(self):
        """ get_value turns XML response into attribute objects """
        resp_file = Path(__file__).parent / 'sample_data' / 'example.xml'
        parsed = parse_response(resp_file.read_text())
        values = get_value(parsed)
        root = values.fantasy_content.root

        self.assertEqual(root.individual_item, 'item value')
        self.assertEqual(root.list_of_items.item_object[0].name, 'Item 1')
Ejemplo n.º 4
0
 def teams(self, persist_ttl=DEFAULT_TTL):
     logger.debug("Looking up teams")
     data = self.ctx._load_or_fetch('teams.' + self.id,
                                    'teams',
                                    league=self.id)
     teams = []
     for team in data['fantasy_content']['league']['teams']['team']:
         t = Team(self.ctx, self, get_value(team['team_key']))
         from_response_object(t, team)
         teams.append(t)
     return teams
Ejemplo n.º 5
0
 def standings(self, persist_ttl=DEFAULT_TTL):
     logger.debug("Looking up standings")
     data = self.ctx._load_or_fetch('standings.' + self.id,
                                    'standings',
                                    league=self.id)
     standings = []
     for team in data['fantasy_content']['league']['standings']['teams'][
             'team']:
         standing = Standings(self.ctx, self, get_value(team['team_key']))
         from_response_object(standing, team)
         standings.append(standing)
     return standings
Ejemplo n.º 6
0
    def get_leagues(self, game, season, persist_ttl=DEFAULT_TTL):
        """ Get a list of all leagues for a given game and season

        Args:
            game (str) - the fantasy game we're looking at
            season (int/str) - the fantasy season to get leagues for
        """
        game_id = get_game_id(game, season)
        data = self._load_or_fetch(
            'leagues',
            'users;use_login=1/games;game_keys={}/leagues'.format(game_id))
        leagues = []
        for league_data in as_list(get(
                data, 'fantasy_content.users.user.games.game.leagues.league')):
            league = League(self, get_value(league_data['league_key']))
            from_response_object(league, league_data)
            leagues.append(league)
        return leagues
Ejemplo n.º 7
0
 def fetch_player_stats(self):
     """ Fetch the stats for every player on the roster for the given week """
     player_keys = ",".join([p.player_key for p in self.players])
     keys = ('season', '')
     if self.week_num:
         keys = (str(self.week_num), f"type=week;week={self.week_num}")
     data = self.team.league.ctx._load_or_fetch(
         f"roster.{self.team.id}.stats.{self.team.league.id}.{keys[0]}",
         f"league/{self.team.league.id}/players;player_keys={player_keys}/stats;{keys[1]}",
     )
     # Populate the stats caches of the individual players too
     player_map = {p.player_key: p for p in self.players}
     player_refs = data['fantasy_content']['league']['players']['player']
     for player_ref in player_refs:
         player_key = get_value(player_ref['player_key'])
         player_obj = player_map.get(player_key)
         if not player_obj:
             logger.warn(f"Player stats found for {player_key} but they are not on the roster")
             continue
         player_obj._stats_cache[self.week_num] = player_ref
     return data
Ejemplo n.º 8
0
 def get_points(self, week_num=None):
     """ Get this player's points for a given week or the whole season """
     data = self._fetch_stats(week_num)
     return get_value(data['fantasy_content']['league']['players']['player']
                      ['player_points']['total'])