The linkpreview
package provides a simple and efficient way to fetch the title, description, and image from a given URL. It includes caching functionality to minimize repeated requests and allows for a customizable user agent.
To install the package, run:
go get github.com/AmanuelCh/linkpreview
Import the package in your Go file:
import "github.com/AmanuelCh/linkpreview"
You can create a new LinkPreviewer instance with a custom user agent:
lp := linkpreview.NewLinkPreviewer("MyCustomUserAgent/1.0")
Use the GetLinkPreview
function to fetch the metadata from a URL:
url := "https://github.com/AmanuelCh/hahu"
title, description, image, err := lp.GetLinkPreview(url)
if err != nil {
// Handle error
}
fmt.Printf("Title: %s\nDescription: %s\nImage: %s\n", title, description, image)
The GetLinkPreview
function returns the following values:
- title: The title of the webpage.
- description: The description of the webpage.
- image: The URL of the main image on the webpage.
- err: An error, if any occurred during the process.
The package caches the results for one hour. If the same URL is requested within this time frame, the cached data will be returned instead of making a new HTTP request.
You can specify a custom user agent when creating the LinkPreviewer
instance. This can help in identifying the requests made by your application.
package main
import (
"fmt"
"log"
"github.com/AmanuelCh/linkpreview"
)
func main() {
lp := linkpreview.NewLinkPreviewer("MyCustomUserAgent/1.0")
url := "https://github.com/AmanuelCh/hahu"
title, description, image, err := lp.GetLinkPreview(url)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Title: %s\nDescription: %s\nImage: %s\n", title, description, image)
}