Prevent stale torrents
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user