videos.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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 Videos struct {
  10. widgetBase `yaml:",inline"`
  11. Videos feed.Videos `yaml:"-"`
  12. VideoUrlTemplate string `yaml:"video-url-template"`
  13. Style string `yaml:"style"`
  14. CollapseAfterRows int `yaml:"collapse-after-rows"`
  15. Channels []string `yaml:"channels"`
  16. Limit int `yaml:"limit"`
  17. }
  18. func (widget *Videos) Initialize() error {
  19. widget.withTitle("Videos").withCacheDuration(time.Hour)
  20. if widget.Limit <= 0 {
  21. widget.Limit = 25
  22. }
  23. if widget.CollapseAfterRows == 0 || widget.CollapseAfterRows < -1 {
  24. widget.CollapseAfterRows = 4
  25. }
  26. return nil
  27. }
  28. func (widget *Videos) Update(ctx context.Context) {
  29. videos, err := feed.FetchYoutubeChannelUploads(widget.Channels, widget.VideoUrlTemplate)
  30. if !widget.canContinueUpdateAfterHandlingErr(err) {
  31. return
  32. }
  33. if len(videos) > widget.Limit {
  34. videos = videos[:widget.Limit]
  35. }
  36. widget.Videos = videos
  37. }
  38. func (widget *Videos) Render() template.HTML {
  39. if widget.Style == "grid-cards" {
  40. return widget.render(widget, assets.VideosGridTemplate)
  41. }
  42. return widget.render(widget, assets.VideosTemplate)
  43. }