glance/internal/widget/twitch-channels.go

56 lines
1.3 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 {
2024-06-29 17:10:43 +02:00
widget.
withTitle("Twitch Channels").
withTitleURL("https://www.twitch.tv/directory/following").
withCacheDuration(time.Minute * 10)
2024-04-27 21:10:24 +02:00
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)
}