move download/upload into application

This commit is contained in:
lyx0 2023-10-05 21:21:26 +02:00
parent bd7799ca63
commit bda3b1e05b
4 changed files with 291 additions and 320 deletions

View file

@ -84,16 +84,16 @@ func (app *application) handleCommand(message twitch.PrivateMessage) {
} }
case "catbox": case "catbox":
go commands.NewDownload("catbox", target, cmdParams[1], app.TwitchClient, app.Log) go app.NewDownload("catbox", target, cmdParams[1])
case "kappa": case "kappa":
go commands.NewDownload("kappa", target, cmdParams[1], app.TwitchClient, app.Log) go app.NewDownload("kappa", target, cmdParams[1])
case "yaf": case "yaf":
go commands.NewDownload("yaf", target, cmdParams[1], app.TwitchClient, app.Log) go app.NewDownload("yaf", target, cmdParams[1])
case "gofile": case "gofile":
go commands.NewDownload("gofile", target, cmdParams[1], app.TwitchClient, app.Log) go app.NewDownload("gofile", target, cmdParams[1])
case "mail": case "mail":
app.SendEmail("Test command used!", "This is an email test") app.SendEmail("Test command used!", "This is an email test")

223
cmd/nourybot/download.go Normal file
View file

@ -0,0 +1,223 @@
package main
import (
"context"
"fmt"
"io"
"os"
"github.com/google/uuid"
"github.com/wader/goutubedl"
)
func (app *application) NewDownload(destination, target, link string) {
switch destination {
case "catbox":
app.CatboxDownload(target, link)
case "yaf":
app.YafDownload(target, link)
case "kappa":
app.KappaDownload(target, link)
case "gofile":
app.GofileDownload(target, link)
}
}
func (app *application) YafDownload(target, link string) {
goutubedl.Path = "yt-dlp"
app.Send(target, "Downloading... dankCircle")
result, err := goutubedl.New(context.Background(), link, goutubedl.Options{})
if err != nil {
app.Log.Errorln(err)
app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err))
return
}
rExt := result.Info.Ext
downloadResult, err := result.Download(context.Background(), "best")
if err != nil {
app.Log.Errorln(err)
app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err))
return
}
app.Send(target, "Downloaded.")
uuidFilename, err := uuid.NewUUID()
if err != nil {
app.Log.Errorln(err)
app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err))
return
}
fileName := fmt.Sprintf("%s.%s", uuidFilename, rExt)
f, err := os.Create(fileName)
app.Send(target, fmt.Sprintf("Filename: %s", fileName))
if err != nil {
app.Log.Errorln(err)
app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err))
return
}
defer f.Close()
if _, err = io.Copy(f, downloadResult); err != nil {
app.Log.Errorln(err)
app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err))
return
}
downloadResult.Close()
f.Close()
// duration := 5 * time.Second
// dl.twitchClient.Say(target, "ResidentSleeper ..")
// time.Sleep(duration)
go app.NewUpload("yaf", fileName, target)
}
func (app *application) KappaDownload(target, link string) {
goutubedl.Path = "yt-dlp"
app.Send(target, "Downloading... dankCircle")
result, err := goutubedl.New(context.Background(), link, goutubedl.Options{})
if err != nil {
app.Log.Errorln(err)
app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err))
return
}
rExt := result.Info.Ext
downloadResult, err := result.Download(context.Background(), "best")
if err != nil {
app.Log.Errorln(err)
app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err))
return
}
app.Send(target, "Downloaded.")
uuidFilename, err := uuid.NewUUID()
if err != nil {
app.Log.Errorln(err)
app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err))
return
}
fileName := fmt.Sprintf("%s.%s", uuidFilename, rExt)
f, err := os.Create(fileName)
app.Send(target, fmt.Sprintf("Filename: %s", fileName))
if err != nil {
app.Log.Errorln(err)
app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err))
return
}
defer f.Close()
if _, err = io.Copy(f, downloadResult); err != nil {
app.Log.Errorln(err)
app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err))
return
}
downloadResult.Close()
f.Close()
// duration := 5 * time.Second
// dl.twitchClient.Say(target, "ResidentSleeper ..")
// time.Sleep(duration)
go app.NewUpload("kappa", fileName, target)
}
func (app *application) GofileDownload(target, link string) {
goutubedl.Path = "yt-dlp"
app.Send(target, "Downloading... dankCircle")
result, err := goutubedl.New(context.Background(), link, goutubedl.Options{})
if err != nil {
app.Log.Errorln(err)
app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err))
return
}
safeFilename := fmt.Sprintf("download_%s", result.Info.Title)
rExt := result.Info.Ext
downloadResult, err := result.Download(context.Background(), "best")
if err != nil {
app.Log.Errorln(err)
app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err))
return
}
app.Send(target, "Downloaded.")
fileName := fmt.Sprintf("%s.%s", safeFilename, rExt)
f, err := os.Create(fileName)
app.TwitchClient.Say(target, fmt.Sprintf("Filename: %s", fileName))
if err != nil {
app.Log.Errorln(err)
app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err))
return
}
defer f.Close()
if _, err = io.Copy(f, downloadResult); err != nil {
app.Log.Errorln(err)
app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err))
return
}
downloadResult.Close()
f.Close()
// duration := 5 * time.Second
// dl.twitchClient.Say(target, "ResidentSleeper ..")
// time.Sleep(duration)
go app.NewUpload("gofile", fileName, target)
}
func (app *application) CatboxDownload(target, link string) {
goutubedl.Path = "yt-dlp"
var fileName string
app.Send(target, "Downloading... dankCircle")
result, err := goutubedl.New(context.Background(), link, goutubedl.Options{})
if err != nil {
app.Log.Errorln(err)
app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err))
return
}
// I don't know why but I need to set it to mp4, otherwise if
// I use `result.Into.Ext` catbox won't play the video in the
// browser and say this message:
// `No video with supported format and MIME type found.`
rExt := "mp4"
downloadResult, err := result.Download(context.Background(), "best")
if err != nil {
app.Log.Errorln(err)
app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err))
return
}
app.Send(target, "Downloaded.")
uuidFilename, err := uuid.NewUUID()
if err != nil {
app.Log.Errorln(err)
app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err))
return
}
fileName = fmt.Sprintf("%s.%s", uuidFilename, rExt)
f, err := os.Create(fileName)
app.Send(target, fmt.Sprintf("Filename: %s", fileName))
if err != nil {
app.Log.Errorln(err)
app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err))
return
}
defer f.Close()
if _, err = io.Copy(f, downloadResult); err != nil {
app.Log.Errorln(err)
app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err))
return
}
downloadResult.Close()
f.Close()
go app.NewUpload("catbox", fileName, target)
}

