fixes here and there

This commit is contained in:
Ben Sarmiento
2023-11-11 02:34:46 +01:00
parent 147c0bd444
commit cd96c7bd38
14 changed files with 181 additions and 155 deletions

View File

@@ -14,17 +14,16 @@ import (
"github.com/debridmediamanager.com/zurg/pkg/logutil"
"github.com/debridmediamanager.com/zurg/pkg/realdebrid"
"github.com/elliotchance/orderedmap/v2"
"github.com/nutsdb/nutsdb"
"go.uber.org/zap"
)
type TorrentManager struct {
TorrentMap *orderedmap.OrderedMap[string, *Torrent] // accessKey -> Torrent
repairMap *orderedmap.OrderedMap[string, bool] // accessKey -> bool
requiredVersion string
rd *realdebrid.RealDebrid
checksum string
config config.ConfigInterface
db *nutsdb.DB
workerPool chan bool
mu *sync.Mutex
log *zap.SugaredLogger
@@ -33,19 +32,18 @@ type TorrentManager struct {
// NewTorrentManager creates a new torrent manager
// it will fetch all torrents and their info in the background
// and store them in-memory and cached in files
func NewTorrentManager(config config.ConfigInterface, db *nutsdb.DB) *TorrentManager {
func NewTorrentManager(config config.ConfigInterface, rd *realdebrid.RealDebrid) *TorrentManager {
t := &TorrentManager{
TorrentMap: orderedmap.NewOrderedMap[string, *Torrent](),
repairMap: orderedmap.NewOrderedMap[string, bool](),
requiredVersion: "10.11.2023",
rd: realdebrid.NewRealDebrid(config.GetToken(), logutil.NewLogger().Named("realdebrid")),
rd: rd,
config: config,
db: db,
workerPool: make(chan bool, config.GetNumOfWorkers()),
mu: &sync.Mutex{},
log: logutil.NewLogger().Named("manager"),
}
// start with a clean slate
t.mu.Lock()
newTorrents, _, err := t.rd.GetTorrents(0)
@@ -170,7 +168,7 @@ func (t *TorrentManager) getChecksum() string {
totalCount = torrentsResp.totalCount
case count = <-countChan:
case err := <-errChan:
t.log.Errorf("Checksum API Error: %v\n", err)
t.log.Warnf("Checksum API Error: %v\n", err)
return ""
}
}
@@ -199,7 +197,7 @@ func (t *TorrentManager) startRefreshJob() {
newTorrents, _, err := t.rd.GetTorrents(0)
if err != nil {
t.log.Errorf("Cannot get torrents: %v\n", err)
t.log.Warnf("Cannot get torrents: %v\n", err)
continue
}
t.log.Infof("Detected changes! Refreshing %d torrents", len(newTorrents))
@@ -271,7 +269,7 @@ func (t *TorrentManager) getMoreInfo(rdTorrent realdebrid.Torrent) *Torrent {
if info == nil {
info, err = t.rd.GetTorrentInfo(rdTorrent.ID)
if err != nil {
t.log.Errorf("Cannot get info for id=%s: %v\n", rdTorrent.ID, err)
t.log.Warnf("Cannot get info for id=%s: %v\n", rdTorrent.ID, err)
return nil
}
}
@@ -296,14 +294,13 @@ func (t *TorrentManager) getMoreInfo(rdTorrent realdebrid.Torrent) *Torrent {
})
}
if selectedFiles.Len() > len(info.Links) && info.Progress == 100 {
t.log.Debugf("%d links has expired for %s %s", selectedFiles.Len()-len(info.Links), info.ID, info.Name)
// chaotic file means RD will not output the desired file selection
// e.g. even if we select just a single mkv, it will output a rar
var isChaotic bool
selectedFiles, isChaotic = t.organizeChaos(info.Links, selectedFiles)
if isChaotic {
t.log.Errorf("Torrent id=%s %s is unfixable, it is always returning an unstreamable link (it is no longer shown in your directories)", info.ID, info.Name)
t.log.Debugf("You can try fixing it yourself magnet:?xt=urn:btih:%s", info.Hash)
// t.log.Warnf("Torrent id=%s %s is unfixable, it is always returning an unstreamable link (it is no longer shown in your directories)", info.ID, info.Name)
// t.log.Debugf("You can try fixing it yourself magnet:?xt=urn:btih:%s", info.Hash)
return nil
} else {
if streamableCount > 1 {
@@ -314,8 +311,8 @@ func (t *TorrentManager) getMoreInfo(rdTorrent realdebrid.Torrent) *Torrent {
t.log.Infof("Torrent id=%s %s marked for repair", info.ID, info.Name)
forRepair = true
} else {
t.log.Errorf("Torrent id=%s %s is unfixable, the lone streamable link has expired (it is no longer shown in your directories)", info.ID, info.Name)
t.log.Debugf("You can try fixing it yourself magnet:?xt=urn:btih:%s", info.Hash)
// t.log.Warnf("Torrent id=%s %s is unfixable, the lone streamable link has expired (it is no longer shown in your directories)", info.ID, info.Name)
// t.log.Debugf("You can try fixing it yourself magnet:?xt=urn:btih:%s", info.Hash)
return nil
}
}
@@ -525,9 +522,19 @@ func (t *TorrentManager) repairAll() {
}
func (t *TorrentManager) Repair(accessKey string) {
if _, exists := t.repairMap.Get(accessKey); exists {
return
}
t.repairMap.Set(accessKey, true)
if !t.config.EnableRepair() {
t.log.Warn("Repair is disabled; if you do not have other zurg instances running, you should enable repair")
return
}
torrent, _ := t.TorrentMap.Get(accessKey)
if torrent == nil {
t.log.Errorf("Cannot find torrent %s anymore to repair it", accessKey)
t.log.Warnf("Cannot find torrent %s anymore to repair it", accessKey)
return
}
if torrent.InProgress {
@@ -602,7 +609,7 @@ func (t *TorrentManager) reinsertTorrent(torrent *Torrent, missingFiles string)
// redownload torrent
resp, err := t.rd.AddMagnetHash(torrent.Instances[0].Hash)
if err != nil {
t.log.Errorf("Cannot redownload torrent: %v", err)
t.log.Warnf("Cannot redownload torrent: %v", err)
return false
}
time.Sleep(1 * time.Second)
@@ -611,7 +618,7 @@ func (t *TorrentManager) reinsertTorrent(torrent *Torrent, missingFiles string)
newTorrentID := resp.ID
err = t.rd.SelectTorrentFiles(newTorrentID, missingFiles)
if err != nil {
t.log.Errorf("Cannot start redownloading: %v", err)
t.log.Warnf("Cannot start redownloading: %v", err)
t.rd.DeleteTorrent(newTorrentID)
return false
}
@@ -620,13 +627,13 @@ func (t *TorrentManager) reinsertTorrent(torrent *Torrent, missingFiles string)
// see if the torrent is ready
info, err := t.rd.GetTorrentInfo(newTorrentID)
if err != nil {
t.log.Errorf("Cannot get info on redownloaded torrent id=%s : %v", newTorrentID, err)
t.log.Warnf("Cannot get info on redownloaded torrent id=%s : %v", newTorrentID, err)
t.rd.DeleteTorrent(newTorrentID)
return false
}
if info.Status == "magnet_error" || info.Status == "error" || info.Status == "virus" || info.Status == "dead" {
t.log.Errorf("Redownloaded torrent id=%s is in error state: %s", newTorrentID, info.Status)
t.log.Warnf("Redownloaded torrent id=%s is in error state: %s", newTorrentID, info.Status)
t.rd.DeleteTorrent(newTorrentID)
return false
}
@@ -656,7 +663,7 @@ func (t *TorrentManager) canCapacityHandle() bool {
for {
count, err := t.rd.GetActiveTorrentCount()
if err != nil {
t.log.Errorf("Cannot get active downloads count: %v", err)
t.log.Warnf("Cannot get active downloads count: %v", err)
if retryCount >= maxRetries {
t.log.Error("Max retries reached. Exiting.")
return false