Giphy oembed in place

This commit is contained in:
Shivek Khurana 2021-01-19 21:23:27 +05:30 committed by Andrea Maria Piana
parent 93bbc9c318
commit 47e17624d6
2 changed files with 59 additions and 4 deletions

View File

@ -12,12 +12,18 @@ import (
"github.com/keighl/metabolize" "github.com/keighl/metabolize"
) )
type OembedData struct { type YoutubeOembedData struct {
ProviderName string `json:"provider_name"` ProviderName string `json:"provider_name"`
Title string `json:"title"` Title string `json:"title"`
ThumbnailURL string `json:"thumbnail_url"` ThumbnailURL string `json:"thumbnail_url"`
} }
type GiphyOembedData struct {
ProviderName string `json:"provider_name"`
Title string `json:"title"`
URL string `json:"url"`
}
type LinkPreviewData struct { type LinkPreviewData struct {
Site string `json:"site" meta:"og:site_name"` Site string `json:"site" meta:"og:site_name"`
Title string `json:"title" meta:"og:title"` Title string `json:"title" meta:"og:title"`
@ -31,7 +37,8 @@ type Site struct {
ImageSite bool `json:"imageSite"` ImageSite bool `json:"imageSite"`
} }
const YouTubeOembedLink = "https://www.youtube.com/oembed?format=json&url=%s" const YoutubeOembedLink = "https://www.youtube.com/oembed?format=json&url=%s"
const GiphyOembedLink = "https://giphy.com/services/oembed?url=%s"
var httpClient = http.Client{ var httpClient = http.Client{
Timeout: 30 * time.Second, Timeout: 30 * time.Second,
@ -78,8 +85,8 @@ func GetURLContent(url string) (data []byte, err error) {
return ioutil.ReadAll(response.Body) return ioutil.ReadAll(response.Body)
} }
func GetYoutubeOembed(url string) (data OembedData, err error) { func GetYoutubeOembed(url string) (data YoutubeOembedData, err error) {
oembedLink := fmt.Sprintf(YouTubeOembedLink, url) oembedLink := fmt.Sprintf(YoutubeOembedLink, url)
jsonBytes, err := GetURLContent(oembedLink) jsonBytes, err := GetURLContent(oembedLink)
if err != nil { if err != nil {
@ -123,6 +130,36 @@ func GetGithubPreviewData(link string) (previewData LinkPreviewData, err error)
return previewData, nil return previewData, nil
} }
func GetGiphyOembed(url string) (data GiphyOembedData, err error) {
oembedLink := fmt.Sprintf(GiphyOembedLink, url)
jsonBytes, err := GetURLContent(oembedLink)
if err != nil {
return data, fmt.Errorf("Can't get bytes from Giphy oembed response at %s", oembedLink)
}
err = json.Unmarshal(jsonBytes, &data)
if err != nil {
return data, fmt.Errorf("Can't unmarshall json")
}
return data, nil
}
func GetGiphyPreviewData(link string) (previewData LinkPreviewData, err error) {
oembedData, err := GetGiphyOembed(link)
if err != nil {
return previewData, err
}
previewData.Title = oembedData.Title
previewData.Site = oembedData.ProviderName
previewData.ThumbnailURL = oembedData.URL
return previewData, nil
}
func GetLinkPreviewData(link string) (previewData LinkPreviewData, err error) { func GetLinkPreviewData(link string) (previewData LinkPreviewData, err error) {
url, err := url.Parse(link) url, err := url.Parse(link)

View File

@ -30,3 +30,21 @@ func TestGetLinkPreviewData(t *testing.T) {
require.Error(t, err) require.Error(t, err)
} }
func TestGetGiphyPreviewData(t *testing.T) {
validGiphyLink := "https://giphy.com/gifs/FullMag-robot-boston-dynamics-dance-lcG3qwtTKSNI2i5vst"
previewData, err := GetGiphyPreviewData(validGiphyLink)
bostonDynamicsEthGifData := LinkPreviewData{
Site: "GIPHY",
Title: "Boston Dynamics Yes GIF by FullMag - Find & Share on GIPHY",
ThumbnailURL: "https://media1.giphy.com/media/lcG3qwtTKSNI2i5vst/giphy.gif",
}
require.NoError(t, err)
require.Equal(t, bostonDynamicsEthGifData.Site, previewData.Site)
require.Equal(t, bostonDynamicsEthGifData.Title, previewData.Title)
require.Equal(t, bostonDynamicsEthGifData.ThumbnailURL, previewData.ThumbnailURL)
invalidGiphyLink := "https://giphy.com/gifs/this-gif-does-not-exist-44444"
_, err = GetGiphyPreviewData(invalidGiphyLink)
require.Error(t, err)
}