A tool to generate json and ics files for the sports I care about.
  • Python 99.5%
  • Shell 0.5%
Find a file
Sean Graham a01018b974 Update men's hockey medal round matchups from TBD
Bronze: Finland vs Slovakia, Gold: USA vs Canada

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-02-20 22:32:04 -05:00
.vscode added workspace files 2025-09-26 12:02:56 -04:00
.gitignore Implement caching for past games in sports schedule processing. Introduce 'omit_past_json' configuration to filter past events from JSON output. Update relevant classes and methods to support caching and filtering, enhancing performance and flexibility in event management. 2025-10-26 23:54:47 -04:00
CLAUDE.md Add Olympics knockout rounds and fix MLB test data 2026-02-06 16:09:30 -05:00
config.yaml Add HTML output format with mobile-first compact cards 2026-02-11 22:03:48 -05:00
cron-generate.sh updated cron script 2025-09-26 15:41:50 +00:00
environment.yaml initial import 2025-09-26 11:17:53 -04:00
generate_mlb_teams.py initial import 2025-09-26 11:17:53 -04:00
generate_sports_schedule.py Add HTML output format with mobile-first compact cards 2026-02-11 22:03:48 -05:00
LICENSE initial import 2025-09-26 11:17:53 -04:00
milano-cortina-2026-complete-schedule.ics Add 2026 Winter Olympics ice hockey schedule support 2026-02-04 09:21:57 -05:00
mlb_teams.json initial import 2025-09-26 11:17:53 -04:00
olympics_hockey_2026.json Update men's hockey medal round matchups from TBD 2026-02-20 22:32:04 -05:00
package-lock.json initial import 2025-09-26 11:17:53 -04:00
pwhl_teams.json Refactor PWHL team processing to use external JSON file for team nicknames and update season ID in configuration. Added error handling for team data loading and improved logging for team processing. 2025-10-01 10:54:17 -04:00
README.md initial import 2025-09-26 11:17:53 -04:00
requirements.txt initial import 2025-09-26 11:17:53 -04:00
test_generate_sports_schedule.py Remove date, time, url fields from JSON output; use epoch everywhere 2026-02-11 18:48:48 -05:00

sports-schedule

Description

A Python tool to fetch sports schedules directly from APIs. Supports MLB, NHL, and PWHL team schedules with JSON output format.

Features

  • Fetch NHL game schedules directly from the NHL API (configurable date range, default 28 days)
  • Fetch MLB game schedules directly from the MLB API (configurable date range, default 28 days)
  • Fetch PWHL game schedules directly from the PWHL API
  • Process MLB team names using team codes
  • Output JSON format for easy integration
  • Configurable timezone support
  • Configuration file support for easy team and API management
  • Sport enable/disable controls - turn individual sports on/off
  • Configurable PWHL season_id for when seasons change
  • Automatic deduplication to prevent duplicate events
  • Organized configuration structure with all sport settings grouped together

Configuration

The script supports a YAML configuration file (config.yaml) for easy management of teams and settings. The configuration is organized with all sport-specific settings grouped together for better maintainability.

Key configuration options:

  • Sport Enable/Disable: Control which sports to process (MLB, NHL, PWHL)
  • Teams: Pre-configure teams for each sport
  • API Settings: Configure base URLs, keys, and date ranges per sport
  • Filtering: Control which game statuses to skip per sport
  • Output Settings: Default file names, timezone, and iCal options
  • Global Settings: Logging and global filtering options

Command line arguments take precedence over configuration file values.

Usage

python generate_sports_schedule.py \
  --nhl-teams team1 team2 \
  --mlb-teams team1 team2 \
  --pwhl-teams team1 team2 \
  --teams-file mlb_teams.json \
  --output-json sports.json \
  --verbose

Examples

Note: Sports are enabled/disabled in the configuration file using sports.{sport}.enabled. Command line arguments only override team lists and other settings.

Override teams for Bruins games (NHL must be enabled in config)

python generate_sports_schedule.py --nhl-teams Bruins --output-json bruins.json

Override teams for Red Sox games (MLB must be enabled in config)

python generate_sports_schedule.py --mlb-teams "Boston Red Sox" --output-json redsox.json

Override teams for multiple sports (both NHL and MLB must be enabled in config)

python generate_sports_schedule.py --nhl-teams Bruins --mlb-teams "Boston Red Sox" --output-json boston_sports.json

