Files
go-fa-api/examples/download/main.go
2026-05-25 22:27:18 +02:00

50 lines
1.1 KiB
Go

// download fetches a single submission and streams its main file to disk.
// Demonstrates that downloads share the SDK's rate limiter and cookie jar
// with metadata fetches.
//
// go run ./examples/download 12345678 out.jpg
package main
import (
"context"
"log"
"os"
"strconv"
fa "git.anthrove.art/public/go-fa-api"
)
func main() {
if len(os.Args) < 3 {
log.Fatalf("usage: %s <submission-id> <out-path>", os.Args[0])
}
id, err := strconv.ParseInt(os.Args[1], 10, 64)
if err != nil {
log.Fatalf("invalid id: %v", err)
}
client := fa.New(
fa.WithCookies(fa.Cookies{A: os.Getenv("FA_A"), B: os.Getenv("FA_B")}),
fa.WithCloudflare(fa.CFCookies{Clearance: os.Getenv("CF_CLEARANCE")}),
fa.WithUserAgent(os.Getenv("FA_UA")),
)
ctx := context.Background()
sub, err := client.GetSubmission(ctx, fa.SubmissionID(id))
if err != nil {
log.Fatalf("GetSubmission: %v", err)
}
out, err := os.Create(os.Args[2])
if err != nil {
log.Fatalf("create: %v", err)
}
defer out.Close()
n, err := client.Download(ctx, sub, out)
if err != nil {
log.Fatalf("download: %v", err)
}
log.Printf("wrote %d bytes to %s", n, os.Args[2])
}