|
|
|
|
@@ -36,60 +36,60 @@ func (t *TorrentManager) refreshTorrents(isInitialRun bool) []string {
|
|
|
|
|
close(infoChan)
|
|
|
|
|
t.log.Infof("Fetched info for %d torrents", len(instances))
|
|
|
|
|
|
|
|
|
|
newlyFetchedKeys := mapset.NewSet[string]()
|
|
|
|
|
var updatedPaths []string
|
|
|
|
|
noInfoCount := 0
|
|
|
|
|
allTorrents, _ := t.DirectoryMap.Get(INT_ALL)
|
|
|
|
|
freshAccessKeys := mapset.NewSet[string]()
|
|
|
|
|
deletedIDs := t.allIDs.Clone()
|
|
|
|
|
for info := range infoChan {
|
|
|
|
|
if info == nil {
|
|
|
|
|
noInfoCount++
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
accessKey := t.GetKey(info)
|
|
|
|
|
if !info.AnyInProgress() { // since it's single instance info, Any == All
|
|
|
|
|
newlyFetchedKeys.Add(accessKey)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
torrent, exists := allTorrents.Get(accessKey)
|
|
|
|
|
infoID, _ := info.DownloadedIDs.Clone().Pop()
|
|
|
|
|
deletedIDs.Remove(infoID)
|
|
|
|
|
accessKey := t.GetKey(info)
|
|
|
|
|
freshAccessKeys.Add(accessKey)
|
|
|
|
|
|
|
|
|
|
// update allTorrents
|
|
|
|
|
mainTorrent, exists := allTorrents.Get(accessKey)
|
|
|
|
|
if !exists {
|
|
|
|
|
allTorrents.Set(accessKey, info)
|
|
|
|
|
continue
|
|
|
|
|
} else {
|
|
|
|
|
if !mainTorrent.DownloadedIDs.Contains(infoID) {
|
|
|
|
|
merged := t.mergeToMain(mainTorrent, info)
|
|
|
|
|
allTorrents.Set(accessKey, merged)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
newIDs := info.DownloadedIDs.Union(info.InProgressIDs)
|
|
|
|
|
oldIDs := torrent.DownloadedIDs.Union(torrent.InProgressIDs)
|
|
|
|
|
if !newIDs.Difference(oldIDs).IsEmpty() {
|
|
|
|
|
mainTorrent := t.mergeToMain(torrent, info)
|
|
|
|
|
allTorrents.Set(accessKey, &mainTorrent)
|
|
|
|
|
|
|
|
|
|
// check for newly finished torrents for assigning to directories
|
|
|
|
|
isDone := info.DownloadedIDs.Cardinality() > 0 && info.InProgressIDs.IsEmpty()
|
|
|
|
|
if isDone && !t.allIDs.Contains(infoID) {
|
|
|
|
|
var directories []string
|
|
|
|
|
mainTor, _ := allTorrents.Get(accessKey)
|
|
|
|
|
t.assignedDirectoryCb(mainTor, func(directory string) {
|
|
|
|
|
listing, _ := t.DirectoryMap.Get(directory)
|
|
|
|
|
listing.Set(accessKey, mainTor)
|
|
|
|
|
updatedPaths = append(updatedPaths, fmt.Sprintf("%s/%s", directory, accessKey))
|
|
|
|
|
// this is just for the logs
|
|
|
|
|
if directory != config.ALL_TORRENTS {
|
|
|
|
|
directories = append(directories, directory)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
t.allIDs.Add(infoID)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
t.allIDs.RemoveAll(deletedIDs.ToSlice()...)
|
|
|
|
|
t.log.Infof("Compiled into %d torrents, %d were missing info", allTorrents.Count(), noInfoCount)
|
|
|
|
|
|
|
|
|
|
var updatedPaths []string
|
|
|
|
|
// torrents yet to be assigned in a directory
|
|
|
|
|
newlyFetchedKeys.Difference(t.allAccessKeys).Each(func(accessKey string) bool {
|
|
|
|
|
// assign to directories
|
|
|
|
|
tor, ok := allTorrents.Get(accessKey)
|
|
|
|
|
if !ok {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
var directories []string
|
|
|
|
|
t.assignedDirectoryCb(tor, func(directory string) {
|
|
|
|
|
torrents, _ := t.DirectoryMap.Get(directory)
|
|
|
|
|
torrents.Set(accessKey, tor)
|
|
|
|
|
updatedPaths = append(updatedPaths, fmt.Sprintf("%s/%s", directory, accessKey))
|
|
|
|
|
// this is just for the logs
|
|
|
|
|
if directory != config.ALL_TORRENTS {
|
|
|
|
|
directories = append(directories, directory)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
// t.log.Debugf("Added %s to %v", accessKey, directories)
|
|
|
|
|
t.allAccessKeys.Add(accessKey)
|
|
|
|
|
return false
|
|
|
|
|
})
|
|
|
|
|
// removed torrents
|
|
|
|
|
t.allAccessKeys.Difference(newlyFetchedKeys).Each(func(accessKey string) bool {
|
|
|
|
|
t.allAccessKeys.Difference(freshAccessKeys).Each(func(accessKey string) bool {
|
|
|
|
|
t.Delete(accessKey, false)
|
|
|
|
|
t.allAccessKeys.Remove(accessKey)
|
|
|
|
|
return false
|
|
|
|
|
})
|
|
|
|
|
t.allAccessKeys.Append(freshAccessKeys.ToSlice()...)
|
|
|
|
|
|
|
|
|
|
if t.Config.EnableRepair() {
|
|
|
|
|
if isInitialRun {
|
|
|
|
|
@@ -138,33 +138,17 @@ func (t *TorrentManager) StartRefreshJob() {
|
|
|
|
|
func (t *TorrentManager) getMoreInfo(rdTorrent realdebrid.Torrent) *Torrent {
|
|
|
|
|
infoCache, _ := t.DirectoryMap.Get(INT_INFO_CACHE)
|
|
|
|
|
|
|
|
|
|
if torrentFromCache, exists := infoCache.Get(rdTorrent.ID); exists &&
|
|
|
|
|
!torrentFromCache.AnyInProgress() &&
|
|
|
|
|
torrentFromCache.SelectedFiles.Count() == len(rdTorrent.Links) {
|
|
|
|
|
if cachedTor, exists := infoCache.Get(rdTorrent.ID); exists &&
|
|
|
|
|
cachedTor.SelectedFiles.Count() == len(rdTorrent.Links) {
|
|
|
|
|
|
|
|
|
|
return torrentFromCache
|
|
|
|
|
return cachedTor
|
|
|
|
|
|
|
|
|
|
} else if !exists {
|
|
|
|
|
} else if diskTor := t.readTorrentFromFile(rdTorrent.ID); diskTor != nil &&
|
|
|
|
|
diskTor.SelectedFiles.Count() == len(rdTorrent.Links) {
|
|
|
|
|
|
|
|
|
|
torrentFromFile := t.readTorrentFromFile(rdTorrent.ID)
|
|
|
|
|
|
|
|
|
|
if torrentFromFile != nil &&
|
|
|
|
|
torrentFromFile.SelectedFiles.Count() == len(rdTorrent.Links) {
|
|
|
|
|
|
|
|
|
|
hasBrokenFiles := false
|
|
|
|
|
torrentFromFile.SelectedFiles.IterCb(func(filepath string, file *File) {
|
|
|
|
|
if file.IsBroken && !file.IsDeleted {
|
|
|
|
|
hasBrokenFiles = true
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
if !hasBrokenFiles {
|
|
|
|
|
infoCache.Set(rdTorrent.ID, torrentFromFile)
|
|
|
|
|
t.ResetSelectedFiles(torrentFromFile)
|
|
|
|
|
return torrentFromFile
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
infoCache.Set(rdTorrent.ID, diskTor)
|
|
|
|
|
t.ResetSelectedFiles(diskTor)
|
|
|
|
|
return diskTor
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
info, err := t.Api.GetTorrentInfo(rdTorrent.ID)
|
|
|
|
|
@@ -184,7 +168,6 @@ func (t *TorrentManager) getMoreInfo(rdTorrent realdebrid.Torrent) *Torrent {
|
|
|
|
|
// if it is empty, it means the file is no longer available
|
|
|
|
|
// Files+Links together are the same as SelectedFiles
|
|
|
|
|
var selectedFiles []*File
|
|
|
|
|
// if some Links are empty, we need to repair it
|
|
|
|
|
for _, file := range info.Files {
|
|
|
|
|
if file.Selected == 0 {
|
|
|
|
|
continue
|
|
|
|
|
@@ -192,7 +175,7 @@ func (t *TorrentManager) getMoreInfo(rdTorrent realdebrid.Torrent) *Torrent {
|
|
|
|
|
selectedFiles = append(selectedFiles, &File{
|
|
|
|
|
File: file,
|
|
|
|
|
Ended: info.Ended,
|
|
|
|
|
Link: "", // no link yet
|
|
|
|
|
Link: "", // no link yet, consider it broken
|
|
|
|
|
IsBroken: true,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
@@ -253,7 +236,7 @@ func (t *TorrentManager) ResetSelectedFiles(torrent *Torrent) {
|
|
|
|
|
torrent.SelectedFiles = newSelectedFiles
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (t *TorrentManager) mergeToMain(existing, toMerge *Torrent) Torrent {
|
|
|
|
|
func (t *TorrentManager) mergeToMain(existing, toMerge *Torrent) *Torrent {
|
|
|
|
|
var newer, older *Torrent
|
|
|
|
|
if existing.Added < toMerge.Added {
|
|
|
|
|
newer = toMerge
|
|
|
|
|
@@ -322,7 +305,7 @@ func (t *TorrentManager) mergeToMain(existing, toMerge *Torrent) Torrent {
|
|
|
|
|
t.CheckDeletedStatus(&mainTorrent)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return mainTorrent
|
|
|
|
|
return &mainTorrent
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (t *TorrentManager) assignedDirectoryCb(tor *Torrent, cb func(string)) {
|
|
|
|
|
|