tube/importers/youtube_importer.go

41 lines
932 B
Go
Raw Normal View History

2020-03-28 05:16:45 +03:00
package importers
import (
2021-03-25 14:36:39 +03:00
"context"
2020-03-28 05:16:45 +03:00
"fmt"
"strings"
2021-03-25 14:36:39 +03:00
"github.com/Andreychik32/ytdl"
2020-03-28 05:16:45 +03:00
)
type YoutubeImporter struct{}
func (i *YoutubeImporter) GetVideoInfo(url string) (videoInfo VideoInfo, err error) {
if strings.HasPrefix(strings.ToLower(url), "youtube:") {
url = strings.TrimSpace(strings.SplitN(url, ":", 2)[1])
2020-03-28 05:16:45 +03:00
}
2021-03-25 14:36:39 +03:00
ctx := context.Background()
info, err := ytdl.GetVideoInfo(ctx, url)
2020-03-28 05:16:45 +03:00
if err != nil {
err = fmt.Errorf("error retriving youtube video info: %w", err)
return
}
2021-03-25 14:36:39 +03:00
ctx = context.Background()
videoURL, err := ytdl.DefaultClient.GetDownloadURL(ctx, info, info.Formats[0])
2020-03-28 05:16:45 +03:00
if err != nil {
err = fmt.Errorf("error retriving youtube video url: %w", err)
return
}
videoInfo.VideoURL = videoURL.String()
videoInfo.ThumbnailURL = info.GetThumbnailURL(ytdl.ThumbnailQualityHigh).String()
videoInfo.ID = info.ID
videoInfo.Title = info.Title
videoInfo.Description = info.Description
return
}