This commit is contained in:
@@ -1,7 +1,11 @@
|
||||
package helpers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
@@ -20,3 +24,46 @@ func StripUrl(webLink string, keepParam string) string {
|
||||
u.RawQuery = values.Encode()
|
||||
return u.String()
|
||||
}
|
||||
|
||||
func GetParam(webLink string, param string) string {
|
||||
u, err := url.Parse(webLink)
|
||||
if err != nil {
|
||||
slog.Info("StripUrl", "error", err)
|
||||
return webLink
|
||||
}
|
||||
values := u.Query()
|
||||
for key := range values {
|
||||
if key == param {
|
||||
return values.Get(key)
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
type OEmbedResponse struct {
|
||||
Title string `json:"title"`
|
||||
}
|
||||
|
||||
func GetVideoTitle(videoUrl string) (string, error) {
|
||||
videoID := GetParam(videoUrl, "v")
|
||||
// Construct the oEmbed URL
|
||||
url := fmt.Sprintf("https://www.youtube.com/oembed?url=https://www.youtube.com/watch?v=%s&format=json", videoID)
|
||||
|
||||
resp, err := http.Get(url)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
var data OEmbedResponse
|
||||
if err := json.Unmarshal(body, &data); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return data.Title, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user