nbacli

repository·master·Indexed 11 days ago

https://github.com/dylantientcheu/nbacli

An unofficial command line interface for NBA games featuring a terminal-based UI to fetch live scores, game statistics, and conference standings. It includes a GoLang client for interacting with the NBA stats API, supporting gzip decompression and specific header requirements to avoid being blocked.

Tokens
1.7K
Snippets
10
Records
11
Agent score
48%

What's inside nbacli

  1. Install nbacli via Binaries, Source, or Homebrew

    master

    You can install nbacli using one of the following methods:

    Binaries

    Download the latest version for Linux, macOS, or Windows from the releases page.

    From Source

    Clone the repository and use go install. Ensure that $GOPATH/bin is included in your PATH environment variable.

    Homebrew

    Use the following commands to tap the repository and install via Homebrew:

    brew tap dylantientcheu/dylantientcheu && brew install nbacli
    git clone https://github.com/dylantientcheu/nbacli.git && cd nbacli
    # install
    go install
    # run
    nbacli games
  2. Get NBA game scores and stats

    master

    Use the games command to view the latest NBA scores.

    Navigation:

    • Use the <kbd>↑</kbd> and <kbd>↓</kbd> arrow keys to navigate through the list of games.
    • Press <kbd>Enter</kbd> on a specific game to view its detailed stats.

    Date Options:

    • --yesterday: Get scores from the previous day.
    • --tomorrow: Get scores for the following day.
    • --date YYYYMMDD: Get scores for a specific date in YYYYMMDD format.
    # Get the latest scores
    nbacli games
    
    # Get the stats for the day before
    nbacli games --yesterday
    
    # Get the stats for the day after
    nbacli games --tomorrow
    
    # Get the stats for a specific date
    nbacli games --date YYYYMMDD
  3. Execute requests and unpack responses with Client.Do()

    master

    The Do(req *http.Request) method sends an HTTP request to the configured BaseURL and handles the response lifecycle. It automatically:

    1. Checks if the status code is http.StatusOK (200).
    2. Reads the response body.
    3. Unpacks the response using gzip decompression.

    It returns the raw []byte of the decompressed JSON response or an error if the request fails, the status code is non-200, or decompression fails.

    // Assuming client is an initialized *nag.Client
    req, err := http.NewRequest("GET", client.BaseURL.String()+"/endpoint", nil)
    if err != nil {
        return err
    }
    
    // Add necessary headers (see DefaultStatsHeader for reference)
    req.Header = DefaultStatsHeader
    
    data, err := client.Do(req)
    if err != nil {
        return err
    }
    // 'data' now contains the decompressed response body
  4. Initialize a new NBA stats client with NewDefaultClient()

    master

    Use NewDefaultClient() to create a *Client instance configured with the default NBA stats base URL (https://stats.nba.com/stats) and the standard library's http.DefaultClient. This is the recommended way to start making requests to the NBA stats API.

    import "github.com/dylantientcheu/dylantientcheu/nbacli/nag"
    
    client := nag.NewDefaultClient()
  5. Use flags with the 'games' command

    master

    The games command supports the following flags to control the date of the schedule retrieved:

    FlagShorthandDescription
    --date-dDate to get the schedule for in YYYYMMDD format
    --yesterday-yGet yesterday's games
    --tomorrow-tGet tomorrow's games

    Constraint: The flags --yesterday, --tomorrow, and --date are mutually exclusive.

  6. Use DefaultStatsHeader for NBA API requests

    master

    The NBA stats API requires specific headers to avoid being blocked. When constructing your *http.Request, you should populate the Header field with the values provided in nag.DefaultStatsHeader.

    // Example of applying the default headers to a request
    req.Header = nag.DefaultStatsHeader
  7. Reference: DefaultStatsHeader keys

    master

    The following headers are used by the nag client to mimic a browser and satisfy NBA stats API requirements:

    {
    	"Host":               ["stats.nba.com"],
    	"Referer":            ["https://stats.nba.com"],
    	"User-Agent":         ["Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:72.0) Gecko/20100101 Firefox/72.0"],
    	"Connection":         ["keep-alive"],
    	"Pragma":             ["no-cache"],
    	"Cache-Control":      ["no-cache"],
    	"Accept":             ["application/json", "text/plain", "*/*"],
    	"Accept-Encoding":    ["gzip", "deflate", "br"],
    	"Accept-Language":    ["en-US,en;q=0.9"],
    	"x-nba-stats-origin": ["stats"],
    	"x-nba-stats-token":  ["true"],
    }
  8. Use the nbacli CLI

    master

    The nbacli command is the entrypoint for the NBA GoLang CLI client. It is used to interact with NBA data via the command line. To run the CLI, you must call the Execute() function from the main package, which initializes the root command and its subcommands.

    # Basic invocation
    nbacli
  9. Get NBA game schedules with the 'games' command

    master

    Use the games command to view the NBA schedule for a specific date. By default, if no flags are provided, it shows today's games. You can specify a specific date, or use convenience flags for yesterday or tomorrow.

    Note that --yesterday, --tomorrow, and --date are mutually exclusive; you can only use one of them at a time.

    # Get today's games
    nbacli games
    
    # Get games for a specific date (YYYYMMDD)
    nbacli games --date 20231225
    
    # Get yesterday's games
    nbacli games --yesterday
    
    # Get tomorrow's games
    nbacli games --tomorrow