Fix unrestrict issue

This commit is contained in:
Ben Sarmiento
2023-11-28 00:41:15 +01:00
parent c8334ecb3b
commit 3d380e468f
9 changed files with 131 additions and 68 deletions

View File

@@ -27,6 +27,7 @@ const (
type TorrentManager struct {
DirectoryMap cmap.ConcurrentMap[string, cmap.ConcurrentMap[string, *Torrent]] // directory -> accessKey -> Torrent
DownloadCache cmap.ConcurrentMap[string, *realdebrid.Download]
checksum string
latestAdded string
requiredVersion string
@@ -70,10 +71,35 @@ func NewTorrentManager(cfg config.ConfigInterface, api *realdebrid.RealDebrid, p
t.DirectoryMap.Set(directory, cmap.New[*Torrent]())
}
newTorrents, _, err := t.api.GetTorrents(0)
if err != nil {
t.log.Fatalf("Cannot get torrents: %v\n", err)
}
var initWait sync.WaitGroup
initWait.Add(2)
// Fetch downloads
go func() {
defer initWait.Done()
downloads, _, err := t.api.GetDownloads()
if err != nil {
t.log.Fatalf("Cannot get downloads: %v\n", err)
}
t.DownloadCache = cmap.New[*realdebrid.Download]()
for _, download := range downloads {
if !t.DownloadCache.Has(download.Link) {
t.DownloadCache.Set(download.Link, &download)
}
}
}()
// Fetch torrents
var newTorrents []realdebrid.Torrent
go func() {
defer initWait.Done()
newTorrents, _, err = t.api.GetTorrents(0)
if err != nil {
t.log.Fatalf("Cannot get torrents: %v\n", err)
}
}()
initWait.Wait()
torrentsChan := make(chan *Torrent, len(newTorrents))
var wg sync.WaitGroup
@@ -178,8 +204,8 @@ func (t *TorrentManager) mergeToMain(mainTorrent, torrentToMerge *Torrent) *Torr
}
// proxy
func (t *TorrentManager) UnrestrictUntilOk(link string) *realdebrid.UnrestrictResponse {
retChan := make(chan *realdebrid.UnrestrictResponse, 1)
func (t *TorrentManager) UnrestrictUntilOk(link string) *realdebrid.Download {
retChan := make(chan *realdebrid.Download, 1)
t.unrestrictPool.Submit(func() {
retChan <- t.api.UnrestrictUntilOk(link, t.cfg.ShouldServeFromRclone())
time.Sleep(1 * time.Second)
@@ -388,7 +414,7 @@ func (t *TorrentManager) getMoreInfo(rdTorrent realdebrid.Torrent) *Torrent {
var info *realdebrid.TorrentInfo
var err error
// file cache
torrentFromFile := t.readFromFile(rdTorrent.ID)
torrentFromFile := t.readTorrentFromFile(rdTorrent.ID)
if torrentFromFile != nil && len(torrentFromFile.ID) > 0 && len(torrentFromFile.Links) > 0 && len(torrentFromFile.Links) == len(rdTorrent.Links) && torrentFromFile.Links[0] == rdTorrent.Links[0] {
info = torrentFromFile
info.Progress = rdTorrent.Progress
@@ -440,7 +466,7 @@ func (t *TorrentManager) getMoreInfo(rdTorrent realdebrid.Torrent) *Torrent {
}
if len(selectedFiles) > 0 && torrentFromFile == nil {
t.writeToFile(info) // only when there are selected files, else it's useless
t.writeTorrentToFile(info) // only when there are selected files, else it's useless
}
infoCache.Set(rdTorrent.ID, &torrent)
@@ -462,7 +488,7 @@ func (t *TorrentManager) getName(name, originalName string) string {
}
}
func (t *TorrentManager) writeToFile(torrent *realdebrid.TorrentInfo) error {
func (t *TorrentManager) writeTorrentToFile(torrent *realdebrid.TorrentInfo) error {
filePath := DATA_DIR + "/" + torrent.ID + ".bin"
file, err := os.Create(filePath)
if err != nil {
@@ -481,7 +507,7 @@ func (t *TorrentManager) writeToFile(torrent *realdebrid.TorrentInfo) error {
return nil
}
func (t *TorrentManager) readFromFile(torrentID string) *realdebrid.TorrentInfo {
func (t *TorrentManager) readTorrentFromFile(torrentID string) *realdebrid.TorrentInfo {
filePath := DATA_DIR + "/" + torrentID + ".bin"
file, err := os.Open(filePath)
if err != nil {
@@ -506,7 +532,7 @@ func (t *TorrentManager) readFromFile(torrentID string) *realdebrid.TorrentInfo
func (t *TorrentManager) organizeChaos(links []string, selectedFiles []*File) ([]*File, bool) {
type Result struct {
Response *realdebrid.UnrestrictResponse
Response *realdebrid.Download
}
resultsChan := make(chan Result, len(links))