diff --git a/tiktok/cobalt.go b/common/cobalt.go similarity index 85% rename from tiktok/cobalt.go rename to common/cobalt.go index db60149..122a1ca 100644 --- a/tiktok/cobalt.go +++ b/common/cobalt.go @@ -1,12 +1,10 @@ -package tiktok +package common import ( "bytes" "encoding/json" "errors" "net/http" - - "nulo.in/dlbot/common" ) // https://github.com/wukko/cobalt/blob/current/docs/api.md @@ -26,7 +24,11 @@ type jsonResponse struct { 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{ 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("Accept", "application/json") - resp, err := r.Client.Do(req) + resp, err := c.Do(req) if err != nil { return nil, err } @@ -62,7 +64,7 @@ func (r *TikTok) cobaltLookup(urlS string) (*common.Uploadable, error) { } if len(jsonRes.Url) > 0 { - return &common.Uploadable{ + return &Uploadable{ VideoUrl: jsonRes.Url, }, nil } @@ -72,8 +74,8 @@ func (r *TikTok) cobaltLookup(urlS string) (*common.Uploadable, error) { imageUrls = append(imageUrls, i.Url) } - return &common.Uploadable{ - ImagesWithAudio: &common.ImagesWithAudio{ + return &Uploadable{ + ImagesWithAudio: &ImagesWithAudio{ AudioUrl: jsonRes.AudioUrl, ImageUrls: imageUrls, }, diff --git a/go.mod b/go.mod index 13ef9da..98ef25a 100644 --- a/go.mod +++ b/go.mod @@ -7,6 +7,7 @@ require ( 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 + nulo.in/dlbot/youtube v0.0.0-00010101000000-000000000000 ) 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/tiktok => ./tiktok + +replace nulo.in/dlbot/youtube => ./youtube diff --git a/main.go b/main.go index 09fa470..7837347 100644 --- a/main.go +++ b/main.go @@ -14,6 +14,7 @@ import ( "nulo.in/dlbot/common" "nulo.in/dlbot/instagram" "nulo.in/dlbot/tiktok" + "nulo.in/dlbot/youtube" ) type Config struct { @@ -155,6 +156,7 @@ func main() { Responders: []common.Responder{ instagram.Responder, tiktok.Responder, + youtube.Responder, }, } diff --git a/tiktok/main.go b/tiktok/main.go index fcf9bc7..e1d8b98 100644 --- a/tiktok/main.go +++ b/tiktok/main.go @@ -28,7 +28,9 @@ func (r *TikTok) Respond(url *url.URL) (*common.Uploadable, common.Error) { urlString := url.String() - lookup, err := r.cobaltLookup(urlString) + cobalt := common.CobaltClient{Client: &r.Client} + + lookup, err := cobalt.Lookup(urlString) if err != nil { log.Println(err) return nil, common.HadError diff --git a/youtube/go.mod b/youtube/go.mod new file mode 100644 index 0000000..a9f6248 --- /dev/null +++ b/youtube/go.mod @@ -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 diff --git a/youtube/main.go b/youtube/main.go new file mode 100644 index 0000000..2539cf3 --- /dev/null +++ b/youtube/main.go @@ -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 +}