Files
zurg/pkg/premium/monitor.go
2024-06-07 19:19:18 +02:00

47 lines
1.3 KiB
Go

package premium
import (
"time"
"github.com/debridmediamanager/zurg/pkg/logutil"
"github.com/debridmediamanager/zurg/pkg/realdebrid"
"github.com/panjf2000/ants/v2"
)
const (
PREMIUM_THRESHOLD = 172800 // 2 days
MINIMUM_SLEEP = 60 // 60 seconds
)
func MonitorPremiumStatus(workerPool *ants.Pool, rd *realdebrid.RealDebrid, zurglog *logutil.Logger) {
workerPool.Submit(func() {
for {
userInfo, err := rd.GetUserInformation()
if err != nil {
zurglog.Errorf("Failed to get user information: %v", err)
time.Sleep(5 * time.Minute)
continue
}
if userInfo.Premium <= MINIMUM_SLEEP {
zurglog.Fatal("Your account is no longer premium, exiting...")
} else {
if userInfo.Premium <= PREMIUM_THRESHOLD {
zurglog.Warnf("Your account will expire in %d hours", userInfo.Premium/3600)
} else {
zurglog.Infof("Your account will expire in %d days", userInfo.Premium/86400)
}
}
remaining := userInfo.Premium - PREMIUM_THRESHOLD
if remaining < MINIMUM_SLEEP {
// Ensure minimum sleep duration is 60 seconds
remaining = MINIMUM_SLEEP
} else {
// Round up to the nearest multiple of 60 seconds
remaining = ((remaining + MINIMUM_SLEEP - 1) / MINIMUM_SLEEP) * MINIMUM_SLEEP
}
sleepDuration := time.Duration(remaining) * time.Second
time.Sleep(sleepDuration)
}
})
}