This commit is contained in:
Jeffrey Paul 2024-05-20 04:22:09 -07:00
parent 413bd25d22
commit 3b568e5722
1 changed files with 124 additions and 20 deletions

144
main.go
View File

@ -8,6 +8,92 @@ import (
"os"
)
// Card represents the structure of a card in the API response.
type Card struct {
ID string `json:"id"`
Slug string `json:"slug"`
Name string `json:"name"`
Hotscore int `json:"hotscore"`
Guardian Guardian `json:"guardian"`
Elements []Element `json:"elements"`
Variants []Variant `json:"variants"`
}
// Guardian represents the structure of a guardian in the card details.
type Guardian struct {
ID string `json:"id"`
Type string `json:"type"`
Rarity string `json:"rarity"`
TypeText string `json:"typeText"`
SubType string `json:"subType"`
RulesText string `json:"rulesText"`
Cost int `json:"cost"`
Attack *int `json:"attack"`
Defense *int `json:"defense"`
Life *int `json:"life"`
WaterThreshold int `json:"waterThreshold"`
EarthThreshold int `json:"earthThreshold"`
FireThreshold int `json:"fireThreshold"`
AirThreshold int `json:"airThreshold"`
CardID string `json:"cardId"`
}
// Element represents the structure of an element in the card details.
type Element struct {
ID string `json:"id"`
Name string `json:"name"`
}
// Variant represents the structure of a variant in the card details.
type Variant struct {
ID string `json:"id"`
Slug string `json:"slug"`
Src string `json:"src"`
Finish string `json:"finish"`
Product string `json:"product"`
Artist string `json:"artist"`
FlavorText string `json:"flavorText"`
CardID string `json:"cardId"`
SetCardID string `json:"setCardId"`
SetCard SetCard `json:"setCard"`
}
// SetCard represents the structure of a set card in the card details.
type SetCard struct {
ID string `json:"id"`
Slug string `json:"slug"`
SetID string `json:"setId"`
CardID string `json:"cardId"`
Meta MetaData `json:"meta"`
SetDetails SetDetails `json:"set"`
}
// MetaData represents the structure of meta data in the set card details.
type MetaData struct {
ID string `json:"id"`
Type string `json:"type"`
Rarity string `json:"rarity"`
TypeText string `json:"typeText"`
SubType string `json:"subType"`
RulesText string `json:"rulesText"`
Cost int `json:"cost"`
Attack *int `json:"attack"`
Defense *int `json:"defense"`
Life *int `json:"life"`
WaterThreshold int `json:"waterThreshold"`
EarthThreshold int `json:"earthThreshold"`
FireThreshold int `json:"fireThreshold"`
AirThreshold int `json:"airThreshold"`
SetCardID string `json:"setCardId"`
}
// SetDetails represents the structure of set details in the set card.
type SetDetails struct {
ID string `json:"id"`
Name string `json:"name"`
ReleaseDate string `json:"releaseDate"`
}
// JSONPayload represents the structure of the nested JSON for the API request.
type JSONPayload struct {
Query string `json:"query"`
@ -28,10 +114,8 @@ type Input struct {
} `json:"0"`
}
func main() {
// Parameterize the Limit and Cursor values
limit := 35
cursor := 35
// fetchCards fetches cards from the API using the provided limit and cursor.
func fetchCards(limit, cursor int) ([]*Card, error) {
set := "bet"
// Define the input data structure
@ -56,25 +140,19 @@ func main() {
// 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))
return nil, fmt.Errorf("failed to marshal input data: %w", err)
}
// URL encode the JSON data
urlEncodedInput := url.QueryEscape(string(jsonData))
// Print the URL-encoded JSON input for debugging
fmt.Fprintln(os.Stderr, "URL-encoded JSON input:", urlEncodedInput)
// Construct the full URL
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)
// Create a new HTTP request
req, err := http.NewRequest("GET", apiURL, nil)
if err != nil {
logErrorAndExit(fmt.Errorf("failed to create request: %w", err))
return nil, fmt.Errorf("failed to create request: %w", err)
}
// Set essential headers
@ -84,28 +162,54 @@ func main() {
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
logErrorAndExit(fmt.Errorf("failed to perform request: %w", err))
return nil, 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))
return nil, 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)
// Read and decode the JSON response
var result []struct {
Result struct {
Data struct {
JSON struct {
Cards []*Card `json:"cards"`
} `json:"json"`
} `json:"data"`
} `json:"result"`
}
err = json.NewDecoder(resp.Body).Decode(&result)
if err != nil {
logErrorAndExit(fmt.Errorf("failed to decode response body: %w", err))
return nil, fmt.Errorf("failed to decode response body: %w", err)
}
prettyData, err := json.MarshalIndent(prettyJSON, "", " ")
// Extract cards from the result
if len(result) > 0 {
return result[0].Result.Data.JSON.Cards, nil
}
return nil, nil
}
func main() {
// Example usage
limit := 35
cursor := 35
cards, err := fetchCards(limit, cursor)
if err != nil {
logErrorAndExit(err)
}
// Output the cards as pretty JSON
prettyData, err := json.MarshalIndent(cards, "", " ")
if err != nil {
logErrorAndExit(fmt.Errorf("failed to marshal pretty JSON: %w", err))
}
// Output the pretty JSON to stdout
fmt.Println(string(prettyData))
}