~gjabell/go-yt

28861dacf3b660f68fad4c99f98382718aabe186 — Galen Abell 6 months ago e9f19f9
Handle empty search results
1 files changed, 11 insertions(+), 3 deletions(-)

M yt.go
M yt.go => yt.go +11 -3
@@ 172,13 172,21 @@ func parseYoutubeData(data string) ([]SearchResult, error) {

	var videoData []SearchResult
	for _, renderer := range videoRenderers {
		videoData = append(videoData, convertYoutubeData(renderer.VideoRenderer))
		result := convertYoutubeData(renderer.VideoRenderer)
		if result != nil {
			videoData = append(videoData, *result)
		}
	}

	return videoData, nil
}

func convertYoutubeData(renderer videoRenderer) SearchResult {
func convertYoutubeData(renderer videoRenderer) *SearchResult {
	// check for invalid data (video ID should always be non-empty)
	if renderer.VideoID == "" {
		return nil
	}

	description := ""
	if len(renderer.DescriptionSnippet.Runs) > 0 {
		description = renderer.DescriptionSnippet.Runs[0].Text


@@ 206,7 214,7 @@ func convertYoutubeData(renderer videoRenderer) SearchResult {
		watchURL = ""
	}

	return SearchResult{
	return &SearchResult{
		Description: description,
		Channel:     channel,
		ID:          renderer.VideoID,