npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

node-red-contrib-nba

v1.0.9

Published

Access the NBA Stats API with Node-RED

Downloads

8

Readme

node-red-contrib-nba

Access the NBA Stats API using Node-RED.

Overview

The goal of this project is to provide easy access to the robust, yet undocumented NBA Stats API using Node-RED, a visual, flow-based development tool. Whether you're looking to create an NBA database, get the latest scores and stats, peform data analysis, or build an interactive website, this tool will help you get the data you need without having to research all the endpoints of the NBA Stats API.

With the NBA and other Node-RED nodes, you can create charts like this ...

fg3m-dashboard

... with just a few lines of JavaScript:

fg3m-js-flow

... or no JavaScript at all:

fg3m-nojs-flow

Dependencies and references

This project uses the nba npm module to call the various endpoints of the NBA Stats API. Please refer to this module's GitHub for futher information regarding the API's stability and usability as well as blacklisted IP addresses.

Nodes

There are a total of 5 nba nodes. Two of the nodes require no ID:

database: get player and team ids.

league: get leaguewide data.

The other three require an ID:

player: get data in relation to a given player.

team: get data in relation to a given team.

game: get data in relation to a given game.

Database

This node has the following uses:

  • get all NBA players
  • get all NBA teams
  • get one NBA player by name
  • get one NBA player by id
  • get one NBA player by name
  • get one NBA team by name
  • get one NBA team by id
  • update the database

Each player or team object contains a unique id used by the NBA Stats API. These are required by the player and team nodes.

The first 7 functions do not make API calls. Instead, they search files of the nba npm module. The final function listed above makes an API call to update these files. They are also updated when the Node-RED application starts and imports the node-red-contrib-nba module.

Player

This node requires a player id. It will return the following information in relation to this player, based on its configuration:

  • the player's profile
  • the player's career stats
  • the player's shot chart
  • the player's season splits

Team

This node requires a team id. It will return the following information in relation to this team, based on its configuration:

  • the team's profile
  • the team's roster
  • the team's schedule
  • the team's season stats
  • the team's leaders
  • the team's shooting stats
  • the team's season splits
  • the team's lineups
  • the team's on/off details
  • the team's shot chart

League

This node does not require an id, as it provides data for the entire league. It will return the following data based on its configuration:

  • the scoreboard for a given game day
  • league leaders
  • league standings
  • game logs grouped by player or team
  • player tracking grouped by player or team
  • shooting stats grouped by player or team
  • hustle stats grouped by player or team
  • clutch stats grouped by player or team
  • lineups
  • schedule
  • playoffs bracket

Game

This node requires a game id. It will return the following information in relation to this game, based on its configuration:

  • the game's box score
  • the game's play-by-play
  • the game's shot chart
  • the game's preview article
  • the game's recap article
  • the game's lead tracker

The get a game id, use the league node to get a scoreboard for a given game day. The game id will be located in msg.payload.sports_content.games.game[i].id.

Using the nodes

Valid Parameters

The nodes dynamically load dropdown options from the nba.json file in the nba-client-template npm module. If you load inputs dynamically, please refer either to this file or the dropdown options. If you send an invalid parameter value, then you should receive an error from the NBA API explicitly saying what it accepts for the particular endpoint.

Dynamically loading inputs

Most of the node inputs can be dynamically loaded. There are two ways to do this.

Text fields

Text fields may be hardcoded or dynamically populated via mustache syntax relative to the incoming msg object. For example, if the player id is located in msg.payload.playerId, then you can fill {{payload.playerId}} in the id field.

A simple example is using the database node to get a team by name and then sending the response to a team node. Because the database node returns a team object in msg.payload with the team id in msg.payload.teamId, you can now fill in {{payload.teamId}} in the team id field of the team node.

database-to-team

team-profile-mustache

Dropdown options

Dropdown fields may be selected from the menu or dynamically populated via the specified property of the incoming msg object. For example, if loading measure type dynamically, the dropdown menu designates msg.measure_type as the specified property. When using this option, the property must be set earlier in the flow.

dynamic-dropdown

dynamic-dropdown-function

dynamic-dropdown-team

Getting id's

Use the database node to get player and team id's.

Use the league node to get a scoreboard for a given game day. The game id will be located in msg.payload.sports_content.games.game[i].id.

The cleanedData field

Some responses from the NBA Stats API return result sets that map headers, an array of strings, to rowSet, an array of arrays.

In this case, the node-red-contrib-nba module adds a cleanedData field that combines these arrays into an array of objects that might be easier to work with.

Ex result set: obj.headers = [PTS, MIN, NAME]
               obj.rowSet = [[25, 37, "James Harden"], [40, 35, "Stephen Curry"]]

Ex cleanedData = [{PTS: 25, MIN: 37, NAME: "James Harden}, 
                  {PTS: 40, MIN: 35, NAME: "Stephen Curry}]

Tests

The test directiory contains unit tests for each node using the node-red-test-helper and mocha libraries.

To run all tests:

npm test

To test the database node:

npm run test-database

To test the player node:

npm run test-player

To test the team node:

npm run test-team

To test the league node:

npm run test-league

To test the game node:

npm run test-game

Example flows

Get all players. Download the flow.

get-all-players-flow

Schedule a daily database update to get the latest player and team info. Download the flow.

update-database-flow

Develop an interactive website that displays a game day's scoreboard. Download the flow.

scoreboard-flow

scoreboard

Display a game's box score with live updates. Download the flow.

box-score-flow

box-score

Develop an interactive website that displays a team's best lineups. Download the flow.

lineups-flow

lineups

Develop an interactive website that displays the current year's standings. Download the flow.

standings-flow

standings

Display a game's play-by-play. Download the flow.

play-by-play-flow

play-by-play

Create a Node-RED dashboard that displays the top 3pt shooters in the league. Download the flow with JS or the flow without JS

fg3m-dashboard

fg3m-js-flow

fg3m-nojs-flow

Download all website flows at once with a menu toolbar

all-sites-flow