Verify if downloadable

This commit is contained in:
Ben Sarmiento
2024-05-25 16:02:18 +02:00
parent b8c67a83cf
commit 7886ed69a2
3 changed files with 22 additions and 22 deletions

View File

@@ -93,12 +93,12 @@ func NewTorrentManager(cfg config.ConfigInterface, api *realdebrid.RealDebrid, w
} }
// proxy function // proxy function
func (t *TorrentManager) UnrestrictLinkUntilOk(link string) *realdebrid.Download { func (t *TorrentManager) UnrestrictLinkUntilOk(link string, checkFirstByte bool) *realdebrid.Download {
if strings.HasPrefix(link, "https://real-debrid.com/d/") && t.DownloadMap.Has(link[0:39]) { if strings.HasPrefix(link, "https://real-debrid.com/d/") && t.DownloadMap.Has(link[0:39]) {
ret, _ := t.DownloadMap.Get(link[0:39]) ret, _ := t.DownloadMap.Get(link[0:39])
return ret return ret
} }
ret, err := t.api.UnrestrictLink(link, t.Config.ShouldServeFromRclone()) ret, err := t.api.UnrestrictLink(link, checkFirstByte)
t.DownloadMap.Set(ret.Link[0:39], ret) t.DownloadMap.Set(ret.Link[0:39], ret)
if err != nil { if err != nil {
t.log.Warnf("Cannot unrestrict link %s: %v", link, err) t.log.Warnf("Cannot unrestrict link %s: %v", link, err)
@@ -108,11 +108,11 @@ func (t *TorrentManager) UnrestrictLinkUntilOk(link string) *realdebrid.Download
return ret return ret
} }
func (t *TorrentManager) UnrestrictFileUntilOk(file *File) *realdebrid.Download { func (t *TorrentManager) UnrestrictFileUntilOk(file *File, checkFirstByte bool) *realdebrid.Download {
if !file.State.Is("ok_file") { if !file.State.Is("ok_file") {
return nil return nil
} }
return t.UnrestrictLinkUntilOk(file.Link) return t.UnrestrictLinkUntilOk(file.Link, checkFirstByte)
} }
func (t *TorrentManager) GetKey(torrent *Torrent) string { func (t *TorrentManager) GetKey(torrent *Torrent) string {

View File

@@ -177,7 +177,7 @@ func (t *TorrentManager) repair(torrent *Torrent) {
if !file.State.Is("ok_file") { if !file.State.Is("ok_file") {
return return
} }
if t.UnrestrictFileUntilOk(file) == nil { if t.UnrestrictFileUntilOk(file, true) == nil {
file.State.Event(context.Background(), "break_file") file.State.Event(context.Background(), "break_file")
} }
}) })
@@ -292,7 +292,7 @@ func (t *TorrentManager) assignLinks(torrent *Torrent) bool {
torrent.UnassignedLinks.Each(func(link string) bool { torrent.UnassignedLinks.Each(func(link string) bool {
// unrestrict each unassigned link that was filled out during torrent init // unrestrict each unassigned link that was filled out during torrent init
unrestrict := t.UnrestrictLinkUntilOk(link) unrestrict := t.UnrestrictLinkUntilOk(link, true)
if unrestrict == nil { if unrestrict == nil {
expiredCount++ expiredCount++
return false // next unassigned link return false // next unassigned link
@@ -393,6 +393,13 @@ func (t *TorrentManager) redownloadTorrent(torrent *Torrent, selection []string)
prevState := t.latestState prevState := t.latestState
resp, err := t.api.AddMagnetHash(torrent.Hash) resp, err := t.api.AddMagnetHash(torrent.Hash)
if err != nil { if err != nil {
if strings.Contains(err.Error(), "timeout") {
newState := t.getCurrentState()
if prevState.Eq(newState) {
return t.redownloadTorrent(torrent, selection)
}
newTorrentID = t.latestState.FirstTorrentId
} else {
if strings.Contains(err.Error(), "infringing") { if strings.Contains(err.Error(), "infringing") {
t.markAsUnfixable(torrent, "infringing torrent") t.markAsUnfixable(torrent, "infringing torrent")
} else if strings.Contains(err.Error(), "unsupported") { } else if strings.Contains(err.Error(), "unsupported") {
@@ -406,13 +413,6 @@ func (t *TorrentManager) redownloadTorrent(torrent *Torrent, selection []string)
} else if strings.Contains(err.Error(), "allowed") { } else if strings.Contains(err.Error(), "allowed") {
t.markAsUnfixable(torrent, "torrent not allowed") t.markAsUnfixable(torrent, "torrent not allowed")
} }
if strings.Contains(err.Error(), "timeout") {
newState := t.getCurrentState()
if prevState.Eq(newState) {
return t.redownloadTorrent(torrent, selection)
}
newTorrentID = t.latestState.FirstTorrentId
} else {
return nil, fmt.Errorf("cannot add magnet of torrent %s (hash=%s): %v", t.GetKey(torrent), torrent.Hash, err) return nil, fmt.Errorf("cannot add magnet of torrent %s (hash=%s): %v", t.GetKey(torrent), torrent.Hash, err)
} }
} }
@@ -590,7 +590,7 @@ func (t *TorrentManager) isStillBroken(info *realdebrid.TorrentInfo, brokenFiles
// check if the broken files can now be unrestricted // check if the broken files can now be unrestricted
for _, oldFile := range brokenFiles { for _, oldFile := range brokenFiles {
for idx, newFile := range selectedFiles { for idx, newFile := range selectedFiles {
if oldFile.ID == newFile.ID && t.UnrestrictFileUntilOk(selectedFiles[idx]) == nil { if oldFile.ID == newFile.ID && t.UnrestrictFileUntilOk(selectedFiles[idx], true) == nil {
return true return true
} }
} }

View File

@@ -61,7 +61,7 @@ func (dl *Downloader) DownloadFile(
return return
} }
unrestrict := torMgr.UnrestrictFileUntilOk(file) unrestrict := torMgr.UnrestrictFileUntilOk(file, cfg.ShouldServeFromRclone())
if unrestrict == nil { if unrestrict == nil {
log.Warnf("File %s cannot be unrestricted (link=%s)", fileName, file.Link) log.Warnf("File %s cannot be unrestricted (link=%s)", fileName, file.Link)
if err := file.State.Event(context.Background(), "break_file"); err != nil { if err := file.State.Event(context.Background(), "break_file"); err != nil {
@@ -104,7 +104,7 @@ func (dl *Downloader) DownloadLink(
log *logutil.Logger, log *logutil.Logger,
) { ) {
// log.Debugf("Opening file %s (%s)", fileName, link) // log.Debugf("Opening file %s (%s)", fileName, link)
unrestrict := torMgr.UnrestrictLinkUntilOk(link) unrestrict := torMgr.UnrestrictLinkUntilOk(link, cfg.ShouldServeFromRclone())
if unrestrict == nil { if unrestrict == nil {
log.Warnf("File %s cannot be unrestricted (link=%s)", fileName, link) log.Warnf("File %s cannot be unrestricted (link=%s)", fileName, link)
http.Error(resp, "File is not available", http.StatusInternalServerError) http.Error(resp, "File is not available", http.StatusInternalServerError)