From 24bbd8926c2044b5227614011bc1d8de05c1b8d0 Mon Sep 17 00:00:00 2001 From: Watn3y Date: Fri, 21 Apr 2023 07:35:42 +0200 Subject: [PATCH 1/4] test download command --- Dockerfile | 5 +- commands/commands.go | 3 + commands/download/download.go | 176 ++++++++++++++++++++++++++++++++++ commonlogic/commonlogic.go | 10 ++ config.toml.example | 5 +- config/config.go | 3 + main.go | 2 + videos/index.html | 0 webserver/webserver.go | 17 ++++ 9 files changed, 219 insertions(+), 2 deletions(-) create mode 100644 commands/download/download.go create mode 100644 videos/index.html create mode 100644 webserver/webserver.go diff --git a/Dockerfile b/Dockerfile index 1845826..d01a714 100644 --- a/Dockerfile +++ b/Dockerfile @@ -5,7 +5,10 @@ WORKDIR /tmp/build #install update system and install packages RUN apk update 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 COPY . . diff --git a/commands/commands.go b/commands/commands.go index 78b8611..2de9db1 100644 --- a/commands/commands.go +++ b/commands/commands.go @@ -8,6 +8,7 @@ import ( "strings" "time" "watn3y/bloaterbotv3/botIO" + "watn3y/bloaterbotv3/commands/download" "watn3y/bloaterbotv3/commands/gaypoints" "watn3y/bloaterbotv3/commands/notify" "watn3y/bloaterbotv3/config" @@ -30,6 +31,8 @@ func Commands(update tgbotapi.Update, bot *tgbotapi.BotAPI) { gaypoints.SetGP(update, bot) case "remindme": notify.Reminder(update, bot) + case "download": + go download.Download(update, bot) case "help": help(update, bot) case "info": diff --git a/commands/download/download.go b/commands/download/download.go new file mode 100644 index 0000000..02fcf82 --- /dev/null +++ b/commands/download/download.go @@ -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) { + +} diff --git a/commonlogic/commonlogic.go b/commonlogic/commonlogic.go index 58d7b2c..7721dc5 100644 --- a/commonlogic/commonlogic.go +++ b/commonlogic/commonlogic.go @@ -9,3 +9,13 @@ func ContainsInt64(a []int64, b int64) bool { return false } + +func ContainsString(a []string, b string) bool { + for _, v := range a { + if v == b { + return true + } + } + + return false +} diff --git a/config.toml.example b/config.toml.example index 5b29d51..0f0e9ab 100644 --- a/config.toml.example +++ b/config.toml.example @@ -15,4 +15,7 @@ TriggerWords = ["french","france","baguette","casque bleu"] [GayPoints] EnabledChats = [-00000,-111111,-22222] -ModifyUsers = [-00000,-111111,-22222] \ No newline at end of file +ModifyUsers = [-00000,-111111,-22222] + +[Download] +Prefix = "http://192.168.0.10:3556" diff --git a/config/config.go b/config/config.go index 89a9775..6d3b807 100644 --- a/config/config.go +++ b/config/config.go @@ -26,6 +26,9 @@ type config struct { EnabledChats []int64 ModifyUsers []int64 } + Download struct { + Prefix string + } } var BotConfig config diff --git a/main.go b/main.go index daf7141..0155671 100644 --- a/main.go +++ b/main.go @@ -4,12 +4,14 @@ import ( "watn3y/bloaterbotv3/commands/gaypoints" "watn3y/bloaterbotv3/commands/notify" "watn3y/bloaterbotv3/config" + "watn3y/bloaterbotv3/webserver" ) func main() { config.LoadConfig() gaypoints.InitDB() notify.InitDB() + go webserver.RunWeb() bot() } diff --git a/videos/index.html b/videos/index.html new file mode 100644 index 0000000..e69de29 diff --git a/webserver/webserver.go b/webserver/webserver.go new file mode 100644 index 0000000..623d525 --- /dev/null +++ b/webserver/webserver.go @@ -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) + } +} From 26cddc7ae76f3ae3fe2305e189a3ea7783ca2af6 Mon Sep 17 00:00:00 2001 From: Watn3y Date: Mon, 1 May 2023 04:06:15 +0200 Subject: [PATCH 2/4] download command finalized, beta --- botIO/sending.go | 40 +++++++- commands/commands.go | 2 + commands/download/download.go | 178 +++++++++++++++++++++------------- config.toml.example | 4 + config/config.go | 4 + 5 files changed, 157 insertions(+), 71 deletions(-) diff --git a/botIO/sending.go b/botIO/sending.go index 29c75ff..3da8645 100644 --- a/botIO/sending.go +++ b/botIO/sending.go @@ -5,20 +5,52 @@ import ( "log" ) -func SendMessage(message tgbotapi.MessageConfig, bot *tgbotapi.BotAPI) { - _, err := bot.Send(message) +func SendMessage(message tgbotapi.MessageConfig, bot *tgbotapi.BotAPI) (result tgbotapi.Message) { + result, err := bot.Send(message) if err != nil { log.Printf("Failed to send message: %v\n", err) return } log.Printf("[bot] Sent Message: %s", message.Text) + return result } -func SendSticker(sticker tgbotapi.StickerConfig, bot *tgbotapi.BotAPI) { - _, err := bot.Send(sticker) +func EditMessage(message tgbotapi.EditMessageTextConfig, bot *tgbotapi.BotAPI) (result tgbotapi.Message) { + result, err := bot.Send(message) + if err != nil { + log.Printf("Failed to send message: %v\n", err) + return + } + log.Printf("[bot] Edited Message: %s", message.Text) + return result +} + +func SendVideo(message tgbotapi.VideoConfig, bot *tgbotapi.BotAPI) (result tgbotapi.Message) { + result, err := bot.Send(message) + if err != nil { + log.Printf("Failed to send message: %v\n", err) + return + } + log.Printf("[bot] Sent Video: %s", message.File) + return result +} + +func SendPhoto(message tgbotapi.PhotoConfig, bot *tgbotapi.BotAPI) (result tgbotapi.Message) { + result, err := bot.Send(message) + if err != nil { + log.Printf("Failed to send message: %v\n", err) + return + } + log.Printf("[bot] Sent Photo: %s", message.File) + return result +} + +func SendSticker(sticker tgbotapi.StickerConfig, bot *tgbotapi.BotAPI) (result tgbotapi.Message) { + result, err := bot.Send(sticker) if err != nil { log.Printf("Failed to send Sticker: %v\n", err) return } log.Printf("[bot] Sent Sticker") + return result } diff --git a/commands/commands.go b/commands/commands.go index 2de9db1..4a73309 100644 --- a/commands/commands.go +++ b/commands/commands.go @@ -33,6 +33,8 @@ func Commands(update tgbotapi.Update, bot *tgbotapi.BotAPI) { notify.Reminder(update, bot) case "download": go download.Download(update, bot) + case "dl": + go download.Download(update, bot) case "help": help(update, bot) case "info": diff --git a/commands/download/download.go b/commands/download/download.go index 02fcf82..1ef784f 100644 --- a/commands/download/download.go +++ b/commands/download/download.go @@ -3,110 +3,96 @@ package download import ( "errors" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" + "io" "log" "math/rand" + "net/http" "net/url" "os" "os/exec" + "path/filepath" "regexp" "strconv" "strings" "time" "watn3y/bloaterbotv3/botIO" + "watn3y/bloaterbotv3/commonlogic" "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)) + log.Println("[download] Downloading 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", + msg := tgbotapi.MessageConfig{ + BaseChat: tgbotapi.BaseChat{ChatID: update.Message.Chat.ID, ReplyToMessageID: update.Message.MessageID}, + Text: "Downloading your media...", + } + workingMessage := botIO.SendMessage(msg, bot) + + if len(commandArgs) >= 1 && matchURL(commandArgs[0]) != "" { + //TODO distinguish + //service := matchURL(commandArgs[0]) + } else { + log.Println("[download] Failed to download URL " + update.Message.CommandArguments() + " NO URL") + message := tgbotapi.EditMessageTextConfig{ + BaseEdit: tgbotapi.BaseEdit{ChatID: workingMessage.Chat.ID, MessageID: workingMessage.MessageID}, + Text: "Please specify a valid YouTube or Reddit URL to download something\\nI can only download media that is hosted by YouTube or Reddit. Reddit posts that link to Imgur, for example, won't work\"", } - botIO.SendMessage(message, bot) + botIO.EditMessage(message, bot) return } downloadTarget := randomString(20) - videoDownloaded := runYTDL(arg, downloadTarget) + downloaded := runYTDL(commandArgs[0], 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 :(", + if !downloaded { + message := tgbotapi.EditMessageTextConfig{ + BaseEdit: tgbotapi.BaseEdit{ChatID: workingMessage.Chat.ID, MessageID: workingMessage.MessageID}, + Text: "Something went wrong while downloading your media :(", } - botIO.SendMessage(message, bot) + botIO.EditMessage(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 :(", + log.Println("[download] Failed to process URL "+update.Message.CommandArguments(), " FAILED TO READ DIRECTORY: ", err.Error()) + + message := tgbotapi.EditMessageTextConfig{ + BaseEdit: tgbotapi.BaseEdit{ChatID: workingMessage.Chat.ID, MessageID: workingMessage.MessageID}, + Text: "Something went wrong while downloading your media :(", } - botIO.SendMessage(message, bot) + botIO.EditMessage(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 :(", + if len(files) > 1 { + log.Println("[download] Failed to process URL "+update.Message.CommandArguments(), " TOO MANY FILES") + message := tgbotapi.EditMessageTextConfig{ + BaseEdit: tgbotapi.BaseEdit{ChatID: workingMessage.Chat.ID, MessageID: workingMessage.MessageID}, + Text: "Something went wrong while downloading your media :(", } - botIO.SendMessage(message, bot) + botIO.EditMessage(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.", + message := tgbotapi.EditMessageTextConfig{ + BaseEdit: tgbotapi.BaseEdit{ChatID: workingMessage.Chat.ID, MessageID: workingMessage.MessageID}, + Text: "Downloaded! Uploading now", } - botIO.SendMessage(message, bot) + botIO.EditMessage(message, bot) - time.Sleep(30 * time.Minute) + serveMedia(update, bot, downloadTarget, files[0].Name()) + + time.Sleep(10 * time.Second) + + bot.Send(tgbotapi.NewDeleteMessage(workingMessage.Chat.ID, workingMessage.MessageID)) - 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 { @@ -121,7 +107,7 @@ func randomString(n int) string { 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) + cmd := exec.Command("yt-dlp", "-f", "bv*[ext=mp4]+ba[ext=m4a] / bv*+ba/b", "--no-playlist", "-o", "videos/"+targetDir+"/"+"%(title)s.%(ext)s", "--write-thumbnail", "--convert-thumbnails", "jpg", "-o", "thumbnail:videos/"+targetDir+"thumb"+"/"+"%(title)s.%(ext)s", URL) out, err := cmd.CombinedOutput() var ( @@ -130,19 +116,19 @@ func runYTDL(URL string, targetDir string) (success bool) { ) if errors.As(err, &ee) { - log.Println("[download.ytdl] Failed to download Video URL "+URL, " NON ZERO EXIT CODE: ", ee.ExitCode()) + log.Println("[download.ytdl] Failed to download 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()) + log.Println("[download.ytdl] Failed to download 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()) + log.Println("[download.ytdl] Failed to download URL "+URL, " GENERAL ERROR: ", err.Error()) success = false } else { - log.Println("[download.ytdl] Downloaded Video URL " + URL) + log.Println("[download.ytdl] Downloaded URL " + URL) success = true } @@ -157,6 +143,7 @@ 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$)`) @@ -171,6 +158,63 @@ func matchURL(URL string) (matchedURL string) { } -func serverFile(file os.File) { +func shortURL(URL string) (shorturl string) { + resp, err := http.Get(config.BotConfig.Download.Yourls.APIPath + "?signature=" + config.BotConfig.Download.Yourls.Signature + "&action=shorturl&format=simple&url=" + URL) + if err != nil { + log.Fatal(err) + } + defer resp.Body.Close() + + body, err := io.ReadAll(resp.Body) + + if err != nil { + log.Fatal(err) + } + + return string(body) +} + +func serveMedia(update tgbotapi.Update, bot *tgbotapi.BotAPI, randomNoise string, file string) { + fsPath := "./videos/" + randomNoise + "/" + file + fsThumbPath := "./videos/" + randomNoise + "thumb" + "/" + strings.TrimSuffix(file, "mp4") + "jpg" + fExt := filepath.Ext(fsPath) + + imageTypes := []string{".jpg", ".jpeg", ".png"} + + shortURL := shortURL(url.PathEscape(config.BotConfig.Download.Prefix + "/" + randomNoise + "/" + (file))) + + fsPathStat, _ := os.Stat(fsPath) + + if fsPathStat.Size() >= 52428800 { + fExt = "thisfuckingshitistoobig" + } + + if fExt == ".mp4" { + vid := tgbotapi.VideoConfig{ + BaseFile: tgbotapi.BaseFile{BaseChat: tgbotapi.BaseChat{ChatID: update.Message.Chat.ID, ReplyToMessageID: update.Message.MessageID}, File: tgbotapi.FilePath(fsPath)}, + Thumb: tgbotapi.FilePath(fsThumbPath), + Caption: shortURL, + SupportsStreaming: true, + } + if _, err := os.Stat(fsThumbPath); err != nil { + vid.Thumb = nil + } + botIO.SendVideo(vid, bot) + + } else if commonlogic.ContainsString(imageTypes, fExt) { + pic := tgbotapi.PhotoConfig{ + BaseFile: tgbotapi.BaseFile{BaseChat: tgbotapi.BaseChat{ChatID: update.Message.Chat.ID, ReplyToMessageID: update.Message.MessageID}, File: tgbotapi.FilePath(fsPath)}, + Caption: shortURL, + } + botIO.SendPhoto(pic, bot) + } else { + message := tgbotapi.MessageConfig{ + BaseChat: tgbotapi.BaseChat{ChatID: update.Message.Chat.ID, ReplyToMessageID: update.Message.MessageID}, + DisableWebPagePreview: false, + ParseMode: "html", + Text: shortURL, + } + botIO.SendMessage(message, bot) + } } diff --git a/config.toml.example b/config.toml.example index 0f0e9ab..625db27 100644 --- a/config.toml.example +++ b/config.toml.example @@ -19,3 +19,7 @@ ModifyUsers = [-00000,-111111,-22222] [Download] Prefix = "http://192.168.0.10:3556" +[Download.Yourls] +APIPath = "https://yourl.tld/yourls-api.php" +Signature = "u8zibfd876sfvb" + diff --git a/config/config.go b/config/config.go index 6d3b807..48b791d 100644 --- a/config/config.go +++ b/config/config.go @@ -28,6 +28,10 @@ type config struct { } Download struct { Prefix string + Yourls struct { + APIPath string + Signature string + } } } From 591164c586c33e8a00c3cb928a7d44b53f388909 Mon Sep 17 00:00:00 2001 From: Watn3y Date: Mon, 1 May 2023 04:17:32 +0200 Subject: [PATCH 3/4] smol fix --- commands/download/download.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/commands/download/download.go b/commands/download/download.go index 1ef784f..42d122d 100644 --- a/commands/download/download.go +++ b/commands/download/download.go @@ -182,7 +182,7 @@ func serveMedia(update tgbotapi.Update, bot *tgbotapi.BotAPI, randomNoise string imageTypes := []string{".jpg", ".jpeg", ".png"} - shortURL := shortURL(url.PathEscape(config.BotConfig.Download.Prefix + "/" + randomNoise + "/" + (file))) + shortURL := shortURL(url.PathEscape(config.BotConfig.Download.Prefix + "/" + randomNoise + "/" + url.PathEscape(file))) fsPathStat, _ := os.Stat(fsPath) From 467d12602e7f38826c8a9018c713cdc3c2f0e94c Mon Sep 17 00:00:00 2001 From: Watn3y Date: Tue, 1 Aug 2023 03:15:45 +0200 Subject: [PATCH 4/4] removed reddit download --- commands/download/download.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/commands/download/download.go b/commands/download/download.go index 42d122d..86a99d5 100644 --- a/commands/download/download.go +++ b/commands/download/download.go @@ -39,7 +39,7 @@ func Download(update tgbotapi.Update, bot *tgbotapi.BotAPI) { log.Println("[download] Failed to download URL " + update.Message.CommandArguments() + " NO URL") message := tgbotapi.EditMessageTextConfig{ BaseEdit: tgbotapi.BaseEdit{ChatID: workingMessage.Chat.ID, MessageID: workingMessage.MessageID}, - Text: "Please specify a valid YouTube or Reddit URL to download something\\nI can only download media that is hosted by YouTube or Reddit. Reddit posts that link to Imgur, for example, won't work\"", + Text: "Please specify a valid YouTube URL to download something.", } botIO.EditMessage(message, bot) return @@ -88,6 +88,7 @@ func Download(update tgbotapi.Update, bot *tgbotapi.BotAPI) { botIO.EditMessage(message, bot) serveMedia(update, bot, downloadTarget, files[0].Name()) + log.Println("[download] Served URL " + update.Message.CommandArguments() + " for " + strconv.FormatInt(update.Message.From.ID, 10) + " in chat " + strconv.FormatInt(update.Message.Chat.ID, 10)) time.Sleep(10 * time.Second) @@ -146,7 +147,7 @@ func matchURL(URL string) (matchedURL string) { } - var domainRegex = regexp.MustCompile(`(youtube\.com$)|(youtu\.be$)|(reddit\.com$)`) + var domainRegex = regexp.MustCompile(`(youtube\.com$)|(youtu\.be$)`) m := domainRegex.FindStringSubmatch(parsedURL.Hostname())