Tenor unfurling + connected Giphy and Tenor to GetPreviewData fn

This commit is contained in:
Shivek Khurana 2021-01-20 16:44:24 +05:30 committed by Andrea Maria Piana
parent 47e17624d6
commit d37b7a9729
2 changed files with 64 additions and 1 deletions

View File

@ -24,6 +24,13 @@ type GiphyOembedData struct {
URL string `json:"url"`
}
type TenorOembedData struct {
ProviderName string `json:"provider_name"`
ThumbnailURL string `json:"thumbnail_url"`
AuthorName string `json:"author_name"`
}
type LinkPreviewData struct {
Site string `json:"site" meta:"og:site_name"`
Title string `json:"title" meta:"og:title"`
@ -39,6 +46,8 @@ type Site struct {
const YoutubeOembedLink = "https://www.youtube.com/oembed?format=json&url=%s"
const GiphyOembedLink = "https://giphy.com/services/oembed?url=%s"
const TenorOembedLink = "https://tenor.com/oembed?url=%s"
var httpClient = http.Client{
Timeout: 30 * time.Second,
@ -160,8 +169,37 @@ func GetGiphyPreviewData(link string) (previewData LinkPreviewData, err error) {
return previewData, nil
}
func GetLinkPreviewData(link string) (previewData LinkPreviewData, err error) {
func GetTenorOembed(url string) (data TenorOembedData, err error) {
oembedLink := fmt.Sprintf(TenorOembedLink, url)
jsonBytes, err := GetURLContent(oembedLink)
if err != nil {
return data, fmt.Errorf("Can't get bytes from Tenor 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 GetTenorPreviewData(link string) (previewData LinkPreviewData, err error) {
oembedData, err := GetTenorOembed(link)
if err != nil {
return previewData, err
}
previewData.Title = oembedData.AuthorName // Tenor Oembed service doesn't return title of the Gif
previewData.Site = oembedData.ProviderName
previewData.ThumbnailURL = oembedData.ThumbnailURL
return previewData, nil
}
func GetLinkPreviewData(link string) (previewData LinkPreviewData, err error) {
url, err := url.Parse(link)
if err != nil {
return previewData, fmt.Errorf("Cant't parse link %s", link)
@ -177,6 +215,12 @@ func GetLinkPreviewData(link string) (previewData LinkPreviewData, err error) {
if "github.com" == hostname {
return GetGithubPreviewData(link)
}
if "giphy.com" == hostname {
return GetGiphyPreviewData(link)
}
if "tenor.com" == hostname {
return GetTenorPreviewData(link)
}
for _, site := range LinkPreviewWhitelist() {
if strings.HasSuffix(hostname, site.Address) && site.ImageSite {

View File

@ -48,3 +48,22 @@ func TestGetGiphyPreviewData(t *testing.T) {
_, err = GetGiphyPreviewData(invalidGiphyLink)
require.Error(t, err)
}
func TestGetTenorPreviewData(t *testing.T) {
validTenorLink := "https://tenor.com/view/robot-dance-do-you-love-me-boston-boston-dynamics-dance-gif-19998728"
previewData, err := GetTenorPreviewData(validTenorLink)
gifData := LinkPreviewData{
Site: "Tenor",
Title: "Annihere",
ThumbnailURL: "https://media.tenor.com/images/975f6b95d188c277ebba62d9b5511685/tenor.gif",
}
require.NoError(t, err)
require.Equal(t, gifData.Site, previewData.Site)
require.Equal(t, gifData.Title, previewData.Title)
require.Equal(t, gifData.ThumbnailURL, previewData.ThumbnailURL)
invalidTenorLink := "https://giphy.com/gifs/this-gif-does-not-exist-44444"
_, err = GetTenorPreviewData(invalidTenorLink)
require.Error(t, err)
}