diff --git a/backend/internal/ktmtrainbot/backgroundbookingjob.go b/backend/internal/ktmtrainbot/backgroundbookingjob.go index f79fd93..87de235 100644 --- a/backend/internal/ktmtrainbot/backgroundbookingjob.go +++ b/backend/internal/ktmtrainbot/backgroundbookingjob.go @@ -434,14 +434,32 @@ func selectBookingSlot(ctx context.Context, page *rod.Page, timeCode string) *ro time.Sleep(5 * time.Second) twoCaptchaAPIKey := os.Getenv("TWOCAPTCHA_API_KEY") + needToWait := false + // Initial closing of maintenance modal bodyText := page.MustElement("body").MustText() if strings.Contains(bodyText, "System maintenance scheduled at 23:00 to 00:15 (UTC+8)") { closeModalButton := page.MustElement("#popupModalCloseButton") closeModalButton.Eval(`this.click()`) + + needToWait = true + time.Sleep(1000 * time.Millisecond) } + // Wait for 1215am + if needToWait { + log.Println("Waiting for 1215am") + destinationTime := time.Date(time.Now().Year(), time.Now().Month(), time.Now().Day(), 0, 15, 1, 0, time.Now().Location()) + needToWaitTimer := time.NewTimer(time.Until(destinationTime)) + select { + case <-ctx.Done(): + return page + case <-needToWaitTimer.C: + log.Println("1215am reached") + } + } + // Start probing reportedTicketDetails := false completed := false @@ -498,6 +516,19 @@ func selectBookingSlot(ctx context.Context, page *rod.Page, timeCode string) *ro closeModalButton := page.MustElement("#popupModalCloseButton") closeModalButton.Eval(`this.click()`) time.Sleep(1000 * time.Millisecond) + + continue + } + + // Repeat if there's no seats -- seats might be released later + if strings.Contains(bodyText, "Not enough seat for onward trip") { + completed = false + + closeModalButton := page.MustElement("#popupModalCloseButton") + closeModalButton.Eval(`this.click()`) + time.Sleep(1000 * time.Millisecond) + + continue } // Check if there is captcha @@ -516,15 +547,6 @@ func selectBookingSlot(ctx context.Context, page *rod.Page, timeCode string) *ro page.Eval(`RecaptchaCallback()`) } - // Repeat if there's no seats -- seats might be released later - if strings.Contains(bodyText, "Not enough seat for onward trip") { - completed = false - - closeModalButton := page.MustElement("#popupModalCloseButton") - closeModalButton.Eval(`this.click()`) - time.Sleep(1000 * time.Millisecond) - } - styleAttribute := selectButtonElement.MustAttribute("style") if styleAttribute == nil || !strings.Contains(*styleAttribute, "display: none") { completed = false