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 CGO_ENABLED=1
|
||||||
ENV GOOS=linux
|
ENV GOOS=linux
|
||||||
# Changed from Apple Silicon arm64 config.
|
# Changed from Apple Silicon arm64 config.
|
||||||
ENV GOARCH=amd64
|
# ENV GOARCH=amd64
|
||||||
# ENV GOARCH=arm64
|
ENV GOARCH=arm64
|
||||||
|
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
|
|
||||||
|
|||||||
@@ -101,12 +101,61 @@ func importBoxScores(db *gorm.DB) {
|
|||||||
log.Printf("--- Finished Box Score Data Import ---")
|
log.Printf("--- Finished Box Score Data Import ---")
|
||||||
}
|
}
|
||||||
|
|
||||||
// importPlayerShotChart fetches shot-charts for every known player
|
// importPlayerShotCharts fetches shot charts for a PREDEFINED list of players for a given range of seasons.
|
||||||
// func importPlayerShotChart(db *gorm.DB) {
|
func importPlayerShotCharts(db *gorm.DB) {
|
||||||
// const firstID = "hardeja01"
|
log.Println("--- Starting Player Shot Chart Import from Predefined List ---")
|
||||||
// log.Printf("▶️ importing shot chart for player %s…", firstID)
|
|
||||||
// if err := services.FetchAndStoreShotChartForPlayer(db, firstID); err != nil {
|
// 1. Define the season range. Your service fetches from newest to oldest.
|
||||||
// log.Printf("shot chart import failed for %s: %v", firstID, err)
|
startSeason := 2025
|
||||||
// }
|
endSeason := 2017 // Basketball-Reference has data going back this far.
|
||||||
// // you can add more IDs here or just rely on the API endpoint after
|
|
||||||
// }
|
// 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
|
// Run all DB migrations + import steps exactly once
|
||||||
db := config.InitDB(true)
|
db := config.InitDB(true)
|
||||||
|
|
||||||
importPlayerAdvanced(db)
|
// importPlayerAdvanced(db)
|
||||||
log.Println("🎉 Player Advanced Import completed successfully")
|
// log.Println("🎉 Player Advanced Import completed successfully")
|
||||||
|
|
||||||
importPlayerAdvancedPlayoffs(db)
|
// importPlayerAdvancedPlayoffs(db)
|
||||||
log.Println("🎉 Player Advanced Playoffs Import completed successfully")
|
// log.Println("🎉 Player Advanced Playoffs Import completed successfully")
|
||||||
|
|
||||||
importPlayerTotalsScrape(db)
|
// importPlayerTotalsScrape(db)
|
||||||
log.Println("🎉 Player Totals (scraped) Import completed successfully")
|
// log.Println("🎉 Player Totals (scraped) Import completed successfully")
|
||||||
|
|
||||||
importPlayerTotalsPlayoffsScrape(db)
|
// importPlayerTotalsPlayoffsScrape(db)
|
||||||
log.Println("🎉 Player Playoffs (scraped) Import completed successfully")
|
// log.Println("🎉 Player Playoffs (scraped) Import completed successfully")
|
||||||
|
|
||||||
importGameSchedules(db)
|
// importGameSchedules(db)
|
||||||
log.Println("🎉 Game Imports completed successfully 🏀")
|
// log.Println("🎉 Game Imports completed successfully 🏀")
|
||||||
|
|
||||||
importBoxScores(db)
|
// importBoxScores(db)
|
||||||
log.Println("🎉 Related Box Score Imports completed successfully 📦")
|
// log.Println("🎉 Related Box Score Imports completed successfully 📦")
|
||||||
|
|
||||||
|
importPlayerShotCharts(db)
|
||||||
|
log.Println("🎉 Player Shot Chart Import completed successfully 🎯")
|
||||||
|
|
||||||
|
|
||||||
log.Println("🏀 ALL Imports completed successfully ✅ 🙌")
|
log.Println("🏀 ALL Imports completed successfully ✅ 🙌")
|
||||||
|
|||||||
@@ -15,6 +15,9 @@ import (
|
|||||||
"github.com/nprasad2077/NBA_Go/models"
|
"github.com/nprasad2077/NBA_Go/models"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
"gorm.io/gorm/clause"
|
"gorm.io/gorm/clause"
|
||||||
|
|
||||||
|
"github.com/nprasad2077/NBA_Go/utils"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
// FetchAndStoreShotChartScrapedForPlayer scrapes the shot chart pages on
|
// FetchAndStoreShotChartScrapedForPlayer scrapes the shot chart pages on
|
||||||
@@ -48,6 +51,7 @@ func FetchAndStoreShotChartScrapedForPlayer(
|
|||||||
resp.Body.Close()
|
resp.Body.Close()
|
||||||
// This is not an error, just means the player might not have data for that season.
|
// 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)
|
log.Printf("⚠️ Skipping season %d for player %s (Status: %s)", season, playerID, resp.Status)
|
||||||
|
utils.SleepWithJitter(2500 * time.Millisecond)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
bodyBytes, err := io.ReadAll(resp.Body)
|
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)
|
log.Printf("No shots found to import for player %s in season %d.", playerID, season)
|
||||||
}
|
}
|
||||||
// --- BATCHING LOGIC END ---
|
// --- BATCHING LOGIC END ---
|
||||||
|
log.Printf("Pausing before next season...")
|
||||||
|
utils.SleepWithJitter(3500 * time.Millisecond) // Adjust duration as needed
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user