mirror-nourybot-matrix/cmd/bot/download.go

183 lines
5.1 KiB
Go
Raw Normal View History

package main
import (
"context"
"fmt"
"io"
"os"
"strings"
"github.com/google/uuid"
2024-01-15 22:06:57 +01:00
ffmpeg "github.com/u2takey/ffmpeg-go"
"github.com/wader/goutubedl"
"maunium.net/go/mautrix/event"
)
const (
//CATBOX_ENDPOINT = "https://litterbox.catbox.moe/resources/internals/api.php"
//KAPPA_ENDPOINT = "https://kappa.lol/api/upload"
YAF_ENDPOINT = "https://i.yaf.li/upload"
)
func (app *application) NewDownload(destination string, evt *event.Event, link string) {
uuid := uuid.NewString()
app.Log.Info().Msgf("Link: %s", link)
switch destination {
case "yaf":
app.YafDownload(evt, link, uuid)
2024-01-10 21:05:06 +01:00
// case "catbox":
// app.CatboxDownload(target, link, identifier, msg)
//case "kappa":
// app.KappaDownload(target, link, identifier, msg)
case "gofile":
app.GofileDownload(evt, link)
}
}
2024-01-15 22:06:57 +01:00
func (app *application) ConvertToMP4(evt *event.Event, link string) {
fName := "in"
goutubedl.Path = "yt-dlp"
var rExt string
app.SendText(evt, "Downloading...")
result, err := goutubedl.New(context.Background(), link, goutubedl.Options{})
if err != nil {
app.Log.Error().Err(err).Msg("Failed to download")
app.SendText(evt, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err))
return
}
// For some reason youtube links return webm as result.Info.Ext but
// are in reality mp4.
if strings.HasPrefix(link, "https://www.youtube.com/") || strings.HasPrefix(link, "https://youtu.be/") {
rExt = "mp4"
} else {
rExt = result.Info.Ext
}
downloadResult, err := result.Download(context.TODO(), "best")
if err != nil {
app.Log.Error().Err(err).Msg("Failed to download")
app.SendText(evt, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err))
return
}
defer downloadResult.Close()
fileName := fmt.Sprintf("%s.%s", fName, rExt)
f, err := os.Create(fileName)
if err != nil {
app.Log.Error().Err(err).Msg("Failed to download")
app.SendText(evt, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err))
return
}
defer f.Close()
io.Copy(f, downloadResult)
out := "out.mp4"
fn, err := os.Create(out)
if err != nil {
app.Log.Error().Err(err).Msg("Failed to download")
app.SendText(evt, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err))
return
}
defer fn.Close()
err = ffmpeg.Input(fileName).
Output(out).
OverWriteOutput().ErrorToStdOut().Run()
defer os.Remove(fileName)
app.NewUpload("yaf", evt, out)
}
func (app *application) YafDownload(evt *event.Event, link, uuid string) {
goutubedl.Path = "yt-dlp"
var rExt string
app.SendText(evt, "Downloading...")
result, err := goutubedl.New(context.Background(), link, goutubedl.Options{})
if err != nil {
app.Log.Error().Err(err).Msg("Failed to download")
app.SendText(evt, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err))
return
}
// For some reason youtube links return webm as result.Info.Ext but
// are in reality mp4.
2024-01-11 00:49:33 +01:00
if strings.HasPrefix(link, "https://www.youtube.com/") || strings.HasPrefix(link, "https://youtu.be/") {
rExt = "mp4"
} else {
rExt = result.Info.Ext
}
downloadResult, err := result.Download(context.TODO(), "best")
if err != nil {
app.Log.Error().Err(err).Msg("Failed to download")
app.SendText(evt, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err))
return
}
defer downloadResult.Close()
fileName := fmt.Sprintf("%s.%s", uuid, rExt)
f, err := os.Create(fileName)
if err != nil {
app.Log.Error().Err(err).Msg("Failed to download")
app.SendText(evt, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err))
return
}
defer f.Close()
io.Copy(f, downloadResult)
app.NewUpload("yaf", evt, fileName)
}
2024-01-10 21:05:06 +01:00
func (app *application) GofileDownload(evt *event.Event, link string) {
goutubedl.Path = "yt-dlp"
var rExt string
app.SendText(evt, "Downloading...")
result, err := goutubedl.New(context.Background(), link, goutubedl.Options{})
if err != nil {
app.Log.Error().Err(err).Msg("Failed to download")
app.SendText(evt, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err))
return
}
// For some reason youtube links return webm as result.Info.Ext but
// are in reality mp4.
2024-01-11 00:49:33 +01:00
if strings.HasPrefix(link, "https://www.youtube.com/") || strings.HasPrefix(link, "https://youtu.be/") {
2024-01-10 21:05:06 +01:00
rExt = "mp4"
} else {
rExt = result.Info.Ext
}
safeFilename := fmt.Sprintf("download_%s", result.Info.Title)
downloadResult, err := result.Download(context.Background(), "best")
if err != nil {
app.Log.Error().Err(err).Msg("Failed to download")
app.SendText(evt, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err))
return
}
fileName := fmt.Sprintf("%s.%s", safeFilename, rExt)
f, err := os.Create(fileName)
//app.Send(target, fmt.Sprintf("Filename: %s", fileName), msg)
if err != nil {
app.Log.Error().Err(err).Msg("Failed to download")
app.SendText(evt, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err))
return
}
defer f.Close()
if _, err = io.Copy(f, downloadResult); err != nil {
app.Log.Error().Err(err).Msg("Failed to download")
app.SendText(evt, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err))
return
}
downloadResult.Close()
f.Close()
app.SendText(evt, fileName)
go app.NewUpload("gofile", evt, fileName)
}