From 2861541546d1cf9c3007e5efd68a2925c4b3cbab Mon Sep 17 00:00:00 2001 From: Ravi Prasad Date: Wed, 6 Aug 2025 01:30:54 -0500 Subject: [PATCH 1/2] shot chart scrape service and import code update --- Dockerfile | 4 +- import.go | 67 +++++++++++++++++--- main.go | 27 ++++---- services/player_shot_chart_scrape_service.go | 6 ++ 4 files changed, 81 insertions(+), 23 deletions(-) diff --git a/Dockerfile b/Dockerfile index 9f0af2e..0700431 100644 --- a/Dockerfile +++ b/Dockerfile @@ -4,8 +4,8 @@ FROM golang:1.24-bullseye AS builder ENV CGO_ENABLED=1 ENV GOOS=linux # Changed from Apple Silicon arm64 config. -ENV GOARCH=amd64 -# ENV GOARCH=arm64 +# ENV GOARCH=amd64 +ENV GOARCH=arm64 WORKDIR /app diff --git a/import.go b/import.go index a38dd60..ff7d9e4 100644 --- a/import.go +++ b/import.go @@ -101,12 +101,61 @@ func importBoxScores(db *gorm.DB) { log.Printf("--- Finished Box Score Data Import ---") } -// importPlayerShotChart fetches shot-charts for every known player -// func importPlayerShotChart(db *gorm.DB) { -// const firstID = "hardeja01" -// log.Printf("▶️ importing shot chart for player %s…", firstID) -// if err := services.FetchAndStoreShotChartForPlayer(db, firstID); err != nil { -// log.Printf("shot chart import failed for %s: %v", firstID, err) -// } -// // you can add more IDs here or just rely on the API endpoint after -// } +// importPlayerShotCharts fetches shot charts for a PREDEFINED list of players for a given range of seasons. +func importPlayerShotCharts(db *gorm.DB) { + log.Println("--- Starting Player Shot Chart Import from Predefined List ---") + + // 1. Define the season range. Your service fetches from newest to oldest. + startSeason := 2025 + endSeason := 2017 // Basketball-Reference has data going back this far. + + // 2. Define the static list of player IDs to import. + // You can add or remove any Basketball-Reference player ID here. + // Examples: + // LeBron James: "jamesle01" + // Stephen Curry: "curryst01" + // Nikola Jokić: "jokicni01" + // Luka Dončić: "doncilu01" + targetPlayerIDs := []string{ + "jamesle01", + "curryst01", + "jokicni01", + "doncilu01", + "hardeja01", + "irvinky01", + "duranke01", + // "youngtr01", + // "lillada01", + // "gilgesh01", + // "brunsja01", + // "edwaran01", + // "mitchdo01", + // "bookede01", + // "derozde01", + + // Add more player IDs here as needed + } + + if len(targetPlayerIDs) == 0 { + log.Println("⚠️ Player ID list is empty. Skipping shot chart import.") + return + } + + log.Printf("Found %d players in the predefined list. Starting shot chart import for seasons %d down to %d.", len(targetPlayerIDs), startSeason, endSeason) + + // 3. Loop through each player ID and fetch their shot chart data. + for i, playerID := range targetPlayerIDs { + log.Printf("(%d/%d) Importing shot charts for player: %s", i+1, len(targetPlayerIDs), playerID) + + err := services.FetchAndStoreShotChartScrapedForPlayer(db, playerID, startSeason, endSeason) + if err != nil { + // Log the error but continue with the next player. + log.Printf("❌ Shot chart import failed for player %s: %v", playerID, err) + } + + // 4. Be a good internet citizen. Pause between scraping each player. + utils.SleepWithJitter(10000 * time.Millisecond) + } + + log.Println("--- Finished Player Shot Chart Import from Predefined List ---") +} \ No newline at end of file diff --git a/main.go b/main.go index ad09549..7ae1302 100644 --- a/main.go +++ b/main.go @@ -50,23 +50,26 @@ func main() { // Run all DB migrations + import steps exactly once db := config.InitDB(true) - importPlayerAdvanced(db) - log.Println("🎉 Player Advanced Import completed successfully") + // importPlayerAdvanced(db) + // log.Println("🎉 Player Advanced Import completed successfully") - importPlayerAdvancedPlayoffs(db) - log.Println("🎉 Player Advanced Playoffs Import completed successfully") + // importPlayerAdvancedPlayoffs(db) + // log.Println("🎉 Player Advanced Playoffs Import completed successfully") - importPlayerTotalsScrape(db) - log.Println("🎉 Player Totals (scraped) Import completed successfully") + // importPlayerTotalsScrape(db) + // log.Println("🎉 Player Totals (scraped) Import completed successfully") - importPlayerTotalsPlayoffsScrape(db) - log.Println("🎉 Player Playoffs (scraped) Import completed successfully") + // importPlayerTotalsPlayoffsScrape(db) + // log.Println("🎉 Player Playoffs (scraped) Import completed successfully") - importGameSchedules(db) - log.Println("🎉 Game Imports completed successfully 🏀") + // importGameSchedules(db) + // log.Println("🎉 Game Imports completed successfully 🏀") - importBoxScores(db) - log.Println("🎉 Related Box Score Imports completed successfully 📦") + // importBoxScores(db) + // log.Println("🎉 Related Box Score Imports completed successfully 📦") + + importPlayerShotCharts(db) + log.Println("🎉 Player Shot Chart Import completed successfully 🎯") log.Println("🏀 ALL Imports completed successfully ✅ 🙌") diff --git a/services/player_shot_chart_scrape_service.go b/services/player_shot_chart_scrape_service.go index cefa62c..5c481d9 100644 --- a/services/player_shot_chart_scrape_service.go +++ b/services/player_shot_chart_scrape_service.go @@ -15,6 +15,9 @@ import ( "github.com/nprasad2077/NBA_Go/models" "gorm.io/gorm" "gorm.io/gorm/clause" + + "github.com/nprasad2077/NBA_Go/utils" + "time" ) // FetchAndStoreShotChartScrapedForPlayer scrapes the shot chart pages on @@ -48,6 +51,7 @@ func FetchAndStoreShotChartScrapedForPlayer( resp.Body.Close() // This is not an error, just means the player might not have data for that season. log.Printf("⚠️ Skipping season %d for player %s (Status: %s)", season, playerID, resp.Status) + utils.SleepWithJitter(2500 * time.Millisecond) continue } bodyBytes, err := io.ReadAll(resp.Body) @@ -193,6 +197,8 @@ func FetchAndStoreShotChartScrapedForPlayer( log.Printf("No shots found to import for player %s in season %d.", playerID, season) } // --- BATCHING LOGIC END --- + log.Printf("Pausing before next season...") + utils.SleepWithJitter(3500 * time.Millisecond) // Adjust duration as needed } return nil } From b5ea8d4e4b864211db744ccfcf14fd7325a63eb0 Mon Sep 17 00:00:00 2001 From: Ravi Prasad Date: Wed, 6 Aug 2025 01:31:17 -0500 Subject: [PATCH 2/2] amd64 --- Dockerfile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index 0700431..9f0af2e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -4,8 +4,8 @@ FROM golang:1.24-bullseye AS builder ENV CGO_ENABLED=1 ENV GOOS=linux # Changed from Apple Silicon arm64 config. -# ENV GOARCH=amd64 -ENV GOARCH=arm64 +ENV GOARCH=amd64 +# ENV GOARCH=arm64 WORKDIR /app