mirror of
https://github.com/lyx0/nourybot.git
synced 2024-11-13 19:49:55 +01:00
move download/upload into application
This commit is contained in:
parent
bd7799ca63
commit
bda3b1e05b
4 changed files with 291 additions and 320 deletions
|
@ -84,16 +84,16 @@ func (app *application) handleCommand(message twitch.PrivateMessage) {
|
|||
}
|
||||
|
||||
case "catbox":
|
||||
go commands.NewDownload("catbox", target, cmdParams[1], app.TwitchClient, app.Log)
|
||||
go app.NewDownload("catbox", target, cmdParams[1])
|
||||
|
||||
case "kappa":
|
||||
go commands.NewDownload("kappa", target, cmdParams[1], app.TwitchClient, app.Log)
|
||||
go app.NewDownload("kappa", target, cmdParams[1])
|
||||
|
||||
case "yaf":
|
||||
go commands.NewDownload("yaf", target, cmdParams[1], app.TwitchClient, app.Log)
|
||||
go app.NewDownload("yaf", target, cmdParams[1])
|
||||
|
||||
case "gofile":
|
||||
go commands.NewDownload("gofile", target, cmdParams[1], app.TwitchClient, app.Log)
|
||||
go app.NewDownload("gofile", target, cmdParams[1])
|
||||
|
||||
case "mail":
|
||||
app.SendEmail("Test command used!", "This is an email test")
|
||||
|
|
223
cmd/nourybot/download.go
Normal file
223
cmd/nourybot/download.go
Normal 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)
|
||||
}
|
|
@ -6,7 +6,7 @@
|
|||
// Use of this source code is governed by the MIT
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package commands
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
@ -18,9 +18,6 @@ import (
|
|||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"github.com/gempir/go-twitch-irc/v4"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
const (
|
||||
|
@ -30,48 +27,30 @@ const (
|
|||
YAF_ENDPOINT = "https://i.yaf.ee/upload"
|
||||
)
|
||||
|
||||
type Uploader struct {
|
||||
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,
|
||||
}
|
||||
func (app *application) NewUpload(destination, fileName, target string) {
|
||||
|
||||
switch destination {
|
||||
case "catbox":
|
||||
go ul.CatboxUpload(target, fileName)
|
||||
go app.CatboxUpload(target, fileName)
|
||||
case "yaf":
|
||||
go ul.YafUpload(target, fileName)
|
||||
go app.YafUpload(target, fileName)
|
||||
case "kappa":
|
||||
go ul.KappaUpload(target, fileName)
|
||||
go app.KappaUpload(target, fileName)
|
||||
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)
|
||||
file, err := os.Open(fileName)
|
||||
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
|
||||
}
|
||||
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 {
|
||||
// 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()
|
||||
|
||||
m.WriteField("reqtype", "fileupload")
|
||||
m.WriteField("time", ul.Time)
|
||||
m.WriteField("time", "24h")
|
||||
part, err := m.CreateFormFile("fileToUpload", filepath.Base(file.Name()))
|
||||
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
|
||||
}
|
||||
|
||||
|
@ -100,27 +79,31 @@ func (ul *Uploader) CatboxUpload(target, fileName string) {
|
|||
req, _ := http.NewRequest(http.MethodPost, CATBOX_ENDPOINT, r)
|
||||
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 {
|
||||
ul.TwitchClient.Say(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err))
|
||||
app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err))
|
||||
return
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := ioutil.ReadAll(resp.Body)
|
||||
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
|
||||
}
|
||||
|
||||
reply := string(body)
|
||||
ul.TwitchClient.Say(target, fmt.Sprintf("Removing file: %s", fileName))
|
||||
ul.TwitchClient.Say(target, reply)
|
||||
app.Send(target, fmt.Sprintf("Removing file: %s", fileName))
|
||||
app.Send(target, reply)
|
||||
}
|
||||
|
||||
func (ul *Uploader) GofileUpload(target, path string) {
|
||||
func (app *application) GofileUpload(target, path string) {
|
||||
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()
|
||||
form := multipart.NewWriter(pw)
|
||||
|
||||
|
@ -143,21 +126,21 @@ func (ul *Uploader) GofileUpload(target, path string) {
|
|||
|
||||
file, err := os.Open(path) // path to image file
|
||||
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)
|
||||
return
|
||||
}
|
||||
|
||||
w, err := form.CreateFormFile("file", path)
|
||||
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)
|
||||
return
|
||||
}
|
||||
|
||||
_, err = io.Copy(w, file)
|
||||
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)
|
||||
return
|
||||
}
|
||||
|
@ -167,7 +150,7 @@ func (ul *Uploader) GofileUpload(target, path string) {
|
|||
|
||||
req, err := http.NewRequest(http.MethodPost, GOFILE_ENDPOINT, pr)
|
||||
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)
|
||||
return
|
||||
}
|
||||
|
@ -178,39 +161,39 @@ func (ul *Uploader) GofileUpload(target, path string) {
|
|||
}
|
||||
resp, err := httpClient.Do(req)
|
||||
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)
|
||||
ul.Log.Errorln("Error while sending HTTP request:", err)
|
||||
app.Log.Errorln("Error while sending HTTP request:", err)
|
||||
|
||||
return
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
ul.TwitchClient.Say(target, "Uploaded PogChamp")
|
||||
app.Send(target, "Uploaded PogChamp")
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
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)
|
||||
ul.Log.Errorln("Error while reading response:", err)
|
||||
app.Log.Errorln("Error while reading response:", err)
|
||||
return
|
||||
}
|
||||
|
||||
jsonResponse := new(gofileResponse)
|
||||
if err := json.Unmarshal(body, jsonResponse); err != nil {
|
||||
ul.TwitchClient.Say(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err))
|
||||
ul.Log.Errorln("Error while unmarshalling JSON response:", err)
|
||||
app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err))
|
||||
app.Log.Errorln("Error while unmarshalling JSON response:", err)
|
||||
return
|
||||
}
|
||||
|
||||
var reply = jsonResponse.Data.DownloadPage
|
||||
|
||||
ul.TwitchClient.Say(target, fmt.Sprintf("Removing file: %s", path))
|
||||
ul.TwitchClient.Say(target, reply)
|
||||
app.Send(target, fmt.Sprintf("Removing file: %s", path))
|
||||
app.Send(target, reply)
|
||||
}
|
||||
|
||||
func (ul *Uploader) KappaUpload(target, path string) {
|
||||
func (app *application) KappaUpload(target, path string) {
|
||||
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()
|
||||
form := multipart.NewWriter(pw)
|
||||
|
||||
|
@ -223,28 +206,28 @@ func (ul *Uploader) KappaUpload(target, path string) {
|
|||
|
||||
err := form.WriteField("name", "xd")
|
||||
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)
|
||||
return
|
||||
}
|
||||
|
||||
file, err := os.Open(path) // path to image file
|
||||
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)
|
||||
return
|
||||
}
|
||||
|
||||
w, err := form.CreateFormFile("file", path)
|
||||
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)
|
||||
return
|
||||
}
|
||||
|
||||
_, err = io.Copy(w, file)
|
||||
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)
|
||||
return
|
||||
}
|
||||
|
@ -254,7 +237,7 @@ func (ul *Uploader) KappaUpload(target, path string) {
|
|||
|
||||
req, err := http.NewRequest(http.MethodPost, KAPPA_ENDPOINT, pr)
|
||||
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)
|
||||
return
|
||||
}
|
||||
|
@ -265,38 +248,38 @@ func (ul *Uploader) KappaUpload(target, path string) {
|
|||
}
|
||||
resp, err := httpClient.Do(req)
|
||||
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)
|
||||
ul.Log.Errorln("Error while sending HTTP request:", err)
|
||||
app.Log.Errorln("Error while sending HTTP request:", err)
|
||||
|
||||
return
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
ul.TwitchClient.Say(target, "Uploaded PogChamp")
|
||||
app.Send(target, "Uploaded PogChamp")
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
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)
|
||||
ul.Log.Errorln("Error while reading response:", err)
|
||||
app.Log.Errorln("Error while reading response:", err)
|
||||
return
|
||||
}
|
||||
|
||||
jsonResponse := new(kappaResponse)
|
||||
if err := json.Unmarshal(body, jsonResponse); err != nil {
|
||||
ul.TwitchClient.Say(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err))
|
||||
ul.Log.Errorln("Error while unmarshalling JSON response:", err)
|
||||
app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err))
|
||||
app.Log.Errorln("Error while unmarshalling JSON response:", err)
|
||||
return
|
||||
}
|
||||
|
||||
var reply = jsonResponse.Link
|
||||
|
||||
ul.TwitchClient.Say(target, fmt.Sprintf("Removing file: %s", path))
|
||||
ul.TwitchClient.Say(target, reply)
|
||||
app.Send(target, fmt.Sprintf("Removing file: %s", path))
|
||||
app.Send(target, reply)
|
||||
}
|
||||
func (ul *Uploader) YafUpload(target, path string) {
|
||||
func (app *application) YafUpload(target, path string) {
|
||||
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()
|
||||
form := multipart.NewWriter(pw)
|
||||
|
||||
|
@ -305,28 +288,28 @@ func (ul *Uploader) YafUpload(target, path string) {
|
|||
|
||||
err := form.WriteField("name", "xd")
|
||||
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)
|
||||
return
|
||||
}
|
||||
|
||||
file, err := os.Open(path) // path to image file
|
||||
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)
|
||||
return
|
||||
}
|
||||
|
||||
w, err := form.CreateFormFile("file", path)
|
||||
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)
|
||||
return
|
||||
}
|
||||
|
||||
_, err = io.Copy(w, file)
|
||||
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)
|
||||
return
|
||||
}
|
||||
|
@ -336,7 +319,7 @@ func (ul *Uploader) YafUpload(target, path string) {
|
|||
|
||||
req, err := http.NewRequest(http.MethodPost, YAF_ENDPOINT, pr)
|
||||
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)
|
||||
return
|
||||
}
|
||||
|
@ -347,25 +330,24 @@ func (ul *Uploader) YafUpload(target, path string) {
|
|||
}
|
||||
resp, err := httpClient.Do(req)
|
||||
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)
|
||||
ul.Log.Errorln("Error while sending HTTP request:", err)
|
||||
app.Log.Errorln("Error while sending HTTP request:", err)
|
||||
|
||||
return
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
ul.TwitchClient.Say(target, "Uploaded PogChamp")
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
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)
|
||||
ul.Log.Errorln("Error while reading response:", err)
|
||||
app.Log.Errorln("Error while reading response:", err)
|
||||
return
|
||||
}
|
||||
|
||||
var reply = string(body[:])
|
||||
|
||||
ul.TwitchClient.Say(target, fmt.Sprintf("Removing file: %s", path))
|
||||
ul.TwitchClient.Say(target, reply)
|
||||
app.Send(target, fmt.Sprintf("Removing file: %s", path))
|
||||
app.Send(target, reply)
|
||||
}
|
|
@ -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)
|
||||
}
|
Loading…
Reference in a new issue