|
1 | | -# webinfo |
2 | | -Extract metadata and structured information from web pages |
| 1 | +# webinfo -- Extract metadata and structured information from web pages |
| 2 | + |
| 3 | +[](https://github.com/goark/webinfo/actions) |
| 4 | +[](https://raw.githubusercontent.com/goark/webinfo/master/LICENSE) |
| 5 | +[](https://github.com/goark/webinfo/releases/latest) |
| 6 | + |
| 7 | +webinfo is a small Go module that extracts common metadata from web pages and provides utilities |
| 8 | +to download representative images and create thumbnails. |
| 9 | + |
| 10 | +**Quick overview** |
| 11 | + |
| 12 | +- **Package**: `webinfo` |
| 13 | +- **Repository**: `github.com/goark/webinfo` |
| 14 | +- **Purpose**: fetch page metadata (title, description, canonical, image, etc.) and download images |
| 15 | + |
| 16 | +**Features** |
| 17 | + |
| 18 | +- Fetch page metadata with `Fetch` (handles encodings and meta tag precedence). |
| 19 | +- Download an image referenced by `Webinfo.ImageURL` using `(*Webinfo).DownloadImage`. |
| 20 | +- Create a thumbnail from the referenced image using `(*Webinfo).DownloadThumbnail`. |
| 21 | + |
| 22 | +**Install** |
| 23 | + |
| 24 | +Use Go modules (Go 1.25+ as used by the project): |
| 25 | + |
| 26 | +```bash |
| 27 | +go get github.com/goark/webinfo@latest |
| 28 | +``` |
| 29 | + |
| 30 | +**Basic usage** |
| 31 | + |
| 32 | +Example showing fetch and download thumbnail (error handling omitted for brevity): |
| 33 | + |
| 34 | +```go |
| 35 | +package main |
| 36 | + |
| 37 | +import ( |
| 38 | + "context" |
| 39 | + "fmt" |
| 40 | + "github.com/goark/webinfo" |
| 41 | +) |
| 42 | + |
| 43 | +func main() { |
| 44 | + ctx := context.Background() |
| 45 | + // Fetch metadata for a page (empty UA uses default) |
| 46 | + info, err := webinfo.Fetch(ctx, "https://example.com", "") |
| 47 | + if err != nil { |
| 48 | + fmt.Fprintln(os.Stderr, "error fetching webinfo:", err) |
| 49 | + panic(err) |
| 50 | + } |
| 51 | + |
| 52 | + // Download thumbnail: width 150, to directory "thumbnails", permanent file |
| 53 | + thumbPath, err := info.DownloadThumbnail(ctx, "thumbnails", 150, false) |
| 54 | + if err != nil { |
| 55 | + panic(err) |
| 56 | + } |
| 57 | + fmt.Println("thumbnail saved:", thumbPath) |
| 58 | +} |
| 59 | +``` |
| 60 | + |
| 61 | +**API notes** |
| 62 | + |
| 63 | +- `Fetch(ctx, url, userAgent)` — Parse and extract metadata. Pass an empty userAgent to use the module default. |
| 64 | +- `(*Webinfo).DownloadImage(ctx, destDir, temporary)` — Download the image in `Webinfo.ImageURL` and save it. If |
| 65 | + `temporary` is true (or `destDir` is empty), a temporary file is created. |
| 66 | +- `(*Webinfo).DownloadThumbnail(ctx, destDir, width, temporary)` — Download the referenced image and produce a |
| 67 | + thumbnail resized to `width` pixels (height is preserved by aspect ratio). If `destDir` is empty the method |
| 68 | + creates a temporary file; when `temporary` is false the thumbnail file is named based on the original image |
| 69 | + name with `-thums` appended before the extension. |
| 70 | + |
| 71 | +**Error handling** |
| 72 | + |
| 73 | +The package uses `github.com/goark/errs` for wrapping errors with contextual keys (e.g. `url`, `path`, `dir`). |
| 74 | +Callers should inspect returned errors accordingly. |
| 75 | + |
| 76 | +**Tests & development** |
| 77 | + |
| 78 | +- Run all tests: `go test ./...` |
| 79 | +- The repository includes `Taskfile.yml` tasks for common workflows; see that file for CI/test commands. |
| 80 | + |
| 81 | +## Modules Requirement Graph |
| 82 | + |
| 83 | +[](./dependency.png) |
| 84 | + |
| 85 | +[webinfo]: https://github.com/goark/webinfo "goark/webinfo: Extract metadata and structured information from web pages" |
0 commit comments