Skip to content

Commit

Permalink
chore(youtube): Add funcs for URL generation and timestamp parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
gabe565 committed Mar 3, 2025
1 parent 552ebf8 commit 2ad14a4
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 18 deletions.
2 changes: 1 addition & 1 deletion internal/youtube/playlist/description.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func (i *Item) TemplateDescription(embed bool) g.Group {
html.Width("640"),
html.Height("390"),
g.Attr("frameborder", "0"),
html.Src("https://youtube.com/embed/"+i.ResourceId.VideoId),
html.Src(i.EmbedURL().String()),
),
)
}),
Expand Down
33 changes: 24 additions & 9 deletions internal/youtube/playlist/feed_item.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package playlist

import (
"net/url"
"path"
"time"

"github.com/gorilla/feeds"
Expand All @@ -11,24 +12,38 @@ import (
type Item youtube.PlaylistItemSnippet

func (i *Item) FeedItem(embed bool) (*feeds.Item, error) {
published, err := time.Parse(time.RFC3339, i.PublishedAt)
published, err := i.ParsePublishedAt()
if err != nil {
return nil, err
}

u := url.URL{
Scheme: "https",
Host: "youtube.com",
Path: "/watch",
RawQuery: url.Values{"v": []string{i.ResourceId.VideoId}}.Encode(),
}

return &feeds.Item{
Title: i.Title,
Link: &feeds.Link{Href: u.String()},
Link: &feeds.Link{Href: i.URL().String()},
Author: &feeds.Author{Name: i.ChannelTitle},
Id: i.ResourceId.VideoId,
Created: published,
Content: i.TemplateDescription(embed).String(),
}, nil
}

func (i *Item) ParsePublishedAt() (time.Time, error) {
return time.Parse(time.RFC3339, i.PublishedAt)
}

func (i *Item) URL() *url.URL {
return &url.URL{
Scheme: "https",
Host: "youtube.com",
Path: "/watch",
RawQuery: url.Values{"v": []string{i.ResourceId.VideoId}}.Encode(),
}
}

func (i *Item) EmbedURL() *url.URL {
return &url.URL{
Scheme: "https",
Host: "youtube.com",
Path: path.Join("embed", i.ResourceId.VideoId),
}
}
18 changes: 10 additions & 8 deletions internal/youtube/playlist/playlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,9 @@ func (p Playlist) Feed(ctx context.Context) (*feeds.Feed, error) {
return nil, err
}

u := url.URL{
Scheme: "https",
Host: "youtube.com",
Path: "/playlist",
RawQuery: url.Values{"list": []string{p.ID}}.Encode(),
}

feed := &feeds.Feed{
Title: "YouTube - " + meta.Title,
Link: &feeds.Link{Href: u.String()},
Link: &feeds.Link{Href: p.URL().String()},
Description: meta.Description,
Author: &feeds.Author{
Name: meta.ChannelTitle,
Expand Down Expand Up @@ -144,3 +137,12 @@ func (p Playlist) FeedItems(ctx context.Context) ([]*feeds.Item, error) {

return feedItems, nil
}

func (p *Playlist) URL() *url.URL {
return &url.URL{
Scheme: "https",
Host: "youtube.com",
Path: "/playlist",
RawQuery: url.Values{"list": []string{p.ID}}.Encode(),
}
}

0 comments on commit 2ad14a4

Please sign in to comment.