mirror-nourybot/cmd/nourybot/download.go

217 lines
6.1 KiB
Go
Raw Normal View History

2023-10-05 21:21:26 +02:00
package main
import (
"context"
"fmt"
"io"
"os"
"github.com/gempir/go-twitch-irc/v4"
2023-10-05 21:21:26 +02:00
"github.com/google/uuid"
"github.com/wader/goutubedl"
)
func (app *application) NewDownload(destination, target, link string, msg twitch.PrivateMessage) {
identifier := uuid.NewString()
go app.Models.Uploads.Insert(
msg.User.Name,
msg.User.ID,
msg.Channel,
msg.Message,
destination,
link,
identifier,
)
2023-10-05 21:21:26 +02:00
switch destination {
case "catbox":
app.CatboxDownload(target, link, identifier, msg)
2023-10-05 21:21:26 +02:00
case "yaf":
app.YafDownload(target, link, identifier, msg)
2023-10-05 21:21:26 +02:00
case "kappa":
app.KappaDownload(target, link, identifier, msg)
2023-10-05 21:21:26 +02:00
case "gofile":
app.GofileDownload(target, link, identifier, msg)
2023-10-05 21:21:26 +02:00
}
}
func (app *application) YafDownload(target, link, identifier string, msg twitch.PrivateMessage) {
2023-10-05 21:21:26 +02:00
goutubedl.Path = "yt-dlp"
app.Send(target, "Downloading... dankCircle", msg)
2023-10-05 21:21:26 +02:00
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), msg)
2023-10-05 21:21:26 +02:00
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), msg)
2023-10-05 21:21:26 +02:00
return
}
2023-12-03 15:58:33 +01:00
//app.Send(target, "Downloaded.", msg)
fileName := fmt.Sprintf("%s.%s", identifier, rExt)
2023-10-05 21:21:26 +02:00
f, err := os.Create(fileName)
2023-12-03 15:58:33 +01:00
//app.Send(target, fmt.Sprintf("Filename: %s", fileName), msg)
2023-10-05 21:21:26 +02:00
if err != nil {
app.Log.Errorln(err)
app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err), msg)
2023-10-05 21:21:26 +02:00
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), msg)
2023-10-05 21:21:26 +02:00
return
}
downloadResult.Close()
f.Close()
// duration := 5 * time.Second
// dl.twitchClient.Say(target, "ResidentSleeper ..")
// time.Sleep(duration)
go app.NewUpload("yaf", fileName, target, identifier, msg)
2023-10-05 21:21:26 +02:00
}
func (app *application) KappaDownload(target, link, identifier string, msg twitch.PrivateMessage) {
2023-10-05 21:21:26 +02:00
goutubedl.Path = "yt-dlp"
app.Send(target, "Downloading... dankCircle", msg)
2023-10-05 21:21:26 +02:00
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), msg)
2023-10-05 21:21:26 +02:00
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), msg)
2023-10-05 21:21:26 +02:00
return
}
2023-12-03 15:58:33 +01:00
//app.Send(target, "Downloaded.", msg)
fileName := fmt.Sprintf("%s.%s", identifier, rExt)
2023-10-05 21:21:26 +02:00
f, err := os.Create(fileName)
2023-12-03 15:58:33 +01:00
//app.Send(target, fmt.Sprintf("Filename: %s", fileName), msg)
2023-10-05 21:21:26 +02:00
if err != nil {
app.Log.Errorln(err)
app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err), msg)
2023-10-05 21:21:26 +02:00
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), msg)
2023-10-05 21:21:26 +02:00
return
}
downloadResult.Close()
f.Close()
// duration := 5 * time.Second
// dl.twitchClient.Say(target, "ResidentSleeper ..")
// time.Sleep(duration)
go app.NewUpload("kappa", fileName, target, identifier, msg)
2023-10-05 21:21:26 +02:00
}
func (app *application) GofileDownload(target, link, identifier string, msg twitch.PrivateMessage) {
2023-10-05 21:21:26 +02:00
goutubedl.Path = "yt-dlp"
app.Send(target, "Downloading... dankCircle", msg)
2023-10-05 21:21:26 +02:00
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), msg)
2023-10-05 21:21:26 +02:00
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), msg)
2023-10-05 21:21:26 +02:00
return
}
2023-12-03 15:58:33 +01:00
//app.Send(target, "Downloaded.", msg)
2023-10-05 21:21:26 +02:00
fileName := fmt.Sprintf("%s.%s", safeFilename, rExt)
f, err := os.Create(fileName)
2023-12-03 15:58:33 +01:00
//app.Send(target, fmt.Sprintf("Filename: %s", fileName), msg)
2023-10-05 21:21:26 +02:00
if err != nil {
app.Log.Errorln(err)
app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err), msg)
2023-10-05 21:21:26 +02:00
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), msg)
2023-10-05 21:21:26 +02:00
return
}
downloadResult.Close()
f.Close()
// duration := 5 * time.Second
// dl.twitchClient.Say(target, "ResidentSleeper ..")
// time.Sleep(duration)
go app.NewUpload("gofile", fileName, target, identifier, msg)
2023-10-05 21:21:26 +02:00
}
func (app *application) CatboxDownload(target, link, identifier string, msg twitch.PrivateMessage) {
2023-10-05 21:21:26 +02:00
goutubedl.Path = "yt-dlp"
var fileName string
app.Send(target, "Downloading... dankCircle", msg)
2023-10-05 21:21:26 +02:00
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), msg)
2023-10-05 21:21:26 +02:00
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), msg)
2023-10-05 21:21:26 +02:00
return
}
2023-12-03 15:58:33 +01:00
//app.Send(target, "Downloaded.", msg)
fileName = fmt.Sprintf("%s.%s", identifier, rExt)
2023-10-05 21:21:26 +02:00
f, err := os.Create(fileName)
2023-12-03 15:58:33 +01:00
//app.Send(target, fmt.Sprintf("Filename: %s", fileName), msg)
2023-10-05 21:21:26 +02:00
if err != nil {
app.Log.Errorln(err)
app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err), msg)
2023-10-05 21:21:26 +02:00
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), msg)
2023-10-05 21:21:26 +02:00
return
}
downloadResult.Close()
f.Close()
go app.NewUpload("catbox", fileName, target, identifier, msg)
2023-10-05 21:21:26 +02:00
}