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

61 lines
1.7 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
)
// MonitorPremiumStatus is a permanent job that monitors the premium status of the user
func MonitorPremiumStatus(workerPool *ants.Pool, rd *realdebrid.RealDebrid, zurglog *logutil.Logger) {
var userInfo *realdebrid.User
var err error
for {
userInfo, err = rd.GetUserInformation()
if err != nil {
zurglog.Errorf("Failed to get user information: %v trying again in 30 seconds", err)
time.Sleep(30 * time.Second)
continue
}
break
}
workerPool.Submit(func() {
for {
if userInfo.Premium <= MINIMUM_SLEEP {
zurglog.Errorf("YOUR ACCOUNT IS NO LONGER PREMIUM. PLEASE RENEW YOUR SUBSCRIPTION.")
} 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)
// fetch user information again
userInfo, err = rd.GetUserInformation()
if err != nil {
zurglog.Errorf("Failed to get user information: %v", err)
time.Sleep(time.Duration(MINIMUM_SLEEP) * time.Second)
}
}
})
}