Skip to content

Commit

Permalink
Add ChannelHandle to Video struct (#299)
Browse files Browse the repository at this point in the history
* Add channel handle to video info
* Add test for data present in web client
  • Loading branch information
xoltia authored Sep 11, 2023
1 parent d2576ac commit 287d7aa
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
29 changes: 29 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
)

var testClient = Client{Debug: true}
var testWebClient = Client{Debug: true, client: &WebClient}

const (
dwlURL string = "https://www.youtube.com/watch?v=rFejpH_tAHM"
Expand Down Expand Up @@ -106,6 +107,34 @@ func TestGetVideoWithoutManifestURL(t *testing.T) {
// assert.Equal("2015-12-02 00:00:00 +0000 UTC", video.PublishDate.String())
}

func TestWebClientGetVideoWithoutManifestURL(t *testing.T) {
assert, require := assert.New(t), require.New(t)

video, err := testWebClient.GetVideo(dwlURL)
require.NoError(err, "get video")
require.NotNil(video)

assert.NotEmpty(video.Thumbnails)
assert.Greater(len(video.Thumbnails), 0)
assert.NotEmpty(video.Thumbnails[0].URL)
assert.Empty(video.HLSManifestURL)
assert.Empty(video.DASHManifestURL)

assert.NotEmpty(video.CaptionTracks)
assert.Greater(len(video.CaptionTracks), 0)
assert.NotEmpty(video.CaptionTracks[0].BaseURL)

assert.Equal("rFejpH_tAHM", video.ID)
assert.Equal("dotGo 2015 - Rob Pike - Simplicity is Complicated", video.Title)
assert.Equal("dotconferences", video.Author)
assert.GreaterOrEqual(video.Duration, 1390*time.Second)
assert.Contains(video.Description, "Go is often described as a simple language.")

// Publishing date and channel handle are present in web client
assert.Equal("2015-12-02 00:00:00 +0000 UTC", video.PublishDate.String())
assert.Equal("@dotconferences", video.ChannelHandle)
}

func TestGetVideoWithManifestURL(t *testing.T) {
assert, require := assert.New(t), require.New(t)

Expand Down
6 changes: 6 additions & 0 deletions video.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"errors"
"fmt"
"net/url"
"regexp"
"sort"
"strconv"
Expand All @@ -17,6 +18,7 @@ type Video struct {
Description string
Author string
ChannelID string
ChannelHandle string
Views int
Duration time.Duration
PublishDate time.Time
Expand Down Expand Up @@ -117,6 +119,10 @@ func (v *Video) extractDataFromPlayerResponse(prData playerResponseData) error {
v.PublishDate, _ = time.Parse(dateFormat, str)
}

if profileURL, err := url.Parse(prData.Microformat.PlayerMicroformatRenderer.OwnerProfileURL); err == nil && len(profileURL.Path) > 1 {
v.ChannelHandle = profileURL.Path[1:]
}

// Assign Streams
v.Formats = append(prData.StreamingData.Formats, prData.StreamingData.AdaptiveFormats...)
if len(v.Formats) == 0 {
Expand Down

0 comments on commit 287d7aa

Please sign in to comment.