Prevent stale torrents

This commit is contained in:
Ben Adrian Sarmiento
2024-06-28 21:10:31 +02:00
parent 7cd8c9e5c9
commit c781a5fc7c
5 changed files with 9 additions and 121 deletions

View File

@@ -55,8 +55,6 @@ func NewRealDebrid(apiClient, unrestrictClient, downloadClient *zurghttp.HTTPCli
rd.UnrestrictMap.Set(token, cmap.New[*Download]())
}
rd.loadCachedTorrents()
return rd
}

View File

@@ -2,10 +2,8 @@ package realdebrid
import (
"fmt"
"io"
"net/http"
"net/url"
"os"
"strconv"
)
@@ -106,7 +104,7 @@ func (rd *RealDebrid) GetTorrents(onlyOne bool) ([]Torrent, int, error) {
page += maxParallelThreads
}
rd.cacheTorrents(allTorrents)
rd.torrentsCache = allTorrents
return allTorrents, len(allTorrents), nil
}
@@ -187,51 +185,3 @@ func (rd *RealDebrid) fetchPageOfTorrents(page, limit int) fetchTorrentsResult {
err: nil,
}
}
func (rd *RealDebrid) cacheTorrents(torrents []Torrent) {
filePath := "data/info/all.json"
file, err := os.Create(filePath)
if err != nil {
rd.log.Warnf("Cannot create info file %s: %v", filePath, err)
return
}
defer file.Close()
jsonData, err := json.Marshal(torrents)
if err != nil {
rd.log.Warnf("Cannot marshal torrent info: %v", err)
return
}
if _, err := file.Write(jsonData); err != nil {
rd.log.Warnf("Cannot write to info file %s: %v", filePath, err)
return
}
rd.torrentsCache = torrents
}
func (rd *RealDebrid) loadCachedTorrents() {
filePath := "data/info/all.json"
file, err := os.Open(filePath)
if err != nil {
rd.log.Warnf("Cannot open info file %s: %v", filePath, err)
return
}
defer file.Close()
jsonData, err := io.ReadAll(file)
if err != nil {
rd.log.Warnf("Cannot read info file %s: %v", filePath, err)
return
}
var torrents []Torrent
err = json.Unmarshal(jsonData, &torrents)
if err != nil {
rd.log.Warnf("Cannot unmarshal torrent info: %v", err)
return
}
rd.torrentsCache = torrents
}