From 47ea78901a24bff3c388ae8b8ca02fd5eedf7219 Mon Sep 17 00:00:00 2001 From: Ravi Prasad Date: Thu, 29 May 2025 00:29:00 -0500 Subject: [PATCH] advanced scraping usage works --- import.go | 10 +++++----- main.go | 5 ++++- utils/sleep.go | 23 +++++++++++++++++++++++ 3 files changed, 32 insertions(+), 6 deletions(-) create mode 100644 utils/sleep.go diff --git a/import.go b/import.go index c6592e1..39235af 100644 --- a/import.go +++ b/import.go @@ -11,7 +11,7 @@ import ( // importPlayerAdvanced fetches and stores advanced stats for seasons 2023–2025 func importPlayerAdvanced(db *gorm.DB) { for season := 2023; season <= 2025; season++ { - if err := services.FetchAndStorePlayerAdvancedStats(db, season, false); err != nil { + if err := services.FetchAndStorePlayerAdvancedScrapedStats(db, season, false); err != nil { log.Printf("advanced import failed for %d: %v", season, err) } time.Sleep(1100 * time.Millisecond) @@ -21,8 +21,8 @@ func importPlayerAdvanced(db *gorm.DB) { // importPlayerAdvancedPlayoffs fetches and stores advanced stats for playoffs seasons 2023–2025 func importPlayerAdvancedPlayoffs(db *gorm.DB) { - for season := 2023; season <= 2024; season++ { - if err := services.FetchAndStorePlayerAdvancedPlayoffsStats(db, season, true); err != nil { + for season := 2023; season <= 2025; season++ { + if err := services.FetchAndStorePlayerAdvancedScrapedStats(db, season, true); err != nil { log.Printf("advanced import failed for %d: %v", season, err) } time.Sleep(1100 * time.Millisecond) @@ -70,8 +70,8 @@ func importPlayerTotalsScrape(db *gorm.DB) { } // importPlayerPlayoffsScrape fetches & stores scraped playoff total stats -func importPlayerPlayoffsScrape(db *gorm.DB) { - for season := 2023; season <= 2024; season++ { +func importPlayerTotalsPlayoffsScrape(db *gorm.DB) { + for season := 2023; season <= 2025; season++ { if err := services.FetchAndStorePlayerTotalScrapedStats(db, season, true); err != nil { log.Printf("scraped playoffs import failed for %d: %v", season, err) } diff --git a/main.go b/main.go index 50f4039..336e96f 100644 --- a/main.go +++ b/main.go @@ -26,6 +26,7 @@ import ( fiberswagger "github.com/swaggo/fiber-swagger" "github.com/nprasad2077/NBA_Go/config" + "github.com/nprasad2077/NBA_Go/utils" "github.com/nprasad2077/NBA_Go/controllers" "github.com/nprasad2077/NBA_Go/routes" "github.com/nprasad2077/NBA_Go/utils/middleware" @@ -39,6 +40,8 @@ func main() { importPlayerAdvanced(db) log.Println("🎉 Player Advanced Import completed successfully") + utils.SleepWithJitter(1100 * time.Millisecond) + importPlayerAdvancedPlayoffs(db) log.Println("🎉 Player Advanced Playoffs Import completed successfully") @@ -49,7 +52,7 @@ func main() { importPlayerTotalsScrape(db) log.Println("🎉 Player Totals (scraped) Import completed successfully") - importPlayerPlayoffsScrape(db) + importPlayerTotalsPlayoffsScrape(db) log.Println("🎉 Player Playoffs (scraped) Import completed successfully") // importPlayerShotChart(db) diff --git a/utils/sleep.go b/utils/sleep.go new file mode 100644 index 0000000..1eced58 --- /dev/null +++ b/utils/sleep.go @@ -0,0 +1,23 @@ +// File: utils/sleep.go +package utils + +import ( + "math/rand" + "time" +) + +func init() { + // Seed the RNG once when the package is initialized + rand.Seed(time.Now().UnixNano()) +} + +// SleepWithJitter sleeps for the given base duration plus or minus 25% random jitter. +// This helps to avoid perfectly uniform request intervals. +func SleepWithJitter(base time.Duration) { + half := base / 2 + // Random value in [0, half) + randOffset := time.Duration(rand.Int63n(int64(half))) + // Shift to center jitter around zero: [-half/2, +half/2) + delta := randOffset - half/2 + time.Sleep(base + delta) +} \ No newline at end of file