8000 Add sort-by option for videos widget by igorkim · Pull Request #752 · glanceapp/glance · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add sort-by option for videos widget #752

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -870,6 +870,7 @@ Preview:
| playlists | array | no | |
| limit | integer | no | 25 |
| style | string | no | horizontal-cards |
| sort-by | string | no | posted |
| collapse-after | integer | no | 7 |
| collapse-after-rows | integer | no | 4 |
| include-shorts | boolean | no | false |
Expand Down Expand Up @@ -905,6 +906,10 @@ https://www.youtube.com...&list={ID}&...
##### `limit`
The maximum number of videos to show.

##### `sort-by`
Used to specify the order in which the videos should get returned. Possible values are `none`, `updated`, and `posted`.
Default value is `posted`.

##### `collapse-after`
Specify the number of videos to show when using the `vertical-list` style before the "SHOW MORE" button appears.

Expand Down
28 changes: 24 additions & 4 deletions internal/glance/widget-videos.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type videosWidget struct {
Playlists []string `yaml:"playlists"`
Limit int `yaml:"limit"`
IncludeShorts bool `yaml:"include-shorts"`
SortBy string `yaml:"sort-by"`
}

func (widget *videosWidget) initialize() error {
Expand Down Expand Up @@ -64,7 +65,7 @@ func (widget *videosWidget) initialize() error {
}

func (widget *videosWidget) update(ctx context.Context) {
videos, err := fetchYoutubeChannelUploads(widget.Channels, widget.VideoUrlTemplate, widget.IncludeShorts)
videos, err := fetchYoutubeChannelUploads(widget.Channels, widget.VideoUrlTemplate, widget.IncludeShorts, widget.SortBy)

if !widget.canContinueUpdateAfterHandlingErr(err) {
return
Expand Down Expand Up @@ -98,6 +99,7 @@ type youtubeFeedResponseXml struct {
Videos []struct {
Title string `xml:"title"`
Published string `xml:"published"`
Updated string `xml:"updated"`
Link struct {
Href string `xml:"href,attr"`
} `xml:"link"`
Expand Down Expand Up @@ -126,19 +128,28 @@ type video struct {
Author string
AuthorUrl string
TimePosted time.Time
TimeUpdated time.Time
}

type videoList []video

func (v videoList) sortByNewest() videoList {
func (v videoList) sortByPosted() videoList {
sort.Slice(v, func(i, j int) bool {
return v[i].TimePosted.After(v[j].TimePosted)
})

return v
}

func fetchYoutubeChannelUploads(channelOrPlaylistIDs []string, videoUrlTemplate string, includeShorts bool) (videoList, error) {
func (v videoList) sortByUpdated() videoList {
sort.Slice(v, func(i, j int) bool {
return v[i].TimeUpdated.After(v[j].TimeUpdated)
})

return v
}

func fetchYoutubeChannelUploads(channelOrPlaylistIDs []string, videoUrlTemplate string, includeShorts bool, sortBy string) (videoList, error) {
requests := make([]*http.Request, 0, len(channelOrPlaylistIDs))

for i := range channelOrPlaylistIDs {
Expand Down Expand Up @@ -198,6 +209,7 @@ func fetchYoutubeChannelUploads(channelOrPlaylistIDs []string, videoUrlTemplate
Author: response.Channel,
AuthorUrl: response.ChannelLink + "/videos",
TimePosted: parseYoutubeFeedTime(v.Published),
TimeUpdated: parseYoutubeFeedTime(v.Updated),
})
}
}
Expand All @@ -206,7 +218,15 @@ func fetchYoutubeChannelUploads(channelOrPlaylistIDs []string, videoUrlTemplate
return nil, errNoContent
}

videos.sortByNewest()
switch sortBy {
case "none":
case "updated":
videos.sortByUpdated()
case "posted":
videos.sortByPosted()
default: // "posted"
videos.sortByPosted()
}

if failed > 0 {
return videos, fmt.Errorf("%w: missing videos from %d channels", errPartialContent, failed)
Expand Down
0