Files
zurg/internal/torrent/latestState.go
Ben Sarmiento 9990bf90ca Fix rewrites
2024-05-22 02:26:20 +02:00

58 lines
1.7 KiB
Go

package torrent
import "github.com/debridmediamanager/zurg/pkg/logutil"
type LibraryState struct {
TotalCount int
ActiveCount int
FirstTorrentId string
log *logutil.Logger
}
func (ls *LibraryState) Eq(a LibraryState) bool {
if ls.TotalCount == 0 || ls.FirstTorrentId == "" {
ls.log.Debugf("Checksum is empty")
return false
} else if a.TotalCount != ls.TotalCount {
ls.log.Debugf("Checksum total count mismatch: %d != %d", a.TotalCount, ls.TotalCount)
return false
} else if a.ActiveCount != ls.ActiveCount {
ls.log.Debugf("Checksum active count mismatch: %d != %d", a.ActiveCount, ls.ActiveCount)
return false
} else if a.FirstTorrentId != ls.FirstTorrentId {
ls.log.Debugf("Checksum first torrent id mismatch: %s != %s", a.FirstTorrentId, ls.FirstTorrentId)
return false
}
return true
}
func (t *TorrentManager) setNewLatestState(checksum LibraryState) {
t.latestState.ActiveCount = checksum.ActiveCount
t.latestState.TotalCount = checksum.TotalCount
t.latestState.FirstTorrentId = checksum.FirstTorrentId
}
// generates a checksum based on the number of torrents, the first torrent id and the number of active torrents
func (t *TorrentManager) getCurrentState() LibraryState {
var state LibraryState
torrents, totalCount, err := t.api.GetTorrents(true)
if err != nil {
t.log.Errorf("Checksum API Error (GetTorrents): %v", err)
return LibraryState{}
}
state.TotalCount = totalCount
if len(torrents) > 0 {
state.FirstTorrentId = torrents[0].ID
}
count, err := t.api.GetActiveTorrentCount()
if err != nil {
t.log.Errorf("Checksum API Error (GetActiveTorrentCount): %v", err)
return LibraryState{}
}
state.ActiveCount = count.DownloadingCount
return state
}