Override teams for all three sports (all must be enabled in config)

python generate_sports_schedule.py --nhl-teams Bruins --mlb-teams "Boston Red Sox" --pwhl-teams "Boston Fleet" --output-json all_boston_sports.json

Using configuration file

# With teams pre-configured in config.yaml
python generate_sports_schedule.py --output-json my_sports.json

# Override specific config values
python generate_sports_schedule.py --config my_config.yaml --pwhl-teams "Boston Fleet"

Disabling sports in configuration

You can disable individual sports by setting their enabled flag to false:

sports:
  mlb:
    enabled: false  # This will disable MLB processing
  nhl:
    enabled: true   # NHL will still be processed
  pwhl:
    enabled: true   # PWHL will still be processed

When a sport is disabled, the script will log that it's skipping that sport and only process the enabled ones.

Output Formats

JSON Output

The tool generates a JSON file with this structure:

{
  "tasks": [
    {
      "name": "Bruins at Rangers - 19:00",
      "date": "2025-09-23",
      "time": "19:00",
      "location": "Madison Square Garden",
      "url": "https://www.nhl.com/gamecenter/2025010018",
      "source": "nhl_api"
    }
  ]
}

MLB Teams File Format

The mlb_teams.json file contains detailed team information in this format:

{
  "Boston Red Sox": {
    "teamName": "Red Sox",
    "shortName": "Boston",
    "clubName": "Red Sox",
    "locationName": "Boston",
    "abbreviation": "BOS",
    "id": 111
  }
}

Configuration File Structure

The configuration file uses YAML format with a clean, organized structure where all sport-specific settings are grouped together:

# Output settings
output:
  json_file: "sports.json"
  timezone: "America/New_York"
  ical:
    enabled: true  # Set to true to enable iCal output
    file: "sports.ics"
    calendar_name: "Sports Schedule"
    calendar_description: "Sports games and events"

# Sport configurations - each sport has all its settings grouped together
sports:
  mlb:
    enabled: true  # Set to false to disable MLB processing
    teams: ["Boston Red Sox"]
    teams_file: "mlb_teams.json"  # JSON file for team code mapping
    api:
      base_url: "http://statsapi.mlb.com/api/v1"
      date_range_days: 28  # Default days to look ahead
    filtering:
      skip_statuses: ["Final", "Game Over"]
  
  nhl:
    enabled: true  # Set to false to disable NHL processing
    teams: ["Bruins", "Oilers", "Jets", "Canadiens"]
    api:
      base_url: "https://api-web.nhle.com/v1"
      date_range_days: 28  # Default days to look ahead
    filtering:
      skip_statuses: []  # NHL doesn't have specific skip statuses
  
  pwhl:
    enabled: true  # Set to false to disable PWHL processing
    teams: ["Boston Fleet"]
    api:
      season_id: "5"  # Current PWHL season - update this when it changes!
      base_url: "https://lscluster.hockeytech.com/feed"
      key: "446521baf8c38984"
      client_code: "pwhl"
    filtering:
      skip_statuses: ["Final", "Final OT", "Final SO"]

# Global settings
global:
  filtering:
    skip_completed_games: true  # Skip completed games across all sports
  
  logging:
    level: "INFO"  # DEBUG, INFO, WARNING, ERROR
    verbose: false

Requirements

Python Version

  • Python 3.9+ (required for zoneinfo module)

Dependencies

Install dependencies using one of these methods:

Option 1: Using pip

pip install -r requirements.txt

Option 2: Using conda

conda env create -f environment.yaml
conda activate sports-schedule

Required Packages

  • requests>=2.25.0 - For HTTP requests to sports APIs
  • PyYAML>=6.0 - For parsing configuration files
  • icalendar>=5.0.0 - For generating iCal calendar files
  • zoneinfo - For timezone handling (built into Python 3.9+)

Untested Note: If using Python < 3.9, you'll need to install backports.zoneinfo:

pip install backports.zoneinfo

Utilities

Generate MLB Teams File

The generate_mlb_teams.py script creates an updated mlb_teams.json file from the MLB API teams data:

python generate_mlb_teams.py

This script:

  • Reads teams.json (from MLB API)
  • Filters for active Major League Baseball teams
  • Creates mlb_teams.json with detailed team information
  • Supports both old and new file formats automatically

License

MIT