optionally return custom formats
Marco Andronaco andronacomarco@gmail.com
Tue, 15 Oct 2024 23:36:14 +0200
4 files changed,
22 insertions(+),
16 deletions(-)
M
README.md
→
README.md
@@ -22,12 +22,12 @@ cp docker/swag.env.example docker/swag.env
nano docker/swag.env ``` -Finally: `docker-compose up -d`. +Finally: `docker compose up -d`. ### Docker without reverse proxy Just run: ``` -docker-compose -f docker-compose.simple.yaml up -d +docker compose -f docker-compose.simple.yaml up -d ``` ## Test and debug locally
M
src/app/handlers.go
→
src/app/handlers.go
@@ -4,6 +4,7 @@ import (
"log" "net/http" "regexp" + "strconv" "text/template" "time"@@ -56,7 +57,12 @@ http.Error(w, "Invalid video ID.", http.StatusBadRequest)
return } - video, format, err := getVideo(videoID) + formatID, err := strconv.ParseUint(r.PathValue("formatID"), 10, 64) + if err != nil { + formatID = 0 + } + + video, format, err := getVideo(videoID, int(formatID)) if err != nil { http.Error(w, err500, http.StatusInternalServerError) return
M
src/app/main.go
→
src/app/main.go
@@ -55,13 +55,12 @@ serveMux = limit(limiter, r)
} r.HandleFunc("GET /", indexHandler) - // r.HandleFunc("/proxy/{videoId}", proxyHandler) - // r.HandleFunc("/clear", clearHandler) // video handlers r.HandleFunc("GET /watch", videoHandler) + r.HandleFunc("GET /shorts/{videoID}", videoHandler) r.HandleFunc("GET /{videoID}", videoHandler) - r.HandleFunc("GET /shorts/{videoID}", videoHandler) + r.HandleFunc("GET /{videoID}/{formatID}", videoHandler) // r.HandleFunc("GET /robots.txt", robotsHandler)
M
src/app/video.go
→
src/app/video.go
@@ -34,13 +34,14 @@
return time.Until(time.Unix(expireTimestamp, 0)) } -func getFormat(video youtube.Video) *youtube.Format { +func getFormat(video youtube.Video, formatID int) *youtube.Format { formats := video.Formats.Select(formatsSelectFn) - if len(formats) == 0 { + l := len(formats) + if l == 0 { return nil } - return &formats[0] + return &formats[formatID%l] } func formatsSelectFn(f youtube.Format) bool {@@ -51,7 +52,7 @@ func getURL(videoID string) string {
return fmt.Sprintf(fmtYouTubeURL, videoID) } -func getFromCache(videoID string) (video *youtube.Video, format *youtube.Format, err error) { +func getFromCache(videoID string, formatID int) (video *youtube.Video, format *youtube.Format, err error) { video, err = g.KS.Get(videoID) if err != nil { return@@ -67,11 +68,11 @@ err = errors.New("no formats for this video")
return } - format = getFormat(*video) + format = getFormat(*video, formatID) return } -func getFromYT(videoID string) (video *youtube.Video, format *youtube.Format, err error) { +func getFromYT(videoID string, formatID int) (video *youtube.Video, format *youtube.Format, err error) { url := getURL(videoID) log.Println("Requesting video ", url)@@ -80,7 +81,7 @@ if err != nil {
return } - format = getFormat(*video) + format = getFormat(*video, formatID) duration := defaultCacheDuration v := emptyVideo if format != nil {@@ -92,10 +93,10 @@ g.KS.Set(videoID, v, duration)
return } -func getVideo(videoID string) (video *youtube.Video, format *youtube.Format, err error) { - video, format, err = getFromCache(videoID) +func getVideo(videoID string, formatID int) (video *youtube.Video, format *youtube.Format, err error) { + video, format, err = getFromCache(videoID, formatID) if err != nil { - video, format, err = getFromYT(videoID) + video, format, err = getFromYT(videoID, formatID) } return }