From d37b7a97296893f89a46e51dd83a75e9c0355c20 Mon Sep 17 00:00:00 2001 From: Shivek Khurana Date: Wed, 20 Jan 2021 16:44:24 +0530 Subject: [PATCH] Tenor unfurling + connected Giphy and Tenor to GetPreviewData fn --- protocol/urls/urls.go | 46 +++++++++++++++++++++++++++++++++++++- protocol/urls/urls_test.go | 19 ++++++++++++++++ 2 files changed, 64 insertions(+), 1 deletion(-) diff --git a/protocol/urls/urls.go b/protocol/urls/urls.go index 31866f294..3ff4da676 100644 --- a/protocol/urls/urls.go +++ b/protocol/urls/urls.go @@ -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 { diff --git a/protocol/urls/urls_test.go b/protocol/urls/urls_test.go index f777f2223..f595c69b7 100644 --- a/protocol/urls/urls_test.go +++ b/protocol/urls/urls_test.go @@ -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) +}