From 4553d0f6daa9c9a3e33ce73803155f081286b863 Mon Sep 17 00:00:00 2001 From: sneak Date: Mon, 20 May 2024 04:58:38 -0700 Subject: [PATCH] latest --- main.go | 60 +++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 37 insertions(+), 23 deletions(-) diff --git a/main.go b/main.go index f30d306..7bd5672 100644 --- a/main.go +++ b/main.go @@ -48,15 +48,15 @@ type Element struct { // 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"` + 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"` } @@ -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 } -