From 67e7a2e655b472a215c3e5c3e1cf3849f6cff174 Mon Sep 17 00:00:00 2001 From: lyx0 Date: Wed, 27 Sep 2023 16:44:42 +0200 Subject: [PATCH] add dl command to download videos with yt-dlp --- cmd/nourybot/commands.go | 3 +++ cmd/nourybot/download.go | 38 ++++++++++++++++++++++++++++++++++++++ cmd/nourybot/send.go | 6 ++++-- go.mod | 1 + go.sum | 4 ++++ 5 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 cmd/nourybot/download.go diff --git a/cmd/nourybot/commands.go b/cmd/nourybot/commands.go index 83db2b4..0172aca 100644 --- a/cmd/nourybot/commands.go +++ b/cmd/nourybot/commands.go @@ -83,6 +83,9 @@ func (app *application) handleCommand(message twitch.PrivateMessage) { reply, _ = commands.Currency(cmdParams[1], cmdParams[2], cmdParams[4]) } + case "dl": + reply, _ = app.Download(target, cmdParams[1]) + case "mail": app.SendEmail("Test command used!", "This is an email test") diff --git a/cmd/nourybot/download.go b/cmd/nourybot/download.go new file mode 100644 index 0000000..39e6d99 --- /dev/null +++ b/cmd/nourybot/download.go @@ -0,0 +1,38 @@ +package main + +import ( + "context" + "io" + "os" + "os/exec" + + "github.com/wader/goutubedl" +) + +func (app *application) Download(channel, link string) (string, error) { + goutubedl.Path = "yt-dlp" + + app.Send(channel, "dankCircle") + result, err := goutubedl.New(context.Background(), link, goutubedl.Options{}) + if err != nil { + app.Log.Fatal(err) + } + downloadResult, err := result.Download(context.Background(), "best") + if err != nil { + app.Log.Fatal(err) + } + defer downloadResult.Close() + f, err := os.Create("output.mp4") + if err != nil { + app.Log.Fatal(err) + } + defer f.Close() + io.Copy(f, downloadResult) + + out, err := exec.Command("curl", "-L", "-F", "file=@output.mp4", "i.yaf.ee/upload").Output() + if err != nil { + app.Log.Fatal(err) + } + return string(out), nil + +} diff --git a/cmd/nourybot/send.go b/cmd/nourybot/send.go index 63b72e4..76b4f8d 100644 --- a/cmd/nourybot/send.go +++ b/cmd/nourybot/send.go @@ -47,12 +47,14 @@ func (app *application) checkMessage(text string) (bool, string) { "message": text, }) if err != nil { - log.Fatal(err) + app.Log.Error(err) + return true, "could not check banphrase api" } resp, err := http.Post(banPhraseUrl, "application/json", bytes.NewBuffer(reqBody)) if err != nil { - log.Panic(err) + app.Log.Error(err) + return true, "could not check banphrase api" } defer resp.Body.Close() diff --git a/go.mod b/go.mod index bac95e9..49b6599 100644 --- a/go.mod +++ b/go.mod @@ -14,6 +14,7 @@ require ( ) require ( + github.com/wader/goutubedl v0.0.0-20230924165737-427b7fa536e6 // indirect go.uber.org/atomic v1.7.0 // indirect go.uber.org/multierr v1.6.0 // indirect gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect diff --git a/go.sum b/go.sum index 5e573a2..fee0156 100644 --- a/go.sum +++ b/go.sum @@ -7,6 +7,7 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY= github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto= +github.com/fortytw2/leaktest v1.3.0/go.mod h1:jDsjWgpAGjm2CA7WthBh/CdZYEPF31XHquHwclZch5g= github.com/gempir/go-twitch-irc/v4 v4.0.0 h1:sHVIvbWOv9nHXGEErilclxASv0AaQEr/r/f9C0B9aO8= github.com/gempir/go-twitch-irc/v4 v4.0.0/go.mod h1:QsOMMAk470uxQ7EYD9GJBGAVqM/jDrXBNbuePfTauzg= github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= @@ -36,6 +37,9 @@ github.com/shkh/lastfm-go v0.0.0-20191215035245-89a801c244e0/go.mod h1:n3nudMl17 github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk= +github.com/wader/goutubedl v0.0.0-20230924165737-427b7fa536e6 h1:KHJV3fnnKsdWdGu5IKrDAA0Oa5RzGwrJpfx+bvVAjLA= +github.com/wader/goutubedl v0.0.0-20230924165737-427b7fa536e6/go.mod h1:5KXd5tImdbmz4JoVhePtbIokCwAfEhUVVx3WLHmjYuw= +github.com/wader/osleaktest v0.0.0-20191111175233-f643b0fed071/go.mod h1:XD6emOFPHVzb0+qQpiNOdPL2XZ0SRUM0N5JHuq6OmXo= go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw= go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= go.uber.org/goleak v1.1.11 h1:wy28qYRKZgnJTxGxvye5/wgWr1EKjmUDGYox5mGlRlI=