diff --git a/internal/torrent/manager.go b/internal/torrent/manager.go index 8167b2d..a61253f 100644 --- a/internal/torrent/manager.go +++ b/internal/torrent/manager.go @@ -288,6 +288,26 @@ func (t *TorrentManager) readTorrentFromFile(filePath string) *Torrent { if torrent.Version != t.requiredVersion { return nil } + + torrent.SelectedFiles.IterCb(func(_ string, file *File) { + if strings.HasPrefix(file.Link, "https://real-debrid.com/d/") { + // set link to max 39 chars (26 + 13) + file.Link = file.Link[0:39] + } + }) + + unassignedLinks := mapset.NewSet[string]() + torrent.UnassignedLinks.Each(func(link string) bool { + if strings.HasPrefix(link, "https://real-debrid.com/d/") { + // set link to max 39 chars (26 + 13) + unassignedLinks.Add(link[0:39]) + } else { + unassignedLinks.Add(link) + } + return false + }) + torrent.UnassignedLinks = unassignedLinks + return torrent } @@ -342,6 +362,9 @@ func (t *TorrentManager) readInfoFromFile(torrentID string) *realdebrid.TorrentI if err := json.Unmarshal(jsonData, &info); err != nil { return nil } + if info.Progress != 100 { + return nil + } return info } diff --git a/internal/torrent/refresh.go b/internal/torrent/refresh.go index 3a64400..31b827b 100644 --- a/internal/torrent/refresh.go +++ b/internal/torrent/refresh.go @@ -180,6 +180,12 @@ func (t *TorrentManager) getMoreInfo(rdTorrent realdebrid.Torrent) *realdebrid.T } t.writeInfoToFile(info) } + for i := range info.Links { + if strings.HasPrefix(info.Links[i], "https://real-debrid.com/d/") { + // set link to max 39 chars (26 + 13) + info.Links[i] = info.Links[i][0:39] + } + } return info } @@ -226,9 +232,6 @@ func (t *TorrentManager) convertToTorrent(info *realdebrid.TorrentInfo) *Torrent // all links are still intact! good! for i, file := range selectedFiles { file.Link = info.Links[i] - if strings.HasPrefix(file.Link, "https://real-debrid.com/d/") { - file.Link = file.Link[0:39] - } file.State.Event(context.Background(), "repair_file") } torrent.UnassignedLinks = mapset.NewSet[string]() @@ -236,9 +239,6 @@ func (t *TorrentManager) convertToTorrent(info *realdebrid.TorrentInfo) *Torrent } else { torrent.UnassignedLinks = mapset.NewSet[string]() for _, link := range info.Links { - if strings.HasPrefix(link, "https://real-debrid.com/d/") { - link = link[0:39] - } torrent.UnassignedLinks.Add(link) } } diff --git a/internal/torrent/repair.go b/internal/torrent/repair.go index 88cacb9..4f0811c 100644 --- a/internal/torrent/repair.go +++ b/internal/torrent/repair.go @@ -354,9 +354,6 @@ func (t *TorrentManager) assignLinks(torrent *Torrent) bool { if !assigned && file.State.Is("broken_file") && file.Bytes == unrestrict.Filesize && strings.HasSuffix(strings.ToLower(file.Path), strings.ToLower(unrestrict.Filename)) { file.Link = link assignedLinks = append(assignedLinks, link) - if strings.HasPrefix(file.Link, "https://real-debrid.com/d/") { - file.Link = file.Link[0:39] - } file.State.Event(context.Background(), "repair_file") assigned = true assignedCount++ @@ -531,6 +528,12 @@ func (t *TorrentManager) redownloadTorrent(torrent *Torrent, selection []string) t.DeleteByID(newTorrentID) return nil, fmt.Errorf("cannot get info on redownloaded : %v", err) } + for i := range info.Links { + if strings.HasPrefix(info.Links[i], "https://real-debrid.com/d/") { + // set link to max 39 chars (26 + 13) + info.Links[i] = info.Links[i][0:39] + } + } if info.Status == "magnet_conversion" { time.Sleep(60 * time.Second) @@ -658,9 +661,6 @@ func (t *TorrentManager) checkIfBroken(info *realdebrid.TorrentInfo, brokenFiles for i, file := range selectedFiles { file.Link = info.Links[i] - if strings.HasPrefix(file.Link, "https://real-debrid.com/d/") { - file.Link = file.Link[0:39] - } file.State.Event(context.Background(), "repair_file") } diff --git a/pkg/http/client.go b/pkg/http/client.go index 2a9ec5b..894e6b0 100644 --- a/pkg/http/client.go +++ b/pkg/http/client.go @@ -276,11 +276,14 @@ func (r *HTTPClient) shouldRetry(req *http.Request, resp *http.Response, err err return -1 // don't retry } } else if downloadErr, ok := err.(*DownloadErrorResponse); ok { - switch downloadErr.Code { - case http.StatusServiceUnavailable: // Service unavailable + switch downloadErr.Message { + case "bytes_limit_reached": // 503 return -1 - default: + case "invalid_download_code": // 404 + time.Sleep(time.Duration(rateLimitSleep) * time.Second) return 1 + default: + return 1 // retry } } if err != nil && strings.Contains(err.Error(), "timeout") { diff --git a/pkg/realdebrid/api.go b/pkg/realdebrid/api.go index 450eda5..2cdd1f6 100644 --- a/pkg/realdebrid/api.go +++ b/pkg/realdebrid/api.go @@ -78,12 +78,12 @@ func (rd *RealDebrid) UnrestrictAndVerify(link string) (*Download, error) { rd.TokenManager.SetTokenAsExpired(token, "bandwidth limit exceeded") continue } - if err != nil { - return nil, err + if err == nil { + rd.verifiedLinks.Set(download.ID, time.Now().Unix()+60*60*24) + return download, nil } - rd.verifiedLinks.Set(download.ID, time.Now().Unix()+60*60*24) - - return download, nil + rd.verifiedLinks.Remove(download.ID) + tokenMap.Remove(link) } download, err := rd.UnrestrictLinkWithToken(link, token)