whatadewitt / yahoo-fantasy-sports-api

NodeJS wrapper for the Yahoo! Fantasy Sports API
https://yahoo-fantasy-node-docs.vercel.app/
MIT License
192 stars 52 forks source link

Player Resource - Stats missing weeks sub-resource #79

Closed cjhaviland closed 2 years ago

cjhaviland commented 3 years ago

I am really interested in using the weeks sub-resource on Players Stats, but it looks like it's not actually working. It always pulls the full season data no matter what. I did check the Yahoo API docs (which suck) and it seems like the only sub-resource is ;metadata? I could be wrong.

charlietfl commented 3 years ago

You get full seasons stats by default unless you specify a week number. There is an older more informative set of Yahoo docs on wayback machine https://web.archive.org/web/20130927114616if_/http://developer.yahoo.com/fantasysports/guide/player-resource.html. As for this API check https://yahoo-fantasy-node-docs.vercel.app/resource/player/stats and it shows you to optionally include week also

whatadewitt commented 3 years ago

@cjhaviland did that solve the issue for you? I can look in if no.

thanks @charlietfl !!

cjhaviland commented 3 years ago

@whatadewitt i can't make it work, but I'm also doing it for baseball right now. Which I think is just dates but I can't seem to get that either.

I would prefer a players stats for a week in my league if possible. Thanks.

whatadewitt commented 3 years ago

@cjhaviland just confirming... you've tried simply entering a week number, rather than a date, yes?

i haven't had a chance to check this out myself yet, but you could also probably get the league settings, which would give you weeks/dates and do a promise.all to get them all? it's a lot of work but it would do the trick in the interim?

cjhaviland commented 3 years ago

@whatadewitt so what I am currently doing is grabbing yf.roster.players(teamKey, weekNum).

Then doing a Promise.all() for yf.player.stats(playerKey, weekNum) which gives back a player object with these properties:

"stats": {
    "coverage_type": "season",
    "coverage_value": "2021",
     ...
}

I've tried using it in my app and in your hosted app, and neither will give me stats for a week, only the whole season.

I also now see that the League.Players resource can take a team and week, but that's not bringing back any stats whether I put a week in or not. I am starting to think Yahoo change the APIs around or something.

whatadewitt commented 3 years ago

Indeed this is an issue.

Let me reach out to my contact at Yahoo! to see if this is a known bug.

rdbaron commented 2 years ago

Just here to say I'm running into the same issue, but also to say that what you've put together here is a beautiful thing and it's very much appreciated!

rdbaron commented 2 years ago

Hey @whatadewitt ! Just curious if your Yahoo! contact ever got back to you about this?

whatadewitt commented 2 years ago

Unfortunately no. I just followed up again.

whatadewitt commented 2 years ago

I got a reponse, let me know if it helps:

So, I think the trick here w/ MLB players is that MLB is not naturally week based -- we aggregate stats by date and season, and some other splits (last 7 days, last 14 days, last 30, etc), but we don't naturally aggregate by week except in the context of an H2H league. So that means that trying to fetch a random player's stats by a specific matchup week isn't strictly supported within our APIs, as far as I'm aware.

What you do about it sort of depends on what you're trying to build. If you're trying to understand the player's last week performance (sliding window), then you can pass something like this instead:

/fantasy/v2/player/mlb.p.6619/stats;type=lastweek

If you're looking for a particular team+player's stats (ie, how many stats were accumulated for a player on a certain team), you can get that too, but it'll be in the context of how that player was used in that h2h league, not globally (ie, if the player was never started during the week, they would have 0 stats for that week).

Lemme know what you're trying to do and we can see if there are any other workarounds.

So if you'd like, we can craft a response here and see what we can get done!

rdbaron commented 2 years ago

Well, unless I'm doing something wrong, this issue is not limited to MLB players. In my case, I am using NFL players, and even when specifying a week number, I get their full seasons stats (same result using your testing tool.)

whatadewitt commented 2 years ago

I will follow up with this feedback along with some other data!

mihirmodiofficial commented 2 years ago

can anyone tell me where i get player projected points?

whatadewitt commented 2 years ago

