Skip to main content

A comprehensive Python client for accessing NHL statistics and data

Project description

NHL Stats API Client

A comprehensive Python client for accessing NHL statistics and data through various official NHL APIs. This library provides easy access to player stats, team information, schedules, standings, and more.

Features

  • 🏒 Player Statistics: Access comprehensive player stats including scoring, real-time, power play, penalty kill, and faceoff data
  • 🥅 Goalie Statistics: Retrieve goalie stats, advanced metrics, and shootout performance
  • 🏆 Team Data: Get team statistics, power play/penalty kill data, and faceoff percentages
  • 📅 Schedule & Standings: Access current schedules, standings, and game information
  • 🎯 Advanced Analytics: Built-in support for advanced statistical analysis
  • 📊 Data Export: Export data to CSV and JSON formats
  • 🚀 Easy to Use: Simple, intuitive API with comprehensive error handling

Installation

From PyPI

pip install nhl-stats-api-client

From Source

git clone https://github.com/liahimratman/nhl-api-client.git
cd nhl-api-client
pip install -e .

Requirements

  • Python 3.8+
  • pandas
  • requests

Quick Start

from nhl_api_client import NHLAPIClient

# Initialize the client
client = NHLAPIClient()

# Get all NHL teams
teams = client.get_teams()
print(f"Found {len(teams['data'])} teams")

# Get player stats for Connor McDavid (2023-24 season)
player_stats = client.get_player_season_stats(8478402, "20232024")
print(f"Goals: {player_stats['goals']}, Assists: {player_stats['assists']}")

# Get current standings
standings = client.get_standings_now()

# Get today's schedule
schedule = client.get_schedule_now()

# Export top scorers to CSV
top_scorers = client.get_top_scorers("20232024", limit=50)
top_scorers.to_csv("top_scorers.csv", index=False)

API Reference

Team Information

# Get all NHL teams
teams = client.get_teams()
print(f"Teams: {teams['data']}")

# Get team roster
roster = client.get_team_roster("TOR")  # Toronto Maple Leafs

Player Statistics

Individual Player Methods

# Get basic season stats for a player
stats = client.get_player_season_stats(player_id, season_id)

# Get real-time stats (hits, blocks, giveaways, etc.)
realtime = client.get_player_realtime_stats(player_id, season_id)

# Get biographical information
bio = client.get_player_bios(player_id)

# Get power play statistics
pp_stats = client.get_player_powerplay_stats(player_id, season_id)

# Get penalty statistics
penalty_stats = client.get_player_penalty_stats(player_id, season_id)

# Get faceoff statistics
faceoff_stats = client.get_player_faceoff_stats(player_id, season_id)

# Get penalty kill statistics
penaltykill_stats = client.get_player_penaltykill_stats(player_id, season_id)

# Get shootout statistics
shootout_stats = client.get_player_shootout_stats(player_id, season_id)

# Get time on ice statistics
timeonice_stats = client.get_player_timeonice_stats(player_id, season_id)

# Get comprehensive stats from multiple endpoints
all_stats = client.get_comprehensive_player_stats(player_id, season_id)

Bulk Player Data Methods

# Get top scorers
top_scorers = client.get_top_scorers(season_id, limit=100)

# Get skater stats by category
summary_stats = client.get_skater_stats_by_season(season_id, limit=500)
realtime_stats = client.get_skater_realtime_stats(season_id, limit=500)
powerplay_stats = client.get_skater_powerplay_stats(season_id, limit=500)
penaltykill_stats = client.get_skater_penaltykill_stats(season_id, limit=500)
penalty_stats = client.get_skater_penalty_stats(season_id, limit=500)
faceoff_stats = client.get_skater_faceoff_stats(season_id, limit=500)
shootout_stats = client.get_skater_shootout_stats(season_id, limit=500)
timeonice_stats = client.get_skater_timeonice_stats(season_id, limit=500)
bios_stats = client.get_skater_bios(season_id, limit=500)

