Follow short urls to get long url and then use that long url with oembed

This commit is contained in:
Shivek Khurana 2021-02-03 18:41:53 +05:30 committed by Andrea Maria Piana
parent 499f1702d1
commit ad6229bc4f
2 changed files with 65 additions and 3 deletions

View File

@ -68,6 +68,11 @@ func LinkPreviewWhitelist() []Site {
Address: "tenor.com",
ImageSite: true,
},
Site{
Title: "GIPHY GIFs Short URLS",
Address: "gph.se",
ImageSite: true,
},
Site{
Title: "GIPHY GIFs",
Address: "giphy.com",
@ -171,6 +176,34 @@ func GetGiphyPreviewData(link string) (previewData LinkPreviewData, err error) {
return previewData, nil
}
// Giphy has a shortener service called gph.se, the oembed service doesn't work with shortened urls,
// so we need to fetch the long url first
func GetGiphyLongURL(shortURL string) (longURL string, err error) {
res, err := http.Get(shortURL)
if err != nil {
return longURL, fmt.Errorf("can't get bytes from Giphy's short url at %s", shortURL)
}
canonicalURL := res.Request.URL.String()
if (canonicalURL == shortURL) {
// no redirect, ie. not a valid url
return longURL, fmt.Errorf("unable to process Giphy's short url at %s", shortURL)
} else {
return canonicalURL, err
}
}
func GetGiphyShortURLPreviewData (shortURL string) (data LinkPreviewData, err error) {
longURL, err := GetGiphyLongURL(shortURL)
if err != nil {
return data, err
} else {
return GetGiphyPreviewData(longURL)
}
}
func GetTenorOembed(url string) (data TenorOembedData, err error) {
oembedLink := fmt.Sprintf(TenorOembedLink, url)
@ -216,6 +249,8 @@ func GetLinkPreviewData(link string) (previewData LinkPreviewData, err error) {
return GetGithubPreviewData(link)
case "giphy.com":
return GetGiphyPreviewData(link)
case "gph.se":
return GetGiphyShortURLPreviewData(link)
case "tenor.com":
return GetTenorPreviewData(link)
default:

View File

@ -58,16 +58,43 @@ func TestGetGiphyPreviewData(t *testing.T) {
require.Error(t, err)
// Other link shapes
// shortLink := "https://gph.is/g/aXLyK7P"
mediaLink := "https://media.giphy.com/media/lcG3qwtTKSNI2i5vst/giphy.gif"
// shortLinkData, _ := GetGiphyPreviewData(shortLink)
mediaLinkData, _ := GetGiphyPreviewData(mediaLink)
require.Equal(t, thumbnailUrlWithoutSubdomain(mediaLinkData.ThumbnailURL), thumbnailUrlWithoutSubdomain(previewData.ThumbnailURL))
}
func TestGetGiphyLongURL(t *testing.T) {
shortURL := "https://gph.is/g/aXLyK7P"
computedLongURL, _ := GetGiphyLongURL(shortURL)
actualLongURL := "https://giphy.com/gifs/FullMag-robot-boston-dynamics-dance-lcG3qwtTKSNI2i5vst"
require.Equal(t, computedLongURL, actualLongURL)
_, err := GetGiphyLongURL("http://this-giphy-site-doesn-not-exist.se/bogus-url")
require.Error(t, err)
_, err = GetGiphyLongURL("http://gph.se/bogus-url-but-correct-domain")
require.Error(t, err)
}
func TestGetGiphyShortURLPreviewData(t *testing.T) {
shortURL := "https://gph.is/g/aXLyK7P"
previewData, err := GetGiphyShortURLPreviewData(shortURL)
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)
}
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)