Reestructurar más

This commit is contained in:
Cat /dev/Nulo 2023-01-02 23:02:17 -03:00
parent a15f2d9559
commit fb2d827e97
6 changed files with 156 additions and 110 deletions

2
.gitignore vendored
View file

@ -1 +1 @@
dlbot
tiktok/tiktok

5
common/go.mod Normal file
View file

@ -0,0 +1,5 @@
module nulo.in/dlbot/common
go 1.19
require github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1 // indirect

2
common/go.sum Normal file
View file

@ -0,0 +1,2 @@
github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1 h1:wG8n/XJQ07TmjbITcGiUaOtXxdrINDz1b0J1w0SzqDc=
github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1/go.mod h1:A2S0CWkNylc2phvKXWBBdD3K0iGnDBGbzRpISP2zBl8=

122
common/main.go Normal file
View file

@ -0,0 +1,122 @@
package common
import (
"log"
"net/url"
"os"
"strings"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
)
type Config struct {
Respond func(bot *tgbotapi.BotAPI, update tgbotapi.Update, url *url.URL) Result
}
type Result uint8
const (
NotValid Result = iota
HadError
Uploaded
)
func Main(config Config) {
token := os.Getenv("TELEGRAM_TOKEN")
if token == "" {
log.Panic("No telegram token")
}
var debug bool
if os.Getenv("DEBUG") != "" {
debug = true
}
bot, err := tgbotapi.NewBotAPI(token)
if err != nil {
log.Panic(err)
}
bot.Debug = debug
log.Printf("Authorized on account %s", bot.Self.UserName)
u := tgbotapi.NewUpdate(0)
u.Timeout = 60
updates := bot.GetUpdatesChan(u)
for update := range updates {
if update.Message == nil {
continue
}
go config.handleMessage(bot, update)
}
}
func respondWith(msg *tgbotapi.Message, str string) tgbotapi.MessageConfig {
res := tgbotapi.NewMessage(msg.Chat.ID, str)
res.ReplyToMessageID = msg.MessageID
res.DisableWebPagePreview = true
res.ParseMode = "markdown"
return res
}
func RespondWithMany(msg *tgbotapi.Message, s ...string) tgbotapi.MessageConfig {
var res strings.Builder
for _, v := range s {
res.WriteString(v)
}
return respondWith(msg, res.String())
}
func (config Config) handleMessage(bot *tgbotapi.BotAPI, update tgbotapi.Update) {
var explicit bool
msg := update.Message
if strings.HasPrefix(msg.Text, "/dl") || msg.Chat.IsPrivate() {
explicit = true
}
searchMsg := msg
if msg.ReplyToMessage != nil && explicit {
searchMsg = msg.ReplyToMessage
}
hasDownloadables := false
for i := 0; i < len(searchMsg.Entities); i++ {
e := searchMsg.Entities[i]
if e.Type != "url" {
continue
}
urlString := searchMsg.Text[e.Offset : e.Offset+e.Length]
url, err := url.Parse(urlString)
if err != nil {
if explicit {
bot.Send(RespondWithMany(msg, "No se pudo detectar la URL ", urlString, "."))
}
continue
}
result := config.Respond(bot, update, url)
if explicit && result == NotValid {
bot.Send(RespondWithMany(msg, "La URL ", urlString, " no es compatible con este bot."))
continue
}
if result == HadError || result == Uploaded {
hasDownloadables = true
}
if result == HadError {
bot.Send(RespondWithMany(update.Message, "Hubo un error al descargar ", urlString, "."))
continue
}
}
if !hasDownloadables && explicit {
bot.Send(RespondWithMany(msg, "No encontré URLs descargables en ese mensaje."))
}
}

View file

@ -2,4 +2,8 @@ module nulo.in/dlbot/tiktok/v4
go 1.18
require github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1 // indirect
require github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1
require nulo.in/dlbot/common v0.0.0-00010101000000-000000000000 // indirect
replace nulo.in/dlbot/common => ../common

View file