Goalie Statistics

# Get goalie summary stats
goalie_stats = client.get_goalie_stats(season_id, limit=100)

# Get advanced goalie stats
advanced_stats = client.get_goalie_advanced_stats(season_id, limit=100)

# Get goalie biographical information
goalie_bios = client.get_goalie_bios(season_id, limit=100)

# Get shootout statistics
shootout_stats = client.get_goalie_shootout_stats(season_id, limit=100)

# Get starts and wins data
starts_stats = client.get_goalie_starts_stats(season_id, limit=100)

Team Statistics

# Get team summary statistics
team_stats = client.get_team_stats(season_id)

# Get team power play stats
pp_stats = client.get_team_powerplay_stats(season_id)

# Get team penalty kill stats
pk_stats = client.get_team_penaltykill_stats(season_id)

# Get team faceoff percentages
faceoff_stats = client.get_team_faceoff_percentages(season_id)

# Get team real-time stats
realtime_stats = client.get_team_realtime_stats(season_id)

# Get penalty statistics
penalty_stats = client.get_team_penalty_stats(season_id)

Schedule and Standings

# Get current standings
standings = client.get_standings_now()

# Get today's schedule
today_schedule = client.get_schedule_now()

# Get schedule for a specific date
date_schedule = client.get_schedule_by_date("2024-01-15")

# Get team's full season schedule
team_schedule = client.get_club_schedule("TOR")  # Toronto Maple Leafs

Game Data

# Get game center data
game_data = client.get_gamecenter(game_id)

# Get play-by-play data
pbp_data = client.get_play_by_play(game_id)

# Get player game logs
game_log = client.get_player_game_log(player_id, season_id)

Usage Examples

Example 1: Player Analysis

from nhl_api_client import NHLAPIClient

client = NHLAPIClient()

# Analyze Connor McDavid's 2023-24 season
mcdavid_id = 8478402
season = "20232024"

# Get comprehensive stats
stats = client.get_comprehensive_player_stats(mcdavid_id, season)

print(f"Goals: {stats['summary']['goals']}")
print(f"Assists: {stats['summary']['assists']}")
print(f"Points: {stats['summary']['points']}")
print(f"Hits: {stats['realtime']['hits']}")
print(f"Power Play Goals: {stats['powerplay']['powerPlayGoals']}")
print(f"Penalty Kill Time: {stats['penaltykill']['timeOnIcePerGame']}")
print(f"Shootout Attempts: {stats['shootout']['shootoutAttempts']}")
print(f"Time on Ice: {stats['timeonice']['timeOnIcePerGame']}")

Example 2: Team Comparison

# Compare team power play effectiveness
pp_stats = client.get_team_powerplay_stats("20232024")

# Sort by power play percentage
pp_stats_sorted = pp_stats.sort_values('powerPlayPct', ascending=False)

print("Top 5 Power Play Teams:")
print(pp_stats_sorted[['teamFullName', 'powerPlayPct', 'powerPlayGoals']].head())

Example 3: Export Data

# Export comprehensive player data to CSV and JSON
import pandas as pd

# Get various stats
summary = client.get_skater_stats_by_season("20232024", limit=500)
realtime = client.get_skater_realtime_stats("20232024", limit=500)
powerplay = client.get_skater_powerplay_stats("20232024", limit=500)

# Export to CSV
summary.to_csv('summary_stats.csv', index=False)
realtime.to_csv('realtime_stats.csv', index=False)
powerplay.to_csv('powerplay_stats.csv', index=False)

# Export to JSON
summary.to_json('summary_stats.json', orient='records')

Example 4: Schedule Analysis

# Analyze today's games
schedule = client.get_schedule_now()

if schedule and 'gameWeek' in schedule:
    for day in schedule['gameWeek']:
        if day.get('games'):
            for game in day['games']:
                away = game['awayTeam']['placeName']['default']
                home = game['homeTeam']['placeName']['default']
                print(f"{away} @ {home}")

Example 5: Team Roster Analysis

