Documentation
¶
Overview ¶
Package ChromeDL uses chromedp to download the files. It may come handy when one needs to get a file from a protected website that doesn't allow regular methods, such as curl or http.Get().
It is heavily based on https://github.com/chromedp/examples/tree/master/download_file with minor modifications.
Index ¶
Examples ¶
Constants ¶
const DefaultUA = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36"
DefaultUA is the default user agent string that will be used by the browser instance. Can be changed
Variables ¶
var ErrNoChrome = errors.New("no chrome instance in the context")
ErrNoChrome indicates that there's no chrome instance in the context.
Functions ¶
func Download ¶ added in v0.1.1
Download downloads a file from the provided uri using the chromedp capabilities. It will return the reader with the file contents (buffered), and an error if any. If the error is present, reader may not be nil if the file was downloaded and read successfully. It will store the file in the temporary directory once the download is complete, then buffer it and try to cleanup afterwards. Set the timeout on context if required, by default no timeout is set. Optionally one can pass the configuration options for the downloader.
Example ¶
const rbnzRates = "https://www.rbnz.govt.nz/-/media/ReserveBank/Files/Statistics/tables/b1/hb1-daily.xlsx?revision=5fa61401-a877-4607-b7ae-2e060c09935d" r, err := Download(context.Background(), rbnzRates) if err != nil { log.Fatal(err) } data, err := ioutil.ReadAll(r) if err != nil { log.Fatal(err) } fmt.Printf("file size > 0: %v\n", len(data) > 0) fmt.Printf("file signature: %s\n", string(data[0:2]))
Output: file size > 0: true file signature: PK
Types ¶
type Instance ¶ added in v0.1.0
type Instance struct {
// contains filtered or unexported fields
}
Instance is the browser instance that will be used for downloading files.
func New ¶ added in v0.1.0
New creates a new Instance, starting up the headless chrome to do the download. Once finished, call Stop to terminate the browser.
func NewWithChromeCtx ¶ added in v0.1.1
NewWithChromeCtx creates new Instance for existing browser instance. Stop will not terminate the browser, but will cancel the event listener.
func (*Instance) Download ¶ added in v0.1.1
Download downloads the file returning the reader with contents.