spilchen / yahoo_fantasy_api

python bindings for the Yahoo! Fantasy API (https://yahoo-fantasy-api.readthedocs.io/)
MIT License
91 stars 33 forks source link

NFL league_ids yielding RuntimeError #11

Open Phloot opened 3 years ago

Phloot commented 3 years ago

When attempting to return league_ids with a year specified, a RuntimeError is returned.

Code:

from yahoo_oauth import OAuth2
import yahoo_fantasy_api as yfa

oauth = OAuth2(None, None, from_file='oauth2.json')

if not oauth.token_is_valid():
    oauth.refresh_access_token()

gm = yfa.Game(oauth, code='nfl')
gm.league_ids(year=2020)

Returns:

Traceback (most recent call last):
  File "~/testing.py", line 18, in <module>
    gm.league_ids(year=2020)
  File "C:\Program Files\Python37\lib\site-packages\yahoo_fantasy_api\game.py", line 42, in league_ids
    t = objectpath.Tree(self.yhandler.get_teams_raw())
  File "C:\Program Files\Python37\lib\site-packages\yahoo_fantasy_api\yhandler.py", line 68, in get_teams_raw
    return self.get("users;use_login=1/games/teams")
  File "C:\Program Files\Python37\lib\site-packages\yahoo_fantasy_api\yhandler.py", line 25, in get
    raise RuntimeError(response.content)
RuntimeError: b'{\n    "error": {\n        "xml:lang": "en-us",\n        "yahoo:uri": "\\/fantasy\\/v2\\/users;use_login=1\\/games\\/teams?format=json",\n        "description": "Team key 39.l.15338.t.1 does not exist.",\n        "detail": ""\n    }\n}'

I am able to successfully return league information, team information, etc if I manually input the league ID, however it makes it much more difficult to automate the querying of data if I manually need to identify the game ID for the current year (it is 399 for 2020 NFL for example) and league ID.

I also do not recognize the game ID (39), or league ID (15338) being returned in the traceback.

Any thoughts?

spilchen commented 3 years ago

Hmm, something is weird with that league. You are part of the league but the first team in the league doesn't exist. Are you able to to construct a league object for the league?

lg = yfa.League(oauth, '39.l.15338')
lg.teams()
Demon-tk commented 3 years ago

Is your team key object an array still? I had to concatenate mine into a string like this:

def yahoo_login():
    print("-- Logging to into Yahoo")
    oauth = OAuth2(None, None, from_file='oauth.json')
    game = yfa.Game(oauth, 'nfl')
    print("-- Logged in to Yahoo")
    game_id_array = game.league_ids(year=2020)
    game_id_str = ''.join(map(str, game_id_array))
    league = yfa.League(oauth, game_id_str)
    print("-- Created local Yahoo league to extract info from")
    return league

So that it would work properly, doesn't look as clean but works!

spilchen commented 3 years ago

@Demon-tk, league_ids returns an array in case you are in multiple leagues. What you have above only works if you are in a single league. You can just as easily reference the first element in the array and pass that through when creating the League object. @Phloot opened the issue because the league_ids call dies with a bad response from Yahoo!