Skip to content

Commit

Permalink
Fix missing RSS thumbnail images
Browse files Browse the repository at this point in the history
  • Loading branch information
svilenmarkov committed Aug 9, 2024
1 parent b8b9045 commit 139937f
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions internal/feed/rss.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"time"

"github.com/mmcdole/gofeed"
gofeedext "github.com/mmcdole/gofeed/extensions"
)

type RSSFeedItem struct {
Expand Down Expand Up @@ -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
}
Expand All @@ -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)
Expand Down

0 comments on commit 139937f

Please sign in to comment.