MCPcopy Index your code
hub / github.com/cavaliergopher/grab

github.com/cavaliergopher/grab @v3.0.1

Chat with this repo
repository ↗ · DeepWiki ↗ · release v3.0.1 ↗ · + Follow
160 symbols 682 edges 27 files 70 documented · 44% 9 cross-repo links
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

grab

GoDoc Build Status Go Report Card

Downloading the internet, one goroutine at a time!

$ go get github.com/cavaliergopher/grab/v3

Grab is a Go package for downloading files from the internet with the following rad features:

  • Monitor download progress concurrently
  • Auto-resume incomplete downloads
  • Guess filename from content header or URL path
  • Safely cancel downloads using context.Context
  • Validate downloads using checksums
  • Download batches of files concurrently
  • Apply rate limiters

Requires Go v1.7+

Example

The following example downloads a PDF copy of the free eBook, "An Introduction to Programming in Go" into the current working directory.

resp, err := grab.Get(".", "http://www.golang-book.com/public/pdf/gobook.pdf")
if err != nil {
    log.Fatal(err)
}

fmt.Println("Download saved to", resp.Filename)

The following, more complete example allows for more granular control and periodically prints the download progress until it is complete.

The second time you run the example, it will auto-resume the previous download and exit sooner.

package main

import (
    "fmt"
    "os"
    "time"

    "github.com/cavaliergopher/grab/v3"
)

func main() {
    // create client
    client := grab.NewClient()
    req, _ := grab.NewRequest(".", "http://www.golang-book.com/public/pdf/gobook.pdf")

    // start download
    fmt.Printf("Downloading %v...\n", req.URL())
    resp := client.Do(req)
    fmt.Printf("  %v\n", resp.HTTPResponse.Status)

    // start UI loop
    t := time.NewTicker(500 * time.Millisecond)
    defer t.Stop()

Loop:
    for {
        select {
        case <-t.C:
            fmt.Printf("  transferred %v / %v bytes (%.2f%%)\n",
                resp.BytesComplete(),
                resp.Size,
                100*resp.Progress())

        case <-resp.Done:
            // download is complete
            break Loop
        }
    }

    // check for errors
    if err := resp.Err(); err != nil {
        fmt.Fprintf(os.Stderr, "Download failed: %v\n", err)
        os.Exit(1)
    }

    fmt.Printf("Download saved to ./%v \n", resp.Filename)

    // Output:
    // Downloading http://www.golang-book.com/public/pdf/gobook.pdf...
    //   200 OK
    //   transferred 42970 / 2893557 bytes (1.49%)
    //   transferred 1207474 / 2893557 bytes (41.73%)
    //   transferred 2758210 / 2893557 bytes (95.32%)
    // Download saved to ./gobook.pdf
}

Design trade-offs

The primary use case for Grab is to concurrently downloading thousands of large files from remote file repositories where the remote files are immutable. Examples include operating system package repositories or ISO libraries.

Grab aims to provide robust, sane defaults. These are usually determined using the HTTP specifications, or by mimicking the behavior of common web clients like cURL, wget and common web browsers.

Grab aims to be stateless. The only state that exists is the remote files you wish to download and the local copy which may be completed, partially completed or not yet created. The advantage to this is that the local file system is not cluttered unnecessarily with addition state files (like a .crdownload file). The disadvantage of this approach is that grab must make assumptions about the local and remote state; specifically, that they have not been modified by another program.

If the local or remote file are modified outside of grab, and you download the file again with resuming enabled, the local file will likely become corrupted. In this case, you might consider making remote files immutable, or disabling resume.

Grab aims to enable best-in-class functionality for more complex features through extensible interfaces, rather than reimplementation. For example, you can provide your own Hash algorithm to compute file checksums, or your own rate limiter implementation (with all the associated trade-offs) to rate limit downloads.

Extension points exported contracts — how you extend this code

HTTPClient (Interface)
HTTPClient provides an interface allowing us to perform HTTP requests. [2 implementers]
v3/client.go
RateLimiter (Interface)
RateLimiter is an interface that must be satisfied by any third-party rate limiters that may be used to limit download t [1 …
v3/rate_limiter.go
Gauge (Interface)
Gauge is the common interface for all BPS gauges in this package. Given a set of samples over time, each gauge type can [1 …
v3/pkg/bps/bps.go
Hook (FuncType)
A Hook is a user provided callback function that can be called by grab at various stages of a requests lifecycle. If a h
v3/request.go
SampleFunc (FuncType)
SampleFunc is used by Watch to take periodic samples of a monitored stream.
v3/pkg/bps/bps.go
StatusCodeFunc (FuncType)
(no doc)
v3/pkg/grabtest/handler.go
HandlerOption (FuncType)
(no doc)
v3/pkg/grabtest/handler_option.go

Core symbols most depended-on inside this repo

Err
called by 38
v3/response.go
Size
called by 32
v3/response.go
Do
called by 24
v3/client.go
Error
called by 19
v3/error.go
BytesComplete
called by 18
v3/response.go
IsComplete
called by 12
v3/response.go
SetChecksum
called by 10
v3/request.go
NewRequest
called by 9
v3/request.go

Shape

Function 89
Method 51
Struct 10
FuncType 5
Interface 4
TypeAlias 1

Languages

Go100%

Modules by API surface

v3/client.go22 symbols
v3/response.go17 symbols
v3/client_test.go17 symbols
v3/pkg/grabtest/handler_option.go11 symbols
v3/pkg/grabtest/handler_test.go8 symbols
v3/pkg/grabtest/handler.go8 symbols
v3/pkg/grabtest/assert.go8 symbols
v3/request.go7 symbols
v3/pkg/grabui/console_client.go7 symbols
v3/transfer.go5 symbols
v3/rate_limiter_test.go5 symbols
v3/pkg/bps/sma_test.go5 symbols

For agents

$ claude mcp add grab \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact

Ask about this repo answers extend the page