all repos — fixyoutube-go @ f1072314494f5d5e72b46d17768a5a3509c01f6c

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

add refresh endpoint
Marco Andronaco andronacomarco@gmail.com
Sun, 24 Nov 2024 16:01:17 +0100
commit

f1072314494f5d5e72b46d17768a5a3509c01f6c

parent

5d8032326bc28d82f3f69f6b43488ba232ad88b9

3 files changed, 33 insertions(+), 8 deletions(-)

jump to
M fixyoutube.gofixyoutube.go

@@ -62,8 +62,10 @@

r := http.NewServeMux() r.HandleFunc("GET /", indexHandler) r.HandleFunc("GET /watch", watchHandler) - r.HandleFunc("GET /proxy/{videoId}", proxyHandler) - r.HandleFunc("GET /{videoId}", shortHandler) + r.HandleFunc("GET /{videoID}", shortHandler) + r.HandleFunc("GET /proxy/{videoID}", proxyHandler) + r.HandleFunc("GET /refresh/{videoID}", refreshHandler) + r.HandleFunc("POST /download", downloadHandler) var serveMux http.Handler
M handlers.gohandlers.go

@@ -112,19 +112,19 @@ return

} q := u.Query() - videoId := q.Get("v") - videoHandler(videoId, w, r) + videoID := q.Get("v") + videoHandler(videoID, w, r) } func shortHandler(w http.ResponseWriter, r *http.Request) { - videoId := r.PathValue("videoId") - videoHandler(videoId, w, r) + videoID := r.PathValue("videoID") + videoHandler(videoID, w, r) } func proxyHandler(w http.ResponseWriter, r *http.Request) { - videoId := r.PathValue("videoId") + videoID := r.PathValue("videoID") - vb, s := invidious.ProxyVideoId(videoId) + vb, s := invidious.ProxyVideoId(videoID) if s != http.StatusOK { logger.Error("proxyHandler() failed. Final code: ", s) http.Error(w, http.StatusText(s), s)

@@ -179,3 +179,24 @@ }

http.Redirect(w, r, format.URL, http.StatusFound) } + +func refreshHandler(w http.ResponseWriter, r *http.Request) { + videoID := r.PathValue("videoID") + if videoID == "" { + http.Error(w, "bad request", http.StatusBadRequest) + return + } + + if !videoRegex.MatchString(videoID) { + http.Error(w, "not found", http.StatusNotFound) + return + } + + video, err := invidious.RP.GetVideoNoCache(videoID) + if err != nil || video == nil { + http.Error(w, "not found", http.StatusNotFound) + return + } + + http.Redirect(w, r, "/"+videoID, http.StatusFound) +}
M templates/video.htmltemplates/video.html

@@ -70,6 +70,8 @@ </form>

<a href="https://www.youtube.com/watch?v={{ .VideoID }}">Watch on YouTube</a> <br /> <a href="/">What is this?</a> + <br /> + <a href="/refresh/{{ .VideoID }}">Refresh</a> <hr> <section> <small>