glance/internal/feed/rss.go

247 lines
5.5 KiB
Go
Raw Normal View History

2024-04-27 21:10:24 +02:00
package feed
import (
"context"
"fmt"
2024-05-21 18:28:30 +02:00
"html"
2024-04-27 21:10:24 +02:00
"log/slog"
2024-05-28 19:29:14 +02:00
"net/url"
2024-05-21 18:28:30 +02:00
"regexp"
2024-04-27 21:10:24 +02:00
"sort"
2024-05-21 18:28:30 +02:00
"strings"
2024-04-27 21:10:24 +02:00
"time"
"github.com/mmcdole/gofeed"
2024-06-29 18:17:27 +02:00
gofeedext "github.com/mmcdole/gofeed/extensions"
2024-04-27 21:10:24 +02:00
)
type RSSFeedItem struct {
ChannelName string
ChannelURL string
Title string
Link string
ImageURL string
2024-05-21 18:28:30 +02:00
Categories []string
Description string
2024-04-27 21:10:24 +02:00
PublishedAt time.Time
}
2024-05-21 18:28:30 +02:00
// doesn't cover all cases but works the vast majority of the time
var htmlTagsWithAttributesPattern = regexp.MustCompile(`<\/?[a-zA-Z0-9-]+ *(?:[a-zA-Z-]+=(?:"|').*?(?:"|') ?)* *\/?>`)
var sequentialWhitespacePattern = regexp.MustCompile(`\s+`)
func sanitizeFeedDescription(description string) string {
if description == "" {
return ""
}
description = strings.ReplaceAll(description, "\n", " ")
description = htmlTagsWithAttributesPattern.ReplaceAllString(description, "")
description = sequentialWhitespacePattern.ReplaceAllString(description, " ")
description = strings.TrimSpace(description)
description = html.UnescapeString(description)
return description
}
func shortenFeedDescriptionLen(description string, maxLen int) string {
description, _ = limitStringLength(description, 1000)
description = sanitizeFeedDescription(description)
description, limited := limitStringLength(description, maxLen)
if limited {
description += "…"
}
return description
}
2024-04-27 21:10:24 +02:00
type RSSFeedRequest struct {
2024-05-21 18:28:30 +02:00
Url string `yaml:"url"`
Title string `yaml:"title"`
HideCategories bool `yaml:"hide-categories"`
HideDescription bool `yaml:"hide-description"`
2024-05-28 19:29:14 +02:00
ItemLinkPrefix string `yaml:"item-link-prefix"`
IsDetailed bool `yaml:"-"`
2024-04-27 21:10:24 +02:00
}
type RSSFeedItems []RSSFeedItem
func (f RSSFeedItems) SortByNewest() RSSFeedItems {
sort.Slice(f, func(i, j int) bool {
return f[i].PublishedAt.After(f[j].PublishedAt)
})
return f
}
var feedParser = gofeed.NewParser()
func getItemsFromRSSFeedTask(request RSSFeedRequest) ([]RSSFeedItem, error) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()
feed, err := feedParser.ParseURLWithContext(request.Url, ctx)
if err != nil {
return nil, err
}
items := make(RSSFeedItems, 0, len(feed.Items))
for i := range feed.Items {
item := feed.Items[i]
rssItem := RSSFeedItem{
ChannelURL: feed.Link,
2024-05-28 19:29:14 +02:00
}
if request.ItemLinkPrefix != "" {
rssItem.Link = request.ItemLinkPrefix + item.Link
} else if strings.HasPrefix(item.Link, "http://") || strings.HasPrefix(item.Link, "https://") {
rssItem.Link = item.Link
} else {
parsedUrl, err := url.Parse(feed.Link)
if err != nil {
parsedUrl, err = url.Parse(request.Url)
}
if err == nil {
var link string
2024-08-22 11:24:00 +02:00
if len(item.Link) > 0 && item.Link[0] == '/' {
2024-05-28 19:29:14 +02:00
link = item.Link
} else {
link = "/" + item.Link
}
rssItem.Link = parsedUrl.Scheme + "://" + parsedUrl.Host + link
}
2024-04-27 21:10:24 +02:00
}
if item.Title != "" {
rssItem.Title = item.Title
} else {
rssItem.Title = shortenFeedDescriptionLen(item.Description, 100)
}
2024-05-21 18:28:30 +02:00
if request.IsDetailed {
if !request.HideDescription && item.Description != "" && item.Title != "" {
rssItem.Description = shortenFeedDescriptionLen(item.Description, 200)
2024-05-21 18:28:30 +02:00
}
if !request.HideCategories {
var categories = make([]string, 0, 6)
2024-05-21 18:28:30 +02:00
for _, category := range item.Categories {
if len(categories) == 6 {
break
}
2024-05-21 18:28:30 +02:00
if len(category) == 0 || len(category) > 30 {
continue
}
2024-05-21 18:28:30 +02:00
categories = append(categories, category)
2024-05-21 18:28:30 +02:00
}
rssItem.Categories = categories
2024-05-21 18:28:30 +02:00
}
}
2024-04-27 21:10:24 +02:00
if request.Title != "" {
rssItem.ChannelName = request.Title
} else {
rssItem.ChannelName = feed.Title
}
if item.Image != nil {
rssItem.ImageURL = item.Image.URL
2024-06-29 18:17:27 +02:00
} else if url := findThumbnailInItemExtensions(item); url != "" {
rssItem.ImageURL = url
2024-04-27 21:10:24 +02:00
} else if feed.Image != nil {
if len(feed.Image.URL) > 0 && feed.Image.URL[0] == '/' {
rssItem.ImageURL = strings.TrimRight(feed.Link, "/") + feed.Image.URL
} else {
rssItem.ImageURL = feed.Image.URL
}
2024-04-27 21:10:24 +02:00
}
if item.PublishedParsed != nil {
rssItem.PublishedAt = *item.PublishedParsed
} else {
rssItem.PublishedAt = time.Now()
}
items = append(items, rssItem)
}
return items, nil
}
2024-06-29 18:17:27 +02:00
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)
}
2024-04-27 21:10:24 +02:00
func GetItemsFromRSSFeeds(requests []RSSFeedRequest) (RSSFeedItems, error) {
job := newJob(getItemsFromRSSFeedTask, requests).withWorkers(10)
feeds, errs, err := workerPoolDo(job)
if err != nil {
return nil, fmt.Errorf("%w: %v", ErrNoContent, err)
}
failed := 0
entries := make(RSSFeedItems, 0, len(feeds)*10)
for i := range feeds {
if errs[i] != nil {
failed++
slog.Error("failed to get rss feed", "error", errs[i], "url", requests[i].Url)
continue
}
entries = append(entries, feeds[i]...)
}
if failed == len(requests) {
2024-04-27 21:10:24 +02:00
return nil, ErrNoContent
}
entries.SortByNewest()
if failed > 0 {
return entries, fmt.Errorf("%w: missing %d RSS feeds", ErrPartialContent, failed)
}
return entries, nil
}