@ -3,8 +3,8 @@ package main
import (
"log"
"net/url"
"os"
"strings"
"nulo.in/dlbot/common"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
)
@ -12,118 +12,31 @@ import (
// Gracias a https://github.com/Xenzi-XN1/Tiktok-Download
// por enseñarme tikmate.app
func respondWith(msg *tgbotapi.Message, str string) tgbotapi.MessageConfig {
res := tgbotapi.NewMessage(msg.Chat.ID, str)
res.ReplyToMessageID = msg.MessageID
res.DisableWebPagePreview = true
res.ParseMode = "markdown"
return res
}
func respondWithMany(msg *tgbotapi.Message, s ...string) tgbotapi.MessageConfig {
var res strings.Builder
for _, v := range s {
res.WriteString(v)
func respond(bot *tgbotapi.BotAPI, update tgbotapi.Update, url *url.URL) common.Result {
if url.Hostname() != "vm.tiktok.com" && url.Hostname() != "tiktok.com" {
return common.NotValid
}
return respondWith(msg, res.String())
}
urlString := url.String()
func handleMessage(bot *tgbotapi.BotAPI, update tgbotapi.Update) {
var explicit bool
// tikmate no entiende tiktok.com
url.Host = "vm.tiktok.com"
msg := update.Message
if strings.HasPrefix(msg.Text, "/dl") || msg.Chat.IsPrivate() {
explicit = true
log.Printf("Downloading %s", urlString)
lookup, err := Lookup(url.String())
if err != nil {
return common.HadError
}
log.Println(lookup)
if !lookup.Success {
return common.HadError
}
searchMsg := msg
if msg.ReplyToMessage != nil && explicit {
searchMsg = msg.ReplyToMessage
}
hasDownloadables := false
for i := 0; i < len(searchMsg.Entities); i++ {
e := searchMsg.Entities[i]
if e.Type != "url" {
continue
}
urlString := searchMsg.Text[e.Offset : e.Offset+e.Length]
url, err := url.Parse(urlString)
if err != nil {
if explicit {
bot.Send(respondWithMany(msg, "No se pudo detectar la URL ", urlString, "."))
}
continue
}
if url.Hostname() != "vm.tiktok.com" && url.Hostname() != "tiktok.com" {
if explicit {
bot.Send(respondWithMany(msg, "La URL ", urlString, " no es de TikTok."))
}
continue
}
hasDownloadables = true
// tikmate no entiende tiktok.com
url.Host = "vm.tiktok.com"
log.Printf("Downloading %s", urlString)
lookup, err := Lookup(url.String())
if err != nil {
bot.Send(respondWithMany(msg, "Hubo un error al descargar ", urlString, "."))
continue
}
log.Println(lookup)
if !lookup.Success {
if len(lookup.Message) > 0 {
bot.Send(respondWithMany(msg, "Hubo un error al descargar ", urlString, ": `", lookup.Message, "`"))
} else {
bot.Send(respondWithMany(msg, "Hubo un error al descargar ", urlString, "."))
}
continue
}
res := tgbotapi.NewVideo(msg.Chat.ID, *lookup)
res.ReplyToMessageID = msg.MessageID
bot.Send(res)
}
if !hasDownloadables && explicit {
bot.Send(respondWithMany(msg, "No encontré URLs descargables en ese mensaje."))
}
res := tgbotapi.NewVideo(update.Message.Chat.ID, *lookup)
res.ReplyToMessageID = update.Message.MessageID
bot.Send(res)
return common.Uploaded
}
func main() {
token := os.Getenv("TELEGRAM_TOKEN")
if token == "" {
log.Panic("No telegram token")
}
var debug bool
if os.Getenv("DEBUG") != "" {
debug = true
}
bot, err := tgbotapi.NewBotAPI(token)
if err != nil {
log.Panic(err)
}
bot.Debug = debug
log.Printf("Authorized on account %s", bot.Self.UserName)
u := tgbotapi.NewUpdate(0)
u.Timeout = 60
updates := bot.GetUpdatesChan(u)
for update := range updates {
if update.Message == nil {
continue
}
go handleMessage(bot, update)
}
common.Main(common.Config{Respond: respond})
}