From d4126c6d17ec3b5139429025c28586803d85744c Mon Sep 17 00:00:00 2001 From: sneak Date: Mon, 20 May 2024 03:51:53 -0700 Subject: [PATCH] initial, server gives 400 --- main.go | 128 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 128 insertions(+) create mode 100644 main.go diff --git a/main.go b/main.go new file mode 100644 index 0000000..2068d57 --- /dev/null +++ b/main.go @@ -0,0 +1,128 @@ +package main + +import ( + "encoding/json" + "fmt" + "net/http" + "net/url" + "os" +) + +// SearchRequest represents the structure of the input JSON for the API request. +type SearchRequest struct { + Query string `json:"query"` + Sort string `json:"sort"` + Set string `json:"set"` + Filters []interface{} `json:"filters"` + Limit int `json:"limit"` + VariantLimit bool `json:"variantLimit"` + CollectionLimit bool `json:"collectionLimit"` + Direction string `json:"direction"` +} + +// Input represents the top-level structure of the input JSON for the API request. +type Input struct { + Part0 SearchRequest `json:"0"` + Part1 SearchRequest `json:"1"` +} + +func main() { + // Parameterize the Limit value + limit := 35 + + // Define the input data structure + input := Input{ + Part0: SearchRequest{ + Query: "", + Sort: "relevance", + Set: "*", + Filters: []interface{}{}, + Limit: limit, + VariantLimit: false, + CollectionLimit: false, + Direction: "forward", + }, + Part1: SearchRequest{ + Query: "", + Sort: "relevance", + Set: "*", + Filters: []interface{}{}, + CollectionLimit: false, + }, + } + + // Serialize the input data structure to JSON + jsonData, err := json.Marshal(input) + if err != nil { + logErrorAndExit(fmt.Errorf("failed to marshal input data: %w", err)) + } + + // URL encode the JSON data + urlEncodedInput := url.QueryEscape(string(jsonData)) + + // Construct the full URL + apiURL := fmt.Sprintf("https://curiosa.io/api/trpc/card.search,card.count?batch=1&input=%s", urlEncodedInput) + + // Create a new HTTP request + req, err := http.NewRequest("GET", apiURL, nil) + if err != nil { + logErrorAndExit(fmt.Errorf("failed to create request: %w", err)) + } + + // Set headers + setHeaders(req) + + // Perform the HTTP request + client := &http.Client{} + resp, err := client.Do(req) + if err != nil { + logErrorAndExit(fmt.Errorf("failed to perform request: %w", err)) + } + defer resp.Body.Close() + + // Check for non-200 status codes + if resp.StatusCode != http.StatusOK { + logErrorAndExit(fmt.Errorf("non-200 response: %s", resp.Status)) + } + + // Read and pretty print the JSON response + var prettyJSON json.RawMessage + err = json.NewDecoder(resp.Body).Decode(&prettyJSON) + if err != nil { + logErrorAndExit(fmt.Errorf("failed to decode response body: %w", err)) + } + + prettyData, err := json.MarshalIndent(prettyJSON, "", " ") + if err != nil { + logErrorAndExit(fmt.Errorf("failed to marshal pretty JSON: %w", err)) + } + + // Output the pretty JSON to stdout + fmt.Println(string(prettyData)) +} + +// setHeaders sets the necessary headers for the HTTP request. +func setHeaders(req *http.Request) { + req.Header.Set("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:126.0) Gecko/20100101 Firefox/126.0") + req.Header.Set("Accept", "*/*") + req.Header.Set("Accept-Language", "en-US,en;q=0.5") + req.Header.Set("Accept-Encoding", "gzip, deflate") + req.Header.Set("Referer", "https://curiosa.io/cards") + req.Header.Set("Content-Type", "application/json") + req.Header.Set("x-build-id", "a68dd8c6f6e3b605951c534ad22e788557cabd0d") + req.Header.Set("x-trpc-source", "nextjs-react") + req.Header.Set("DNT", "1") + req.Header.Set("Sec-GPC", "1") + req.Header.Set("Connection", "keep-alive") + req.Header.Set("Sec-Fetch-Dest", "empty") + req.Header.Set("Sec-Fetch-Mode", "cors") + req.Header.Set("Sec-Fetch-Site", "same-origin") + req.Header.Set("Priority", "u=4") + req.Header.Set("TE", "trailers") +} + +// logErrorAndExit logs the error to stderr and exits with a non-zero status code. +func logErrorAndExit(err error) { + fmt.Fprintln(os.Stderr, err) + os.Exit(1) +}