Documentation
¶
Overview ¶
Package favifetch discovers and fetches website favicons.
It automatically finds the best favicon from a website by checking HTML <link> tags, web app manifests, common fallback locations, and optionally Vemetric's favicon API.
Basic usage:
result, err := favifetch.Fetch(ctx, "github.com")
if err != nil {
log.Fatal(err)
}
fmt.Printf("Format: %s, Size: %d bytes, Source: %s\n",
result.Format, result.Size, result.Source)
// result.Data contains the raw image bytes
Index ¶
- type DetectedFormat
- type DiscoveredSource
- type ErrPrivateIP
- type FaviconMode
- type FaviconResult
- type Option
- func WithBlockPrivateIPs(block bool) Option
- func WithFallbackAPI(use bool) Option
- func WithFormat(format TargetFormat) Option
- func WithHTTPClient(client *http.Client) Option
- func WithMaxImageSize(size int64) Option
- func WithMaxRedirects(n int) Option
- func WithMode(mode FaviconMode) Option
- func WithPreferredFormats(formats ...DetectedFormat) Option
- func WithSize(size int) Option
- func WithTimeout(d time.Duration) Option
- func WithUserAgent(ua string) Option
- func WithVemetricAPIHost(host string) Option
- type Options
- type TargetFormat
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type DetectedFormat ¶ added in v0.2.0
type DetectedFormat int
DetectedFormat represents an image format detected from actual image data.
const ( FormatUnknown DetectedFormat = iota FormatPNG FormatJPEG FormatSVG FormatICO FormatWebP FormatGIF FormatBMP )
func (DetectedFormat) IsWritable ¶ added in v0.2.0
func (f DetectedFormat) IsWritable() bool
IsWritable returns true if the format can be encoded to (PNG, JPEG, WebP).
func (DetectedFormat) String ¶ added in v0.2.0
func (f DetectedFormat) String() string
func (DetectedFormat) Target ¶ added in v0.2.0
func (f DetectedFormat) Target() (TargetFormat, bool)
Target converts to a TargetFormat if the format is writable. The bool is false for non-writable formats (SVG, ICO, GIF, BMP, Unknown).
type DiscoveredSource ¶
type DiscoveredSource struct {
URL string
Size int
Format DetectedFormat
Source string
Score int
}
DiscoveredSource represents a favicon URL found during discovery.
type ErrPrivateIP ¶
ErrPrivateIP is returned when a URL resolves to a private IP and BlockPrivateIps is enabled.
func (*ErrPrivateIP) Error ¶
func (e *ErrPrivateIP) Error() string
type FaviconMode ¶ added in v0.4.0
type FaviconMode int
FaviconMode controls how favicon candidates are selected.
const ( // ModeBest selects the highest-ranked favicon from all supported sources. ModeBest FaviconMode = iota // ModeBrowser selects a Chromium-style regular tab favicon from the initial // HTML document. When enabled, the fallback API is used if those candidates // cannot be fetched. It returns the original image bytes and does not support // resizing or format conversion. ModeBrowser )
type FaviconResult ¶
type FaviconResult struct {
// Data is the raw image bytes (PNG, SVG, ICO, etc.)
Data []byte
// Format is the detected image format.
Format DetectedFormat
// Width and Height are the image dimensions in pixels.
Width, Height int
// Source indicates where the favicon was found:
// "link-tag", "manifest", "fallback", "fallback-api"
Source string
// SourceURL is the original URL from which the favicon was fetched.
SourceURL string
// Size is the size of Data in bytes.
Size int
}
FaviconResult holds the fetched favicon image data and metadata.
func Fetch ¶
Fetch discovers and fetches the best favicon for the given URL.
The url parameter can be a full URL ("https://github.com") or just a domain ("github.com"). If no scheme is present, https:// is prepended.
Options can be provided to customize behavior (timeout, size, format, etc.). If no options are given, sensible defaults are used.
type Option ¶
type Option func(*Options)
Option is a functional option for configuring the fetcher.
func WithBlockPrivateIPs ¶
WithBlockPrivateIPs enables or disables blocking of private IP ranges.
func WithFallbackAPI ¶
WithFallbackAPI enables or disables the Vemetric favicon API fallback.
func WithFormat ¶
func WithFormat(format TargetFormat) Option
WithFormat sets the desired output format.
func WithHTTPClient ¶
WithHTTPClient sets a custom http.Client.
func WithMaxImageSize ¶
WithMaxImageSize sets the maximum favicon image size in bytes.
func WithMaxRedirects ¶
WithMaxRedirects sets the maximum number of HTTP redirects.
func WithMode ¶ added in v0.4.0
func WithMode(mode FaviconMode) Option
WithMode sets the favicon selection mode.
func WithPreferredFormats ¶ added in v0.3.0
func WithPreferredFormats(formats ...DetectedFormat) Option
WithPreferredFormats sets the preferred favicon formats in priority order. Example: WithPreferredFormats(FormatSVG, FormatPNG) prefers SVG favicons and falls back to PNG if no SVG is found.
func WithUserAgent ¶
WithUserAgent sets the User-Agent header. It is ignored in ModeBrowser, which always uses its Chromium-like User-Agent.
func WithVemetricAPIHost ¶ added in v0.4.1
WithVemetricAPIHost sets the host, optionally including a port, for a self-hosted Vemetric-compatible favicon API. Requests use HTTPS.
type Options ¶
type Options struct {
// Mode controls how favicon candidates are selected. The default ModeBest
// ranks all supported favicon sources by quality. ModeBrowser emulates
// Chromium's regular tab-icon selection from the initial HTML document.
Mode FaviconMode
// UserAgent sent in HTTP requests for HTML and manifest fetching.
// ModeBrowser always uses its Chromium-like User-Agent instead.
UserAgent string
// RequestTimeout is the maximum time for the entire fetch operation.
RequestTimeout time.Duration
// MaxImageSize is the maximum size (in bytes) of a favicon image to accept.
MaxImageSize int64
// MaxRedirects limits the number of HTTP redirects to follow.
MaxRedirects int
// BlockPrivateIps controls whether requests to private IP ranges are rejected.
BlockPrivateIps bool
// UseFallbackAPI enables the Vemetric favicon API as a last-resort fallback.
UseFallbackAPI bool
// VemetricAPIHost is the host, optionally including a port, of a
// Vemetric-compatible favicon API. Requests use HTTPS.
VemetricAPIHost string
// Size is the desired output size in pixels (resizes favicon to size×size).
// 0 means no resizing.
Size int
// Format is the desired output format. Zero value (TargetUnspecified) means keep original.
Format TargetFormat
// HTTPClient is the *http.Client to use for all requests. If nil, a default
// client is created from the other options.
HTTPClient *http.Client
// PreferredFormats is an ordered list of preferred favicon formats.
// The first format is most preferred, the last is least.
// During discovery, sources matching a higher-preference format get a
// large score bonus, so they are tried before lower-preference formats.
// Unlisted formats are still tried as a last resort.
// When nil or empty, the default preference order (SVG > PNG > WebP > JPEG > ICO > GIF > BMP) is used.
PreferredFormats []DetectedFormat
}
Options holds all configuration for the favicon fetcher.
func DefaultOptions ¶
DefaultOptions returns the default configuration. Optional Option arguments can be passed to override defaults.
type TargetFormat ¶ added in v0.2.0
type TargetFormat int
TargetFormat represents an output format for encoding (PNG, JPEG, WebP).
const ( TargetUnspecified TargetFormat = iota TargetPNG TargetJPEG TargetWebP )
func ParseTargetFormat ¶ added in v0.2.0
func ParseTargetFormat(s string) (TargetFormat, error)
ParseTargetFormat parses a string into a TargetFormat. Accepts "png", "jpg", "jpeg", "webp" (case-insensitive).
func (TargetFormat) Detected ¶ added in v0.2.0
func (t TargetFormat) Detected() DetectedFormat
Detected converts TargetFormat to its corresponding DetectedFormat.
func (TargetFormat) String ¶ added in v0.2.0
func (t TargetFormat) String() string