Skip to content

Commit

Permalink
refactor parseStreams
Browse files Browse the repository at this point in the history
  • Loading branch information
Julian-Chu committed Aug 8, 2020
1 parent d8965e9 commit 8741848
Showing 1 changed file with 16 additions and 13 deletions.
29 changes: 16 additions & 13 deletions video.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ import (
)

type Video struct {
ID string
Streams []Stream
Title string
Author string
Duration time.Duration
ID string
Streams []Stream
Formats []Format
AdaptiveFormats []AdaptiveFormat
Title string
Author string
Duration time.Duration
}

type Stream struct {
Expand Down Expand Up @@ -133,21 +135,21 @@ func (v *Video) parseVideoInfo(info string) error {
}

// Get video download link
streams, err := parseStreams(prData)
if err != nil {
if err := v.parseStreams(prData); err != nil {
return err
}

v.Streams = streams
if len(v.Streams) == 0 {
return errors.New("no Stream list found in the server's answer")
}

return nil
}

func parseStreams(prData PlayerResponseData) ([]Stream, error) {
size := len(prData.StreamingData.Formats) + len(prData.StreamingData.AdaptiveFormats)
func (v *Video) parseStreams(prData PlayerResponseData) error {
v.Formats = prData.StreamingData.Formats
v.AdaptiveFormats = prData.StreamingData.AdaptiveFormats
size := len(v.Formats) + len(v.AdaptiveFormats)
streams := make([]Stream, 0, size)

filterFormat := func(stream Stream) {
Expand All @@ -159,11 +161,12 @@ func parseStreams(prData PlayerResponseData) ([]Stream, error) {
streams = append(streams, stream)
}

for _, format := range prData.StreamingData.Formats {
for _, format := range v.Formats {
filterFormat(format.Stream)
}
for _, format := range prData.StreamingData.AdaptiveFormats {
for _, format := range v.AdaptiveFormats {
filterFormat(format.Stream)
}
return streams, nil
v.Streams = streams
return nil
}

0 comments on commit 8741848

Please sign in to comment.