Skip to content

Commit

Permalink
Merge pull request #176 from 3rd/release/v0.6.0
Browse files Browse the repository at this point in the history
feat: add no_shorts option for YouTube feeds
  • Loading branch information
svilenmarkov authored Aug 3, 2024
2 parents 738bcf8 + a66aa74 commit e443525
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
20 changes: 11 additions & 9 deletions internal/feed/youtube.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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, includeShorts 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 !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 {
feedUrl = "https://www.youtube.com/feeds/videos.xml?channel_id=" + channelIds[i]
}

request, _ := http.NewRequest("GET", feedUrl, nil)
requests = append(requests, request)
}

Expand All @@ -70,12 +78,6 @@ func FetchYoutubeChannelUploads(channelIds []string, videoUrlTemplate string) (V

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 == "" {
Expand Down
3 changes: 2 additions & 1 deletion internal/widget/videos.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type Videos struct {
CollapseAfterRows int `yaml:"collapse-after-rows"`
Channels []string `yaml:"channels"`
Limit int `yaml:"limit"`
IncludeShorts bool `yaml:"include-shorts"`
}

func (widget *Videos) Initialize() error {
Expand All @@ -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.IncludeShorts)

if !widget.canContinueUpdateAfterHandlingErr(err) {
return
Expand Down

0 comments on commit e443525

Please sign in to comment.