⭐ Go richpreview library ⭐
An easy to use API to generate rich previews for any site.
How to use
First 'go get' the package
go get -u github.com/cantasaurus/richpreview
The only set up required to use it is to specify a user-agent, or you can use the default provided. Here's the most basic use of the library.
package main
import (
"github.com/cantasaurus/richpreview"
"encoding/json"
"fmt"
)
func main(){
urls := []string{"https://i.imgur.com/vWF1ovD.gifv"}
userAgent := richpreview.DefaultUserAgent()
results := richpreview.RichPreview(urls, userAgent)
for _, elem := range results{
b, err := json.Marshal(elem)
if err != nil {
fmt.Println(err)
}
fmt.Print(string(b), "\n\n")
}
}
The results returned after json marshaling for the above example are as follows.
{
"valid_url":true,
"media_only":false,
"is_twitch_stream":false,
"is_twitch_clip":false,
"is_youtube_video":false,
"twitch_stream_embed":"",
"twitch_clip_embed":"",
"youtube_embed":"",
"title":"Puppy Management",
"description":"",
"url":"https://i.imgur.com/vWF1ovD.gifv",
"content_type":"text/html; charset=utf-8",
"og_title":"Puppy Management",
"og_type":"video.other",
"og_url":"https://i.imgur.com/vWF1ovD.mp4",
"og_site":"",
"og_site_name":"Imgur",
"og_locale":"",
"og_image_alt":"",
"og_image":"https://i.imgur.com/vWF1ovDh.jpg",
"og_video":"https://i.imgur.com/vWF1ovD.mp4",
"og_audio":"",
"og_description":"Imgur: The magic of the Internet"
}
If a youtube video, twitch clip, or twitch stream is detected the corresponding booleans will be set to true and an iframe embed url will also be added to the payload. An example is below.
urls := []string{"https://www.youtube.com/watch?v=dQw4w9WgXcQ"}
Results:
{
"valid_url":true,
"media_only":false,
"is_twitch_stream":false,
"is_twitch_clip":false,
"is_youtube_video":true,
"twitch_stream_embed":"",
"twitch_clip_embed":"",
"youtube_embed":"https://www.youtube.com/embed/dQw4w9WgXcQ",
"title":"Rick Astley - Never Gonna Give You Up (Video) - YouTube",
"description":"Rick Astley - Never Gonna Give You Up (Official Music Video) - Listen On Spotify: http://smarturl.it/AstleySpotify Learn more about the brand new album ‘Beau...",
"url":"https://www.youtube.com/watch?v=dQw4w9WgXcQ",
"content_type":"text/html; charset=utf-8",
"og_title":"Rick Astley - Never Gonna Give You Up (Video)",
"og_type":"video.other",
"og_url":"https://www.youtube.com/watch?v=dQw4w9WgXcQ",
"og_site":"",
"og_site_name":"YouTube",
"og_locale":"",
"og_image_alt":"",
"og_image":"https://i.ytimg.com/vi/dQw4w9WgXcQ/maxresdefault.jpg",
"og_video":"",
"og_audio":"",
"og_description":"Rick Astley - Never Gonna Give You Up (Official Music Video) - Listen On Spotify: http://smarturl.it/AstleySpotify Learn more about the brand new album ‘Beau..."
}
As a quick note, Reddit is fairly odd about the user-agent. With the default I've never ran into an issue, but with common user-agents like "Mozilla/version number" I've ran into problems so just a heads up.