From 4422e49ea0d717e919a3ba3061e3298221e1a6b3 Mon Sep 17 00:00:00 2001 From: lyx0 <66651385+lyx0@users.noreply.github.com> Date: Tue, 19 Dec 2023 22:53:42 +0100 Subject: [PATCH] add meme command. meme command takes in a video url, then downloads the video with yt-dlp and moves it to the folder that docker maps to public/uploads and replies with a url to it --- cmd/nourybot/commands.go | 5 ++++ cmd/nourybot/download.go | 61 ++++++++++++++++++++++++++++++++++++++-- docker-compose.yml | 2 ++ public/uploads/.gitkeep | 0 4 files changed, 65 insertions(+), 3 deletions(-) create mode 100644 public/uploads/.gitkeep diff --git a/cmd/nourybot/commands.go b/cmd/nourybot/commands.go index ccd252c..a097cf2 100644 --- a/cmd/nourybot/commands.go +++ b/cmd/nourybot/commands.go @@ -222,6 +222,11 @@ func (app *application) handleCommand(message twitch.PrivateMessage) { app.ConvertToMP4(cmdParams[1], message) } + case "meme": + if userLevel >= 100 { + app.ConvertAndSave(cmdParams[1], cmdParams[2], message) + } + case "set": switch cmdParams[1] { case "lastfm": diff --git a/cmd/nourybot/download.go b/cmd/nourybot/download.go index dc6e46e..f3d50e7 100644 --- a/cmd/nourybot/download.go +++ b/cmd/nourybot/download.go @@ -35,6 +35,59 @@ func (app *application) NewDownload(destination, target, link string, msg twitch app.GofileDownload(target, link, identifier, msg) } } + +func (app *application) ConvertAndSave(fName, link string, msg twitch.PrivateMessage) { + goutubedl.Path = "yt-dlp" + uuid_og := uuid.NewString() + + app.Send(msg.Channel, "Downloading... dankCircle", msg) + result, err := goutubedl.New(context.Background(), link, goutubedl.Options{}) + if err != nil { + app.Log.Errorln(err) + app.Send(msg.Channel, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err), msg) + return + } + rExt := result.Info.Ext + downloadResult, err := result.Download(context.Background(), "best") + if err != nil { + app.Log.Errorln(err) + app.Send(msg.Channel, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err), msg) + return + } + //app.Send(target, "Downloaded.", msg) + fileName := fmt.Sprintf("%s.%s", uuid_og, rExt) + f, err := os.Create(fileName) + //app.Send(target, fmt.Sprintf("Filename: %s", fileName), msg) + if err != nil { + app.Log.Errorln(err) + app.Send(msg.Channel, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err), msg) + return + } + defer f.Close() + if _, err = io.Copy(f, downloadResult); err != nil { + app.Log.Errorln(err) + app.Send(msg.Channel, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err), msg) + return + } + + downloadResult.Close() + f.Close() + + out := fmt.Sprintf("/public/uploads/%s.mp4", fName) + fn, err := os.Create(out) + if err != nil { + app.Log.Errorln(err) + app.Send(msg.Channel, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err), msg) + return + } + defer fn.Close() + + err = ffmpeg.Input(fileName). + Output(out). + OverWriteOutput().ErrorToStdOut().Run() + + app.Send(msg.Channel, fmt.Sprintf("https://bot.noury.is/uploads/%s.mp4", fName), msg) +} func (app *application) ConvertToMP4(link string, msg twitch.PrivateMessage) { goutubedl.Path = "yt-dlp" uuid_og := uuid.NewString() @@ -72,7 +125,9 @@ func (app *application) ConvertToMP4(link string, msg twitch.PrivateMessage) { downloadResult.Close() f.Close() - fn, err := os.Create("output.mp4") + + out := "/public/uploads/output.mp4" + fn, err := os.Create(out) if err != nil { app.Log.Errorln(err) app.Send(msg.Channel, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err), msg) @@ -81,10 +136,10 @@ func (app *application) ConvertToMP4(link string, msg twitch.PrivateMessage) { defer fn.Close() err = ffmpeg.Input(fileName). - Output("output.mp4"). + Output(out). OverWriteOutput().ErrorToStdOut().Run() - go app.NewUpload("yaf", "output.mp4", msg.Channel, uuid_new, msg) + go app.NewUpload("yaf", out, msg.Channel, uuid_new, msg) } func (app *application) YafDownload(target, link, identifier string, msg twitch.PrivateMessage) { diff --git a/docker-compose.yml b/docker-compose.yml index 8eb08a7..264aa18 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -14,6 +14,8 @@ services: context: . dockerfile: Dockerfile env_file: .env + volumes: + - "./public/uploads:/public/uploads" ports: - "127.0.0.1:42069:8080" depends_on: diff --git a/public/uploads/.gitkeep b/public/uploads/.gitkeep new file mode 100644 index 0000000..e69de29