inital commit
This commit is contained in:
54
examples/browse/main.go
Normal file
54
examples/browse/main.go
Normal file
@@ -0,0 +1,54 @@
|
||||
// browse prints the global front-page feed at /browse/ FA's "what's new
|
||||
// across the site" stream. Uses anon access by default; honours FA_A /
|
||||
// FA_B / CF_CLEARANCE / FA_UA from env when set (recommended, since FA
|
||||
// gates parts of the feed behind login + Cloudflare).
|
||||
//
|
||||
// go run ./examples/browse # 1 page, ~72 items
|
||||
// go run ./examples/browse 3 # 3 pages
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"strconv"
|
||||
|
||||
fa "git.anthrove.art/public/go-fa-api"
|
||||
)
|
||||
|
||||
func main() {
|
||||
maxPages := 1
|
||||
if len(os.Args) >= 2 {
|
||||
if n, err := strconv.Atoi(os.Args[1]); err == nil && n > 0 {
|
||||
maxPages = n
|
||||
}
|
||||
}
|
||||
|
||||
opts := []fa.Option{fa.WithUserAgent("go-fa-api-example/0.1")}
|
||||
if a, b := os.Getenv("FA_A"), os.Getenv("FA_B"); a != "" && b != "" {
|
||||
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...)
|
||||
|
||||
count := 0
|
||||
for sub, err := range client.Browse(context.Background(), fa.BrowseOptions{MaxPages: maxPages}) {
|
||||
if err != nil {
|
||||
log.Fatalf("Browse: %v", err)
|
||||
}
|
||||
count++
|
||||
fmt.Printf("[%d] %s %s by %s\n", sub.ID, sub.Title, sub.Rating, sub.Author.DisplayName)
|
||||
}
|
||||
fmt.Printf("\n%d submissions across %d page(s)\n", count, maxPages)
|
||||
}
|
||||
|
||||
func envOr(key, fallback string) string {
|
||||
if v := os.Getenv(key); v != "" {
|
||||
return v
|
||||
}
|
||||
return fallback
|
||||
}
|
||||
Reference in New Issue
Block a user