From 39f40670d097ab32e518b02a80a1c0d48230f91a Mon Sep 17 00:00:00 2001 From: Svilen Markov <7613769+svilenmarkov@users.noreply.github.com> Date: Sat, 29 Jun 2024 17:17:27 +0100 Subject: [PATCH] Fix missing RSS thumbnail images --- internal/feed/rss.go | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/internal/feed/rss.go b/internal/feed/rss.go index 4d22af6..74f064b 100644 --- a/internal/feed/rss.go +++ b/internal/feed/rss.go @@ -12,6 +12,7 @@ import ( "time" "github.com/mmcdole/gofeed" + gofeedext "github.com/mmcdole/gofeed/extensions" ) type RSSFeedItem struct { @@ -145,6 +146,8 @@ func getItemsFromRSSFeedTask(request RSSFeedRequest) ([]RSSFeedItem, error) { if item.Image != nil { rssItem.ImageURL = item.Image.URL + } else if url := findThumbnailInItemExtensions(item); url != "" { + rssItem.ImageURL = url } else if feed.Image != nil { rssItem.ImageURL = feed.Image.URL } @@ -161,6 +164,36 @@ func getItemsFromRSSFeedTask(request RSSFeedRequest) ([]RSSFeedItem, error) { return items, nil } +func recursiveFindThumbnailInExtensions(extensions map[string][]gofeedext.Extension) string { + for _, exts := range extensions { + for _, ext := range exts { + if ext.Name == "thumbnail" || ext.Name == "image" { + if url, ok := ext.Attrs["url"]; ok { + return url + } + } + + if ext.Children != nil { + if url := recursiveFindThumbnailInExtensions(ext.Children); url != "" { + return url + } + } + } + } + + return "" +} + +func findThumbnailInItemExtensions(item *gofeed.Item) string { + media, ok := item.Extensions["media"] + + if !ok { + return "" + } + + return recursiveFindThumbnailInExtensions(media) +} + func GetItemsFromRSSFeeds(requests []RSSFeedRequest) (RSSFeedItems, error) { job := newJob(getItemsFromRSSFeedTask, requests).withWorkers(10) feeds, errs, err := workerPoolDo(job)