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

56 lines
1.5 KiB
Go

// basic demonstrates use of the SDK: fetching a single submission and
// printing a few fields.
//
// The example runs anonymously by default. If the FA_A / FA_B (and ideally
// CF_CLEARANCE + FA_UA) environment variables are set, it authenticates
// with them required for any submission FA gates behind login, mature
// content guard, or Cloudflare challenges.
//
// go run ./examples/basic 12345678
package main
import (
"context"
"fmt"
"log"
"os"
"strconv"
fa "git.anthrove.art/public/go-fa-api"
)
func main() {
if len(os.Args) < 2 {
log.Fatalf("usage: %s <submission-id>", os.Args[0])
}
id, err := strconv.ParseInt(os.Args[1], 10, 64)
if err != nil {
log.Fatalf("invalid submission id: %v", err)
}
opts := []fa.Option{fa.WithUserAgent("go-fa-api-example/0.1")}
if a, b := os.Getenv("FA_A"), os.Getenv("FA_B"); a != "" && b != "" {
log.Printf("using FA_A/FA_B cookies for authenticated request")
opts = []fa.Option{
fa.WithCookies(fa.Cookies{A: a, B: b}),
fa.WithCloudflare(fa.CFCookies{Clearance: os.Getenv("CF_CLEARANCE")}),
fa.WithUserAgent(envOr("FA_UA", "go-fa-api-example/0.1")),
}
}
client := fa.New(opts...)
sub, err := client.GetSubmission(context.Background(), fa.SubmissionID(id))
if err != nil {
log.Fatalf("GetSubmission: %v", err)
}
fmt.Printf("%s\nby %s\nrating: %s\ntags: %v\nfile: %s\n",
sub.Title, sub.Author.DisplayName, sub.Rating, sub.Tags, sub.FileURL)
}
func envOr(key, fallback string) string {
if v := os.Getenv(key); v != "" {
return v
}
return fallback
}