Limpiar y unificar bots

This commit is contained in:
Cat /dev/Nulo 2023-01-04 13:21:00 -03:00
parent ab08250c1e
commit 26eaae99fb
11 changed files with 161 additions and 142 deletions

3
.gitignore vendored
View file

@ -1,2 +1 @@
tiktok/tiktok dlbot
instagram/instagram

View file

@ -2,26 +2,12 @@ version: '3'
tasks: tasks:
build: build:
dir: "{{.WHICH}}"
label: "build-{{.WHICH}}"
cmds: cmds:
- CGO_ENABLED=0 GOOS=linux go build -a -ldflags '-extldflags "-static"' . - CGO_ENABLED=0 GOOS=linux go build -a -ldflags '-extldflags "-static"' .
upload: upload:
deps: deps:
- task: build - task: build
vars:
WHICH: "{{.WHICH}}"
dir: "{{.WHICH}}"
label: "upload-{{.WHICH}}"
cmds: cmds:
- ssh -p993 root@nulo.in sv stop dlbot-{{.WHICH}} - ssh -p993 root@nulo.in sv stop dlbot
- scp -P993 {{.WHICH}} dlbot@nulo.in:/home/dlbot/bin/ - scp -P993 dlbot dlbot@nulo.in:/home/dlbot/bin/
- ssh -p993 root@nulo.in sv start dlbot-{{.WHICH}} - ssh -p993 root@nulo.in sv start dlbot
upload-all:
deps:
- task: upload
vars:
WHICH: tiktok
- task: upload
vars:
WHICH: instagram

View file

