Skip to content
This repository has been archived by the owner on May 18, 2024. It is now read-only.

Commit

Permalink
rescan all torrents if last update >15 min ago
Browse files Browse the repository at this point in the history
  • Loading branch information
itsToggle authored Oct 6, 2022
1 parent 4f9f01f commit 15efd5b
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions backend/realdebrid/realdebrid.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ var (
//To limit api calls all pages are stored here and are only updated on changes in the total length
var cached []api.Item
var torrents []api.Item
var lastcheck int64 = time.Now().Unix()
var interval int64 = 15 * 60

// Register with Fs
func init() {
Expand Down Expand Up @@ -177,7 +179,6 @@ var retryErrorCodes = []int{
429, // Too Many Requests.
500, // Internal Server Error
502, // Bad Gateway
503, // Service Unavailable
504, // Gateway Timeout
509, // Bandwidth Limit Exceeded
}
Expand Down Expand Up @@ -377,7 +378,7 @@ func (f *Fs) CreateDir(ctx context.Context, pathID, leaf string) (newID string,

// Redownload a dead torrent
func (f *Fs) redownloadTorrent(ctx context.Context, torrent api.Item) (redownloaded_torrent api.Item) {
fmt.Printf("Redownloading dead torrent: " + torrent.Name)
fmt.Println("Redownloading dead torrent: " + torrent.Name)
//Get dead torrent file and hash info
var method = "GET"
var path = "/torrents/info/" + torrent.ID
Expand Down Expand Up @@ -443,6 +444,7 @@ func (f *Fs) redownloadTorrent(ctx context.Context, torrent api.Item) (redownloa
}
_, _ = f.srv.CallJSON(ctx, &opts, nil, &torrent)
torrent.Status = "downloaded"
lastcheck = time.Now().Unix() - interval
return torrent
}

Expand All @@ -468,7 +470,6 @@ func (f *Fs) listAll(ctx context.Context, dirID string, directoriesOnly bool, fi
if f.opt.RootFolderID == "torrents" {
if dirID == rootID {
//update global cached list
//fmt.Printf("Updating RealDebrid Direct Links ... ")
opts := rest.Opts{
Method: method,
Path: path,
Expand All @@ -479,14 +480,19 @@ func (f *Fs) listAll(ctx context.Context, dirID string, directoriesOnly bool, fi
var newcached []api.Item
err = f.pacer.Call(func() (bool, error) {
var totalcount int
var printed = false
totalcount = 2
for len(newcached) < totalcount {
partialresult = nil
resp, err = f.srv.CallJSON(ctx, &opts, nil, &partialresult)
if err == nil {
totalcount, err = strconv.Atoi(resp.Header["X-Total-Count"][0])
if err == nil {
if totalcount != len(cached) {
if totalcount != len(cached) || time.Now().Unix()-lastcheck > interval {
if time.Now().Unix()-lastcheck > interval && !printed {
fmt.Println("Last update more than 15min ago. Updating links and torrents.")
printed = true
}
newcached = append(newcached, partialresult...)
opts.Parameters.Set("offset", strconv.Itoa(len(newcached)))
opts.Parameters.Set("limit", "100")
Expand Down Expand Up @@ -523,7 +529,7 @@ func (f *Fs) listAll(ctx context.Context, dirID string, directoriesOnly bool, fi
if err == nil {
totalcount, err = strconv.Atoi(resp.Header["X-Total-Count"][0])
if err == nil {
if totalcount != len(torrents) {
if totalcount != len(torrents) || time.Now().Unix()-lastcheck > interval {
newtorrents = append(newtorrents, partialresult...)
opts.Parameters.Set("offset", strconv.Itoa(len(newtorrents)))
opts.Parameters.Set("limit", "100")
Expand All @@ -539,6 +545,7 @@ func (f *Fs) listAll(ctx context.Context, dirID string, directoriesOnly bool, fi
}
return shouldRetry(ctx, resp, err)
})
lastcheck = time.Now().Unix()
//fmt.Printf("Done.\n")
torrents = newtorrents
//Handle dead torrents
Expand Down

0 comments on commit 15efd5b

Please sign in to comment.