Unfortunately they don’t expose this info.

On Tue, Oct 26, 2021 at 10:43 AM mihirmodiofficial @.***> wrote:

can anyone tell me where i get player projected points?

β€” You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/whatadewitt/yahoo-fantasy-sports-api/issues/79#issuecomment-951954145, or unsubscribe https://github.com/notifications/unsubscribe-auth/AACYZUQ6MKXDJRCTMKTFUR3UI2V7XANCNFSM467UL7UQ .

jimweigandt commented 2 years ago

I will follow up with this feedback along with some other data!

I'm also running into wanting to grab points on a weekly basis for some NFL players. Did you get any updates on this at all or get resolved? Thanks for the work on this API!

whatadewitt commented 2 years ago

Unfortunately I haven't followed up since the last response. I can try to look into it for football, but I'd need a league ID that is public to check it out...

jimweigandt commented 2 years ago

Here is my current leagues public key for football. Hopefully this helps!

406.l.11131

jimweigandt commented 2 years ago

Unfortunately I haven't followed up since the last response. I can try to look into it for football, but I'd need a league ID that is public to check it out...

Just wanted to follow up as I think I found the issue on why weekly stats are not being fetched even though you specify a date/week in all games.

I took a look at the Yahoo documentation after you mentioned that you could fetch lastweek stats in non-NFL games by specifying a stat "type". You can use this same parameter to also sort stats that are being returned. So I figured I would try and specify the type of the other parameters you can use in sort_type filter.

The url being used looked like this: https://fantasysports.yahooapis.com/fantasy/v2/player/406.p.32692/stats;type=week;week=10

Lo and behold by setting a type of week and then setting the week right after it worked. It generated a response where the player_stats resource looked likes this:

