rss.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package widget
  2. import (
  3. "context"
  4. "html/template"
  5. "time"
  6. "github.com/glanceapp/glance/internal/assets"
  7. "github.com/glanceapp/glance/internal/feed"
  8. )
  9. type RSS struct {
  10. widgetBase `yaml:",inline"`
  11. FeedRequests []feed.RSSFeedRequest `yaml:"feeds"`
  12. Style string `yaml:"style"`
  13. ThumbnailHeight float64 `yaml:"thumbnail-height"`
  14. CardHeight float64 `yaml:"card-height"`
  15. Items feed.RSSFeedItems `yaml:"-"`
  16. Limit int `yaml:"limit"`
  17. CollapseAfter int `yaml:"collapse-after"`
  18. }
  19. func (widget *RSS) Initialize() error {
  20. widget.withTitle("RSS Feed").withCacheDuration(1 * time.Hour)
  21. if widget.Limit <= 0 {
  22. widget.Limit = 25
  23. }
  24. if widget.CollapseAfter == 0 || widget.CollapseAfter < -1 {
  25. widget.CollapseAfter = 5
  26. }
  27. if widget.ThumbnailHeight < 0 {
  28. widget.ThumbnailHeight = 0
  29. }
  30. if widget.CardHeight < 0 {
  31. widget.CardHeight = 0
  32. }
  33. if widget.Style != "detailed-list" {
  34. for i := range widget.FeedRequests {
  35. widget.FeedRequests[i].HideCategories = true
  36. widget.FeedRequests[i].HideDescription = true
  37. }
  38. }
  39. return nil
  40. }
  41. func (widget *RSS) Update(ctx context.Context) {
  42. items, err := feed.GetItemsFromRSSFeeds(widget.FeedRequests)
  43. if !widget.canContinueUpdateAfterHandlingErr(err) {
  44. return
  45. }
  46. if len(items) > widget.Limit {
  47. items = items[:widget.Limit]
  48. }
  49. widget.Items = items
  50. }
  51. func (widget *RSS) Render() template.HTML {
  52. if widget.Style == "horizontal-cards" {
  53. return widget.render(widget, assets.RSSHorizontalCardsTemplate)
  54. }
  55. if widget.Style == "horizontal-cards-2" {
  56. return widget.render(widget, assets.RSSHorizontalCards2Template)
  57. }
  58. if widget.Style == "detailed-list" {
  59. return widget.render(widget, assets.RSSDetailedListTemplate)
  60. }
  61. return widget.render(widget, assets.RSSListTemplate)
  62. }