72 lines
2.0 KiB
Go
72 lines
2.0 KiB
Go
package torrent
|
|
|
|
import (
|
|
"github.com/debridmediamanager/zurg/pkg/realdebrid"
|
|
)
|
|
|
|
type LibraryState struct {
|
|
TotalCount int
|
|
ActiveCount int
|
|
FirstTorrent *realdebrid.Torrent
|
|
FirstActiveTorrent *realdebrid.Torrent
|
|
}
|
|
|
|
func (ls LibraryState) equal(a LibraryState) bool {
|
|
if a.TotalCount != ls.TotalCount || a.ActiveCount != ls.ActiveCount {
|
|
return false
|
|
}
|
|
if (ls.FirstTorrent == nil) != (a.FirstTorrent == nil) {
|
|
return false
|
|
}
|
|
if ls.FirstTorrent != nil && (ls.FirstTorrent.ID != a.FirstTorrent.ID || ls.FirstTorrent.Status != a.FirstTorrent.Status) {
|
|
return false
|
|
}
|
|
if (ls.FirstActiveTorrent == nil) != (a.FirstActiveTorrent == nil) {
|
|
return false
|
|
}
|
|
if ls.FirstActiveTorrent != nil && (ls.FirstActiveTorrent.ID != a.FirstActiveTorrent.ID || ls.FirstActiveTorrent.Status != a.FirstActiveTorrent.Status) {
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
|
|
func (t *TorrentManager) SetNewLatestState(checksum LibraryState) {
|
|
t.latestState.ActiveCount = checksum.ActiveCount
|
|
t.latestState.TotalCount = checksum.TotalCount
|
|
t.latestState.FirstTorrent = checksum.FirstTorrent
|
|
t.latestState.FirstActiveTorrent = checksum.FirstActiveTorrent
|
|
}
|
|
|
|
// 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(1, false)
|
|
if err != nil {
|
|
t.log.Warnf("Checksum API Error (GetTorrents): %v", err)
|
|
return LibraryState{}
|
|
}
|
|
if len(torrents) > 0 {
|
|
state.FirstTorrent = &torrents[0]
|
|
}
|
|
state.TotalCount = totalCount
|
|
|
|
activeTorrents, _, err := t.Api.GetTorrents(1, true)
|
|
if err != nil {
|
|
t.log.Warnf("Checksum API Error (GetTorrents): %v", err)
|
|
return LibraryState{}
|
|
}
|
|
if len(activeTorrents) > 0 {
|
|
state.FirstActiveTorrent = &activeTorrents[0]
|
|
}
|
|
|
|
count, err := t.Api.GetActiveTorrentCount()
|
|
if err != nil {
|
|
t.log.Warnf("Checksum API Error (GetActiveTorrentCount): %v", err)
|
|
return LibraryState{}
|
|
}
|
|
state.ActiveCount = count.DownloadingCount
|
|
|
|
return state
|
|
}
|