all repos — fixyoutube-go @ fae2afdff695e4d35f8d1f87d38c94badab89425

A better way to embed YouTube videos everywhere (inspired by FixTweet).

remove formatIndex
Marco Andronaco andronacomarco@gmail.com
Sat, 13 Jan 2024 12:09:53 +0100
commit

fae2afdff695e4d35f8d1f87d38c94badab89425

parent

dfabe54c50cb40c8ffbe27dd08c2c0da0d8055b4

4 files changed, 10 insertions(+), 27 deletions(-)

jump to
M fixyoutube.gofixyoutube.go

@@ -27,15 +27,6 @@ var videoRegex = regexp.MustCompile(`(?i)^[a-z0-9_-]{11}$`)

var apiKey string -func parseFormatIndex(formatIndexString string) int { - formatIndex, err := strconv.Atoi(formatIndexString) - if err != nil || formatIndex < 0 { - logger.Debug("Could not parse formatIndex.") - return 0 - } - return formatIndex -} - func indexHandler(w http.ResponseWriter, r *http.Request) { buf := &bytes.Buffer{} err := indexTemplate.Execute(buf, nil)

@@ -73,7 +64,7 @@ logger.Info("Cache cleared.")

http.Error(w, "Done.", http.StatusOK) } -func videoHandler(videoId string, formatIndex int, invidiousClient *invidious.Client, w http.ResponseWriter, r *http.Request) { +func videoHandler(videoId string, invidiousClient *invidious.Client, w http.ResponseWriter, r *http.Request) { userAgent := r.UserAgent() res := userAgentRegex.MatchString(userAgent) if !res {

@@ -96,8 +87,6 @@ http.Error(w, "Wrong video ID.", http.StatusNotFound)

return } - video.FormatIndex = formatIndex % len(video.Formats) - buf := &bytes.Buffer{} err = videoTemplate.Execute(buf, video) if err != nil {

@@ -118,8 +107,7 @@ return

} q := u.Query() videoId := q.Get("v") - formatIndex := parseFormatIndex(q.Get("f")) - videoHandler(videoId, formatIndex, invidiousClient, w, r) + videoHandler(videoId, invidiousClient, w, r) } }

@@ -127,8 +115,7 @@ func shortHandler(invidiousClient *invidious.Client) http.HandlerFunc {

return func(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) videoId := vars["videoId"] - formatIndex := parseFormatIndex(vars["formatIndex"]) - videoHandler(videoId, formatIndex, invidiousClient, w, r) + videoHandler(videoId, invidiousClient, w, r) } }

@@ -136,7 +123,6 @@ func proxyHandler(invidiousClient *invidious.Client) http.HandlerFunc {

return func(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) videoId := vars["videoId"] - formatIndex := parseFormatIndex(vars["formatIndex"]) video, err := invidious.GetVideoDB(videoId) if err != nil {

@@ -146,13 +132,12 @@ return

} fmtAmount := len(video.Formats) - idx := formatIndex % fmtAmount var httpStatus = http.StatusNotFound - for i := fmtAmount - 1 - idx; i >= 0; i-- { + for i := fmtAmount - 1; i >= 0; i-- { url := video.Formats[i].Url - b, l, httpStatus := invidiousClient.ProxyVideo(url, formatIndex) + b, l, httpStatus := invidiousClient.ProxyVideo(url) switch httpStatus { case http.StatusOK: h := w.Header()

@@ -197,9 +182,7 @@ r.HandleFunc("/", indexHandler)

r.HandleFunc("/clear", clearHandler) r.HandleFunc("/watch", watchHandler(videoapi)) r.HandleFunc("/proxy/{videoId}", proxyHandler(videoapi)) - r.HandleFunc("/proxy/{videoId}/{formatIndex}", proxyHandler(videoapi)) r.HandleFunc("/{videoId}", shortHandler(videoapi)) - r.HandleFunc("/{videoId}/{formatIndex}", shortHandler(videoapi)) /* // native go implementation r := http.NewServeMux()
M invidious/invidious.goinvidious/invidious.go

@@ -48,7 +48,6 @@ Duration int `json:"lengthSeconds"`

Formats []Format `json:"formatStreams"` Timestamp time.Time Expire time.Time - FormatIndex int } func filter[T any](ss []T, test func(T) bool) (ret []T) {
M invidious/proxy.goinvidious/proxy.go

@@ -6,7 +6,7 @@ "io"

"net/http" ) -func (c *Client) ProxyVideo(url string, formatIndex int) (*bytes.Buffer, int64, int) { +func (c *Client) ProxyVideo(url string) (*bytes.Buffer, int64, int) { req, err := http.NewRequest(http.MethodGet, url, nil) if err != nil { logger.Error(err) // bad request

@@ -20,7 +20,7 @@ return nil, 0, http.StatusGone

} if resp.ContentLength > maxSizeBytes { - logger.Debug("Format ", formatIndex, ": Content-Length exceeds max size.") + logger.Debug("Content-Length exceeds max size.") return nil, 0, http.StatusBadRequest } defer resp.Body.Close()

@@ -28,6 +28,7 @@

b := new(bytes.Buffer) l, err := io.Copy(b, resp.Body) if l != resp.ContentLength { + logger.Debug("Content-Length is inconsistent.") return nil, 0, http.StatusBadRequest }
M templates/video.htmltemplates/video.html

@@ -21,8 +21,8 @@ <meta property="og:description" content="{{ .Description }}" />

<meta property="og:site_name" content="FixYouTube ({{ .Uploader }})" /> <meta property="twitter:image" content="0" /> <meta property="twitter:player:stream:content_type" content="video/mp4" /> - <meta property="og:video" content="/proxy/{{ .VideoId }}/{{ .FormatIndex }}" /> - <meta property="og:video:secure_url" content="/proxy/{{ .VideoId }}/{{ .FormatIndex }}" /> + <meta property="og:video" content="/proxy/{{ .VideoId }}" /> + <meta property="og:video:secure_url" content="/proxy/{{ .VideoId }}" /> <meta property="og:video:duration" content="{{ .Duration }}"> <meta property="og:video:type" content="video/mp4" /> </head><body></body></html>