usar cobalt para tiktok + subir imagenes con audio
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

This commit is contained in:
Cat /dev/Nulo 2023-12-28 23:11:52 -03:00
parent 79fdc8b479
commit 892af35ea3
6 changed files with 128 additions and 13 deletions

View file

@ -8,9 +8,14 @@ type Responder interface {
Respond(url *url.URL) (*Uploadable, Error) Respond(url *url.URL) (*Uploadable, Error)
} }
type Uploadable struct { type Uploadable struct {
Url string VideoUrl string
*ImagesWithAudio
Caption string Caption string
} }
type ImagesWithAudio struct {
ImageUrls []string
AudioUrl string
}
type Error uint8 type Error uint8

View file

@ -33,7 +33,7 @@ func (r *Instagram) Respond(url *url.URL) (*common.Uploadable, common.Error) {
} }
return &common.Uploadable{ return &common.Uploadable{
Url: lookup.VideoUrl, VideoUrl: lookup.VideoUrl,
Caption: "instagram.com/" + lookup.Author, Caption: "instagram.com/" + lookup.Author,
}, common.OK }, common.OK
} }

40
main.go
View file

@ -95,13 +95,39 @@ func (config Config) handleMessage(bot *tgbotapi.BotAPI, update tgbotapi.Update)
} }
if uploadable != nil { if uploadable != nil {
res := tgbotapi.NewVideo(update.Message.Chat.ID, FileURL(uploadable.Url)) if uploadable.ImagesWithAudio != nil {
res.ReplyToMessageID = update.Message.MessageID var files []interface{}
res.Caption = uploadable.Caption for _, u := range uploadable.ImagesWithAudio.ImageUrls {
_, err := bot.Send(res) files = append(files,
if err != nil { tgbotapi.NewInputMediaPhoto(tgbotapi.FileURL(u)),
log.Println("Error subiendo", url.String(), err) )
bot.Send(respondWithMany(update.Message, "Hubo un error al descargar ", url.String(), ".")) }
log.Println(files)
mediaGroup := tgbotapi.NewMediaGroup(update.Message.Chat.ID, files)
mediaGroup.ReplyToMessageID = update.Message.MessageID
msgs, err := bot.SendMediaGroup(mediaGroup)
if err != nil {
log.Println("Error subiendo", url.String(), err)
bot.Send(respondWithMany(update.Message, "Hubo un error al descargar ", url.String(), "."))
}
res := tgbotapi.NewAudio(update.Message.Chat.ID, FileURL(uploadable.AudioUrl))
res.ReplyToMessageID = msgs[0].MessageID
_, err = bot.Send(res)
if err != nil {
log.Println("Error subiendo", url.String(), err)
bot.Send(respondWithMany(update.Message, "Hubo un error al descargar ", url.String(), "."))
}
} else {
res := tgbotapi.NewVideo(update.Message.Chat.ID, FileURL(uploadable.VideoUrl))
res.ReplyToMessageID = update.Message.MessageID
res.Caption = uploadable.Caption
_, err := bot.Send(res)
if err != nil {
log.Println("Error subiendo", url.String(), err)
bot.Send(respondWithMany(update.Message, "Hubo un error al descargar ", url.String(), "."))
}
} }
} }

84
tiktok/cobalt.go Normal file
View file

@ -0,0 +1,84 @@
package tiktok
import (
"bytes"
"encoding/json"
"errors"
"net/http"
"nulo.in/dlbot/common"
)
// https://github.com/wukko/cobalt/blob/current/docs/api.md
type jsonRequest struct {
Url string `json:"url"`
}
type jsonResponse struct {
Status string `json:"status"`
Text string `json:"text"`
Url string `json:"url"`
PickerType string `json:"pickerType"`
Picker []struct {
Url string `json:"url"`
} `json:"picker"`
AudioUrl string `json:"audio"`
}
func (r *TikTok) cobaltLookup(urlS string) (*common.Uploadable, error) {
jsonReq := jsonRequest{
Url: urlS,
}
reqByt, err := json.Marshal(jsonReq)
if err != nil {
return nil, err
}
req, err := http.NewRequest(
"POST",
"https://co.wuk.sh/api/json",
bytes.NewReader(reqByt),
)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "application/json")
resp, err := r.Client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var jsonRes jsonResponse
err = json.NewDecoder(resp.Body).Decode(&jsonRes)
if err != nil {
return nil, err
}
if jsonRes.Status == "error" {
return nil, errors.New("cobalt error: " + jsonRes.Text)
}
if len(jsonRes.Url) > 0 {
return &common.Uploadable{
VideoUrl: jsonRes.Url,
}, nil
}
if len(jsonRes.AudioUrl) > 0 && jsonRes.PickerType == "images" && len(jsonRes.Picker) > 0 {
var imageUrls []string
for _, i := range jsonRes.Picker {
imageUrls = append(imageUrls, i.Url)
}
return &common.Uploadable{
ImagesWithAudio: &common.ImagesWithAudio{
AudioUrl: jsonRes.AudioUrl,
ImageUrls: imageUrls,
},
}, nil
}
return nil, errors.New("Faltan datos de cobalt")
}

View file

@ -28,11 +28,11 @@ func (r *TikTok) Respond(url *url.URL) (*common.Uploadable, common.Error) {
urlString := url.String() urlString := url.String()
lookup, err := r.lookup(urlString) lookup, err := r.cobaltLookup(urlString)
if err != nil { if err != nil {
log.Println(err) log.Println(err)
return nil, common.HadError return nil, common.HadError
} }
return &common.Uploadable{Url: lookup}, common.OK return lookup, common.OK
} }

View file

@ -42,5 +42,5 @@ func (r *TikTok) lookup(urlS string) (string, error) {
if !lookup.Success { if !lookup.Success {
return "", errors.New(lookup.Message) return "", errors.New(lookup.Message)
} }
return "https://tikmate.app/download/" + lookup.Token + "/" + lookup.ID + ".mp4?hd=1", nil return "https://tikmate.app/download/" + lookup.Token + "/" + lookup.ID + ".mp4", nil
} }