| 1 | /* |
| 2 | Package grab provides a HTTP download manager implementation. |
| 3 | |
| 4 | Get is the most simple way to download a file: |
| 5 | |
| 6 | resp, err := grab.Get("/tmp", "http://example.com/example.zip") |
| 7 | // ... |
| 8 | |
| 9 | Get will download the given URL and save it to the given destination directory. |
| 10 | The destination filename will be determined automatically by grab using |
| 11 | Content-Disposition headers returned by the remote server, or by inspecting the |
| 12 | requested URL path. |
| 13 | |
| 14 | An empty destination string or "." means the transfer will be stored in the |
| 15 | current working directory. |
| 16 | |
| 17 | If a destination file already exists, grab will assume it is a complete or |
| 18 | partially complete download of the requested file. If the remote server supports |
| 19 | resuming interrupted downloads, grab will resume downloading from the end of the |
| 20 | partial file. If the server does not support resumed downloads, the file will be |
| 21 | retransferred in its entirety. If the file is already complete, grab will return |
| 22 | successfully. |
| 23 | |
| 24 | For control over the HTTP client, destination path, auto-resume, checksum |
| 25 | validation and other settings, create a Client: |
| 26 | |
| 27 | client := grab.NewClient() |
| 28 | client.HTTPClient.Transport.DisableCompression = true |
| 29 | |
| 30 | req, err := grab.NewRequest("/tmp", "http://example.com/example.zip") |
| 31 | // ... |
| 32 | req.NoResume = true |
| 33 | req.HTTPRequest.Header.Set("Authorization", "Basic YWxhZGRpbjpvcGVuc2VzYW1l") |
| 34 | |
| 35 | resp := client.Do(req) |
| 36 | // ... |
| 37 | |
| 38 | You can monitor the progress of downloads while they are transferring: |
| 39 | |
| 40 | client := grab.NewClient() |
| 41 | req, err := grab.NewRequest("", "http://example.com/example.zip") |
| 42 | // ... |
| 43 | resp := client.Do(req) |
| 44 | |
| 45 | t := time.NewTicker(time.Second) |
| 46 | defer t.Stop() |
| 47 | |
| 48 | for { |
| 49 | select { |
| 50 | case <-t.C: |
| 51 | fmt.Printf("%.02f%% complete\n", resp.Progress()) |
| 52 | |
| 53 | case <-resp.Done: |
| 54 | if err := resp.Err(); err != nil { |
| 55 | // ... |
| 56 | } |
| 57 | |
| 58 | // ... |
| 59 | return |
| 60 | } |
| 61 | } |
| 62 | */ |
| 63 | package grab |