glance/internal/widget/twitch-channels.go

53 lines
1.2 KiB
Go
Raw Normal View History

2024-04-27 21:10:24 +02:00
package widget
import (
"context"
"html/template"
"time"
"github.com/glanceapp/glance/internal/assets"
"github.com/glanceapp/glance/internal/feed"
)
type TwitchChannels struct {
widgetBase `yaml:",inline"`
ChannelsRequest []string `yaml:"channels"`
Channels []feed.TwitchChannel `yaml:"-"`
CollapseAfter int `yaml:"collapse-after"`
SortBy string `yaml:"sort-by"`
2024-04-27 21:10:24 +02:00
}
func (widget *TwitchChannels) Initialize() error {
widget.withTitle("Twitch Channels").withCacheDuration(time.Minute * 10)
if widget.CollapseAfter == 0 || widget.CollapseAfter < -1 {
widget.CollapseAfter = 5
}
2024-05-18 12:01:34 +02:00
if widget.SortBy != "viewers" && widget.SortBy != "live" {
widget.SortBy = "viewers"
}
2024-04-27 21:10:24 +02:00
return nil
}
func (widget *TwitchChannels) Update(ctx context.Context) {
channels, err := feed.FetchChannelsFromTwitch(widget.ChannelsRequest)
if !widget.canContinueUpdateAfterHandlingErr(err) {
return
}
if widget.SortBy == "viewers" {
channels.SortByViewers()
2024-05-18 12:01:34 +02:00
} else if widget.SortBy == "live" {
channels.SortByLive()
}
2024-04-27 21:10:24 +02:00
widget.Channels = channels
}
func (widget *TwitchChannels) Render() template.HTML {
return widget.render(widget, assets.TwitchChannelsTemplate)
}