From 3ba265d0ea1440e5f1b1c15e17e8399844b62795 Mon Sep 17 00:00:00 2001 From: FireMasterK <20838718+FireMasterK@users.noreply.github.com> Date: Tue, 27 Oct 2020 15:29:49 +0530 Subject: [PATCH] Add support for Invidious's maxres.jpg. --- main.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) 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)