Handle media.giphy.com and expose dimensions of gifs

This commit is contained in:
Shivek Khurana 2021-02-04 16:50:10 +05:30 committed by Andrea Maria Piana
parent 757dbcd98e
commit 0feab5c6a7
2 changed files with 19 additions and 1 deletions

View File

@ -22,12 +22,16 @@ type GiphyOembedData struct {
ProviderName string `json:"provider_name"` ProviderName string `json:"provider_name"`
Title string `json:"title"` Title string `json:"title"`
URL string `json:"url"` URL string `json:"url"`
Height int `json:"height"`
Width int `json:"width"`
} }
type TenorOembedData struct { type TenorOembedData struct {
ProviderName string `json:"provider_name"` ProviderName string `json:"provider_name"`
ThumbnailURL string `json:"thumbnail_url"` ThumbnailURL string `json:"thumbnail_url"`
AuthorName string `json:"author_name"` AuthorName string `json:"author_name"`
Height int `json:"height"`
Width int `json:"width"`
} }
type LinkPreviewData struct { type LinkPreviewData struct {
@ -35,6 +39,8 @@ type LinkPreviewData struct {
Title string `json:"title" meta:"og:title"` Title string `json:"title" meta:"og:title"`
ThumbnailURL string `json:"thumbnailUrl" meta:"og:image"` ThumbnailURL string `json:"thumbnailUrl" meta:"og:image"`
ContentType string `json:"contentType"` ContentType string `json:"contentType"`
Height int `json:"height"`
Width int `json:"width"`
} }
type Site struct { type Site struct {
@ -172,6 +178,8 @@ func GetGiphyPreviewData(link string) (previewData LinkPreviewData, err error) {
previewData.Title = oembedData.Title previewData.Title = oembedData.Title
previewData.Site = oembedData.ProviderName previewData.Site = oembedData.ProviderName
previewData.ThumbnailURL = oembedData.URL previewData.ThumbnailURL = oembedData.URL
previewData.Height = oembedData.Height
previewData.Width = oembedData.Width
return previewData, nil return previewData, nil
} }
@ -231,6 +239,8 @@ func GetTenorPreviewData(link string) (previewData LinkPreviewData, err error) {
previewData.Title = oembedData.AuthorName // Tenor Oembed service doesn't return title of the Gif previewData.Title = oembedData.AuthorName // Tenor Oembed service doesn't return title of the Gif
previewData.Site = oembedData.ProviderName previewData.Site = oembedData.ProviderName
previewData.ThumbnailURL = oembedData.ThumbnailURL previewData.ThumbnailURL = oembedData.ThumbnailURL
previewData.Height = oembedData.Height
previewData.Width = oembedData.Width
return previewData, nil return previewData, nil
} }
@ -248,7 +258,7 @@ func GetLinkPreviewData(link string) (previewData LinkPreviewData, err error) {
return GetYoutubePreviewData(link) return GetYoutubePreviewData(link)
case "github.com": case "github.com":
return GetGithubPreviewData(link) return GetGithubPreviewData(link)
case "giphy.com": case "giphy.com", "media.giphy.com":
return GetGiphyPreviewData(link) return GetGiphyPreviewData(link)
case "gph.is": case "gph.is":
return GetGiphyShortURLPreviewData(link) return GetGiphyShortURLPreviewData(link)

View File

@ -44,10 +44,14 @@ func TestGetGiphyPreviewData(t *testing.T) {
Site: "GIPHY", Site: "GIPHY",
Title: "Boston Dynamics Yes GIF by FullMag - Find & Share on GIPHY", Title: "Boston Dynamics Yes GIF by FullMag - Find & Share on GIPHY",
ThumbnailURL: "https://media1.giphy.com/media/lcG3qwtTKSNI2i5vst/giphy.gif", ThumbnailURL: "https://media1.giphy.com/media/lcG3qwtTKSNI2i5vst/giphy.gif",
Height: 480,
Width: 480,
} }
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, bostonDynamicsEthGifData.Site, previewData.Site) require.Equal(t, bostonDynamicsEthGifData.Site, previewData.Site)
require.Equal(t, bostonDynamicsEthGifData.Title, previewData.Title) require.Equal(t, bostonDynamicsEthGifData.Title, previewData.Title)
require.Equal(t, bostonDynamicsEthGifData.Height, previewData.Height)
require.Equal(t, bostonDynamicsEthGifData.Width, previewData.Width)
// Giphy oembed returns links to different servers: https://media1.giphy.com, https://media2.giphy.com and so on // Giphy oembed returns links to different servers: https://media1.giphy.com, https://media2.giphy.com and so on
// We don't care about the server as long as other parts are equal, so we split at "." and ignore the first item // We don't care about the server as long as other parts are equal, so we split at "." and ignore the first item
@ -101,11 +105,15 @@ func TestGetTenorPreviewData(t *testing.T) {
Site: "Tenor", Site: "Tenor",
Title: "Annihere", Title: "Annihere",
ThumbnailURL: "https://media.tenor.com/images/975f6b95d188c277ebba62d9b5511685/tenor.gif", ThumbnailURL: "https://media.tenor.com/images/975f6b95d188c277ebba62d9b5511685/tenor.gif",
Height: 400,
Width: 600,
} }
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, gifData.Site, previewData.Site) require.Equal(t, gifData.Site, previewData.Site)
require.Equal(t, gifData.Title, previewData.Title) require.Equal(t, gifData.Title, previewData.Title)
require.Equal(t, gifData.ThumbnailURL, previewData.ThumbnailURL) require.Equal(t, gifData.ThumbnailURL, previewData.ThumbnailURL)
require.Equal(t, gifData.Height, previewData.Height)
require.Equal(t, gifData.Width, previewData.Width)
invalidTenorLink := "https://giphy.com/gifs/this-gif-does-not-exist-44444" invalidTenorLink := "https://giphy.com/gifs/this-gif-does-not-exist-44444"
_, err = GetTenorPreviewData(invalidTenorLink) _, err = GetTenorPreviewData(invalidTenorLink)