View file

@ -6,7 +6,7 @@
// Use of this source code is governed by the MIT // Use of this source code is governed by the MIT
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
package commands package main
import ( import (
"encoding/json" "encoding/json"
@ -18,9 +18,6 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"time" "time"
"github.com/gempir/go-twitch-irc/v4"
"go.uber.org/zap"
) )
const ( const (
@ -30,48 +27,30 @@ const (
YAF_ENDPOINT = "https://i.yaf.ee/upload" YAF_ENDPOINT = "https://i.yaf.ee/upload"
) )
type Uploader struct { func (app *application) NewUpload(destination, fileName, target string) {
Client *http.Client
Time string
Userhash string
TwitchClient *twitch.Client
Log *zap.SugaredLogger
}
func NewUpload(destination, fileName, target string, twitchClient *twitch.Client, log *zap.SugaredLogger) {
client := &http.Client{
Timeout: 300 * time.Second,
}
ul := &Uploader{
Client: client,
Time: "24h",
TwitchClient: twitchClient,
Log: log,
}
switch destination { switch destination {
case "catbox": case "catbox":
go ul.CatboxUpload(target, fileName) go app.CatboxUpload(target, fileName)
case "yaf": case "yaf":
go ul.YafUpload(target, fileName) go app.YafUpload(target, fileName)
case "kappa": case "kappa":
go ul.KappaUpload(target, fileName) go app.KappaUpload(target, fileName)
case "gofile": case "gofile":
go ul.GofileUpload(target, fileName) go app.GofileUpload(target, fileName)
} }
} }
func (ul *Uploader) CatboxUpload(target, fileName string) { func (app *application) CatboxUpload(target, fileName string) {
defer os.Remove(fileName) defer os.Remove(fileName)
file, err := os.Open(fileName) file, err := os.Open(fileName)
if err != nil { if err != nil {
ul.TwitchClient.Say(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err)) app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err))
return return
} }
defer file.Close() defer file.Close()
ul.TwitchClient.Say(target, "Uploading to catbox.moe... dankCircle") app.Send(target, "Uploading to catbox.moe... dankCircle")
// if size := helper.FileSize(fileName); size > 209715200 { // if size := helper.FileSize(fileName); size > 209715200 {
// return "", fmt.Errorf("file too large, size: %d MB", size/1024/1024) // return "", fmt.Errorf("file too large, size: %d MB", size/1024/1024)
@ -85,10 +64,10 @@ func (ul *Uploader) CatboxUpload(target, fileName string) {
defer m.Close() defer m.Close()
m.WriteField("reqtype", "fileupload") m.WriteField("reqtype", "fileupload")
m.WriteField("time", ul.Time) m.WriteField("time", "24h")
part, err := m.CreateFormFile("fileToUpload", filepath.Base(file.Name())) part, err := m.CreateFormFile("fileToUpload", filepath.Base(file.Name()))
if err != nil { if err != nil {
ul.TwitchClient.Say(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err)) app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err))
return return
} }
@ -100,27 +79,31 @@ func (ul *Uploader) CatboxUpload(target, fileName string) {
req, _ := http.NewRequest(http.MethodPost, CATBOX_ENDPOINT, r) req, _ := http.NewRequest(http.MethodPost, CATBOX_ENDPOINT, r)
req.Header.Add("Content-Type", m.FormDataContentType()) req.Header.Add("Content-Type", m.FormDataContentType())
resp, err := ul.Client.Do(req) client := &http.Client{
Timeout: 300 * time.Second,
}
resp, err := client.Do(req)
if err != nil { if err != nil {
ul.TwitchClient.Say(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err)) app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err))
return return
} }
defer resp.Body.Close() defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body) body, err := ioutil.ReadAll(resp.Body)
if err != nil { if err != nil {
ul.TwitchClient.Say(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err)) app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err))
return return
} }
reply := string(body) reply := string(body)
ul.TwitchClient.Say(target, fmt.Sprintf("Removing file: %s", fileName)) app.Send(target, fmt.Sprintf("Removing file: %s", fileName))
ul.TwitchClient.Say(target, reply) app.Send(target, reply)
} }
func (ul *Uploader) GofileUpload(target, path string) { func (app *application) GofileUpload(target, path string) {
defer os.Remove(path) defer os.Remove(path)
ul.TwitchClient.Say(target, "Uploading to gofile.io... dankCircle") app.Send(target, "Uploading to gofile.io... dankCircle")
pr, pw := io.Pipe() pr, pw := io.Pipe()
form := multipart.NewWriter(pw) form := multipart.NewWriter(pw)
@ -143,21 +126,21 @@ func (ul *Uploader) GofileUpload(target, path string) {
file, err := os.Open(path) // path to image file file, err := os.Open(path) // path to image file
if err != nil { if err != nil {
ul.TwitchClient.Say(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err)) app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err))
os.Remove(path) os.Remove(path)
return return
} }
w, err := form.CreateFormFile("file", path) w, err := form.CreateFormFile("file", path)
if err != nil { if err != nil {
ul.TwitchClient.Say(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err)) app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err))
os.Remove(path) os.Remove(path)
return return
} }
_, err = io.Copy(w, file) _, err = io.Copy(w, file)
if err != nil { if err != nil {
ul.TwitchClient.Say(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err)) app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err))
os.Remove(path) os.Remove(path)
return return
} }
@ -167,7 +150,7 @@ func (ul *Uploader) GofileUpload(target, path string) {
req, err := http.NewRequest(http.MethodPost, GOFILE_ENDPOINT, pr) req, err := http.NewRequest(http.MethodPost, GOFILE_ENDPOINT, pr)
if err != nil { if err != nil {
ul.TwitchClient.Say(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err)) app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err))
os.Remove(path) os.Remove(path)
return return
} }
@ -178,39 +161,39 @@ func (ul *Uploader) GofileUpload(target, path string) {
} }
resp, err := httpClient.Do(req) resp, err := httpClient.Do(req)
if err != nil { if err != nil {
ul.TwitchClient.Say(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err)) app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err))
os.Remove(path) os.Remove(path)
ul.Log.Errorln("Error while sending HTTP request:", err) app.Log.Errorln("Error while sending HTTP request:", err)
return return
} }
defer resp.Body.Close() defer resp.Body.Close()
ul.TwitchClient.Say(target, "Uploaded PogChamp") app.Send(target, "Uploaded PogChamp")
body, err := io.ReadAll(resp.Body) body, err := io.ReadAll(resp.Body)
if err != nil { if err != nil {
ul.TwitchClient.Say(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err)) app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err))
os.Remove(path) os.Remove(path)
ul.Log.Errorln("Error while reading response:", err) app.Log.Errorln("Error while reading response:", err)
return return
} }
jsonResponse := new(gofileResponse) jsonResponse := new(gofileResponse)
if err := json.Unmarshal(body, jsonResponse); err != nil { if err := json.Unmarshal(body, jsonResponse); err != nil {
ul.TwitchClient.Say(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err)) app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err))
ul.Log.Errorln("Error while unmarshalling JSON response:", err) app.Log.Errorln("Error while unmarshalling JSON response:", err)
return return
} }
var reply = jsonResponse.Data.DownloadPage var reply = jsonResponse.Data.DownloadPage
ul.TwitchClient.Say(target, fmt.Sprintf("Removing file: %s", path)) app.Send(target, fmt.Sprintf("Removing file: %s", path))
ul.TwitchClient.Say(target, reply) app.Send(target, reply)
} }
func (ul *Uploader) KappaUpload(target, path string) { func (app *application) KappaUpload(target, path string) {
defer os.Remove(path) defer os.Remove(path)
ul.TwitchClient.Say(target, "Uploading to kappa.lol... dankCircle") app.Send(target, "Uploading to kappa.lol... dankCircle")
pr, pw := io.Pipe() pr, pw := io.Pipe()
form := multipart.NewWriter(pw) form := multipart.NewWriter(pw)
@ -223,28 +206,28 @@ func (ul *Uploader) KappaUpload(target, path string) {
err := form.WriteField("name", "xd") err := form.WriteField("name", "xd")
if err != nil { if err != nil {
ul.TwitchClient.Say(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err)) app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err))
os.Remove(path) os.Remove(path)
return return
} }
file, err := os.Open(path) // path to image file file, err := os.Open(path) // path to image file
if err != nil { if err != nil {
ul.TwitchClient.Say(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err)) app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err))
os.Remove(path) os.Remove(path)
return return
} }
w, err := form.CreateFormFile("file", path) w, err := form.CreateFormFile("file", path)
if err != nil { if err != nil {
ul.TwitchClient.Say(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err)) app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err))
os.Remove(path) os.Remove(path)
return return
} }
_, err = io.Copy(w, file) _, err = io.Copy(w, file)
if err != nil { if err != nil {
ul.TwitchClient.Say(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err)) app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err))
os.Remove(path) os.Remove(path)
return return
} }
@ -254,7 +237,7 @@ func (ul *Uploader) KappaUpload(target, path string) {
req, err := http.NewRequest(http.MethodPost, KAPPA_ENDPOINT, pr) req, err := http.NewRequest(http.MethodPost, KAPPA_ENDPOINT, pr)
if err != nil { if err != nil {
ul.TwitchClient.Say(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err)) app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err))
os.Remove(path) os.Remove(path)
return return
} }
@ -265,38 +248,38 @@ func (ul *Uploader) KappaUpload(target, path string) {
} }
resp, err := httpClient.Do(req) resp, err := httpClient.Do(req)
if err != nil { if err != nil {
ul.TwitchClient.Say(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err)) app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err))
os.Remove(path) os.Remove(path)
ul.Log.Errorln("Error while sending HTTP request:", err) app.Log.Errorln("Error while sending HTTP request:", err)
return return
} }
defer resp.Body.Close() defer resp.Body.Close()
ul.TwitchClient.Say(target, "Uploaded PogChamp") app.Send(target, "Uploaded PogChamp")
body, err := io.ReadAll(resp.Body) body, err := io.ReadAll(resp.Body)
if err != nil { if err != nil {
ul.TwitchClient.Say(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err)) app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err))
os.Remove(path) os.Remove(path)
ul.Log.Errorln("Error while reading response:", err) app.Log.Errorln("Error while reading response:", err)
return return
} }
jsonResponse := new(kappaResponse) jsonResponse := new(kappaResponse)
if err := json.Unmarshal(body, jsonResponse); err != nil { if err := json.Unmarshal(body, jsonResponse); err != nil {
ul.TwitchClient.Say(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err)) app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err))
ul.Log.Errorln("Error while unmarshalling JSON response:", err) app.Log.Errorln("Error while unmarshalling JSON response:", err)
return return
} }
var reply = jsonResponse.Link var reply = jsonResponse.Link
ul.TwitchClient.Say(target, fmt.Sprintf("Removing file: %s", path)) app.Send(target, fmt.Sprintf("Removing file: %s", path))
ul.TwitchClient.Say(target, reply) app.Send(target, reply)
} }
func (ul *Uploader) YafUpload(target, path string) { func (app *application) YafUpload(target, path string) {
defer os.Remove(path) defer os.Remove(path)
ul.TwitchClient.Say(target, "Uploading to yaf.ee... dankCircle") app.Send(target, "Uploading to yaf.ee... dankCircle")
pr, pw := io.Pipe() pr, pw := io.Pipe()
form := multipart.NewWriter(pw) form := multipart.NewWriter(pw)
@ -305,28 +288,28 @@ func (ul *Uploader) YafUpload(target, path string) {
err := form.WriteField("name", "xd") err := form.WriteField("name", "xd")
if err != nil { if err != nil {
ul.TwitchClient.Say(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err)) app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err))
os.Remove(path) os.Remove(path)
return return
} }
file, err := os.Open(path) // path to image file file, err := os.Open(path) // path to image file
if err != nil { if err != nil {
ul.TwitchClient.Say(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err)) app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err))
os.Remove(path) os.Remove(path)
return return
} }
w, err := form.CreateFormFile("file", path) w, err := form.CreateFormFile("file", path)
if err != nil { if err != nil {
ul.TwitchClient.Say(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err)) app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err))
os.Remove(path) os.Remove(path)
return return
} }
_, err = io.Copy(w, file) _, err = io.Copy(w, file)
if err != nil { if err != nil {
ul.TwitchClient.Say(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err)) app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err))
os.Remove(path) os.Remove(path)
return return
} }
@ -336,7 +319,7 @@ func (ul *Uploader) YafUpload(target, path string) {
req, err := http.NewRequest(http.MethodPost, YAF_ENDPOINT, pr) req, err := http.NewRequest(http.MethodPost, YAF_ENDPOINT, pr)
if err != nil { if err != nil {
ul.TwitchClient.Say(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err)) app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err))
os.Remove(path) os.Remove(path)
return return
} }
@ -347,25 +330,24 @@ func (ul *Uploader) YafUpload(target, path string) {
} }
resp, err := httpClient.Do(req) resp, err := httpClient.Do(req)
if err != nil { if err != nil {
ul.TwitchClient.Say(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err)) app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err))
os.Remove(path) os.Remove(path)
ul.Log.Errorln("Error while sending HTTP request:", err) app.Log.Errorln("Error while sending HTTP request:", err)
return return
} }
defer resp.Body.Close() defer resp.Body.Close()
ul.TwitchClient.Say(target, "Uploaded PogChamp")
body, err := io.ReadAll(resp.Body) body, err := io.ReadAll(resp.Body)
if err != nil { if err != nil {
ul.TwitchClient.Say(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err)) app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err))
os.Remove(path) os.Remove(path)
ul.Log.Errorln("Error while reading response:", err) app.Log.Errorln("Error while reading response:", err)
return return
} }
var reply = string(body[:]) var reply = string(body[:])
ul.TwitchClient.Say(target, fmt.Sprintf("Removing file: %s", path)) app.Send(target, fmt.Sprintf("Removing file: %s", path))
ul.TwitchClient.Say(target, reply) app.Send(target, reply)
} }

