def get_matchups(self, key_map=None): """ Request the matchups for each week of this season from the Sleeper API :param key_map: A dictionary representing the values to be parsed from the API responses :return: A DataFrame representing all the matchups from this season """ all_matchups = list() for week in range( self.start_week, self.playoff_start_week + self.playoff_rounds_count, ): url = f"{self.base_url}/league/{self.season_id}/matchups/{week}" response = api_get_request(url) if key_map: response = format_response(response, key_map) week_matchups = pd.DataFrame(response) week_matchups["season_id"] = self.season_id week_matchups["week"] = week all_matchups.append(pd.DataFrame(week_matchups)) self.matchups = pd.concat(all_matchups, ignore_index=True) return self.matchups
def get_season(self, key_map=None): """ Request a season from ESPN :param key_map: A dictionary representing the values to be parsed from the API response :return: A DataFrame representing the season """ self.response = League( league_id=self.league_id, year=self.year, espn_s2=self.s2, swid=self.swid, ) response = vars(self.response) response["settings"] = vars(response["settings"]) self.start_week = response.get("firstScoringPeriod", 1) self.regular_season_weeks = response["settings"].get( "reg_season_count" ) self.team_objs = [vars(team) for team in response["teams"]] self.draft_picks = [vars(pick) for pick in response["draft"]] self.player_map = response["player_map"] if key_map: response = format_response(response, key_map) self.season = pd.DataFrame([response]) self.season["season_id"] = self.season_id self.season["league_name"] = self.league_name return self.season
def get_teams(self, key_map=None): """ Parse the teams for the season :param key_map: A dictionary representing the values to be parsed from the raw teams data :return: A DataFrame representing the teams """ if key_map: self.teams = format_response(self.team_objs, key_map) self.teams = pd.DataFrame(self.teams) self.teams["season_id"] = self.season_id return self.teams
def get_rosters(self, key_map=None): """ Request the rosters for this season from the Sleeper API :param key_map: A dictionary representing the values to be parsed from the API response :return: A DataFrame representing the rosters """ url = f"{self.base_url}/league/{self.season_id}/rosters" response = api_get_request(url) if key_map: response = format_response(response, key_map) rosters = pd.DataFrame(response) rosters["season_id"] = self.season_id self.rosters = rosters return self.rosters
def get_draft_picks(self, key_map=None): """ Request the full list of draft picks from a particular draft from the Sleeper API :param key_map: A dictionary representing the values to be parsed from the API response :return: A DataFrame representing the draft picks """ url = f"{self.base_url}/draft/{self.draft_id}/picks" response = api_get_request(url) if key_map: response = format_response(response, key_map) draft_picks = pd.DataFrame(response) draft_picks["season_id"] = self.season_id self.draft_picks = draft_picks return self.draft_picks
def get_matchups(self, key_map=None): """ Parse the matchups for the season :return: A DataFrame representing the matchups """ for team in self.team_objs: team["schedule"] = [ opponent.team_id for opponent in team["schedule"] ] team["week"] = [i + 1 for i in range(len(team["schedule"]))] matchups = format_response(self.team_objs, key_map) matchups = pd.DataFrame(matchups) matchups = matchups.apply(pd.Series.explode) matchups["season_id"] = self.season_id self.matchups = matchups return self.matchups
def get_winners_bracket(self, key_map=None): """ Request the playoff winner's bracket for this season from the Sleeper API :param key_map: A dictionary representing the values to be parsed from the API response :return: A DataFrame representing the winner's bracket """ url = f"{self.base_url}/league/{self.season_id}/winners_bracket" response = api_get_request(url) self.playoff_rounds_count = max([matchup["r"] for matchup in response]) if key_map: response = format_response(response, key_map) winners_bracket = pd.DataFrame(response) winners_bracket["season_id"] = self.season_id self.winners_bracket = winners_bracket return self.winners_bracket
def get_draft_picks(self, key_map=None): """ Parse the draft picks for the season :param key_map: A dictionary representing the values to be parsed from the raw draft picks data :return: A DataFrame representing the draft picks """ for pick in self.draft_picks: pick["team"] = vars(pick["team"]) if key_map: self.draft_picks = format_response(self.draft_picks, key_map) self.draft_picks = pd.DataFrame(self.draft_picks) self.draft_picks["position"] = self.draft_picks["player_id"].apply( lambda x: self.response.player_info(playerId=x).position ) self.draft_picks["season_id"] = self.season_id return self.draft_picks
def get_season(self, key_map=None): """ Request a season from the Sleeper API :param key_map: A dictionary representing the values to be parsed from the API response :return: A DataFrame representing the season """ url = f"{self.base_url}/league/{self.season_id}" response = api_get_request(url) self.year = int(response.get("season")) self.previous_season_id = response.get("previous_league_id") self.draft_id = response.get("draft_id") settings = response.get("settings", dict()) self.start_week = settings.get("start_week") self.playoff_start_week = settings.get("playoff_week_start") if key_map: response = format_response(response, key_map) self.season = pd.DataFrame([response]) self.season["year"] = self.season["year"].astype(int) self.season["league_name"] = self.league_name return self.season
def roll(event, context): try: logger = init_logger() logger.debug(context) # Read Telegram API event event_body = json.loads(event.get("body")) logger.debug(event_body) # Inline Queries if event_body.get("inline_query") is not None: ilq = event_body.get("inline_query") # TODO: Handle this a little more gracefully. Prefer usernames where available. first_name = ilq.get("from").get("first_name") # Get the user's query query = ilq.get("query") # Roll the dice! # TODO: Make this a function. result = dice.roll(query, raw=True) response = format_response( "{} rolled {}.".format(first_name, query), result) # Build the ILQ response data response_data = { "cache_time": 0, "inline_query_id": ilq.get("id"), "results": json.dumps([{ "type": "article", "id": str(uuid4()), "title": "Roll {}".format(query), "input_message_content": { "message_text": response, "parse_mode": "HTML" } }]) } # Send our response url = BASE_URL + "/answerInlineQuery" response = requests.post(url, response_data) logging.debug("Response: %s", response.text) # Command Queries elif event_body.get("message") is not None: message = event_body.get("message") message_text = str(message.get("text")) # Roll commands if "/roll" in message_text: # TODO: Handle this a little more gracefully. Prefer usernames where available. # TODO: Make this a function. first_name = message.get("from").get("first_name") # Our query is everything after the first occurrence of "roll" query = message_text.partition("roll ")[2] # Roll the dice! # TODO: Make this a function. result = dice.roll(query, raw=True) response = format_response( "{} rolled {}.".format(first_name, query), result) # Build the command response data response_data = { "text": response.encode("utf8"), "chat_id": message.get("chat").get("id"), "parse_mode": "HTML" } # Send our response url = BASE_URL + "/sendMessage" response = requests.post(url, response_data) logging.debug("Response: %s", response.text) except Exception as e: logging.critical(e) # Let Telegram know we've processed the message return {"statusCode": 200}