Compare commits

...

5 commits

20 changed files with 230 additions and 206 deletions

3
.gitignore vendored
View file

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

View file

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

View file

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

View file

@ -1,2 +0,0 @@
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,122 +1,21 @@
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 Responder interface {
Respond(url *url.URL) (*Uploadable, Error)
}
type Result uint8
type Uploadable struct {
Url string
Caption string
}
type Error uint8
const (
NotValid Result = iota
OK Error = iota
NotValid
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."))
}
}

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

@ -4,7 +4,4 @@ go 1.19
replace nulo.in/dlbot/common => ../common
require (
github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1
nulo.in/dlbot/common v0.0.0-00010101000000-000000000000
)
require nulo.in/dlbot/common v0.0.0-00010101000000-000000000000

View file

@ -1,2 +0,0 @@
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,15 +1,14 @@
package main
package instagram
import (
"encoding/json"
"errors"
"io"
"net/http"
"net/url"
"path"
)
type QueryResponse struct {
type queryResponse struct {
Data struct {
ShortcodeMedia *struct {
Type string `json:"__typename"`
@ -29,16 +28,16 @@ type QueryResponse struct {
} `json:"data"`
}
type Response struct {
type lookupResponse struct {
VideoUrl string
Author string
Text string
}
func Lookup(urlSrc string) (Response, error) {
func (r *Instagram) lookup(urlSrc string) (lookupResponse, error) {
urlSrcParsed, err := url.Parse(urlSrc)
if err != nil {
return Response{}, err
return lookupResponse{}, err
}
url, _ := url.Parse("https://www.instagram.com/graphql/query/?query_hash=b3055c01b4b222b8a47dc12b090e4e64")
@ -46,25 +45,25 @@ func Lookup(urlSrc string) (Response, error) {
query.Add("variables", "{\"shortcode\":\""+path.Base(urlSrcParsed.Path)+"\",\"child_comment_count\":3,\"fetch_comment_count\":40,\"parent_comment_count\":24,\"has_threaded_comments\":true}")
url.RawQuery = query.Encode()
resp, err := http.Get(url.String())
resp, err := r.Client.Get(url.String())
if err != nil {
return Response{}, err
return lookupResponse{}, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
var response QueryResponse
var response queryResponse
err = json.Unmarshal(body, &response)
if err != nil {
return Response{}, err
return lookupResponse{}, err
}
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" {
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,
Author: response.Data.ShortcodeMedia.Owner.Username,
Text: response.Data.ShortcodeMedia.EdgeMediaToCaption.Edges[0].Node.Text,

View file

@ -1,38 +1,36 @@
package main
package instagram
import (
"log"
"net/http"
"net/url"
"strings"
"nulo.in/dlbot/common"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
)
func respond(bot *tgbotapi.BotAPI, update tgbotapi.Update, url *url.URL) common.Result {
type Instagram struct {
http.Client
}
var Responder *Instagram = &Instagram{}
func (r *Instagram) Respond(url *url.URL) (*common.Uploadable, common.Error) {
if url.Hostname() != "instagram.com" && url.Hostname() != "www.instagram.com" {
return common.NotValid
return nil, common.NotValid
}
if strings.Index(url.Path, "/reel/") != 0 {
return common.NotValid
return nil, common.NotValid
}
log.Printf("Downloading %s", url.String())
lookup, err := Lookup(url.String())
lookup, err := r.lookup(url.String())
if err != nil {
log.Println(err)
return common.HadError
return nil, common.HadError
}
log.Println(lookup)
res := tgbotapi.NewVideo(update.Message.Chat.ID, tgbotapi.FileURL(lookup.VideoUrl))
res.ReplyToMessageID = update.Message.MessageID
res.Caption = "@" + lookup.Author
bot.Send(res)
return common.Uploaded
}
func main() {
common.Main(common.Config{Respond: respond})
return &common.Uploadable{
Url: lookup.VideoUrl,
Caption: "instagram.com/" + lookup.Author,
}, common.OK
}

BIN
instagram/pfp.png (Stored with Git LFS) Normal file

Binary file not shown.

141
main.go Normal file
View file

@ -0,0 +1,141 @@
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 Config struct {
Responders []common.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(respondWithMany(msg, "No se pudo detectar la URL ", urlString, "."))
}
continue
}
log.Printf("Downloading %s", url.String())
var uploadable *common.Uploadable
var érror common.Error
for _, responder := range config.Responders {
uploadable, érror = responder.Respond(url)
if érror != common.NotValid {
break
}
}
if uploadable != nil {
res := tgbotapi.NewVideo(update.Message.Chat.ID, tgbotapi.FileURL(uploadable.Url))
res.ReplyToMessageID = update.Message.MessageID
res.Caption = uploadable.Caption
bot.Send(res)
}
if explicit && érror == common.NotValid {
bot.Send(respondWithMany(msg, "La URL ", urlString, " no es compatible con este bot."))
continue
}
if érror == common.HadError || érror == common.OK {
hasDownloadables = true
}
if érror == common.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."))
}
}
func main() {
config := Config{
Responders: []common.Responder{
instagram.Responder,
tiktok.Responder,
},
}
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())
}

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/]])
- Instagram Reels: [@inst4gramdlbot](https://t.me/inst4gramdlbot) (código: [[instagram/]])
[@dlthefourthbot](https://t.me/dlthefourthbot)
Son rápidos ya que ni siquiera descargan el video, solo le pasan a Telegram la URL para descargarlos.

BIN
tiktok/foto de perfil.png (Stored with Git LFS)

Binary file not shown.

View file

@ -2,8 +2,6 @@ module nulo.in/dlbot/tiktok/v4
go 1.18
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
require nulo.in/dlbot/common v0.0.0-00010101000000-000000000000
replace nulo.in/dlbot/common => ../common

View file

@ -1,2 +0,0 @@
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,40 +1,36 @@
package main
package tiktok
import (
"log"
"net/http"
"net/url"
"nulo.in/dlbot/common"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
)
// Gracias a https://github.com/Xenzi-XN1/Tiktok-Download
// por enseñarme tikmate.app
func respond(bot *tgbotapi.BotAPI, update tgbotapi.Update, url *url.URL) common.Result {
type TikTok struct {
http.Client
}
var Responder *TikTok = &TikTok{}
func (r *TikTok) Respond(url *url.URL) (*common.Uploadable, common.Error) {
if url.Hostname() != "vm.tiktok.com" && url.Hostname() != "tiktok.com" {
return common.NotValid
return nil, common.NotValid
}
urlString := url.String()
// tikmate no entiende tiktok.com
url.Host = "vm.tiktok.com"
log.Printf("Downloading %s", urlString)
lookup, err := Lookup(url.String())
lookup, err := r.lookup(urlString)
if err != nil {
log.Println(err)
return common.HadError
return nil, common.HadError
}
log.Println(lookup)
res := tgbotapi.NewVideo(update.Message.Chat.ID, tgbotapi.FileURL(lookup))
res.ReplyToMessageID = update.Message.MessageID
bot.Send(res)
return common.Uploaded
}
func main() {
common.Main(common.Config{Respond: respond})
return &common.Uploadable{Url: lookup}, common.OK
}

BIN
tiktok/pfp.png (Stored with Git LFS) Normal file

Binary file not shown.

View file

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