View file

@ -1,234 +0,0 @@
package commands
import (
"context"
"fmt"
"io"
"os"
"github.com/gempir/go-twitch-irc/v4"
"github.com/google/uuid"
"github.com/wader/goutubedl"
"go.uber.org/zap"
)
type Downloader struct {
TwitchClient *twitch.Client
Log *zap.SugaredLogger
}
func NewDownload(destination, target, link string, tc *twitch.Client, log *zap.SugaredLogger) {
dl := &Downloader{
TwitchClient: tc,
Log: log,
}
switch destination {
case "catbox":
dl.CatboxDownload(target, link)
case "yaf":
dl.YafDownload(target, link)
case "kappa":
dl.KappaDownload(target, link)
case "gofile":
dl.GofileDownload(target, link)
}
}
func (dl *Downloader) YafDownload(target, link string) {
goutubedl.Path = "yt-dlp"
dl.TwitchClient.Say(target, "Downloading... dankCircle")
result, err := goutubedl.New(context.Background(), link, goutubedl.Options{})
if err != nil {
dl.Log.Errorln(err)
dl.TwitchClient.Say(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err))
return
}
rExt := result.Info.Ext
downloadResult, err := result.Download(context.Background(), "best")
if err != nil {
dl.Log.Errorln(err)
dl.TwitchClient.Say(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err))
return
}
dl.TwitchClient.Say(target, "Downloaded.")
uuidFilename, err := uuid.NewUUID()
if err != nil {
dl.Log.Errorln(err)
dl.TwitchClient.Say(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err))
return
}
fileName := fmt.Sprintf("%s.%s", uuidFilename, rExt)
f, err := os.Create(fileName)
dl.TwitchClient.Say(target, fmt.Sprintf("Filename: %s", fileName))
if err != nil {
dl.Log.Errorln(err)
dl.TwitchClient.Say(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err))
return
}
defer f.Close()
if _, err = io.Copy(f, downloadResult); err != nil {
dl.Log.Errorln(err)
dl.TwitchClient.Say(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err))
return
}
downloadResult.Close()
f.Close()
// duration := 5 * time.Second
// dl.twitchClient.Say(target, "ResidentSleeper ..")
// time.Sleep(duration)
go NewUpload("yaf", fileName, target, dl.TwitchClient, dl.Log)
}
func (dl *Downloader) KappaDownload(target, link string) {
goutubedl.Path = "yt-dlp"
dl.TwitchClient.Say(target, "Downloading... dankCircle")
result, err := goutubedl.New(context.Background(), link, goutubedl.Options{})
if err != nil {
dl.Log.Errorln(err)
dl.TwitchClient.Say(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err))
return
}
rExt := result.Info.Ext
downloadResult, err := result.Download(context.Background(), "best")
if err != nil {
dl.Log.Errorln(err)
dl.TwitchClient.Say(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err))
return
}
dl.TwitchClient.Say(target, "Downloaded.")
uuidFilename, err := uuid.NewUUID()
if err != nil {
dl.Log.Errorln(err)
dl.TwitchClient.Say(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err))
return
}
fileName := fmt.Sprintf("%s.%s", uuidFilename, rExt)
f, err := os.Create(fileName)
dl.TwitchClient.Say(target, fmt.Sprintf("Filename: %s", fileName))
if err != nil {
dl.Log.Errorln(err)
dl.TwitchClient.Say(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err))
return
}
defer f.Close()
if _, err = io.Copy(f, downloadResult); err != nil {
dl.Log.Errorln(err)
dl.TwitchClient.Say(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err))
return
}
downloadResult.Close()
f.Close()
// duration := 5 * time.Second
// dl.twitchClient.Say(target, "ResidentSleeper ..")
// time.Sleep(duration)
go NewUpload("kappa", fileName, target, dl.TwitchClient, dl.Log)
}
func (dl *Downloader) GofileDownload(target, link string) {
goutubedl.Path = "yt-dlp"
dl.TwitchClient.Say(target, "Downloading... dankCircle")
result, err := goutubedl.New(context.Background(), link, goutubedl.Options{})
if err != nil {
dl.Log.Errorln(err)
dl.TwitchClient.Say(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err))
return
}
safeFilename := fmt.Sprintf("download_%s", result.Info.Title)
rExt := result.Info.Ext
downloadResult, err := result.Download(context.Background(), "best")
if err != nil {
dl.Log.Errorln(err)
dl.TwitchClient.Say(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err))
return
}
dl.TwitchClient.Say(target, "Downloaded.")
fileName := fmt.Sprintf("%s.%s", safeFilename, rExt)
f, err := os.Create(fileName)
dl.TwitchClient.Say(target, fmt.Sprintf("Filename: %s", fileName))
if err != nil {
dl.Log.Errorln(err)
dl.TwitchClient.Say(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err))
return
}
defer f.Close()
if _, err = io.Copy(f, downloadResult); err != nil {
dl.Log.Errorln(err)
dl.TwitchClient.Say(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err))
return
}
downloadResult.Close()
f.Close()
// duration := 5 * time.Second
// dl.twitchClient.Say(target, "ResidentSleeper ..")
// time.Sleep(duration)
go NewUpload("gofile", fileName, target, dl.TwitchClient, dl.Log)
}
func (dl *Downloader) CatboxDownload(target, link string) {
goutubedl.Path = "yt-dlp"
var fileName string
dl.TwitchClient.Say(target, "Downloading... dankCircle")
result, err := goutubedl.New(context.Background(), link, goutubedl.Options{})
if err != nil {
dl.Log.Errorln(err)
dl.TwitchClient.Say(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err))
return
}
// I don't know why but I need to set it to mp4, otherwise if
// I use `result.Into.Ext` catbox won't play the video in the
// browser and say this message:
// `No video with supported format and MIME type found.`
rExt := "mp4"
downloadResult, err := result.Download(context.Background(), "best")
if err != nil {
dl.Log.Errorln(err)
dl.TwitchClient.Say(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err))
return
}
dl.TwitchClient.Say(target, "Downloaded.")
uuidFilename, err := uuid.NewUUID()
if err != nil {
dl.Log.Errorln(err)
dl.TwitchClient.Say(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err))
return
}
fileName = fmt.Sprintf("%s.%s", uuidFilename, rExt)
f, err := os.Create(fileName)
dl.TwitchClient.Say(target, fmt.Sprintf("Filename: %s", fileName))
if err != nil {
dl.Log.Errorln(err)
dl.TwitchClient.Say(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err))
return
}
defer f.Close()
if _, err = io.Copy(f, downloadResult); err != nil {
dl.Log.Errorln(err)
dl.TwitchClient.Say(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err))
return
}
downloadResult.Close()
f.Close()
go NewUpload("catbox", fileName, target, dl.TwitchClient, dl.Log)
}