From 05f23c53ecd478546d1e8753a3c05397895b582c Mon Sep 17 00:00:00 2001 From: Evan Fiordeliso Date: Fri, 14 Apr 2023 16:07:57 -0400 Subject: [PATCH] Use custom ytdl wrapper --- go.mod | 2 + go.sum | 4 + web/serve.go | 29 ++- web/views.go | 19 +- web/views/download.html | 39 +++- web/views/index.html | 2 +- web/views/layouts/main.html | 2 +- ytdl/data.go | 441 ++++++++++++++++++++++++++++++++++++ ytdl/meta.go | 29 +++ 9 files changed, 559 insertions(+), 8 deletions(-) create mode 100644 ytdl/data.go create mode 100644 ytdl/meta.go diff --git a/go.mod b/go.mod index 83a0341..623c689 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,9 @@ go 1.20 require ( github.com/gofiber/fiber/v2 v2.43.0 github.com/gofiber/template v1.8.0 + github.com/samber/lo v1.38.1 github.com/spf13/cobra v1.7.0 + golang.org/x/exp v0.0.0-20230321023759-10a507213a29 ) require ( diff --git a/go.sum b/go.sum index 0747e01..2abc9d1 100644 --- a/go.sum +++ b/go.sum @@ -331,6 +331,8 @@ github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFR github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts= github.com/sagikazarmark/crypt v0.3.0/go.mod h1:uD/D+6UF4SrIR1uGEv7bBNkNqLGqUr43MRiaGWX1Nig= +github.com/samber/lo v1.38.1 h1:j2XEAqXKb09Am4ebOg31SpvzUTTs6EN3VfgeLUhPdXM= +github.com/samber/lo v1.38.1/go.mod h1:+m/ZKRl6ClXCE2Lgf3MsQlWfh4bn1bz6CXEOxnEXnEA= github.com/savsgio/dictpool v0.0.0-20221023140959-7bf2e61cea94 h1:rmMl4fXJhKMNWl+K+r/fq4FbbKI+Ia2m9hYBLm2h4G4= github.com/savsgio/dictpool v0.0.0-20221023140959-7bf2e61cea94/go.mod h1:90zrgN3D/WJsDd1iXHT96alCoN2KJo6/4x1DZC3wZs8= github.com/savsgio/gotils v0.0.0-20220530130905-52f3993e8d6d/go.mod h1:Gy+0tqhJvgGlqnTF8CVGP0AaGRjwBtXs/a5PA0Y3+A4= @@ -413,6 +415,8 @@ golang.org/x/exp v0.0.0-20191227195350-da58074b4299/go.mod h1:2RIsYlXP63K8oxa1u0 golang.org/x/exp v0.0.0-20200119233911-0405dc783f0a/go.mod h1:2RIsYlXP63K8oxa1u096TMicItID8zy7Y6sNkU49FU4= golang.org/x/exp v0.0.0-20200207192155-f17229e696bd/go.mod h1:J/WKrq2StrnmMY6+EHIKF9dgMWnmCNThgcyBT1FY9mM= golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6/go.mod h1:3jZMyOhIsHpP37uCMkUooju7aAi5cS1Q23tOzKc+0MU= +golang.org/x/exp v0.0.0-20230321023759-10a507213a29 h1:ooxPy7fPvB4kwsA2h+iBNHkAbp/4JxTSwCmvdjEYmug= +golang.org/x/exp v0.0.0-20230321023759-10a507213a29/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc= golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js= golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0= golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= diff --git a/web/serve.go b/web/serve.go index 90e4850..245e8e1 100644 --- a/web/serve.go +++ b/web/serve.go @@ -1,7 +1,13 @@ package web import ( + "net/url" + "sort" + "github.com/gofiber/fiber/v2" + "github.com/samber/lo" + "go.fifitido.net/ytdl-web/ytdl" + "golang.org/x/exp/slog" ) func Serve() error { @@ -13,10 +19,29 @@ func Serve() error { }) app.Get("/download", func(c *fiber.Ctx) error { - url := c.Get("url") + urlBytes, err := url.QueryUnescape(c.Query("url")) + if err != nil { + slog.Error("Failed to decode url param", slog.String("error", err.Error())) + return fiber.ErrBadRequest + } + url := string(urlBytes) + + meta, err := ytdl.GetMetadata(url) + if err != nil { + slog.Error("Failed to get metadata", slog.String("error", err.Error())) + return fiber.ErrInternalServerError + } + + formats := lo.Filter(meta.Formats, func(item ytdl.Format, _ int) bool { + return item.ACodec != "none" && item.VCodec != "none" + }) + + sort.Slice(formats, func(i, j int) bool { + return formats[i].Width > formats[j].Width + }) return c.Render("views/download", fiber.Map{ - "Url": url, + "Url": url, "Meta": meta, "Formats": formats, }, "views/layouts/main") }) diff --git a/web/views.go b/web/views.go index e00f59b..488cdfa 100644 --- a/web/views.go +++ b/web/views.go @@ -2,8 +2,10 @@ package web import ( "embed" + "encoding/json" "html/template" "net/http" + "net/url" "github.com/gofiber/template/html" ) @@ -14,10 +16,23 @@ var viewsfs embed.FS func ViewsEngine() *html.Engine { engine := html.NewFileSystem(http.FS(viewsfs), ".html") engine.AddFunc( - // add unescape function - "unescape", func(s string) template.HTML { + "unsafe", func(s string) template.HTML { return template.HTML(s) }, ) + engine.AddFunc( + "queryEscape", func(s string) string { + return url.QueryEscape(s) + }, + ) + engine.AddFunc( + "jsonMarshal", func(s any) (string, error) { + j, err := json.MarshalIndent(s, "", " ") + if err != nil { + return "", err + } + return string(j), nil + }, + ) return engine } diff --git a/web/views/download.html b/web/views/download.html index c55e2e4..860aaae 100644 --- a/web/views/download.html +++ b/web/views/download.html @@ -1,2 +1,37 @@ -

Download

-

{{.Url}}

+
+

Download Video

+

{{.Meta.Title}}

+

{{.Url}}

+ {{.Meta.Title}} + Download Another Video +
+{{$id := .Meta.ID}} {{$url := .Url}} {{range .Formats}} +
+
{{.Format}}
+ +
+{{end}} diff --git a/web/views/index.html b/web/views/index.html index f8856bd..bc04b9c 100644 --- a/web/views/index.html +++ b/web/views/index.html @@ -7,7 +7,7 @@
- +