# Get all teams and their rosters
teams = client.get_teams()

for team in teams['data']:
    team_abbrev = team['triCode']
    team_name = team['fullName']
    
    print(f"\n{team_name} ({team_abbrev}) Roster:")
    roster = client.get_team_roster(team_abbrev)
    
    # Print forwards
    if 'forwards' in roster:
        print("  Forwards:")
        for player in roster['forwards'][:5]:  # First 5 forwards
            print(f"    {player['firstName']['default']} {player['lastName']['default']}")

Season ID Format

Season IDs follow the format YYYYYYYY where the first four digits are the start year and the last four are the end year:

  • 20232024 = 2023-24 season
  • 20222023 = 2022-23 season
  • 20212022 = 2021-22 season

Player ID

Player IDs are unique NHL identifiers. You can find them:

  1. From the NHL website URLs
  2. Using the roster endpoints
  3. From existing data exports

Example: Connor McDavid's ID is 8478402

Error Handling

The client includes comprehensive error handling:

try:
    stats = client.get_player_season_stats(player_id, season_id)
except Exception as e:
    print(f"Error retrieving stats: {e}")

Rate Limiting

The client respects NHL API rate limits. If you're making many requests, consider:

  • Adding delays between requests
  • Using bulk endpoints when available
  • Caching frequently accessed data

Contributing

Contributions are welcome! Please feel free to submit a Pull Request. Make sure to:

  1. Add tests for new functionality
  2. Update documentation
  3. Follow existing code style
  4. Add examples for new features

License

This project is licensed under the MIT License - see the LICENSE file for details.

Disclaimer

This library is not officially affiliated with the NHL. It provides access to publicly available NHL data through their APIs. Please use responsibly and in accordance with NHL's terms of service.

Changelog

Version 1.0.2

  • Removed unnecessary main() function from library
  • Fixed build process with correct configuration files
  • Clean, reliable build process established
  • Updated package name to nhl-stats-api-client
  • Removed openpyxl dependency (no longer needed)

Version 1.0.1

  • Updated dependencies and configuration

Version 1.0.0

  • Initial release
  • Complete player statistics support
  • Goalie statistics
  • Team statistics
  • Schedule and standings
  • Data export functionality
  • Comprehensive examples

Support

If you encounter any issues or have questions:

  1. Check the examples directory
  2. Open an issue on GitHub: https://github.com/liahimratman/nhl-api-client/issues
  3. Read the API documentation above

Acknowledgments

  • NHL for providing public APIs
  • The Python community for excellent libraries
  • Contributors to the NHL API documentation project

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

nhl_stats_api_client-1.0.3.tar.gz (22.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

nhl_stats_api_client-1.0.3-py3-none-any.whl (10.7 kB view details)

Uploaded Python 3

File details

Details for the file nhl_stats_api_client-1.0.3.tar.gz.

File metadata

  • Download URL: nhl_stats_api_client-1.0.3.tar.gz
  • Upload date:
  • Size: 22.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.11

File hashes

Hashes for nhl_stats_api_client-1.0.3.tar.gz
Algorithm Hash digest
SHA256 282ebdd297c96b167f6c497e7462bba67758a7c2571a96d57962d4fca664ec21
MD5 1b2e62c83ddd7a2fd5720bf5c91dcd57
BLAKE2b-256 e640d61cf3c4a9aa33b6a64e2f0a50d280200f3f587314380167a7fd82130eff

See more details on using hashes here.

File details

Details for the file nhl_stats_api_client-1.0.3-py3-none-any.whl.

File metadata

File hashes

Hashes for nhl_stats_api_client-1.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 87adddd73597a8a014b2805ca5a2d4f8199021573cff3fae7b7c7b6a9e97498b
MD5 d67c4a2feb6f9d579dba10bfc7d7d7bf
BLAKE2b-256 3d56ed146b3cabb498272a50d3f5574c6bcb3919388bb611aebdb69f5f9f55bc

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page