scrcards/main.go

252 lines
6.9 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:22:09 +00:00
// Card represents the structure of a card in the API response.
type Card struct {
2024-05-20 11:27:02 +00:00
ID string `json:"id"`
Slug string `json:"slug"`
Name string `json:"name"`
Hotscore int `json:"hotscore"`
Guardian Guardian `json:"guardian"`
2024-05-20 11:22:09 +00:00
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 {
2024-05-20 11:27:02 +00:00
ID string `json:"id"`
Slug string `json:"slug"`
SetID string `json:"setId"`
CardID string `json:"cardId"`
Meta MetaData `json:"meta"`
2024-05-20 11:22:09 +00:00
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 {
2024-05-20 11:27:02 +00:00
ID string `json:"id"`
Name string `json:"name"`
2024-05-20 11:22:09 +00:00
ReleaseDate string `json:"releaseDate"`
}
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
}
2024-05-20 11:22:09 +00:00
// fetchCards fetches cards from the API using the provided limit and cursor.
func fetchCards(limit, cursor int) ([]*Card, error) {
2024-05-20 11:16:17 +00:00
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 {
2024-05-20 11:22:09 +00:00
return nil, fmt.Errorf("failed to marshal input data: %w", err)
2024-05-20 10:51:53 +00:00
}
// URL encode the JSON data
urlEncodedInput := url.QueryEscape(string(jsonData))
// 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)
2024-05-20 10:51:53 +00:00
// Create a new HTTP request
req, err := http.NewRequest("GET", apiURL, nil)
if err != nil {
2024-05-20 11:22:09 +00:00
return nil, fmt.Errorf("failed to create request: %w", err)
2024-05-20 10:51:53 +00:00
}
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 {
2024-05-20 11:22:09 +00:00
return nil, fmt.Errorf("failed to perform request: %w", err)
2024-05-20 10:51:53 +00:00
}
defer resp.Body.Close()
// Check for non-200 status codes
if resp.StatusCode != http.StatusOK {
2024-05-20 11:22:09 +00:00
return nil, fmt.Errorf("non-200 response: %s", resp.Status)
}
// 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 {
return nil, fmt.Errorf("failed to decode response body: %w", err)
2024-05-20 10:51:53 +00:00
}
2024-05-20 11:22:09 +00:00
// Extract cards from the result
if len(result) > 0 {
return result[0].Result.Data.JSON.Cards, nil
}
return nil, nil
}
func main() {
2024-05-20 11:27:02 +00:00
const limit = 100
var allCards []*Card
cursor := 0
for {
cards, err := fetchCards(limit, cursor)
if err != nil {
logErrorAndExit(err)
}
allCards = append(allCards, cards...)
2024-05-20 11:22:09 +00:00
2024-05-20 11:27:02 +00:00
// If the number of returned cards is less than the limit, we've fetched all cards.
if len(cards) < limit {
break
}
// Update cursor for the next batch
cursor += limit
}
// Write all cards to cards.json in the current working directory
file, err := os.Create("cards.json")
2024-05-20 10:51:53 +00:00
if err != nil {
2024-05-20 11:27:02 +00:00
logErrorAndExit(fmt.Errorf("failed to create cards.json: %w", err))
2024-05-20 10:51:53 +00:00
}
2024-05-20 11:27:02 +00:00
defer file.Close()
2024-05-20 10:51:53 +00:00
2024-05-20 11:27:02 +00:00
prettyData, err := json.MarshalIndent(allCards, "", " ")
2024-05-20 10:51:53 +00:00
if err != nil {
logErrorAndExit(fmt.Errorf("failed to marshal pretty JSON: %w", err))
}
2024-05-20 11:27:02 +00:00
_, err = file.Write(prettyData)
if err != nil {
logErrorAndExit(fmt.Errorf("failed to write to cards.json: %w", err))
}
fmt.Println("All cards have been written to cards.json")
2024-05-20 10:51:53 +00:00
}
// 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