package torrent import ( "fmt" mapset "github.com/deckarep/golang-set/v2" ) func (t *TorrentManager) getUncachedTorrents() ([]*Torrent, error) { t.log.Debug("Checking if torrents are still cached") allTorrents, _ := t.DirectoryMap.Get(INT_ALL) var hashGroups []mapset.Set[string] const maxGroupSize = 399 currentGroup := mapset.NewSet[string]() hashGroups = append(hashGroups, currentGroup) allTorrents.IterCb(func(_ string, torrent *Torrent) { if torrent.AnyInProgress() || torrent.UnrepairableReason != "" { return } if currentGroup.Cardinality() >= maxGroupSize { currentGroup = mapset.NewSet[string]() hashGroups = append(hashGroups, currentGroup) } currentGroup.Add(torrent.Hash) }) var availabilityChecks = make(map[string]bool) for i := range hashGroups { if hashGroups[i].Cardinality() == 0 { break } resp, err := t.Api.AvailabilityCheck(hashGroups[i].ToSlice()) if err != nil { return nil, fmt.Errorf("availability check is incomplete, skipping uncached check: %v", err) } for hash, hosterHash := range resp { // Check if HosterHash is a map (Variants field is used) availabilityChecks[hash] = len(hosterHash.Variants) > 0 } } var uncachedTorrents []*Torrent allTorrents.IterCb(func(_ string, torrent *Torrent) { if torrent.AnyInProgress() || torrent.UnrepairableReason != "" { return } if _, ok := availabilityChecks[torrent.Hash]; !ok || !availabilityChecks[torrent.Hash] { t.log.Debugf("Torrent %s is no longer cached, adding to repair list", t.GetKey(torrent)) uncachedTorrents = append(uncachedTorrents, torrent) } }) t.log.Debugf("Found %d torrents that are no longer cached", len(uncachedTorrents)) return uncachedTorrents, nil }