bttmly / nba

Node.js client for nba.com API endpoints
MIT License
710 stars 180 forks source link

WNBA? #88

Closed alexanderadavid closed 4 years ago

alexanderadavid commented 4 years ago

It seems like the WNBA uses the same or very similar infrastructure as the NBA stats API:

https://stats.wnba.com/

Have you thought about building some abstraction to fit WNBA alongside NBA or building a new repo alltogether? I would be interested in taking a crack at it.

bttmly commented 4 years ago

Huh! I hadn't considered that. Do you have some examples of full URLs for WNBA API requests?

alexanderadavid commented 4 years ago

Whatever powers stats.nba.com must also power stats.wnba.com. From what I've seen all requests and UI are the same.

The first request when visiting Doncic's player dashboard at https://stats.nba.com/player/1629029/

https://stats.nba.com/stats/playerdashboardbyyearoveryear?DateFrom=&DateTo=&GameSegment=&LastNGames=0&LeagueID=00&Location=&MeasureType=Base&Month=0&OpponentTeamID=0&Outcome=&PORound=0&PaceAdjust=N&PerMode=PerGame&Period=0&PlayerID=1629029&PlusMinus=N&Rank=N&Season=2019-20&SeasonSegment=&SeasonType=Regular+Season&ShotClockRange=&Split=yoy&VsConference=&VsDivision=

and the first request when visiting Jordin Canada's dashboard at https://stats.wnba.com/player/1628886/

https://stats.wnba.com/stats/playerdashboardbyyearoveryear?DateFrom=&DateTo=&GameSegment=&LastNGames=0&LeagueID=10&Location=&MeasureType=Base&Month=0&OpponentTeamID=0&Outcome=&PORound=0&PaceAdjust=N&PerMode=PerGame&Period=0&PlayerID=1628886&PlusMinus=N&Rank=N&Season=2019&SeasonSegment=&SeasonType=Regular+Season&ShotClockRange=&Split=yoy&VsConference=&VsDivision=

Here is a play by play request for an nba game https://stats.nba.com/game/0021900821/playbyplay/

https://stats.nba.com/stats/playbyplayv2?EndPeriod=10&EndRange=55800&GameID=0021900821&RangeType=2&Season=2019-20&SeasonType=Regular+Season&StartPeriod=1&StartRange=0

and for a wnba game https://stats.wnba.com/game/1021900187/playbyplay/

https://stats.wnba.com/stats/playbyplayv2?EndPeriod=10&EndRange=55800&GameID=1021900187&RangeType=2&Season=2019-20&SeasonType=Regular+Season&StartPeriod=1&StartRange=0

At a glance, it might be as easy as finding an elegant way to fit a "w" into the url.

brandly commented 4 years ago

if it's just a matter of hitting a different domain, this should be very easy to implement. the wnba package is available.

maybe this is primarily an issue of how to manage two packages. i know Lerna exists, but i don't know much past that. it might be overkill in this situation.

bttmly commented 4 years ago

I was planning to just add a configurable domain parameter. This would also help in cases where you want to run a proxy server e.g. to get around CORS restrictions or what-have-you.

bttmly commented 4 years ago

FWIW you can do this with the library as-is – you can swap out the transport layer like so

const nba = require("nba");
const getJSON = require("nba/src/get-json");

const transport = (url, params, options) => {
  // simply swap the host and then defer the rest to the built in getJSON function
  const fixedURL = url.replace("stats.nba.com", "stats.wnba.com");
  return getJSON(fixedURL, params, options);
};

// create a new stats client here with our WNBA transport
const wnbaStats = nba.stats.withTransport(transport);

(async () => {
  const result = await wnbaStats.playerInfo({ PlayerID: "1628886" });
  console.log(result);
})();

Just tested this and it works. The signature of a transport function is url, query params as object, and a flexible options object as the optional final argument. One gotcha is that NBA.com requires some headers to be present, you can copy the ones I use from there https://github.com/bttmly/nba/blob/master/src/get-json.js#L5-L14

alexanderadavid commented 4 years ago

withTransport() is sufficient for my needs. Thanks for the help!

btw - it looks like the G League is the same story: https://stats.gleague.nba.com

brandly commented 4 years ago

Very cool. Might be worth adding these details to the readme.

bttmly commented 4 years ago

Added!