twitch-top-games.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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 TwitchGames struct {
  10. widgetBase `yaml:",inline"`
  11. Categories []feed.TwitchCategory `yaml:"-"`
  12. Exclude []string `yaml:"exclude"`
  13. Limit int `yaml:"limit"`
  14. CollapseAfter int `yaml:"collapse-after"`
  15. }
  16. func (widget *TwitchGames) Initialize() error {
  17. widget.
  18. withTitle("Top games on Twitch").
  19. withTitleURL("https://www.twitch.tv/directory?sort=VIEWER_COUNT").
  20. withCacheDuration(time.Minute * 10)
  21. if widget.Limit <= 0 {
  22. widget.Limit = 10
  23. }
  24. if widget.CollapseAfter == 0 || widget.CollapseAfter < -1 {
  25. widget.CollapseAfter = 5
  26. }
  27. return nil
  28. }
  29. func (widget *TwitchGames) Update(ctx context.Context) {
  30. categories, err := feed.FetchTopGamesFromTwitch(widget.Exclude, widget.Limit)
  31. if !widget.canContinueUpdateAfterHandlingErr(err) {
  32. return
  33. }
  34. widget.Categories = categories
  35. }
  36. func (widget *TwitchGames) Render() template.HTML {
  37. return widget.render(widget, assets.TwitchGamesListTemplate)
  38. }