From abeb81eea9786961319c4a4f80f4055127371e9e Mon Sep 17 00:00:00 2001 From: ze0s <43699394+zze0s@users.noreply.github.com> Date: Mon, 2 Sep 2024 16:39:40 +0200 Subject: [PATCH] fix(feeds): UNIT3D RSS size parsing (#1701) * fix(feeds): size parsing for UNIT3D RSS feeds * fix(feeds): imports --- internal/feed/rss.go | 52 +++++++++++++++++++++++++++++++++++--------- 1 file changed, 42 insertions(+), 10 deletions(-) diff --git a/internal/feed/rss.go b/internal/feed/rss.go index ace6739..9bdb2aa 100644 --- a/internal/feed/rss.go +++ b/internal/feed/rss.go @@ -17,6 +17,7 @@ import ( "github.com/autobrr/autobrr/internal/release" "github.com/autobrr/autobrr/pkg/errors" + "github.com/dustin/go-humanize" "github.com/mmcdole/gofeed" "github.com/rs/zerolog" ) @@ -182,11 +183,32 @@ func (j *RSSJob) processItem(item *gofeed.Item) *domain.Release { rls.Uploader += v.Name } + if item.Description != "" { + rls.Description = item.Description + + if readSizeFromDescription(item.Description, rls) { + j.Log.Trace().Msgf("Set new size %d from description", rls.Size) + } + } + // When custom->size and enclosures->size differ, `ParseSizeBytesString` will pick the largest one. if size, ok := item.Custom["size"]; ok { rls.ParseSizeBytesString(size) } + if customContentLength, ok := item.Custom["contentlength"]; ok { + if customContentLength != "" { + size, err := strconv.ParseUint(customContentLength, 10, 64) + if err != nil { + j.Log.Error().Err(err).Msgf("could not parse item.Custom.ContentLength: %s", customContentLength) + } + + if size > rls.Size { + rls.Size = size + } + } + } + // additional size parsing // some feeds have a fixed size for enclosure so lets check for custom elements // and parse size from there if it differs @@ -213,13 +235,6 @@ func (j *RSSJob) processItem(item *gofeed.Item) *domain.Release { rls.Bonus = []string{"Freeleech"} } - if item.Description != "" { - rls.Description = item.Description - - readSizeFromDescription(item.Description, rls) - j.Log.Trace().Msgf("Set new size %d from description", rls.Size) - } - // add cookie to release for download if needed if j.Feed.Cookie != "" { rls.RawCookie = j.Feed.Cookie @@ -340,16 +355,33 @@ func isFreeleech(str []string) bool { } // readSizeFromDescription get size from description -func readSizeFromDescription(str string, r *domain.Release) { +func readSizeFromDescription(str string, r *domain.Release) bool { clean := rxpHTML.ReplaceAllString(str, " ") + + found := false + for _, sz := range rxpSize.FindAllString(clean, -1) { - r.ParseSizeBytesString(sz) + if sz == "" { + continue + } + + s, err := humanize.ParseBytes(sz) + if err != nil { + continue + } + + if s > r.Size { + found = true + r.Size = s + } } + + return found } // itemCustomElement // used for some feeds like Aviztas network type itemCustomElement struct { - ContentLength int64 `xml:"contentLength"` + ContentLength int64 `xml:"contentLength,contentlength"` InfoHash string `xml:"infoHash"` }