inital commit
This commit is contained in:
68
examples/search/main.go
Normal file
68
examples/search/main.go
Normal file
@@ -0,0 +1,68 @@
|
||||
// search runs a /search/?q=... query and prints the first N pages of
|
||||
// matches. Works anonymously for general-rated results; mature/adult
|
||||
// searches require login.
|
||||
//
|
||||
// go run ./examples/search dragon
|
||||
// go run ./examples/search "fox knight" --max=2 --rating=general --order=date
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
fa "git.anthrove.art/public/go-fa-api"
|
||||
)
|
||||
|
||||
func main() {
|
||||
maxPages := flag.Int("max", 1, "max pages to fetch")
|
||||
ratingFlag := flag.String("rating", "", "comma-separated: general, mature, adult (empty = all)")
|
||||
orderFlag := flag.String("order", "", "relevancy | date | popularity (empty = default)")
|
||||
flag.Parse()
|
||||
|
||||
if flag.NArg() == 0 {
|
||||
log.Fatalf("usage: %s [flags] <query>", os.Args[0])
|
||||
}
|
||||
query := strings.Join(flag.Args(), " ")
|
||||
|
||||
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...)
|
||||
|
||||
so := fa.SearchOptions{MaxPages: *maxPages}
|
||||
if *ratingFlag != "" {
|
||||
for _, r := range strings.Split(*ratingFlag, ",") {
|
||||
so.Ratings = append(so.Ratings, fa.ParseRating(strings.TrimSpace(r)))
|
||||
}
|
||||
}
|
||||
if *orderFlag != "" {
|
||||
so.OrderBy = fa.SearchOrder(*orderFlag)
|
||||
}
|
||||
|
||||
count := 0
|
||||
for sub, err := range client.Search(context.Background(), query, so) {
|
||||
if err != nil {
|
||||
log.Fatalf("Search: %v", err)
|
||||
}
|
||||
count++
|
||||
fmt.Printf("[%d] %s %s by %s\n",
|
||||
sub.ID, sub.Title, sub.Rating, sub.Author.DisplayName)
|
||||
}
|
||||
fmt.Printf("\n%d results for %q across up to %d page(s)\n", count, query, *maxPages)
|
||||
}
|
||||
|
||||
func envOr(key, fallback string) string {
|
||||
if v := os.Getenv(key); v != "" {
|
||||
return v
|
||||
}
|
||||
return fallback
|
||||
}
|
||||
Reference in New Issue
Block a user