mirror of
https://github.com/nprasad2077/NBA_Go.git
synced 2026-09-22 14:05:13 +00:00
shot chart scrape service and import code update
This commit is contained in:
+2
-2
@@ -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
|
||||
|
||||
|
||||
@@ -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 ---")
|
||||
}
|
||||
@@ -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 ✅ 🙌")
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user