test download command

This commit is contained in:
Watn3y 2023-04-21 07:35:42 +02:00
parent 33d529ad51
commit 24bbd8926c
9 changed files with 219 additions and 2 deletions

View file

@ -5,7 +5,10 @@ WORKDIR /tmp/build
#install update system and install packages #install update system and install packages
RUN apk update RUN apk update
RUN apk upgrade --available RUN apk upgrade --available
RUN apk add alpine-sdk RUN apk add alpine-sdk ffmpeg python3 py3-pip
#install yt-dlp
RUN pip3 install yt-dlp
#build bloaterbot #build bloaterbot
COPY . . COPY . .

View file

@ -8,6 +8,7 @@ import (
"strings" "strings"
"time" "time"
"watn3y/bloaterbotv3/botIO" "watn3y/bloaterbotv3/botIO"
"watn3y/bloaterbotv3/commands/download"
"watn3y/bloaterbotv3/commands/gaypoints" "watn3y/bloaterbotv3/commands/gaypoints"
"watn3y/bloaterbotv3/commands/notify" "watn3y/bloaterbotv3/commands/notify"
"watn3y/bloaterbotv3/config" "watn3y/bloaterbotv3/config"
@ -30,6 +31,8 @@ func Commands(update tgbotapi.Update, bot *tgbotapi.BotAPI) {
gaypoints.SetGP(update, bot) gaypoints.SetGP(update, bot)
case "remindme": case "remindme":
notify.Reminder(update, bot) notify.Reminder(update, bot)
case "download":
go download.Download(update, bot)
case "help": case "help":
help(update, bot) help(update, bot)
case "info": case "info":

View file

@ -0,0 +1,176 @@
package download
import (
"errors"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
"log"
"math/rand"
"net/url"
"os"
"os/exec"
"regexp"
"strconv"
"strings"
"time"
"watn3y/bloaterbotv3/botIO"
"watn3y/bloaterbotv3/config"
)
func Download(update tgbotapi.Update, bot *tgbotapi.BotAPI) {
log.Println("[download] Downloading Video URL " + update.Message.CommandArguments() + " for " + strconv.FormatInt(update.Message.From.ID, 10) + " in chat " + strconv.FormatInt(update.Message.Chat.ID, 10))
commandArgs := strings.Fields(update.Message.CommandArguments())
var arg string
if len(commandArgs) >= 0 {
arg = commandArgs[0]
} else {
log.Println("[download] Failed to download Video URL " + update.Message.CommandArguments() + " NO URL")
message := tgbotapi.MessageConfig{
BaseChat: tgbotapi.BaseChat{ChatID: update.Message.Chat.ID, ReplyToMessageID: update.Message.MessageID},
Text: "Please specify a valid YouTube or Reddit URL to download a Video",
}
botIO.SendMessage(message, bot)
return
}
service := matchURL(arg)
if service == "" {
log.Println("[download] Failed to download Video URL " + update.Message.CommandArguments() + " INVALID URL")
message := tgbotapi.MessageConfig{
BaseChat: tgbotapi.BaseChat{ChatID: update.Message.Chat.ID, ReplyToMessageID: update.Message.MessageID},
Text: "Please specify a valid YouTube or Reddit URL to download a Video",
}
botIO.SendMessage(message, bot)
return
}
downloadTarget := randomString(20)
videoDownloaded := runYTDL(arg, downloadTarget)
if !videoDownloaded {
message := tgbotapi.MessageConfig{
BaseChat: tgbotapi.BaseChat{ChatID: update.Message.Chat.ID, ReplyToMessageID: update.Message.MessageID},
Text: "Something went wrong while downloading your video :(",
}
botIO.SendMessage(message, bot)
return
}
files, err := os.ReadDir("./videos/" + downloadTarget)
if err != nil {
log.Println("[download] Failed to process Video URL "+update.Message.CommandArguments(), " FAILED TO READ DIRECTORY: ", err.Error())
message := tgbotapi.MessageConfig{
BaseChat: tgbotapi.BaseChat{ChatID: update.Message.Chat.ID, ReplyToMessageID: update.Message.MessageID},
Text: "Something went wrong while downloading your video :(",
}
botIO.SendMessage(message, bot)
return
}
if len(files) >= 2 {
log.Println("[download] Failed to process Video URL "+update.Message.CommandArguments(), " TOO MANY FILES")
message := tgbotapi.MessageConfig{
BaseChat: tgbotapi.BaseChat{ChatID: update.Message.Chat.ID, ReplyToMessageID: update.Message.MessageID},
Text: "Something went wrong while downloading your video :(",
}
botIO.SendMessage(message, bot)
return
}
/*fExt := filepath.Ext(config.BotConfig.Download.Prefix + "/" + downloadTarget + "/" + files[0].Name()
imageTypes := []string{"jpg","jpeg","png"}
if commonlogic.ContainsString(imageTypes,fExt) {
} else if fExt == "mp4" {
} else {
}*/
message := tgbotapi.MessageConfig{
BaseChat: tgbotapi.BaseChat{ChatID: update.Message.Chat.ID, ReplyToMessageID: update.Message.MessageID},
DisableWebPagePreview: false,
Text: "Downloaded! You can get your video at: " + config.BotConfig.Download.Prefix + "/" + downloadTarget + "/" + url.PathEscape(files[0].Name()) + "\n It will be available for 30 Minutes.",
}
botIO.SendMessage(message, bot)
time.Sleep(30 * time.Minute)
err = os.RemoveAll("./videos/" + downloadTarget)
if err != nil {
log.Println("[download] Failed to remove old Video "+update.Message.CommandArguments(), err.Error())
return
}
}
func randomString(n int) string {
rand.Seed(time.Now().UnixNano())
var letters = []rune("1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
b := make([]rune, n)
for i := range b {
b[i] = letters[rand.Intn(len(letters))]
}
return string(b)
}
func runYTDL(URL string, targetDir string) (success bool) {
cmd := exec.Command("yt-dlp", "-f", "bv*[ext=mp4]+ba[ext=m4a] / bv*+ba/b", "--no-playlist", "-o", "videos/"+targetDir+"/"+"%(title)s.%(ext)s", URL)
out, err := cmd.CombinedOutput()
var (
ee *exec.ExitError
pe *os.PathError
)
if errors.As(err, &ee) {
log.Println("[download.ytdl] Failed to download Video URL "+URL, " NON ZERO EXIT CODE: ", ee.ExitCode())
success = false
} else if errors.As(err, &pe) {
log.Println("[download.ytdl] Failed to download Video URL "+URL, " OS PATH ERROR: ", pe.Error())
success = false
} else if err != nil {
log.Println("[download.ytdl] Failed to download Video URL "+URL, " GENERAL ERROR: ", err.Error())
success = false
} else {
log.Println("[download.ytdl] Downloaded Video URL " + URL)
success = true
}
if config.BotConfig.DebugMode {
print(string(out))
}
return success
}
func matchURL(URL string) (matchedURL string) {
parsedURL, err := url.Parse(URL)
if err != nil {
log.Fatal(err)
}
var domainRegex = regexp.MustCompile(`(youtube\.com$)|(youtu\.be$)|(reddit\.com$)`)
m := domainRegex.FindStringSubmatch(parsedURL.Hostname())
if len(m) >= 1 {
return m[0]
} else {
return ""
}
}
func serverFile(file os.File) {
}

View file

@ -9,3 +9,13 @@ func ContainsInt64(a []int64, b int64) bool {
return false return false
} }
func ContainsString(a []string, b string) bool {
for _, v := range a {
if v == b {
return true
}
}
return false
}

View file

@ -16,3 +16,6 @@ TriggerWords = ["french","france","baguette","casque bleu"]
[GayPoints] [GayPoints]
EnabledChats = [-00000,-111111,-22222] EnabledChats = [-00000,-111111,-22222]
ModifyUsers = [-00000,-111111,-22222] ModifyUsers = [-00000,-111111,-22222]
[Download]
Prefix = "http://192.168.0.10:3556"

View file

@ -26,6 +26,9 @@ type config struct {
EnabledChats []int64 EnabledChats []int64
ModifyUsers []int64 ModifyUsers []int64
} }
Download struct {
Prefix string
}
} }
var BotConfig config var BotConfig config

View file

@ -4,12 +4,14 @@ import (
"watn3y/bloaterbotv3/commands/gaypoints" "watn3y/bloaterbotv3/commands/gaypoints"
"watn3y/bloaterbotv3/commands/notify" "watn3y/bloaterbotv3/commands/notify"
"watn3y/bloaterbotv3/config" "watn3y/bloaterbotv3/config"
"watn3y/bloaterbotv3/webserver"
) )
func main() { func main() {
config.LoadConfig() config.LoadConfig()
gaypoints.InitDB() gaypoints.InitDB()
notify.InitDB() notify.InitDB()
go webserver.RunWeb()
bot() bot()
} }

0
videos/index.html Normal file
View file

17
webserver/webserver.go Normal file
View file

@ -0,0 +1,17 @@
package webserver
import (
"log"
"net/http"
)
func RunWeb() {
fs := http.FileServer(http.Dir("./videos"))
http.Handle("/", fs)
log.Print("Listening on :3556...")
err := http.ListenAndServe(":3556", nil)
if err != nil {
log.Fatal(err)
}
}