diff --git a/espnff/league.py b/espnff/league.py index 3cbe808..8cbdbc7 100644 --- a/espnff/league.py +++ b/espnff/league.py @@ -1,5 +1,8 @@ import requests +from pandas.io.json import json_normalize +import pandas as pd + from .utils import (two_step_dominance, power_points, ) from .team import Team @@ -131,3 +134,59 @@ def scoreboard(self, week=None): matchup.away_team = team return result + + def team_df(self): + """ Returns a Pandas DataFrame with team data """ + variables = self.teams[0].__dict__.keys() + df = pd.DataFrame([[getattr(i, j) for j in variables] for i in self.teams], columns=variables) + return df + + def player_df(self): + """ Returns a Pandas DataFrame with raw player data """ + params = { + 'leagueId': self.league_id, + 'offset': None, + 'limit': '40', + 'availabilityFilter': '-1', + 'slotCategoryFilter': '-1', + 'useCurrentSeasonRealStats': 'true', + 'useCurrentSeasonProjectedStats': 'true', + 'useCurrentPeriodRealStats': 'true', + 'useCurrentPeriodProjectedStats': 'true', + 'usePreviousPeriodRealStats': 'true', + 'usePreviousSeasonRealStats': 'true', + 'processAverages': 'true', + 'includeRankings': 'true', + 'includeLatestNews': 'true', + 'includeProjectionText': 'true', + } + + cookies = None + if self.espn_s2 and self.swid: + cookies = { + 'espn_s2': self.espn_s2, + 'SWID': self.swid + } + + player_data = [] + + for i in range(20): + params['offset'] = i + + r = requests.get('%splayerInfo' % (self.ENDPOINT,), params=params, cookies=cookies) + self.status = r.status_code + data = r.json() + + if self.status == 401: + raise PrivateLeagueException(data['error'][0]['message']) + elif self.status == 404: + raise InvalidLeagueException(data['error'][0]['message']) + elif self.status != 200: + raise UnknownLeagueException('Unknown %s Error' % self.status) + + player_data.extend(data['playerInfo']['players']) + + df = json_normalize(player_data) + df = df.select(lambda x: 'rawStats' not in x and 'appliedStats' not in x, axis=1) # remove excess statistics + + return df