From f526b93da22479f73107000169be8eda67f05b2c Mon Sep 17 00:00:00 2001 From: Corentin Barreau Date: Mon, 16 Sep 2024 22:23:15 +0200 Subject: [PATCH] enhancement: better YouTube headers handling --- .../pkg/crawl/dependencies/ytdlp/model.go | 34 +++++++++---------- .../pkg/crawl/dependencies/ytdlp/ytdlp.go | 4 ++- .../pkg/crawl/sitespecific/youtube/youtube.go | 4 ++- 3 files changed, 23 insertions(+), 19 deletions(-) diff --git a/internal/pkg/crawl/dependencies/ytdlp/model.go b/internal/pkg/crawl/dependencies/ytdlp/model.go index ea7d892d..d9856878 100644 --- a/internal/pkg/crawl/dependencies/ytdlp/model.go +++ b/internal/pkg/crawl/dependencies/ytdlp/model.go @@ -27,23 +27,23 @@ type Video struct { Fragments []struct { URL string `json:"url"` } `json:"fragments"` - HasDrm bool `json:"has_drm"` - Height float64 `json:"height"` - HTTPHeaders HTTPHeaders `json:"http_headers"` - Language interface{} `json:"language"` - LanguagePreference float64 `json:"language_preference"` - Preference interface{} `json:"preference"` - Protocol string `json:"protocol"` - Quality float64 `json:"quality"` - Resolution string `json:"resolution"` - SourcePreference float64 `json:"source_preference"` - Tbr float64 `json:"tbr"` - URL string `json:"url"` - Vbr float64 `json:"vbr,omitempty"` - Vcodec string `json:"vcodec"` - VideoExt string `json:"video_ext"` - Width float64 `json:"width"` - Abr float64 `json:"abr,omitempty"` + HasDrm bool `json:"has_drm"` + Height float64 `json:"height"` + HTTPHeaders map[string]string `json:"http_headers"` + Language interface{} `json:"language"` + LanguagePreference float64 `json:"language_preference"` + Preference interface{} `json:"preference"` + Protocol string `json:"protocol"` + Quality float64 `json:"quality"` + Resolution string `json:"resolution"` + SourcePreference float64 `json:"source_preference"` + Tbr float64 `json:"tbr"` + URL string `json:"url"` + Vbr float64 `json:"vbr,omitempty"` + Vcodec string `json:"vcodec"` + VideoExt string `json:"video_ext"` + Width float64 `json:"width"` + Abr float64 `json:"abr,omitempty"` } `json:"requested_formats"` Formats []struct { Acodec string `json:"acodec"` diff --git a/internal/pkg/crawl/dependencies/ytdlp/ytdlp.go b/internal/pkg/crawl/dependencies/ytdlp/ytdlp.go index 0a4f5fbe..dd018b98 100644 --- a/internal/pkg/crawl/dependencies/ytdlp/ytdlp.go +++ b/internal/pkg/crawl/dependencies/ytdlp/ytdlp.go @@ -8,7 +8,9 @@ import ( "strconv" ) -func GetJSON(port int) (URLs []string, rawJSON string, HTTPHeaders HTTPHeaders, err error) { +func GetJSON(port int) (URLs []string, rawJSON string, HTTPHeaders map[string]string, err error) { + HTTPHeaders = make(map[string]string) + // Prepare the command cmd := exec.Command("yt-dlp", "--dump-json", "http://localhost:"+strconv.Itoa(port), "-f", "bv[protocol=https]+ba[protocol=https]") diff --git a/internal/pkg/crawl/sitespecific/youtube/youtube.go b/internal/pkg/crawl/sitespecific/youtube/youtube.go index ab5059db..c11c6fc1 100644 --- a/internal/pkg/crawl/sitespecific/youtube/youtube.go +++ b/internal/pkg/crawl/sitespecific/youtube/youtube.go @@ -12,7 +12,9 @@ func IsYouTubeWatchPage(URL *url.URL) bool { return strings.Contains(URL.Host, "youtube.com") && (strings.Contains(URL.Path, "/watch") || strings.Contains(URL.Path, "/v/")) } -func Parse(body io.ReadCloser) (URLs []*url.URL, rawJSON string, HTTPHeaders ytdlp.HTTPHeaders, err error) { +func Parse(body io.ReadCloser) (URLs []*url.URL, rawJSON string, HTTPHeaders map[string]string, err error) { + HTTPHeaders = make(map[string]string) + // Create a temporary server to serve the body and call ytdlp on it port, stopChan, err := ytdlp.ServeBody(body) if err != nil {