twitch-channels.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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 TwitchChannels struct {
  10. widgetBase `yaml:",inline"`
  11. ChannelsRequest []string `yaml:"channels"`
  12. Channels []feed.TwitchChannel `yaml:"-"`
  13. CollapseAfter int `yaml:"collapse-after"`
  14. SortBy string `yaml:"sort-by"`
  15. }
  16. func (widget *TwitchChannels) Initialize() error {
  17. widget.
  18. withTitle("Twitch Channels").
  19. withTitleURL("https://www.twitch.tv/directory/following").
  20. withCacheDuration(time.Minute * 10)
  21. if widget.CollapseAfter == 0 || widget.CollapseAfter < -1 {
  22. widget.CollapseAfter = 5
  23. }
  24. if widget.SortBy != "viewers" && widget.SortBy != "live" {
  25. widget.SortBy = "viewers"
  26. }
  27. return nil
  28. }
  29. func (widget *TwitchChannels) Update(ctx context.Context) {
  30. channels, err := feed.FetchChannelsFromTwitch(widget.ChannelsRequest)
  31. if !widget.canContinueUpdateAfterHandlingErr(err) {
  32. return
  33. }
  34. if widget.SortBy == "viewers" {
  35. channels.SortByViewers()
  36. } else if widget.SortBy == "live" {
  37. channels.SortByLive()
  38. }
  39. widget.Channels = channels
  40. }
  41. func (widget *TwitchChannels) Render() template.HTML {
  42. return widget.render(widget, assets.TwitchChannelsTemplate)
  43. }