youtube shorts

This commit is contained in:
Cat /dev/Nulo 2023-12-28 23:29:28 -03:00
parent 8dcfd5486e
commit 450773588b
6 changed files with 64 additions and 9 deletions

View file

@ -1,12 +1,10 @@
package tiktok package common
import ( import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"errors" "errors"
"net/http" "net/http"
"nulo.in/dlbot/common"
) )
// https://github.com/wukko/cobalt/blob/current/docs/api.md // https://github.com/wukko/cobalt/blob/current/docs/api.md
@ -26,7 +24,11 @@ type jsonResponse struct {
AudioUrl string `json:"audio"` AudioUrl string `json:"audio"`
} }
func (r *TikTok) cobaltLookup(urlS string) (*common.Uploadable, error) { type CobaltClient struct {
*http.Client
}
func (c *CobaltClient) Lookup(urlS string) (*Uploadable, error) {
jsonReq := jsonRequest{ jsonReq := jsonRequest{
Url: urlS, Url: urlS,
} }
@ -45,7 +47,7 @@ func (r *TikTok) cobaltLookup(urlS string) (*common.Uploadable, error) {
} }
req.Header.Set("Content-Type", "application/json") req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "application/json") req.Header.Set("Accept", "application/json")
resp, err := r.Client.Do(req) resp, err := c.Do(req)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -62,7 +64,7 @@ func (r *TikTok) cobaltLookup(urlS string) (*common.Uploadable, error) {
} }
if len(jsonRes.Url) > 0 { if len(jsonRes.Url) > 0 {
return &common.Uploadable{ return &Uploadable{
VideoUrl: jsonRes.Url, VideoUrl: jsonRes.Url,
}, nil }, nil
} }
@ -72,8 +74,8 @@ func (r *TikTok) cobaltLookup(urlS string) (*common.Uploadable, error) {
imageUrls = append(imageUrls, i.Url) imageUrls = append(imageUrls, i.Url)
} }
return &common.Uploadable{ return &Uploadable{
ImagesWithAudio: &common.ImagesWithAudio{ ImagesWithAudio: &ImagesWithAudio{
AudioUrl: jsonRes.AudioUrl, AudioUrl: jsonRes.AudioUrl,
ImageUrls: imageUrls, ImageUrls: imageUrls,
}, },

3
go.mod
View file

@ -7,6 +7,7 @@ require (
nulo.in/dlbot/common v0.0.0-00010101000000-000000000000 nulo.in/dlbot/common v0.0.0-00010101000000-000000000000
nulo.in/dlbot/instagram v0.0.0-00010101000000-000000000000 nulo.in/dlbot/instagram v0.0.0-00010101000000-000000000000
nulo.in/dlbot/tiktok v0.0.0-00010101000000-000000000000 nulo.in/dlbot/tiktok v0.0.0-00010101000000-000000000000
nulo.in/dlbot/youtube v0.0.0-00010101000000-000000000000
) )
replace nulo.in/dlbot/common => ./common replace nulo.in/dlbot/common => ./common
@ -14,3 +15,5 @@ replace nulo.in/dlbot/common => ./common
replace nulo.in/dlbot/instagram => ./instagram replace nulo.in/dlbot/instagram => ./instagram
replace nulo.in/dlbot/tiktok => ./tiktok replace nulo.in/dlbot/tiktok => ./tiktok
replace nulo.in/dlbot/youtube => ./youtube

View file

@ -14,6 +14,7 @@ import (
"nulo.in/dlbot/common" "nulo.in/dlbot/common"
"nulo.in/dlbot/instagram" "nulo.in/dlbot/instagram"
"nulo.in/dlbot/tiktok" "nulo.in/dlbot/tiktok"
"nulo.in/dlbot/youtube"
) )
type Config struct { type Config struct {
@ -155,6 +156,7 @@ func main() {
Responders: []common.Responder{ Responders: []common.Responder{
instagram.Responder, instagram.Responder,
tiktok.Responder, tiktok.Responder,
youtube.Responder,
}, },
} }

View file

@ -28,7 +28,9 @@ func (r *TikTok) Respond(url *url.URL) (*common.Uploadable, common.Error) {
urlString := url.String() urlString := url.String()
lookup, err := r.cobaltLookup(urlString) cobalt := common.CobaltClient{Client: &r.Client}
lookup, err := cobalt.Lookup(urlString)
if err != nil { if err != nil {
log.Println(err) log.Println(err)
return nil, common.HadError return nil, common.HadError

7
youtube/go.mod Normal file
View file

@ -0,0 +1,7 @@
module nulo.in/dlbot/youtube/v4
go 1.18
require nulo.in/dlbot/common v0.0.0-00010101000000-000000000000
replace nulo.in/dlbot/common => ../common

39
youtube/main.go Normal file
View file

@ -0,0 +1,39 @@
package youtube
import (
"log"
"net/http"
"net/url"
"strings"
"nulo.in/dlbot/common"
)
type YouTube struct {
http.Client
}
var Responder *YouTube = &YouTube{}
func (r *YouTube) Respond(url *url.URL) (*common.Uploadable, common.Error) {
if url.Hostname() != "youtube.com" &&
url.Hostname() != "www.youtube.com" &&
url.Hostname() != "youtu.be" {
return nil, common.NotValid
}
if !strings.HasPrefix(url.Path, "/shorts") {
return nil, common.NotValid
}
urlString := url.String()
cobalt := common.CobaltClient{Client: &r.Client}
lookup, err := cobalt.Lookup(urlString)
if err != nil {
log.Println(err)
return nil, common.HadError
}
return lookup, common.OK
}