linkpreview

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 8, 2024 License: MIT Imports: 5 Imported by: 0

README

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.

Installation

To install the package, run:

go get github.com/AmanuelCh/linkpreview

Usage

Import the package in your Go file:

import "github.com/AmanuelCh/linkpreview"
Creating a LinkPreviewer

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.
Caching

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.

Custom User Agent

You can specify a custom user agent when creating the LinkPreviewer instance. This can help in identifying the requests made by your application.

Example code
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)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CacheEntry

type CacheEntry struct {
	Title       string
	Description string
	Image       string
	Expires     time.Time
}

CacheEntry holds the cached data for a URL.

type LinkPreviewer

type LinkPreviewer struct {
	// contains filtered or unexported fields
}

LinkPreviewer struct holds the cache and a mutex for thread safety.

func NewLinkPreviewer

func NewLinkPreviewer(userAgent string) *LinkPreviewer

NewLinkPreviewer creates a new LinkPreviewer with the specified user agent.

func (*LinkPreviewer) GetLinkPreview

func (lp *LinkPreviewer) GetLinkPreview(url string) (title, description, image string, err error)

GetLinkPreview fetches the title, description, and image from the given URL.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL