Add token management

This commit is contained in:
Ben Adrian Sarmiento
2024-06-28 13:19:09 +02:00
parent 962845fb81
commit c3aea427d0
9 changed files with 100 additions and 114 deletions

View File

@@ -13,22 +13,22 @@ import (
"github.com/debridmediamanager/zurg/internal/config"
intTor "github.com/debridmediamanager/zurg/internal/torrent"
zurghttp "github.com/debridmediamanager/zurg/pkg/http"
"github.com/debridmediamanager/zurg/pkg/logutil"
"github.com/debridmediamanager/zurg/pkg/realdebrid"
"github.com/debridmediamanager/zurg/pkg/utils"
"github.com/panjf2000/ants/v2"
)
type Downloader struct {
client *zurghttp.HTTPClient
rd *realdebrid.RealDebrid
workerPool *ants.Pool
RequestedBytes atomic.Uint64
TotalBytes atomic.Uint64
}
func NewDownloader(client *zurghttp.HTTPClient, workerPool *ants.Pool) *Downloader {
func NewDownloader(rd *realdebrid.RealDebrid, workerPool *ants.Pool) *Downloader {
dl := &Downloader{
client: client,
rd: rd,
workerPool: workerPool,
}
@@ -42,10 +42,12 @@ func NewDownloader(client *zurghttp.HTTPClient, workerPool *ants.Pool) *Download
nextMidnightInCET := time.Date(tomorrow.Year(), tomorrow.Month(), tomorrow.Day(), 0, 0, 0, 0, cetTZ)
duration := nextMidnightInCET.Sub(now)
timer := time.NewTimer(duration)
// permanent job for bandwidth reset
workerPool.Submit(func() {
<-timer.C
ticker := time.NewTicker(24 * time.Hour)
for {
rd.TokenManager.ResetAllTokens()
dl.RequestedBytes.Store(0)
dl.TotalBytes.Store(0)
<-ticker.C
@@ -88,7 +90,7 @@ func (dl *Downloader) DownloadFile(
}
unrestrict, err := torMgr.UnrestrictFile(file)
if dlErr, ok := err.(*zurghttp.DownloadErrorResponse); ok && dlErr.Message == "bytes_limit_reached" {
if utils.IsBWLimitExceeded(err) {
// log.Errorf("Your account has reached the bandwidth limit, please try again after 12AM CET")
http.Error(resp, "File is not available (bandwidth limit reached)", http.StatusBadRequest)
return
@@ -161,8 +163,8 @@ func (dl *Downloader) streamFileToResponse(
dlReq.Header.Add("Range", req.Header.Get("Range"))
}
downloadResp, err := dl.client.Do(dlReq)
if dlErr, ok := err.(*zurghttp.DownloadErrorResponse); ok && dlErr.Message == "bytes_limit_reached" {
downloadResp, err := dl.rd.DownloadFile(dlReq)
if utils.IsBWLimitExceeded(err) {
// log.Errorf("Your account has reached the bandwidth limit, please try again after 12AM CET")
http.Error(resp, "File is not available (bandwidth limit reached)", http.StatusBadRequest)
return