diff --git a/main.go b/main.go index a1429af..0ce5e6b 100644 --- a/main.go +++ b/main.go @@ -47,6 +47,10 @@ func genericHTTPProxy(w http.ResponseWriter, req *http.Request) { proxyURL.RawQuery = q.Encode() + if strings.HasSuffix(proxyURL.Path, "maxres.jpg") { + proxyURL.Path = getBestThumbnail(proxyURL.Path) + } + request, err := http.NewRequest("GET", proxyURL.String(), nil) copyHeaders(req.Header, request.Header) @@ -104,6 +108,22 @@ func getHost(path string) (host string) { return host } +func getBestThumbnail(path string) (newpath string) { + + formats := [4]string{"maxresdefault.jpg", "sddefault.jpg", "hqdefault.jpg", "mqdefault.jpg"} + + for _, format := range formats { + newpath = strings.Replace(path, "maxres.jpg", format, 1) + url := "https://i.ytimg.com" + newpath + resp, _ := h2client.Head(url) + if resp.StatusCode == 200 { + return newpath + } + } + + return strings.Replace(path, "maxres.jpg", "mqdefault.jpg", 1) +} + func main() { http.HandleFunc("/videoplayback", genericHTTPProxy) http.HandleFunc("/vi/", genericHTTPProxy)