Skip to content

Commit

Permalink
Add Captions in playerResponseData (#293)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shinku-Chen committed Jun 21, 2023
1 parent a96ec24 commit fb8c4c0
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
4 changes: 4 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ func TestGetVideoWithoutManifestURL(t *testing.T) {
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)
Expand Down
27 changes: 27 additions & 0 deletions response_data.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
package youtube

type playerResponseData struct {
Captions struct {
PlayerCaptionsTracklistRenderer struct {
CaptionTracks []CaptionTrack `json:"captionTracks"`
AudioTracks []struct {
CaptionTrackIndices []int `json:"captionTrackIndices"`
} `json:"audioTracks"`
TranslationLanguages []struct {
LanguageCode string `json:"languageCode"`
LanguageName struct {
SimpleText string `json:"simpleText"`
} `json:"languageName"`
} `json:"translationLanguages"`
DefaultAudioTrackIndex int `json:"defaultAudioTrackIndex"`
} `json:"playerCaptionsTracklistRenderer"`
} `json:"captions"`

PlayabilityStatus struct {
Status string `json:"status"`
Reason string `json:"reason"`
Expand Down Expand Up @@ -110,3 +126,14 @@ type Thumbnail struct {
Width uint
Height uint
}

type CaptionTrack struct {
BaseURL string `json:"baseUrl"`
Name struct {
SimpleText string `json:"simpleText"`
} `json:"name"`
VssID string `json:"vssId"`
LanguageCode string `json:"languageCode"`
Kind string `json:"kind,omitempty"`
IsTranslatable bool `json:"isTranslatable"`
}
2 changes: 2 additions & 0 deletions video.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type Video struct {
Thumbnails Thumbnails
DASHManifestURL string // URI of the DASH manifest file
HLSManifestURL string // URI of the HLS manifest file
CaptionTracks []CaptionTrack
}

const dateFormat = "2006-01-02"
Expand Down Expand Up @@ -98,6 +99,7 @@ func (v *Video) extractDataFromPlayerResponse(prData playerResponseData) error {
v.Author = prData.VideoDetails.Author
v.Thumbnails = prData.VideoDetails.Thumbnail.Thumbnails
v.ChannelID = prData.VideoDetails.ChannelID
v.CaptionTracks = prData.Captions.PlayerCaptionsTracklistRenderer.CaptionTracks

if views, _ := strconv.Atoi(prData.VideoDetails.ViewCount); views > 0 {
v.Views = views
Expand Down

0 comments on commit fb8c4c0

Please sign in to comment.