Skip to content
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

WIP - Add support for YouTube Handle #475

Closed
wants to merge 1 commit into from
Closed
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
13 changes: 13 additions & 0 deletions pkg/builder/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,19 @@ func parseYoutubeURL(parsed *url.URL) (model.Type, string, error) {
return kind, id, nil
}

// - https://www.youtube.com/@fxigr1
if strings.HasPrefix(path, "/@") {
kind := model.TypeHandle
parts := strings.Split(parsed.EscapedPath(), "/")
handle := parts[1]
id := handle[1:]
if id == "" {
return "", "", errors.New("invalid handle id")
}

return kind, id, nil
}

return "", "", errors.New("unsupported link format")
}

Expand Down
12 changes: 12 additions & 0 deletions pkg/builder/url_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ func TestParseYoutubeURL_User(t *testing.T) {
require.Equal(t, "fxigr1", id)
}

func TestParseYoutubeURL_Handle(t *testing.T) {
link, _ := url.ParseRequestURI("https://youtube.com/@fxigr1")
kind, id, err := parseYoutubeURL(link)
require.NoError(t, err)
require.Equal(t, model.TypeHandle, kind)
require.Equal(t, "fxigr1", id)
}

func TestParseYoutubeURL_InvalidLink(t *testing.T) {
link, _ := url.ParseRequestURI("https://www.youtube.com/user///")
_, _, err := parseYoutubeURL(link)
Expand All @@ -53,6 +61,10 @@ func TestParseYoutubeURL_InvalidLink(t *testing.T) {
link, _ = url.ParseRequestURI("https://www.youtube.com/channel//videos")
_, _, err = parseYoutubeURL(link)
require.Error(t, err)

link, _ = url.ParseRequestURI("https://www.youtube.com/@/")
_, _, err = parseYoutubeURL(link)
require.Error(t, err)
}

func TestParseVimeoURL_Group(t *testing.T) {
Expand Down
11 changes: 8 additions & 3 deletions pkg/builder/youtube.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ func (yt *YouTubeBuilder) listChannels(ctx context.Context, linkType model.Type,
req = req.Id(id)
case model.TypeUser:
req = req.ForUsername(id)
case model.TypeHandle:
req = req.ForUsername(id)
default:
return nil, errors.New("unsupported link type")
}
Expand Down Expand Up @@ -177,8 +179,8 @@ func (yt *YouTubeBuilder) queryFeed(ctx context.Context, feed *model.Feed, info
)

switch info.LinkType {
case model.TypeChannel, model.TypeUser:
// Cost: 5 units for channel or user
case model.TypeChannel, model.TypeUser, model.TypeHandle:
// Cost: 5 units for channel, user or handle
channel, err := yt.listChannels(ctx, info.LinkType, info.ItemID, "id,snippet,contentDetails")
if err != nil {
return err
Expand All @@ -190,9 +192,12 @@ func (yt *YouTubeBuilder) queryFeed(ctx context.Context, feed *model.Feed, info
if channel.Kind == "youtube#channel" {
feed.ItemURL = fmt.Sprintf("https://youtube.com/channel/%s", channel.Id)
feed.Author = "<notfound>"
} else {
} else if channel.Kind == "youtube#user" {
feed.ItemURL = fmt.Sprintf("https://youtube.com/user/%s", channel.Snippet.CustomUrl)
feed.Author = channel.Snippet.CustomUrl
} else {
feed.ItemURL = fmt.Sprintf("https://youtube.com/@%s", channel.Snippet.CustomUrl)
feed.Author = channel.Snippet.CustomUrl
}

feed.ItemID = channel.ContentDetails.RelatedPlaylists.Uploads
Expand Down
1 change: 1 addition & 0 deletions pkg/model/link.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const (
TypePlaylist = Type("playlist")
TypeUser = Type("user")
TypeGroup = Type("group")
TypeHandle = Type("handle")
)

type Provider string
Expand Down