Feat(booking-job): Auto refresh after 9 minutes
All checks were successful
ktm-booking-bot/ktm-booking-bot/pipeline/head This commit looks good

This commit is contained in:
2022-10-27 00:52:16 +08:00
parent 5558e0001a
commit 298c7d853a

View File

@@ -275,6 +275,44 @@ func (env *Env) startBooking(job *Booking, username string, password string, cre
default: default:
} }
paymentSucceed := chooseAndMakePayment(timerCtx, creditCardType, creditCard, creditCardCVV, creditCardExpiry, browser, page)
// // Start debug screenshots
// debugScreenshotCtx, cancelDebugScreenshot := context.WithCancel(context.Background())
// go takeDebugScreenshots(debugScreenshotCtx, courtPage)
// // Defer done with debug screenshot
// defer cancelDebugScreenshot()
if !paymentSucceed {
log.Println("Payment failed.")
return false
}
time.Sleep(10 * time.Second)
_ = page
return true
}
func chooseAndMakePayment(timerCtx context.Context, creditCardType, creditCard, creditCardCVV, creditCardExpiry string, browser *rod.Browser, page *rod.Page) bool {
defer func() {
if r := recover(); r != nil {
log.Println("Recovering from chooseAndMakePayment panic...")
chooseAndMakePayment(timerCtx, creditCardType, creditCard, creditCardCVV, creditCardExpiry, browser, page)
}
}()
// Exits if context cancelled
select {
case <-timerCtx.Done():
browser.MustClose()
return false
default:
}
go triggerFuturePageRefresh(page)
if strings.ToUpper(creditCardType) == "VISA" || strings.ToUpper(creditCardType) == "MASTERCARD" { if strings.ToUpper(creditCardType) == "VISA" || strings.ToUpper(creditCardType) == "MASTERCARD" {
log.Println("Credit card payment method chosen.") log.Println("Credit card payment method chosen.")
page = choosePaymentCreditCard(page) page = choosePaymentCreditCard(page)
@@ -310,24 +348,14 @@ func (env *Env) startBooking(job *Booking, username string, password string, cre
expiryMonth := strings.Split(creditCardExpiry, "/")[0] expiryMonth := strings.Split(creditCardExpiry, "/")[0]
expiryYear := strings.Split(creditCardExpiry, "/")[1] expiryYear := strings.Split(creditCardExpiry, "/")[1]
page = makePayment(page, creditCardType, creditCard, expiryMonth, expiryYear, creditCardCVV) makePayment(page, creditCardType, creditCard, expiryMonth, expiryYear, creditCardCVV)
log.Println("Credit card payment made.") log.Println("Credit card payment made.")
} else if strings.ToUpper(creditCardType) == "KTMWALLET" { } else if strings.ToUpper(creditCardType) == "KTMWALLET" {
log.Println("KTM wallet payment method chosen.") log.Println("KTM wallet payment method chosen.")
page = choosePaymentKTMWallet(page) choosePaymentKTMWallet(page)
log.Println("KTM wallet payment made.") log.Println("KTM wallet payment made.")
} }
// // Start debug screenshots
// debugScreenshotCtx, cancelDebugScreenshot := context.WithCancel(context.Background())
// go takeDebugScreenshots(debugScreenshotCtx, courtPage)
// // Defer done with debug screenshot
// defer cancelDebugScreenshot()
time.Sleep(10 * time.Second)
_ = page
return true return true
} }
@@ -592,3 +620,17 @@ func sensitiveCustomForm(
return page return page
} }
func triggerFuturePageRefresh(page *rod.Page) func() {
ctx, cancelFunc := context.WithCancel(context.Background())
go func() {
select {
case <-ctx.Done():
return
// Reload page after 9 minutes
case <-time.After(540 * time.Second):
page.MustReload()
}
}()
return cancelFunc
}