Documentation
¶
Index ¶
- Variables
- type Webinfo
- func (w *Webinfo) DownloadImage(ctx context.Context, destDir string, temporary bool) (outPath string, err error)
- func (w *Webinfo) DownloadThumbnail(ctx context.Context, destDir string, width int, temporary bool) (outPath string, err error)
- func (w *Webinfo) ImageBytes(ctx context.Context) (data []byte, err error)
Constants ¶
This section is empty.
Variables ¶
Functions ¶
This section is empty.
Types ¶
type Webinfo ¶
type Webinfo struct {
URL string `json:"url,omitempty"` // Original page URL
Location string `json:"location,omitempty"` // Location
Canonical string `json:"canonical,omitempty"` // Canonical URL
Title string `json:"title,omitempty"` // Page title
Description string `json:"description,omitempty"` // Meta description
ImageURL string `json:"image_url,omitempty"` // Representative image URL
UserAgent string `json:"user_agent,omitempty"` // User-Agent used to fetch the page
}
Webinfo stores metadata extracted from a web page and values used for follow-up image download operations.
func Fetch ¶
Fetch retrieves metadata from a web page and returns it as Webinfo.
It fetches the page with the given context and User-Agent (or a default one when empty), peeks up to 1024 bytes to determine encoding, then parses the head section with goquery.
Extraction precedence is kept explicit: title: title -> twitter:title -> og:title description: meta[name=description] -> twitter:description -> og:description image: twitter:image -> og:image
Returned errors are wrapped with context. Response close errors are joined.
func (*Webinfo) DownloadImage ¶
func (w *Webinfo) DownloadImage(ctx context.Context, destDir string, temporary bool) (outPath string, err error)
DownloadImage downloads w.ImageURL and writes it under destDir.
If temporary is true, or if the URL path has no filename, a temporary file is created. Otherwise the output file name is derived from the URL path.
Extension resolution order is:
- URL path extension
- response Content-Type
- sniffed content type from up to 512 bytes
- fallback ".img"
Returned errors are wrapped with context and include cleanup failures.
func (*Webinfo) DownloadThumbnail ¶
func (w *Webinfo) DownloadThumbnail(ctx context.Context, destDir string, width int, temporary bool) (outPath string, err error)
DownloadThumbnail downloads the source image, resizes it to width while keeping aspect ratio, and writes the thumbnail to destDir.
width defaults to 150 when width <= 0.
The source image is downloaded to a temporary file first and removed on return. Output uses a temporary name when temporary is true; otherwise it uses "<base>-thumb<ext>" derived from the original image URL.
Returned errors are wrapped with context and include cleanup failures.
func (*Webinfo) ImageBytes ¶ added in v0.2.0
ImageBytes downloads w.ImageURL and returns its contents in memory.
Risk: this method reads the entire response body into memory, so very large images can increase memory usage.
Returned errors are wrapped with context and include response close failures.
