From 09eedc08c18c7a758b0fd65b7ed17b7ccadca1e8 Mon Sep 17 00:00:00 2001 From: 3rd <3rd@users.noreply.github.com> Date: Fri, 2 Aug 2024 23:39:27 +0300 Subject: [PATCH 1/4] feat: add no_shorts option for YouTube feeds --- internal/feed/youtube.go | 12 ++++++++++-- internal/widget/videos.go | 3 ++- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/internal/feed/youtube.go b/internal/feed/youtube.go index e478259..6cc424a 100644 --- a/internal/feed/youtube.go +++ b/internal/feed/youtube.go @@ -39,11 +39,19 @@ func parseYoutubeFeedTime(t string) time.Time { return parsedTime } -func FetchYoutubeChannelUploads(channelIds []string, videoUrlTemplate string) (Videos, error) { +func FetchYoutubeChannelUploads(channelIds []string, videoUrlTemplate string, noShorts bool) (Videos, error) { requests := make([]*http.Request, 0, len(channelIds)) for i := range channelIds { - request, _ := http.NewRequest("GET", "https://www.youtube.com/feeds/videos.xml?channel_id="+channelIds[i], nil) + var feedUrl string + if noShorts && strings.HasPrefix(channelIds[i], "UC") { + playlistId := strings.Replace(channelIds[i], "UC", "UULF", 1) + feedUrl = "https://www.youtube.com/feeds/videos.xml?playlist_id=" + playlistId + } else { + feedUrl = "https://www.youtube.com/feeds/videos.xml?channel_id=" + channelIds[i] + } + + request, _ := http.NewRequest("GET", feedUrl, nil) requests = append(requests, request) } diff --git a/internal/widget/videos.go b/internal/widget/videos.go index 717f302..e47db09 100644 --- a/internal/widget/videos.go +++ b/internal/widget/videos.go @@ -17,6 +17,7 @@ type Videos struct { CollapseAfterRows int `yaml:"collapse-after-rows"` Channels []string `yaml:"channels"` Limit int `yaml:"limit"` + NoShorts bool `yaml:"no_shorts"` } func (widget *Videos) Initialize() error { @@ -34,7 +35,7 @@ func (widget *Videos) Initialize() error { } func (widget *Videos) Update(ctx context.Context) { - videos, err := feed.FetchYoutubeChannelUploads(widget.Channels, widget.VideoUrlTemplate) + videos, err := feed.FetchYoutubeChannelUploads(widget.Channels, widget.VideoUrlTemplate, widget.NoShorts) if !widget.canContinueUpdateAfterHandlingErr(err) { return From acaf50bf8a9612d1026e59f76c587c19dea6b21f Mon Sep 17 00:00:00 2001 From: Svilen Markov <7613769+svilenmarkov@users.noreply.github.com> Date: Sat, 3 Aug 2024 04:02:23 +0100 Subject: [PATCH 2/4] Use author > name instead of title This is because the playlist response returns the name of the playlist in the title, which meant that the channel name was always "Videos" --- internal/feed/youtube.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/feed/youtube.go b/internal/feed/youtube.go index 6cc424a..4880be5 100644 --- a/internal/feed/youtube.go +++ b/internal/feed/youtube.go @@ -10,7 +10,7 @@ import ( ) type youtubeFeedResponseXml struct { - Channel string `xml:"title"` + Channel string `xml:"author>name"` ChannelLink struct { Href string `xml:"href,attr"` } `xml:"link"` From 3949e15f775d7b47501df06dc211957f216bb76a Mon Sep 17 00:00:00 2001 From: Svilen Markov <7613769+svilenmarkov@users.noreply.github.com> Date: Sat, 3 Aug 2024 04:03:16 +0100 Subject: [PATCH 3/4] Remove previous scuffed attempt at filtering shorts --- internal/feed/youtube.go | 6 ------ 1 file changed, 6 deletions(-) diff --git a/internal/feed/youtube.go b/internal/feed/youtube.go index 4880be5..b1489d7 100644 --- a/internal/feed/youtube.go +++ b/internal/feed/youtube.go @@ -78,12 +78,6 @@ func FetchYoutubeChannelUploads(channelIds []string, videoUrlTemplate string, no for j := range response.Videos { video := &response.Videos[j] - - // TODO: figure out a better way of skipping shorts - if strings.Contains(video.Title, "#shorts") { - continue - } - var videoUrl string if videoUrlTemplate == "" { From a66aa74c7f23ce5643b430f173d3acd0a8d050f2 Mon Sep 17 00:00:00 2001 From: Svilen Markov <7613769+svilenmarkov@users.noreply.github.com> Date: Sat, 3 Aug 2024 04:04:02 +0100 Subject: [PATCH 4/4] Invert property --- internal/feed/youtube.go | 4 ++-- internal/widget/videos.go | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/internal/feed/youtube.go b/internal/feed/youtube.go index b1489d7..65a5376 100644 --- a/internal/feed/youtube.go +++ b/internal/feed/youtube.go @@ -39,12 +39,12 @@ func parseYoutubeFeedTime(t string) time.Time { return parsedTime } -func FetchYoutubeChannelUploads(channelIds []string, videoUrlTemplate string, noShorts bool) (Videos, error) { +func FetchYoutubeChannelUploads(channelIds []string, videoUrlTemplate string, includeShorts bool) (Videos, error) { requests := make([]*http.Request, 0, len(channelIds)) for i := range channelIds { var feedUrl string - if noShorts && strings.HasPrefix(channelIds[i], "UC") { + if !includeShorts && strings.HasPrefix(channelIds[i], "UC") { playlistId := strings.Replace(channelIds[i], "UC", "UULF", 1) feedUrl = "https://www.youtube.com/feeds/videos.xml?playlist_id=" + playlistId } else { diff --git a/internal/widget/videos.go b/internal/widget/videos.go index e47db09..8943603 100644 --- a/internal/widget/videos.go +++ b/internal/widget/videos.go @@ -17,7 +17,7 @@ type Videos struct { CollapseAfterRows int `yaml:"collapse-after-rows"` Channels []string `yaml:"channels"` Limit int `yaml:"limit"` - NoShorts bool `yaml:"no_shorts"` + IncludeShorts bool `yaml:"include-shorts"` } func (widget *Videos) Initialize() error { @@ -35,7 +35,7 @@ func (widget *Videos) Initialize() error { } func (widget *Videos) Update(ctx context.Context) { - videos, err := feed.FetchYoutubeChannelUploads(widget.Channels, widget.VideoUrlTemplate, widget.NoShorts) + videos, err := feed.FetchYoutubeChannelUploads(widget.Channels, widget.VideoUrlTemplate, widget.IncludeShorts) if !widget.canContinueUpdateAfterHandlingErr(err) { return