@ -1,17 +1,11 @@
package common package common
import ( import (
"log"
"net/url"
"os"
"strings" "strings"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" 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 type Result uint8
const ( const (
@ -20,40 +14,6 @@ const (
Uploaded 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 { func respondWith(msg *tgbotapi.Message, str string) tgbotapi.MessageConfig {
res := tgbotapi.NewMessage(msg.Chat.ID, str) res := tgbotapi.NewMessage(msg.Chat.ID, str)
res.ReplyToMessageID = msg.MessageID res.ReplyToMessageID = msg.MessageID
@ -69,54 +29,3 @@ func RespondWithMany(msg *tgbotapi.Message, s ...string) tgbotapi.MessageConfig
} }
return respondWith(msg, res.String()) 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."))
}
}

16
go.mod Normal file
View file

@ -0,0 +1,16 @@
module nulo.in/dlbot
go 1.19
require (
github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1
nulo.in/dlbot/common v0.0.0-00010101000000-000000000000
nulo.in/dlbot/instagram v0.0.0-00010101000000-000000000000
nulo.in/dlbot/tiktok v0.0.0-00010101000000-000000000000
)
replace nulo.in/dlbot/common => ./common
replace nulo.in/dlbot/instagram => ./instagram
replace nulo.in/dlbot/tiktok => ./tiktok

2
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=

View file

@ -1,4 +1,4 @@
package main package instagram
import ( import (
"encoding/json" "encoding/json"
@ -9,7 +9,7 @@ import (
"path" "path"
) )
type QueryResponse struct { type queryResponse struct {
Data struct { Data struct {
ShortcodeMedia *struct { ShortcodeMedia *struct {
Type string `json:"__typename"` Type string `json:"__typename"`
@ -29,16 +29,16 @@ type QueryResponse struct {
} `json:"data"` } `json:"data"`
} }
type Response struct { type lookupResponse struct {
VideoUrl string VideoUrl string
Author string Author string
Text string Text string
} }
func Lookup(urlSrc string) (Response, error) { func lookup(urlSrc string) (lookupResponse, error) {
urlSrcParsed, err := url.Parse(urlSrc) urlSrcParsed, err := url.Parse(urlSrc)
if err != nil { if err != nil {
return Response{}, err return lookupResponse{}, err
} }
url, _ := url.Parse("https://www.instagram.com/graphql/query/?query_hash=b3055c01b4b222b8a47dc12b090e4e64") url, _ := url.Parse("https://www.instagram.com/graphql/query/?query_hash=b3055c01b4b222b8a47dc12b090e4e64")
@ -48,23 +48,23 @@ func Lookup(urlSrc string) (Response, error) {
resp, err := http.Get(url.String()) resp, err := http.Get(url.String())
if err != nil { if err != nil {
return Response{}, err return lookupResponse{}, err
} }
defer resp.Body.Close() defer resp.Body.Close()
body, err := io.ReadAll(resp.Body) body, err := io.ReadAll(resp.Body)
var response QueryResponse var response queryResponse
err = json.Unmarshal(body, &response) err = json.Unmarshal(body, &response)
if err != nil { if err != nil {
return Response{}, err return lookupResponse{}, err
} }
if response.Data.ShortcodeMedia == nil { if response.Data.ShortcodeMedia == nil {
return Response{}, errors.New("No encontré el video.") return lookupResponse{}, errors.New("No encontré el video.")
} }
if response.Data.ShortcodeMedia.Type != "GraphVideo" { if response.Data.ShortcodeMedia.Type != "GraphVideo" {
return Response{}, errors.New("Esto no es un video.") return lookupResponse{}, errors.New("Esto no es un video.")
} }
return Response{ return lookupResponse{
VideoUrl: response.Data.ShortcodeMedia.VideoUrl, VideoUrl: response.Data.ShortcodeMedia.VideoUrl,
Author: response.Data.ShortcodeMedia.Owner.Username, Author: response.Data.ShortcodeMedia.Owner.Username,
Text: response.Data.ShortcodeMedia.EdgeMediaToCaption.Edges[0].Node.Text, Text: response.Data.ShortcodeMedia.EdgeMediaToCaption.Edges[0].Node.Text,

View file

@ -1,4 +1,4 @@
package main package instagram
import ( import (
"log" "log"
@ -10,7 +10,7 @@ import (
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
) )
func respond(bot *tgbotapi.BotAPI, update tgbotapi.Update, url *url.URL) common.Result { func Respond(bot *tgbotapi.BotAPI, update tgbotapi.Update, url *url.URL) common.Result {
if url.Hostname() != "instagram.com" && url.Hostname() != "www.instagram.com" { if url.Hostname() != "instagram.com" && url.Hostname() != "www.instagram.com" {
return common.NotValid return common.NotValid
} }
@ -19,7 +19,7 @@ func respond(bot *tgbotapi.BotAPI, update tgbotapi.Update, url *url.URL) common.
} }
log.Printf("Downloading %s", url.String()) log.Printf("Downloading %s", url.String())
lookup, err := Lookup(url.String()) lookup, err := lookup(url.String())
if err != nil { if err != nil {
log.Println(err) log.Println(err)
return common.HadError return common.HadError
@ -32,7 +32,3 @@ func respond(bot *tgbotapi.BotAPI, update tgbotapi.Update, url *url.URL) common.
bot.Send(res) bot.Send(res)
return common.Uploaded return common.Uploaded
} }
func main() {
common.Main(common.Config{Respond: respond})
}

116
main.go Normal file
View file

@ -0,0 +1,116 @@
package main
import (
"log"
"net/url"
"os"
"strings"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
"nulo.in/dlbot/common"
"nulo.in/dlbot/instagram"
"nulo.in/dlbot/tiktok"
)
type Responder func(bot *tgbotapi.BotAPI, update tgbotapi.Update, url *url.URL) common.Result
type Config struct {
Responders []Responder
}
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(common.RespondWithMany(msg, "No se pudo detectar la URL ", urlString, "."))
}
continue
}
var result common.Result
for _, respond := range config.Responders {
result = respond(bot, update, url)
if result != common.NotValid {
break
}
}
if explicit && result == common.NotValid {
bot.Send(common.RespondWithMany(msg, "La URL ", urlString, " no es compatible con este bot."))
continue
}
if result == common.HadError || result == common.Uploaded {
hasDownloadables = true
}
if result == common.HadError {
bot.Send(common.RespondWithMany(update.Message, "Hubo un error al descargar ", urlString, "."))
continue
}
}
if !hasDownloadables && explicit {
bot.Send(common.RespondWithMany(msg, "No encontré URLs descargables en ese mensaje."))
}
}
func main() {
config := Config{
Responders: []Responder{
instagram.Respond,
tiktok.Respond,
},
}
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)
}
}

View file

@ -1,7 +1,6 @@
Esta repo tiene el código de distintos bots para Telegram que permiten descargar videos de distintos lugares. Un bot para Telegram que permite descargar videos de distintos lugares.
- TikTok: [@dlthefourthbot](https://t.me/dlthefourthbot) (código: [[tiktok/]]) [@dlthefourthbot](https://t.me/dlthefourthbot)
- Instagram Reels: [@inst4gramdlbot](https://t.me/inst4gramdlbot) (código: [[instagram/]])
Son rápidos ya que ni siquiera descargan el video, solo le pasan a Telegram la URL para descargarlos. Son rápidos ya que ni siquiera descargan el video, solo le pasan a Telegram la URL para descargarlos.

View file

@ -1,4 +1,4 @@
package main package tiktok
import ( import (
"log" "log"
@ -12,7 +12,7 @@ import (
// Gracias a https://github.com/Xenzi-XN1/Tiktok-Download // Gracias a https://github.com/Xenzi-XN1/Tiktok-Download
// por enseñarme tikmate.app // por enseñarme tikmate.app
func respond(bot *tgbotapi.BotAPI, update tgbotapi.Update, url *url.URL) common.Result { func Respond(bot *tgbotapi.BotAPI, update tgbotapi.Update, url *url.URL) common.Result {
if url.Hostname() != "vm.tiktok.com" && url.Hostname() != "tiktok.com" { if url.Hostname() != "vm.tiktok.com" && url.Hostname() != "tiktok.com" {
return common.NotValid return common.NotValid
} }
@ -22,7 +22,7 @@ func respond(bot *tgbotapi.BotAPI, update tgbotapi.Update, url *url.URL) common.
url.Host = "vm.tiktok.com" url.Host = "vm.tiktok.com"
log.Printf("Downloading %s", urlString) log.Printf("Downloading %s", urlString)
lookup, err := Lookup(url.String()) lookup, err := lookup(urlString)
if err != nil { if err != nil {
log.Println(err) log.Println(err)
return common.HadError return common.HadError
@ -34,7 +34,3 @@ func respond(bot *tgbotapi.BotAPI, update tgbotapi.Update, url *url.URL) common.
bot.Send(res) bot.Send(res)
return common.Uploaded return common.Uploaded
} }
func main() {
common.Main(common.Config{Respond: respond})
}

View file

@ -1,4 +1,4 @@
package main package tiktok
import ( import (
"encoding/json" "encoding/json"
@ -8,7 +8,7 @@ import (
"net/url" "net/url"
) )
type LookupResponse struct { type lookupResponse struct {
AuthorAvatar string `json:"author_avatar"` AuthorAvatar string `json:"author_avatar"`
AuthorID string `json:"author_id"` AuthorID string `json:"author_id"`
AuthorName string `json:"author_name"` AuthorName string `json:"author_name"`
@ -22,7 +22,7 @@ type LookupResponse struct {
Token string `json:"token"` Token string `json:"token"`
} }
func Lookup(urlS string) (string, error) { func lookup(urlS string) (string, error) {
resp, err := http.PostForm( resp, err := http.PostForm(
"https://api.tikmate.app/api/lookup", "https://api.tikmate.app/api/lookup",
url.Values{"url": {urlS}}, url.Values{"url": {urlS}},
@ -33,7 +33,7 @@ func Lookup(urlS string) (string, error) {
defer resp.Body.Close() defer resp.Body.Close()
body, err := io.ReadAll(resp.Body) body, err := io.ReadAll(resp.Body)
var lookup LookupResponse var lookup lookupResponse
err = json.Unmarshal(body, &lookup) err = json.Unmarshal(body, &lookup)
if err != nil { if err != nil {
return "", err return "", err