added proper logging to most of the code
This commit is contained in:
parent
65c6020e49
commit
7d4c9c5ab9
21 changed files with 218 additions and 283 deletions
|
@ -3,8 +3,9 @@ package download
|
|||
import (
|
||||
"errors"
|
||||
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
||||
"github.com/rs/zerolog/log"
|
||||
"io"
|
||||
"log"
|
||||
log2 "log"
|
||||
"math/rand"
|
||||
"net/http"
|
||||
"net/url"
|
||||
|
@ -22,7 +23,7 @@ import (
|
|||
|
||||
func Download(update tgbotapi.Update, bot *tgbotapi.BotAPI) {
|
||||
|
||||
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))
|
||||
log.Debug().Int64("chat", update.Message.Chat.ID).Int64("user", update.Message.From.ID).Msg("starting download")
|
||||
|
||||
commandArgs := strings.Fields(update.Message.CommandArguments())
|
||||
|
||||
|
@ -36,7 +37,7 @@ func Download(update tgbotapi.Update, bot *tgbotapi.BotAPI) {
|
|||
//TODO distinguish
|
||||
//service := matchURL(commandArgs[0])
|
||||
} else {
|
||||
log.Println("[download] Failed to download URL " + update.Message.CommandArguments() + " NO URL")
|
||||
log.Error().Int64("chat", update.Message.Chat.ID).Int64("user", update.Message.From.ID).Str("args", update.Message.CommandArguments()).Msg("failed to download. empty arg")
|
||||
message := tgbotapi.EditMessageTextConfig{
|
||||
BaseEdit: tgbotapi.BaseEdit{ChatID: workingMessage.Chat.ID, MessageID: workingMessage.MessageID},
|
||||
Text: "Please specify a valid YouTube URL to download something.",
|
||||
|
@ -61,7 +62,7 @@ func Download(update tgbotapi.Update, bot *tgbotapi.BotAPI) {
|
|||
files, err := os.ReadDir("./videos/" + downloadTarget)
|
||||
|
||||
if err != nil {
|
||||
log.Println("[download] Failed to process URL "+update.Message.CommandArguments(), " FAILED TO READ DIRECTORY: ", err.Error())
|
||||
log.Error().Err(err).Msg("failed to download. unable to read target directory")
|
||||
|
||||
message := tgbotapi.EditMessageTextConfig{
|
||||
BaseEdit: tgbotapi.BaseEdit{ChatID: workingMessage.Chat.ID, MessageID: workingMessage.MessageID},
|
||||
|
@ -72,7 +73,7 @@ func Download(update tgbotapi.Update, bot *tgbotapi.BotAPI) {
|
|||
}
|
||||
|
||||
if len(files) > 1 {
|
||||
log.Println("[download] Failed to process URL "+update.Message.CommandArguments(), " TOO MANY FILES")
|
||||
log.Error().Err(err).Msg("failed to download. too many files")
|
||||
message := tgbotapi.EditMessageTextConfig{
|
||||
BaseEdit: tgbotapi.BaseEdit{ChatID: workingMessage.Chat.ID, MessageID: workingMessage.MessageID},
|
||||
Text: "Something went wrong while downloading your media :(",
|
||||
|
@ -88,10 +89,11 @@ 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))
|
||||
log2.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)
|
||||
time.Sleep(5 * time.Second)
|
||||
|
||||
log.Info().Msg("downloaded file")
|
||||
bot.Send(tgbotapi.NewDeleteMessage(workingMessage.Chat.ID, workingMessage.MessageID))
|
||||
|
||||
}
|
||||
|
@ -117,19 +119,19 @@ func runYTDL(URL string, targetDir string) (success bool) {
|
|||
)
|
||||
|
||||
if errors.As(err, &ee) {
|
||||
log.Println("[download.ytdl] Failed to download URL "+URL, " NON ZERO EXIT CODE: ", ee.ExitCode())
|
||||
log2.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 URL "+URL, " OS PATH ERROR: ", pe.Error())
|
||||
log2.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 URL "+URL, " GENERAL ERROR: ", err.Error())
|
||||
log2.Println("[download.ytdl] Failed to download URL "+URL, " GENERAL ERROR: ", err.Error())
|
||||
success = false
|
||||
|
||||
} else {
|
||||
log.Println("[download.ytdl] Downloaded URL " + URL)
|
||||
log2.Println("[download.ytdl] Downloaded URL " + URL)
|
||||
success = true
|
||||
}
|
||||
|
||||
|
@ -143,7 +145,7 @@ func runYTDL(URL string, targetDir string) (success bool) {
|
|||
func matchURL(URL string) (matchedURL string) {
|
||||
parsedURL, err := url.Parse(URL)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
log2.Fatal(err)
|
||||
|
||||
}
|
||||
|
||||
|
@ -162,7 +164,7 @@ func matchURL(URL string) (matchedURL string) {
|
|||
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)
|
||||
log2.Fatal(err)
|
||||
}
|
||||
|
||||
defer resp.Body.Close()
|
||||
|
@ -170,7 +172,7 @@ func shortURL(URL string) (shorturl string) {
|
|||
body, err := io.ReadAll(resp.Body)
|
||||
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
log2.Fatal(err)
|
||||
}
|
||||
|
||||
return string(body)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue