fix(feeds): RSS improve torrent size parsing (#667)

* Fixes issue 636 - fail to parse torrent size on some RSS feeds when enclosure is a fixed value

* Pick the biggest size

* minor comment

* Updates release size when new parsed string size is bigger

- Sets default size of `Release` to zero (unparsed)
- When there are parsing errors, then it doesn't RESET the size to 0 (assuming a tracker providing some size that can't be parsed)
- Updates the size to the new size only if the parsed size is bigger than the previous.

* fix(feeds): ignore default 39399 enclosure size

---------

Co-authored-by: ze0s <43699394+zze0s@users.noreply.github.com>
This commit is contained in:
Ignacio 2023-02-05 10:18:17 -08:00 committed by GitHub
parent c17034dcff
commit 9a47dbc588
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 10 deletions

View file

@ -185,6 +185,7 @@ func NewRelease(indexer string) *Release {
Implementation: ReleaseImplementationIRC, Implementation: ReleaseImplementationIRC,
Timestamp: time.Now(), Timestamp: time.Now(),
Tags: []string{}, Tags: []string{},
Size: 0,
} }
return r return r
@ -272,13 +273,13 @@ func (r *Release) ParseReleaseTagsString(tags string) {
} }
} }
// ParseSizeBytesString If there are parsing errors, then it keeps the original (or default size 0)
// Otherwise, it will update the size only if the new size is bigger than the previous one.
func (r *Release) ParseSizeBytesString(size string) { func (r *Release) ParseSizeBytesString(size string) {
s, err := humanize.ParseBytes(size) s, err := humanize.ParseBytes(size)
if err != nil { if err == nil && s > r.Size {
// log could not parse into bytes
r.Size = 0
}
r.Size = s r.Size = s
}
} }
func (r *Release) DownloadTorrentFileCtx(ctx context.Context) error { func (r *Release) DownloadTorrentFileCtx(ctx context.Context) error {

View file

@ -113,7 +113,7 @@ func (j *RSSJob) processItem(item *gofeed.Item) *domain.Release {
if e.Type == "application/x-bittorrent" && e.URL != "" { if e.Type == "application/x-bittorrent" && e.URL != "" {
rls.TorrentURL = e.URL rls.TorrentURL = e.URL
} }
if e.Length != "" { if e.Length != "" && e.Length != "39399" {
rls.ParseSizeBytesString(e.Length) rls.ParseSizeBytesString(e.Length)
} }
} }
@ -154,11 +154,9 @@ func (j *RSSJob) processItem(item *gofeed.Item) *domain.Release {
rls.Uploader += v.Name rls.Uploader += v.Name
} }
if rls.Size == 0 { // When custom->size and enclosures->size differ, `ParseSizeBytesString` will pick the largest one.
// parse size bytes string if size, ok := item.Custom["size"]; ok {
if sz, ok := item.Custom["size"]; ok { rls.ParseSizeBytesString(size)
rls.ParseSizeBytesString(sz)
}
} }
// additional size parsing // additional size parsing