Follow short urls to get long url and then use that long url with oembed
This commit is contained in:
parent
499f1702d1
commit
ad6229bc4f
|
@ -68,6 +68,11 @@ func LinkPreviewWhitelist() []Site {
|
||||||
Address: "tenor.com",
|
Address: "tenor.com",
|
||||||
ImageSite: true,
|
ImageSite: true,
|
||||||
},
|
},
|
||||||
|
Site{
|
||||||
|
Title: "GIPHY GIFs Short URLS",
|
||||||
|
Address: "gph.se",
|
||||||
|
ImageSite: true,
|
||||||
|
},
|
||||||
Site{
|
Site{
|
||||||
Title: "GIPHY GIFs",
|
Title: "GIPHY GIFs",
|
||||||
Address: "giphy.com",
|
Address: "giphy.com",
|
||||||
|
@ -171,6 +176,34 @@ func GetGiphyPreviewData(link string) (previewData LinkPreviewData, err error) {
|
||||||
return previewData, nil
|
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) {
|
func GetTenorOembed(url string) (data TenorOembedData, err error) {
|
||||||
oembedLink := fmt.Sprintf(TenorOembedLink, url)
|
oembedLink := fmt.Sprintf(TenorOembedLink, url)
|
||||||
|
|
||||||
|
@ -216,6 +249,8 @@ func GetLinkPreviewData(link string) (previewData LinkPreviewData, err error) {
|
||||||
return GetGithubPreviewData(link)
|
return GetGithubPreviewData(link)
|
||||||
case "giphy.com":
|
case "giphy.com":
|
||||||
return GetGiphyPreviewData(link)
|
return GetGiphyPreviewData(link)
|
||||||
|
case "gph.se":
|
||||||
|
return GetGiphyShortURLPreviewData(link)
|
||||||
case "tenor.com":
|
case "tenor.com":
|
||||||
return GetTenorPreviewData(link)
|
return GetTenorPreviewData(link)
|
||||||
default:
|
default:
|
||||||
|
|
|
@ -58,16 +58,43 @@ func TestGetGiphyPreviewData(t *testing.T) {
|
||||||
require.Error(t, err)
|
require.Error(t, err)
|
||||||
|
|
||||||
|
|
||||||
// Other link shapes
|
|
||||||
// shortLink := "https://gph.is/g/aXLyK7P"
|
|
||||||
mediaLink := "https://media.giphy.com/media/lcG3qwtTKSNI2i5vst/giphy.gif"
|
mediaLink := "https://media.giphy.com/media/lcG3qwtTKSNI2i5vst/giphy.gif"
|
||||||
|
|
||||||
// shortLinkData, _ := GetGiphyPreviewData(shortLink)
|
|
||||||
mediaLinkData, _ := GetGiphyPreviewData(mediaLink)
|
mediaLinkData, _ := GetGiphyPreviewData(mediaLink)
|
||||||
|
|
||||||
require.Equal(t, thumbnailUrlWithoutSubdomain(mediaLinkData.ThumbnailURL), thumbnailUrlWithoutSubdomain(previewData.ThumbnailURL))
|
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) {
|
func TestGetTenorPreviewData(t *testing.T) {
|
||||||
validTenorLink := "https://tenor.com/view/robot-dance-do-you-love-me-boston-boston-dynamics-dance-gif-19998728"
|
validTenorLink := "https://tenor.com/view/robot-dance-do-you-love-me-boston-boston-dynamics-dance-gif-19998728"
|
||||||
previewData, err := GetTenorPreviewData(validTenorLink)
|
previewData, err := GetTenorPreviewData(validTenorLink)
|
||||||
|
|
Loading…
Reference in New Issue