mirror-nourybot/internal/commands/download.go

150 lines
3.4 KiB
Go
Raw Normal View History

2023-10-03 17:23:52 +02:00
package commands
import (
"context"
"fmt"
"io"
"mime/multipart"
"net/http"
"os"
"time"
"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
URL string
2023-10-03 17:23:52 +02:00
}
func Download(target, link, fileUploadURL string, tc *twitch.Client, log *zap.SugaredLogger) (reply string) {
2023-10-03 17:23:52 +02:00
dloader := &downloader{
Log: log,
twitchClient: tc,
URL: fileUploadURL,
2023-10-03 17:23:52 +02:00
}
go dloader.dlxd(target, link)
2023-10-03 17:23:52 +02:00
return ""
}
func (dl *downloader) dlxd(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)
}
rExt := result.Info.Ext
2023-10-03 17:23:52 +02:00
downloadResult, err := result.Download(context.Background(), "best")
if err != nil {
dl.Log.Errorln(err)
}
dl.twitchClient.Say(target, "Downloaded.")
2023-10-03 17:23:52 +02:00
fn, err := uuid.NewUUID()
if err != nil {
dl.Log.Errorln(err)
}
f, err := os.Create(fmt.Sprintf("%s.%s", fn, rExt))
dl.twitchClient.Say(target, fmt.Sprintf("Filename: %s.%s", fn, rExt))
if err != nil {
dl.Log.Errorln(err)
}
defer f.Close()
if _, err = io.Copy(f, downloadResult); err != nil {
dl.Log.Errorln(err)
}
downloadResult.Close()
f.Close()
2023-10-05 11:58:19 +02:00
// duration := 5 * time.Second
// dl.twitchClient.Say(target, "ResidentSleeper ..")
// time.Sleep(duration)
2023-10-03 17:23:52 +02:00
dl.upload(target, fmt.Sprintf("%s.%s", fn, rExt))
}
func (dl *downloader) upload(target, path string) {
dl.twitchClient.Say(target, "Uploading... dankCircle")
2023-10-03 17:23:52 +02:00
pr, pw := io.Pipe()
form := multipart.NewWriter(pw)
go func() {
defer pw.Close()
err := form.WriteField("name", "xd")
if err != nil {
dl.twitchClient.Say(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err))
2023-10-05 11:58:19 +02:00
os.Remove(path)
2023-10-03 17:23:52 +02:00
return
}
file, err := os.Open(path) // path to image file
if err != nil {
dl.twitchClient.Say(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err))
2023-10-05 11:58:19 +02:00
os.Remove(path)
2023-10-03 17:23:52 +02:00
return
}
w, err := form.CreateFormFile("file", path)
if err != nil {
dl.twitchClient.Say(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err))
2023-10-05 11:58:19 +02:00
os.Remove(path)
2023-10-03 17:23:52 +02:00
return
}
_, err = io.Copy(w, file)
if err != nil {
dl.twitchClient.Say(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err))
2023-10-05 11:58:19 +02:00
os.Remove(path)
2023-10-03 17:23:52 +02:00
return
}
form.Close()
}()
req, err := http.NewRequest(http.MethodPost, dl.URL, pr)
2023-10-03 17:23:52 +02:00
if err != nil {
dl.twitchClient.Say(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err))
2023-10-05 11:58:19 +02:00
os.Remove(path)
2023-10-03 17:23:52 +02:00
return
}
req.Header.Set("Content-Type", form.FormDataContentType())
2023-10-05 11:58:19 +02:00
httpClient := http.Client{
Timeout: 300 * time.Second,
}
2023-10-03 17:23:52 +02:00
resp, err := httpClient.Do(req)
if err != nil {
dl.twitchClient.Say(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err))
2023-10-05 11:58:19 +02:00
os.Remove(path)
2023-10-03 17:23:52 +02:00
dl.Log.Errorln("Error while sending HTTP request:", err)
return
}
defer resp.Body.Close()
dl.twitchClient.Say(target, "Uploaded PogChamp")
2023-10-03 17:23:52 +02:00
body, err := io.ReadAll(resp.Body)
if err != nil {
dl.twitchClient.Say(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err))
2023-10-05 11:58:19 +02:00
os.Remove(path)
2023-10-03 17:23:52 +02:00
dl.Log.Errorln("Error while reading response:", err)
return
}
var reply = string(body[:])
dl.twitchClient.Say(target, fmt.Sprintf("Removing file: %s", path))
os.Remove(path)
dl.twitchClient.Say(target, reply)
}