Use youtube-dl headers for own requests too
Might be some issues downloading images and subtiles if cookies are required etc
This commit is contained in:
23
goutubedl.go
23
goutubedl.go
@ -151,6 +151,7 @@ type Format struct {
|
|||||||
Filesize float64 `json:"filesize"` // The number of bytes, if known in advance
|
Filesize float64 `json:"filesize"` // The number of bytes, if known in advance
|
||||||
FilesizeApprox float64 `json:"filesize_approx"` // An estimate for the number of bytes
|
FilesizeApprox float64 `json:"filesize_approx"` // An estimate for the number of bytes
|
||||||
Protocol string `json:"protocol"` // The protocol that will be used for the actual download
|
Protocol string `json:"protocol"` // The protocol that will be used for the actual download
|
||||||
|
HTTPHeaders map[string]string `json:"http_headers"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Subtitle youtube-dl subtitle
|
// Subtitle youtube-dl subtitle
|
||||||
@ -220,9 +221,6 @@ func New(ctx context.Context, rawURL string, options Options) (result Result, er
|
|||||||
if options.DebugLog == nil {
|
if options.DebugLog == nil {
|
||||||
options.DebugLog = nopPrinter{}
|
options.DebugLog = nopPrinter{}
|
||||||
}
|
}
|
||||||
if options.HTTPClient == nil {
|
|
||||||
options.HTTPClient = http.DefaultClient
|
|
||||||
}
|
|
||||||
|
|
||||||
info, rawJSON, err := infoFromURL(ctx, rawURL, options)
|
info, rawJSON, err := infoFromURL(ctx, rawURL, options)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -327,11 +325,26 @@ func infoFromURL(ctx context.Context, rawURL string, options Options) (info Info
|
|||||||
default:
|
default:
|
||||||
// any type
|
// any type
|
||||||
}
|
}
|
||||||
|
|
||||||
|
get := func(url string) (*http.Response, error) {
|
||||||
|
c := http.DefaultClient
|
||||||
|
if options.HTTPClient != nil {
|
||||||
|
c = options.HTTPClient
|
||||||
|
}
|
||||||
|
|
||||||
|
r, err := http.NewRequest(http.MethodGet, url, nil)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
for k, v := range info.HTTPHeaders {
|
||||||
|
r.Header.Set(k, v)
|
||||||
|
}
|
||||||
|
return c.Do(r)
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: use headers from youtube-dl info for thumbnail and subtitle download?
|
// TODO: use headers from youtube-dl info for thumbnail and subtitle download?
|
||||||
if options.DownloadThumbnail && info.Thumbnail != "" {
|
if options.DownloadThumbnail && info.Thumbnail != "" {
|
||||||
resp, respErr := options.HTTPClient.Get(info.Thumbnail)
|
resp, respErr := get(info.Thumbnail)
|
||||||
if respErr == nil {
|
if respErr == nil {
|
||||||
buf, _ := ioutil.ReadAll(resp.Body)
|
buf, _ := ioutil.ReadAll(resp.Body)
|
||||||
resp.Body.Close()
|
resp.Body.Close()
|
||||||
@ -348,7 +361,7 @@ func infoFromURL(ctx context.Context, rawURL string, options Options) (info Info
|
|||||||
if options.DownloadSubtitles {
|
if options.DownloadSubtitles {
|
||||||
for _, subtitles := range info.Subtitles {
|
for _, subtitles := range info.Subtitles {
|
||||||
for i, subtitle := range subtitles {
|
for i, subtitle := range subtitles {
|
||||||
resp, respErr := options.HTTPClient.Get(subtitle.URL)
|
resp, respErr := get(subtitle.URL)
|
||||||
if respErr == nil {
|
if respErr == nil {
|
||||||
buf, _ := ioutil.ReadAll(resp.Body)
|
buf, _ := ioutil.ReadAll(resp.Body)
|
||||||
resp.Body.Close()
|
resp.Body.Close()
|
||||||
|
Reference in New Issue
Block a user