summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlexander Neonxp Kiryukhin <i@neonxp.ru>2024-08-15 01:18:08 +0300
committerAlexander Neonxp Kiryukhin <i@neonxp.ru>2024-08-15 01:18:08 +0300
commitc523825a006425f55eb55e9b5cf024197129022b (patch)
treec4c5d2f203b2991f400a6616ad0d8979a6f089ef
parent9f4fbd85cd138820e4cf0f87b77a2c80ae770b1a (diff)
fix links parserHEADmaster
-rw-r--r--go.mod1
-rw-r--r--go.sum2
-rw-r--r--main.go25
3 files changed, 25 insertions, 3 deletions
diff --git a/go.mod b/go.mod
index 16040b2..e4d8a78 100644
--- a/go.mod
+++ b/go.mod
@@ -14,4 +14,5 @@ require (
github.com/go-sourcemap/sourcemap v2.1.3+incompatible // indirect
github.com/google/pprof v0.0.0-20240227163752-401108e1b7e7 // indirect
golang.org/x/text v0.14.0 // indirect
+ mvdan.cc/xurls/v2 v2.5.0
)
diff --git a/go.sum b/go.sum
index fa57427..6fc07e1 100644
--- a/go.sum
+++ b/go.sum
@@ -63,3 +63,5 @@ gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
+mvdan.cc/xurls/v2 v2.5.0 h1:lyBNOm8Wo71UknhUs4QTFUNNMyxy2JEIaKKo0RWOh+8=
+mvdan.cc/xurls/v2 v2.5.0/go.mod h1:yQgaGQ1rFtJUzkmKiHYSSfuQxqfYmd//X6PxvholpeE=
diff --git a/main.go b/main.go
index 6c77033..07d550f 100644
--- a/main.go
+++ b/main.go
@@ -8,6 +8,7 @@ import (
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
"github.com/kkdai/youtube/v2"
+ "mvdan.cc/xurls/v2"
)
func main() {
@@ -30,20 +31,33 @@ func main() {
if update.Message != nil { // If we got a message
message := update.Message
- link, err := url.Parse(message.Text)
+ xRelaxed := xurls.Relaxed()
+ linkString := xRelaxed.FindString(message.Text)
+ if linkString == "" {
+ msg := tgbotapi.NewMessage(message.Chat.ID, "Сообщение должно содержать ссылку")
+ log.Println(err)
+ msg.ReplyToMessageID = message.MessageID
+ bot.Send(msg)
+ continue
+ }
+ link, err := url.Parse(linkString)
if err != nil {
+ msg := tgbotapi.NewMessage(message.Chat.ID, "Сообщение должно содержать корректную ссылку на youtube")
+ log.Println(err)
+ msg.ReplyToMessageID = message.MessageID
+ bot.Send(msg)
continue
}
go func(message *tgbotapi.Message) {
- if link.Host == "youtube.com" && link.Query().Get("v") != "" {
+ if strings.HasSuffix(link.Host, "youtube.com") && link.Query().Get("v") != "" {
if err := download(bot, message, link.Query().Get("v")); err != nil {
msg := tgbotapi.NewMessage(message.Chat.ID, "Ошибка: "+err.Error())
log.Println(err)
msg.ReplyToMessageID = message.MessageID
bot.Send(msg)
}
- } else if link.Host == "youtu.be" && link.Path != "" {
+ } else if strings.HasSuffix(link.Host, "youtu.be") && link.Path != "" {
pathParts := strings.SplitN(link.Path, "?", 2)
if err := download(bot, message, pathParts[0]); err != nil {
msg := tgbotapi.NewMessage(message.Chat.ID, "Ошибка: "+err.Error())
@@ -51,6 +65,11 @@ func main() {
msg.ReplyToMessageID = message.MessageID
bot.Send(msg)
}
+ } else {
+ msg := tgbotapi.NewMessage(message.Chat.ID, "Сообщение должно содержать корректную ссылку на youtube")
+ log.Println(err)
+ msg.ReplyToMessageID = message.MessageID
+ bot.Send(msg)
}
}(message)
}