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"`
|
2024-05-18 02:07:34 +02:00
|
|
|
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" {
|
2024-05-18 02:07:34 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-05-18 02:07:34 +02:00
|
|
|
if widget.SortBy == "viewers" {
|
|
|
|
channels.SortByViewers()
|
2024-05-18 12:01:34 +02:00
|
|
|
} else if widget.SortBy == "live" {
|
2024-05-18 02:07:34 +02:00
|
|
|
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)
|
|
|
|
}
|