Compare commits

...

4 commits

Author SHA1 Message Date
Cat /dev/Nulo 1e18e5100d tiktok: Dejar que Telegram suba el video por nosotrxs
Esto lo hace más rápido y encima hace que el código sea más simple.
2023-01-02 23:33:07 -03:00
Cat /dev/Nulo 22a472ad1d Instagram bot 2023-01-02 23:32:35 -03:00
Cat /dev/Nulo fb2d827e97 Reestructurar más 2023-01-02 23:32:33 -03:00
Cat /dev/Nulo a15f2d9559 Reestructurar 2023-01-02 23:32:04 -03:00
16 changed files with 276 additions and 118 deletions

2
.gitattributes vendored
View file

@ -1 +1 @@
*.png filter=lfs diff=lfs merge=lfs -text
*/*.png filter=lfs diff=lfs merge=lfs -text

3
.gitignore vendored
View file

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

View file

@ -1,8 +0,0 @@
.PHONY: dlbot
dlbot:
CGO_ENABLED=0 GOOS=linux go build -a -ldflags '-extldflags "-static"' .
upload: dlbot
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

27
Taskfile.yml Normal file
View file

@ -0,0 +1,27 @@
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

View file

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

View file

@ -1,4 +1,4 @@
package main
package common
import (
"log"
@ -9,92 +9,18 @@ import (
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 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
type Config struct {
Respond func(bot *tgbotapi.BotAPI, update tgbotapi.Update, url *url.URL) Result
}
type Result uint8
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())
}
const (
NotValid Result = iota
HadError
Uploaded
)
func 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
}
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."))
}
}
func main() {
func Main(config Config) {
token := os.Getenv("TELEGRAM_TOKEN")
if token == "" {
log.Panic("No telegram token")
@ -124,6 +50,73 @@ func main() {
continue
}
go handleMessage(bot, update)
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."))
}
}

10
instagram/go.mod Normal file
View file

@ -0,0 +1,10 @@
module nulo.in/dlbot/instagram
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
)

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

60
instagram/instagram.go Normal file
View file

@ -0,0 +1,60 @@
package main
import (
"encoding/json"
"io"
"net/http"
"net/url"
"path"
)
type QueryResponse struct {
Data struct {
ShortcodeMedia struct {
VideoUrl string `json:"video_url"`
Owner struct {
Username string `json:"username"`
} `json:"owner"`
EdgeMediaToCaption struct {
Edges []struct {
Node struct {
Text string `json:"text`
} `json:"node"`
} `json:"edges"`
} `json:"edge_media_to_caption"`
} `json:"shortcode_media"`
} `json:"data"`
}
type Response struct {
VideoUrl string
Author string
Text string
}
func Lookup(urlSrc string) (Response, error) {
url, _ := url.Parse("https://www.instagram.com/graphql/query/?query_hash=b3055c01b4b222b8a47dc12b090e4e64")
query := url.Query()
query.Add("variables", "{\"shortcode\":\""+path.Base(urlSrc)+"\",\"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())
if err != nil {
return Response{}, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
var response QueryResponse
err = json.Unmarshal(body, &response)
if err != nil {
return Response{}, err
}
return Response{
VideoUrl: response.Data.ShortcodeMedia.VideoUrl,
Author: response.Data.ShortcodeMedia.Owner.Username,
Text: response.Data.ShortcodeMedia.EdgeMediaToCaption.Edges[0].Node.Text,
}, nil
}

33
instagram/main.go Normal file
View file

@ -0,0 +1,33 @@
package main
import (
"log"
"net/url"
"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 {
if url.Hostname() != "instagram.com" && url.Hostname() != "www.instagram.com" {
return common.NotValid
}
log.Printf("Downloading %s", url.String())
lookup, err := Lookup(url.String())
if err != nil {
return 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 + ": " + lookup.Text
bot.Send(res)
return common.Uploaded
}
func main() {
common.Main(common.Config{Respond: respond})
}

9
tiktok/go.mod Normal file
View file

@ -0,0 +1,9 @@
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
replace nulo.in/dlbot/common => ../common

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

40
tiktok/main.go Normal file
View file

@ -0,0 +1,40 @@
package main
import (
"log"
"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 {
if url.Hostname() != "vm.tiktok.com" && url.Hostname() != "tiktok.com" {
return 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())
if err != nil {
log.Println(err)
return 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})
}

View file

@ -2,8 +2,8 @@ package main
import (
"encoding/json"
"errors"
"io"
"log"
"net/http"
"net/url"
)
@ -22,35 +22,24 @@ type LookupResponse struct {
Token string `json:"token"`
}
func (lookup LookupResponse) NeedsUpload() bool { return true }
func (lookup LookupResponse) UploadData() (string, io.Reader, error) {
resp, err := http.Get("https://tikmate.app/download/" + lookup.Token + "/" + lookup.ID + ".mp4?hd=1")
if err != nil {
return "", nil, err
}
return lookup.AuthorName, resp.Body, nil
}
func (lookup LookupResponse) SendData() string {
log.Panicln("SendData called")
return ""
}
func Lookup(urlS string) (*LookupResponse, error) {
func Lookup(urlS string) (string, error) {
resp, err := http.PostForm(
"https://api.tikmate.app/api/lookup",
url.Values{"url": {urlS}},
)
if err != nil {
return nil, err
return "", err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
var response LookupResponse
err = json.Unmarshal(body, &response)
var lookup LookupResponse
err = json.Unmarshal(body, &lookup)
if err != nil {
return nil, err
return "", err
}
return &response, nil
if !lookup.Success {
return "", errors.New(lookup.Message)
}
return "https://tikmate.app/download/" + lookup.Token + "/" + lookup.ID + ".mp4?hd=1", nil
}