{
  coverage_type: 'week',
  coverage_value: '10',
  stats: [
    { stat_id: '0', value: '1' },
    { stat_id: '1', value: '0' },
    { stat_id: '2', value: '0' },
    ...
}

Since non-NFL games don't use weeks, but rather use dates, you can specify a stat type of date and then follow it up with the actual date you are looking at. So the URL should look something like this:

https://fantasysports.yahooapis.com/fantasy/v2/player/328.p.8180/stats;type=date;date=2020-07-07

Looking at your stats method on the Player Resource, we would just need to modify the if statement that controls what gets appended to the end of the static URL.

I think you differentiate between a date and week in the Roster Resource players method. So using that if/else statement modified with the "type" of stats we want, it should look something like this.

    if (args.length) {
      let date = args.pop();
      if (date.indexOf("-") > 0) {
        // string is date, of format y-m-d
        url += `;type=date;date=${date}`;
      } else {
        // number is week...
        url += `;type=week;week=${date}`;
      }
    }

I could have a PR ready to go if I had access to the repo, but it's a pretty easy copy and paste update that I'm sure you would be able to do as well.

I still wish they would make the projected points public but I can see why they would want to protect any insight on how those are calculated. Guess I just have to go back to my web scrapper I built and run that instead πŸ˜†

EDIT:

Looks like if I would've look around a bit more I could've found that same solution I suggested... Looks like you are doing that exacted if/else statement in the Team Resource class.

whatadewitt commented 2 years ago

Hey, sorry, I was off the machine during the holidays :D

So you're saying that player.stats doesn't have the conditional that is appending type? I can add that easily!

That said, if you'd like to submit a PR, you should be able to fork the repo, create a branch and submit a PR that way, which I would be happy to accept and add your name as a contributor πŸ˜„

whatadewitt commented 2 years ago

@jimweigandt It works!

There's a bug in my sandbox, but I just pulled this using

player_key: nfl.p.33393 week: 17

{ "player_key": "406.p.33393", "player_id": "33393", "name": { "full": "Ja'Marr Chase", "first": "Ja'Marr", "last": "Chase", "ascii_first": "Ja'Marr", "ascii_last": "Chase" }, "editorial_player_key": "nfl.p.33393", "editorial_team_key": "nfl.t.4", "editorial_team_full_name": "Cincinnati Bengals", "editorial_team_abbr": "Cin", "bye_weeks": { "week": "10" }, "uniform_number": "1", "display_position": "WR", "headshot": { "url": "https://s.yimg.com/iu/api/res/1.2/lvjCNNAuu4f1EuWd9QuRtA--~C/YXBwaWQ9eXNwb3J0cztjaD0yMzM2O2NyPTE7Y3c9MTc5MDtkeD04NTc7ZHk9MDtmaT11bGNyb3A7aD02MDtxPTEwMDt3PTQ2/https://s.yimg.com/xe/i/us/sp/v/nfl_cutout/players_l/08102021/33393.png", "size": "small" }, "image_url": "https://s.yimg.com/iu/api/res/1.2/lvjCNNAuu4f1EuWd9QuRtA--~C/YXBwaWQ9eXNwb3J0cztjaD0yMzM2O2NyPTE7Y3c9MTc5MDtkeD04NTc7ZHk9MDtmaT11bGNyb3A7aD02MDtxPTEwMDt3PTQ2/https://s.yimg.com/xe/i/us/sp/v/nfl_cutout/players_l/08102021/33393.png", "is_undroppable": "0", "position_type": "O", "eligible_positions": [ "WR" ], "has_player_notes": 1, "player_notes_last_timestamp": 1641163273, "stats": { "coverage_type": "week", "coverage_value": "17", "stats": [ { "stat_id": "0", "value": "1" }, { "stat_id": "1", "value": "0" }, { "stat_id": "2", "value": "0" }, { "stat_id": "3", "value": "0" }, { "stat_id": "4", "value": "0" }, { "stat_id": "5", "value": "0" }, { "stat_id": "6", "value": "0" }, { "stat_id": "7", "value": "0" }, { "stat_id": "8", "value": "0" }, { "stat_id": "9", "value": "0" }, { "stat_id": "10", "value": "0" }, { "stat_id": "11", "value": "11" }, { "stat_id": "12", "value": "266" }, { "stat_id": "13", "value": "3" }, { "stat_id": "14", "value": "0" }, { "stat_id": "15", "value": "0" }, { "stat_id": "16", "value": "0" }, { "stat_id": "17", "value": "0" }, { "stat_id": "18", "value": "0" }, { "stat_id": "57", "value": "0" }, { "stat_id": "58", "value": "0" }, { "stat_id": "59", "value": "0" }, { "stat_id": "60", "value": "0" }, { "stat_id": "61", "value": "0" }, { "stat_id": "62", "value": "0" }, { "stat_id": "63", "value": "2" }, { "stat_id": "64", "value": "2" }, { "stat_id": "78", "value": "12" }, { "stat_id": "79", "value": "0" }, { "stat_id": "80", "value": "6" }, { "stat_id": "81", "value": "0" } ] } }

If I don't use the week, I get the full season... and I have confirmed using some other weeks as well... Once I confirm dates in a baseball league then I hope this is good to go...

whatadewitt commented 2 years ago

Ok, so it's not TOTALLY working... but I think I'm getting it sorted out here. The issue is that you can't get stats by week for players in other sports because they don't operate in a "week" setting. Furthermore, when I specify a date, it doesn't appear to be sending back the correct information.

I'm going to make a temporary change to simply append the ;type=week bit, which will fix the issue for football, and in the meantime I'll reach out to my contact at Yahoo! to see if I can get stats by date (though I will have to make sure I haven't already done that)

whatadewitt commented 2 years ago

Final update, it's obviously too late and it may actually be working, but it appears Yahoo! has gotten to work on this years fantasy offering because when I use a baseball player I'm getting 2022 as the "current_season". I'll get the code for 2021 tomorrow when I'm more awake and see if date / week stuff is working out then :|

rdbaron commented 2 years ago

Ok, so it's not TOTALLY working... but I think I'm getting it sorted out here. The issue is that you can't get stats by week for players in other sports because they don't operate in a "week" setting. Furthermore, when I specify a date, it doesn't appear to be sending back the correct information.

I'm going to make a temporary change to simply append the ;type=week bit, which will fix the issue for football, and in the meantime I'll reach out to my contact at Yahoo! to see if I can get stats by date (though I will have to make sure I haven't already done that)

Just a thought, but when you are trying other sports you are adding type=date ?

jimweigandt commented 2 years ago

I'd echo what rdbaron is saying. When I started to test non-football games, I seemed to be getting stats for that week although I didn't go out of my way to manually check them...

But make sure you append something along these lines of this to the end of non-football strings so it knows that it's querying for a date instead of a week number.

type=date;date=2020-07-07

whatadewitt commented 2 years ago

Ok... I've discovered the issue and I think my inkling last night was correct...

You cannot get "week" stats for baseball players, that is, this request does not work.

https://fantasysports.yahooapis.com/fantasy/v2/player/mlb.p.10626/stats;type=week;week=12

No "player_stats" object is returned, which is causing the API to error and the response ends up as an empty object, which I should probably fix.

Here's where it gets weird... I generally only look at "current season" stuff, so when I'm playing around with things I use mlb.p.[PLAYER_KEY] instead of [GAME_KEY].p.[PLAYER_KEY]. So last night I was trying something that ended in this sort of request

https://fantasysports.yahooapis.com/fantasy/v2/player/mlb.p.10626/stats;type=date;date=2021-09-21

The player_stats object for that day was empty, because it appears that Yahoo! has something in their API spec to ensure the proper GAME_KEY is associated with the stats for a season, so if I use the 2021 MLB Game Key instead (404) I get the URL

https://fantasysports.yahooapis.com/fantasy/v2/player/404.p.10626/stats;type=date;date=2021-09-21

Which returns the proper stats. Taking it a step further, I tried

https://fantasysports.yahooapis.com/fantasy/v2/player/404.p.10626/stats;type=week;week=12

But that resulted in the same error as above.

In short: If you're looking for stats for a particular week / day you best make sure the game key used matches up with that year in the Yahoo! system.

@jimweigandt thanks for the PR but where I already have this I'm going to deny it just so I can get a quick error check in for when the player_stats object doesn't exist and append some sort of messaging indicating that the stats aren't available. It's abouti 90% of the way there so I hope to get it done today!

whatadewitt commented 2 years ago
{
  "player_key": "404.p.10626",
  "player_id": "10626",
  "name": {
    "full": "Juan Soto",
    "first": "Juan",
    "last": "Soto",
    "ascii_first": "Juan",
    "ascii_last": "Soto"
  },
  "editorial_player_key": "mlb.p.10626",
  "editorial_team_key": "mlb.t.20",
  "editorial_team_full_name": "Washington Nationals",
  "editorial_team_abbr": "Was",
  "uniform_number": "22",
  "display_position": "OF",
  "headshot": {
    "url": "https://s.yimg.com/iu/api/res/1.2/dldbYiP2r3JSxcGdQJo3Kw--~C/YXBwaWQ9eXNwb3J0cztjaD0yMzM2O2NyPTE7Y3c9MTc5MDtkeD04NTc7ZHk9MDtmaT11bGNyb3A7aD02MDtxPTEwMDt3PTQ2/https://s.yimg.com/xe/i/us/sp/v/mlb_cutout/players_l/04202021/10626.png",
    "size": "small"
  },
  "image_url": "https://s.yimg.com/iu/api/res/1.2/dldbYiP2r3JSxcGdQJo3Kw--~C/YXBwaWQ9eXNwb3J0cztjaD0yMzM2O2NyPTE7Y3c9MTc5MDtkeD04NTc7ZHk9MDtmaT11bGNyb3A7aD02MDtxPTEwMDt3PTQ2/https://s.yimg.com/xe/i/us/sp/v/mlb_cutout/players_l/04202021/10626.png",
  "is_undroppable": "0",
  "position_type": "B",
  "eligible_positions": [
    "DH",
    "LF",
    "OF",
    "RF"
  ],
  "stats": "Cannot retrieve player stats of type 'week' for game '404'"
}

It's working! I need to update the docs. This should work because for other sports you can get the weeks of the game and just make multiple requests for each of the days within that week!

Thanks for all the help on this, gang!!

whatadewitt commented 2 years ago

4.2.0 has been published to NPM

I will close this ticket later today!