Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding owners to Team #487

Merged
merged 5 commits into from Oct 26, 2023
Merged

adding owners to Team #487

merged 5 commits into from Oct 26, 2023

Conversation

bnm91
Copy link
Contributor

@bnm91 bnm91 commented Oct 24, 2023

Problem

Currently there's no way to get info on the "owner" (aka "manager") of a Team. While the team's name is available, those are subject to change making tracking over longer periods difficult

Solution

ESPN has a concept of Members on a League and Owners on a Team. These are tied together by an Id and the Member entity has much more stable information about the actual humans behind the team.
Storing this Member info with the Team will make that info more accessible

Code Changes

  • added members property to the League
  • modified BaseLeague fetch_teams to create and an array of members associated with the owners of each Team
  • added owners property and constructor parameter to Teams

@bnm91 bnm91 mentioned this pull request Oct 24, 2023
@bnm91
Copy link
Contributor Author

bnm91 commented Oct 25, 2023

I’ll take a look at these failures soon

@cwendt94
Copy link
Owner

Don't worry about the hockey team names one those just popped up, but it does look like two new ones are failing for owner logic.

@cwendt94
Copy link
Owner

FYI I did have owners but them removed because they no longer had the actual name, but I can see why we would want at least the unique id. Here was the PR removing it. There is a chance a team might not have a owner see this comment

espn_api/base_league.py Outdated Show resolved Hide resolved
@samthom1
Copy link
Contributor

For what it's worth, there may have been further changes to the ESPN API this morning. I was making my own attempt at fixing this issue when the API call started returning additional fields under data['members']. Restoring the members/owner references from before commit cc82818 allows owner names to flow through again.

@bnm91
Copy link
Contributor Author

bnm91 commented Oct 25, 2023

@cwendt94 I rewrote this to only include the "unique id" as you mentioned above. I also fixed a couple tests broken by these changes.
I still think it would be nice to have the "member" (and therefore the user's actual name) associated with the Team but currently this leaves that exercise to the user. Perhaps I'll open a separate discussion for that improvement

I'm struggling to run the entire test suite in my environment but addressed a few test issues I was aware of.

The following tests will probably still fail but due to separate issue:
test_league_draft (basketball.integration.test_league.LeagueTest)
separate issue -- test uses 2019 data which requires 'location' but none exists

test_box_scores (football.integration.test_league.LeagueTest)
separate issue -- test uses 2019 data which requires 'location' but none exists

test_league_int (hockey.integration.test_league.LeagueTest)
separate issue -- test uses 2021 data which requires 'location' but none exists

espn_api/base_league.py Outdated Show resolved Hide resolved
espn_api/baseball/team.py Outdated Show resolved Hide resolved
@codecov-commenter
Copy link

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (f268c46) 80.42% compared to head (ed238bc) 80.53%.
Report is 7 commits behind head on master.

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #487      +/-   ##
==========================================
+ Coverage   80.42%   80.53%   +0.11%     
==========================================
  Files          59       59              
  Lines        2094     2106      +12     
==========================================
+ Hits         1684     1696      +12     
  Misses        410      410              
Files Coverage Δ
espn_api/base_league.py 100.00% <100.00%> (ø)
espn_api/baseball/team.py 95.45% <ø> (+0.10%) ⬆️
espn_api/basketball/team.py 97.82% <ø> (-2.18%) ⬇️
espn_api/football/team.py 100.00% <ø> (+1.51%) ⬆️
espn_api/hockey/team.py 100.00% <ø> (+2.12%) ⬆️
espn_api/wbasketball/team.py 17.02% <ø> (-0.38%) ⬇️

... and 2 files with indirect coverage changes

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@cwendt94 cwendt94 merged commit 9f5838d into cwendt94:master Oct 26, 2023
3 checks passed
@bnm91 bnm91 deleted the team-owners branch October 26, 2023 18:22
@DesiPilla
Copy link
Contributor

@bnm91 Is there an endpoint / header we can use to get the actual users' names?

@DesiPilla
Copy link
Contributor

I'm using the following workaround for now:

def set_league_endpoint(league: League) -> None:
    """Set the league's endpoint."""

    # Current season
    if league.year >= (datetime.datetime.today() - datetime.timedelta(weeks=12)).year:
        league.endpoint = (
            "https://fantasy.espn.com/apis/v3/games/ffl/seasons/"
            + str(league.year)
            + "/segments/0/leagues/"
            + str(league.league_id)
            + "?"
        )

    # Old season
    else:
        league.endpoint = (
            "https://fantasy.espn.com/apis/v3/games/ffl/leagueHistory/"
            + str(league.league_id)
            + "?seasonId="
            + str(league.year)
            + "&"
        )

def set_owner_names(league: League):
    """This function sets the owner names for each team in the league.
    The team.owners attribute only contains the SWIDs of each owner, not their real name.

    Args:
        league (League): ESPN League object
    """
    endpoint = "{}view=mTeam".format(league.endpoint)
    r = requests.get(endpoint, cookies=league.cookies).json()
    if type(r) == list:
        r = r[0]

    # For each member in the data, create a map from SWID to their full name
    swid_to_name = {}
    for member in r["members"]:
        swid_to_name[member["id"]] = re.sub(
            " +", " ", member["firstName"] + " " + member["lastName"]
        ).title()

    # Set the owner name for each team
    for team in league.teams:
        team.owner = swid_to_name[team.owners[0]]

set_league_endpoint(league)
set_owner_names(league)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants