República Popular China

This commit is contained in:
Cat /dev/Nulo 2022-08-10 20:41:51 +02:00
commit cfc9f9a04f
4 changed files with 188 additions and 0 deletions

5
go.mod Normal file
View file

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

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=

126
main.go Normal file
View file

@ -0,0 +1,126 @@
package main
import (
"log"
"net/url"
"os"
"strings"
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
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 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
}
if searchMsg.Entities == nil || len(searchMsg.Entities) < 1 {
if explicit {
bot.Send(respondWith(msg, "Ese mensaje no tiene ningún link!"))
}
return
}
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, "."))
}
return
}
if url.Hostname() != "vm.tiktok.com" {
if explicit {
bot.Send(respondWithMany(msg, "La URL ", urlString, " no es de TikTok."))
}
return
}
log.Printf("Downloading %s", urlString)
lookup, err := Lookup(urlString)
if err != nil {
bot.Send(respondWithMany(msg, "Hubo un error al descargar ", urlString, "."))
return
}
log.Println(lookup)
res := tgbotapi.NewVideo(msg.Chat.ID, *lookup)
res.ReplyToMessageID = msg.MessageID
// if info, err := readInfoFile(&infoPath); err != nil {
// res.Caption = "Hubo un error consiguiendo el titulo de este video."
// log.Println("error reading info file", err)
// } else {
// res.Caption = info.Title
// }
bot.Send(res)
}
}
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)
}
}

55
tikmate.go Normal file
View file

@ -0,0 +1,55 @@
package main
import (
"encoding/json"
"io"
"log"
"net/http"
"net/url"
)
type LookupResponse struct {
AuthorAvatar string `json:"author_avatar"`
AuthorID string `json:"author_id"`
AuthorName string `json:"author_name"`
CommentCount int `json:"comment_count"`
CreateTime string `json:"create_time"`
ID string `json:"id"`
LikeCount int `json:"like_count"`
ShareCount int `json:"share_count"`
Success bool `json:"success"`
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) {
resp, err := http.PostForm(
"https://api.tikmate.app/api/lookup",
url.Values{"url": {urlS}},
)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
var response LookupResponse
err = json.Unmarshal(body, &response)
if err != nil {
return nil, err
}
return &response, nil
}