Merge pull request #38 from nprasad2077/games

Games
This commit is contained in:
2025-08-06 01:34:14 -05:00
committed by GitHub
3 changed files with 79 additions and 21 deletions
+58 -9
View File
@@ -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 ---")
}
+15 -12
View File
@@ -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
} }