latest
This commit is contained in:
parent
84e26c78ce
commit
4553d0f6da
42
main.go
42
main.go
@ -116,10 +116,8 @@ type Input struct {
|
||||
} `json:"0"`
|
||||
}
|
||||
|
||||
// fetchCards fetches cards from the API using the provided limit and cursor.
|
||||
func fetchCards(limit, cursor int) ([]*Card, error) {
|
||||
set := "bet"
|
||||
|
||||
// fetchCards fetches cards from the API using the provided limit, cursor, and set type.
|
||||
func fetchCards(limit, cursor int, setType string) ([]*Card, error) {
|
||||
// Define the input data structure
|
||||
input := Input{
|
||||
Part0: struct {
|
||||
@ -128,7 +126,7 @@ func fetchCards(limit, cursor int) ([]*Card, error) {
|
||||
JSON: JSONPayload{
|
||||
Query: "",
|
||||
Sort: "relevance",
|
||||
Set: set,
|
||||
Set: setType,
|
||||
Filters: []interface{}{},
|
||||
Limit: limit,
|
||||
VariantLimit: false,
|
||||
@ -203,12 +201,12 @@ func fetchCards(limit, cursor int) ([]*Card, error) {
|
||||
}
|
||||
|
||||
// fetchAllCards fetches all cards by making multiple requests with different cursor values.
|
||||
func fetchAllCards(limit int) ([]*Card, error) {
|
||||
func fetchAllCards(limit int, setType string) ([]*Card, error) {
|
||||
var allCards []*Card
|
||||
cursor := 0
|
||||
|
||||
for {
|
||||
cards, err := fetchCards(limit, cursor)
|
||||
cards, err := fetchCards(limit, cursor, setType)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -229,30 +227,47 @@ func fetchAllCards(limit int) ([]*Card, error) {
|
||||
|
||||
func main() {
|
||||
const limit = 100
|
||||
const dataDir = "./data"
|
||||
|
||||
allCards, err := fetchAllCards(limit)
|
||||
// Ensure the data directory exists
|
||||
err := os.MkdirAll(dataDir, os.ModePerm)
|
||||
if err != nil {
|
||||
logErrorAndExit(fmt.Errorf("failed to create data directory: %w", err))
|
||||
}
|
||||
|
||||
// Fetch and save beta cards
|
||||
saveCards("bet", "beta", limit, dataDir)
|
||||
|
||||
// Fetch and save alpha cards
|
||||
saveCards("alp", "alpha", limit, dataDir)
|
||||
}
|
||||
|
||||
// saveCards fetches all cards for the given set type and saves them to files.
|
||||
func saveCards(apiSetType, filePrefix string, limit int, dataDir string) {
|
||||
allCards, err := fetchAllCards(limit, apiSetType)
|
||||
if err != nil {
|
||||
logErrorAndExit(err)
|
||||
}
|
||||
|
||||
// Write all cards to cards.json in the current working directory
|
||||
writeCardsToFile("beta_cards.json", allCards)
|
||||
// Write all cards to cards.json in the data directory
|
||||
filename := fmt.Sprintf("%s/%s_cards.json", dataDir, filePrefix)
|
||||
writeCardsToFile(filename, allCards)
|
||||
|
||||
// Group cards by element and write to files
|
||||
groupedByElement := groupCardsByElement(allCards)
|
||||
for element, cards := range groupedByElement {
|
||||
filename := fmt.Sprintf("beta_%s_cards.json", strings.ToLower(element))
|
||||
filename := fmt.Sprintf("%s/%s_%s_cards.json", dataDir, filePrefix, strings.ToLower(element))
|
||||
writeCardsToFile(filename, cards)
|
||||
}
|
||||
|
||||
// Group cards by element, rarity, and type and write to files
|
||||
groupedByElementRarityType := groupCardsByElementRarityType(allCards)
|
||||
for key, cards := range groupedByElementRarityType {
|
||||
filename := fmt.Sprintf("beta_%s_cards.json", strings.ToLower(key))
|
||||
filename := fmt.Sprintf("%s/%s_%s_cards.json", dataDir, filePrefix, strings.ToLower(key))
|
||||
writeCardsToFile(filename, cards)
|
||||
}
|
||||
|
||||
fmt.Printf("All cards have been written to beta_cards.json. Total cards: %d\n", len(allCards))
|
||||
fmt.Printf("All cards have been written to %s_cards.json. Total cards: %d\n", filePrefix, len(allCards))
|
||||
}
|
||||
|
||||
// setHeaders sets the necessary headers for the HTTP request.
|
||||
@ -312,4 +327,3 @@ func groupCardsByElementRarityType(cards []*Card) map[string][]*Card {
|
||||
return grouped
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user