scrcards/main.go

125 lines
3.3 KiB
Go
Raw Normal View History

2024-05-20 10:51:53 +00:00
package main
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"os"
)
2024-05-20 11:16:17 +00:00
// JSONPayload represents the structure of the nested JSON for the API request.
type JSONPayload struct {
2024-05-20 10:51:53 +00:00
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"`
2024-05-20 11:16:17 +00:00
Cursor int `json:"cursor"`
2024-05-20 10:51:53 +00:00
Direction string `json:"direction"`
}
// Input represents the top-level structure of the input JSON for the API request.
type Input struct {
2024-05-20 11:16:17 +00:00
Part0 struct {
JSON JSONPayload `json:"json"`
} `json:"0"`
2024-05-20 10:51:53 +00:00
}
func main() {
2024-05-20 11:16:17 +00:00
// Parameterize the Limit and Cursor values
2024-05-20 10:51:53 +00:00
limit := 35
2024-05-20 11:16:17 +00:00
cursor := 35
set := "bet"
2024-05-20 10:51:53 +00:00
// Define the input data structure
input := Input{
2024-05-20 11:16:17 +00:00
Part0: struct {
JSON JSONPayload `json:"json"`
}{
JSON: JSONPayload{
Query: "",
Sort: "relevance",
Set: set,
Filters: []interface{}{},
Limit: limit,
VariantLimit: false,
CollectionLimit: false,
Cursor: cursor,
Direction: "forward",
},
2024-05-20 10:51:53 +00:00
},
}
// 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))
2024-05-20 11:16:17 +00:00
// Print the URL-encoded JSON input for debugging
2024-05-20 11:01:01 +00:00
fmt.Fprintln(os.Stderr, "URL-encoded JSON input:", urlEncodedInput)
2024-05-20 10:51:53 +00:00
// Construct the full URL
2024-05-20 11:16:17 +00:00
apiURL := fmt.Sprintf("https://curiosa.io/api/trpc/card.search?batch=1&input=%s", urlEncodedInput)
// Print the full API URL for debugging
fmt.Fprintln(os.Stderr, "Full API URL:", apiURL)
2024-05-20 10:51:53 +00:00
// Create a new HTTP request
req, err := http.NewRequest("GET", apiURL, nil)
if err != nil {
logErrorAndExit(fmt.Errorf("failed to create request: %w", err))
}
2024-05-20 11:16:17 +00:00
// Set essential headers
2024-05-20 10:51:53 +00:00
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", "*/*")
2024-05-20 11:16:17 +00:00
req.Header.Set("content-type", "application/json")
2024-05-20 10:51:53 +00:00
}
// 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)
}
2024-05-20 11:01:01 +00:00