Exemple #1
0
 def from_response(resp, league):
     trans = from_response_object(Transaction(league), resp)
     for player in as_list(_.get(trans, 'players.player', [])):
         tp = TransactionPlayer(trans)
         from_response_object(tp, player.__dict__)
         trans.involved_players.append(tp)
     return trans
Exemple #2
0
    def test_from_response_object(self):
        class Obj(object):
            @property
            def protected_prop(self):
                """ Don't set existing properties/methods """
                return 4

            def protected_method(self):
                """ Don't set existing properties/methods """
                return 5

        obj = Obj()
        from_response_object(
            obj, {
                'key1': 'value1',
                'nested': {
                    'key2': 'value2',
                },
                'protected_prop': 'ignored',
                'protected_method': 'ignored',
            })
        self.assertEqual(obj.key1, 'value1')
        self.assertEqual(obj.nested.key2, 'value2')
        self.assertEqual(obj.protected_prop, 4)
        self.assertEqual(obj.protected_method(), 5)
Exemple #3
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
Exemple #4
0
 def draft_results(self, persist_ttl=DEFAULT_TTL):
     results = []
     for team in self.teams(persist_ttl):
         data = self.ctx._load_or_fetch(
             'draftresults.' + team.id,
             f'team/{team.id}/draftresults;out=players')
         for result in data['fantasy_content']['team']['draft_results'][
                 'draft_result']:
             dr = DraftResult(self, team)
             from_response_object(dr, result)
             results.append(dr)
     return results
Exemple #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
Exemple #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
Exemple #7
0
 def players(self, persist_ttl=DEFAULT_TTL):
     logger.debug("Looking up current players on team")
     data = self.ctx._load_or_fetch(
         f"team.{self.id}.players",
         f"team/{self.id}/players",
     )
     players = []
     for p in data['fantasy_content']['team']['players']['player']:
         player = Player(self.league)
         player = from_response_object(player, p)
         players.append(player)
     return players
Exemple #8
0
 def sync(self):
     week_data = self.ctx._load_or_fetch(
         'weeks.{}.{}'.format(self.league.id, self.week_num),
         'scoreboard;week={}'.format(self.week_num),
         league=self.league.id)
     matchup_data = get(week_data,
                        'fantasy_content.league.scoreboard.matchups')
     if 'matchup' not in matchup_data:
         return
     self.matchups = []
     for matchup in matchup_data['matchup']:
         matchup_obj = Matchup(self.ctx, self.league, self)
         self.matchups.append(from_response_object(matchup_obj, matchup))
Exemple #9
0
    def roster(self, week_num=None):
        """ Fetch this team's roster for a given week

        If week_num is None fetch the live roster
        """
        # First item is the peristence key, second is the API filter
        keys = ('live', '')
        if week_num:
            keys = (str(week_num), f"week={week_num}")
        data = self.ctx._load_or_fetch(
            f"team.{self.id}.roster.{keys[0]}",
            f"team/{self.id}/roster;{keys[1]}",
        )
        roster_data = data['fantasy_content']['team']['roster']
        roster = Roster(self, week_num)
        roster = from_response_object(roster, roster_data, set_raw=True)
        return roster
Exemple #10
0
 def from_response(resp, league):
     return from_response_object(Player(league), resp)