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 Videos struct {
|
2024-05-17 13:15:44 +02:00
|
|
|
widgetBase `yaml:",inline"`
|
|
|
|
Videos feed.Videos `yaml:"-"`
|
|
|
|
VideoUrlTemplate string `yaml:"video-url-template"`
|
|
|
|
Style string `yaml:"style"`
|
|
|
|
CollapseAfterRows int `yaml:"collapse-after-rows"`
|
|
|
|
Channels []string `yaml:"channels"`
|
|
|
|
Limit int `yaml:"limit"`
|
2024-04-27 21:10:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (widget *Videos) Initialize() error {
|
|
|
|
widget.withTitle("Videos").withCacheDuration(time.Hour)
|
|
|
|
|
|
|
|
if widget.Limit <= 0 {
|
|
|
|
widget.Limit = 25
|
|
|
|
}
|
|
|
|
|
2024-05-17 13:15:44 +02:00
|
|
|
if widget.CollapseAfterRows == 0 || widget.CollapseAfterRows < -1 {
|
|
|
|
widget.CollapseAfterRows = 4
|
|
|
|
}
|
|
|
|
|
2024-04-27 21:10:24 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (widget *Videos) Update(ctx context.Context) {
|
2024-05-01 20:08:25 +02:00
|
|
|
videos, err := feed.FetchYoutubeChannelUploads(widget.Channels, widget.VideoUrlTemplate)
|
2024-04-27 21:10:24 +02:00
|
|
|
|
|
|
|
if !widget.canContinueUpdateAfterHandlingErr(err) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(videos) > widget.Limit {
|
|
|
|
videos = videos[:widget.Limit]
|
|
|
|
}
|
|
|
|
|
|
|
|
widget.Videos = videos
|
|
|
|
}
|
|
|
|
|
|
|
|
func (widget *Videos) Render() template.HTML {
|
2024-05-10 08:41:11 +02:00
|
|
|
if widget.Style == "grid-cards" {
|
|
|
|
return widget.render(widget, assets.VideosGridTemplate)
|
|
|
|
}
|
|
|
|
|
2024-04-27 21:10:24 +02:00
|
|
|
return widget.render(widget, assets.VideosTemplate)
|
|
|
|
}
|