This commit is contained in:
Jeffrey Paul 2024-05-20 04:58:38 -07:00
parent 84e26c78ce
commit 4553d0f6da
1 changed files with 37 additions and 23 deletions

42
main.go
View File

@ -116,10 +116,8 @@ type Input struct {
} `json:"0"` } `json:"0"`
} }
// fetchCards fetches cards from the API using the provided limit and cursor. // fetchCards fetches cards from the API using the provided limit, cursor, and set type.
func fetchCards(limit, cursor int) ([]*Card, error) { func fetchCards(limit, cursor int, setType string) ([]*Card, error) {
set := "bet"
// Define the input data structure // Define the input data structure
input := Input{ input := Input{
Part0: struct { Part0: struct {
@ -128,7 +126,7 @@ func fetchCards(limit, cursor int) ([]*Card, error) {
JSON: JSONPayload{ JSON: JSONPayload{
Query: "", Query: "",
Sort: "relevance", Sort: "relevance",
Set: set, Set: setType,
Filters: []interface{}{}, Filters: []interface{}{},
Limit: limit, Limit: limit,
VariantLimit: false, 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. // 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 var allCards []*Card
cursor := 0 cursor := 0
for { for {
cards, err := fetchCards(limit, cursor) cards, err := fetchCards(limit, cursor, setType)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -229,30 +227,47 @@ func fetchAllCards(limit int) ([]*Card, error) {
func main() { func main() {
const limit = 100 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 { if err != nil {
logErrorAndExit(err) logErrorAndExit(err)
} }
// Write all cards to cards.json in the current working directory // Write all cards to cards.json in the data directory
writeCardsToFile("beta_cards.json", allCards) filename := fmt.Sprintf("%s/%s_cards.json", dataDir, filePrefix)
writeCardsToFile(filename, allCards)
// Group cards by element and write to files // Group cards by element and write to files
groupedByElement := groupCardsByElement(allCards) groupedByElement := groupCardsByElement(allCards)
for element, cards := range groupedByElement { 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) writeCardsToFile(filename, cards)
} }
// Group cards by element, rarity, and type and write to files // Group cards by element, rarity, and type and write to files
groupedByElementRarityType := groupCardsByElementRarityType(allCards) groupedByElementRarityType := groupCardsByElementRarityType(allCards)
for key, cards := range groupedByElementRarityType { 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) 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. // setHeaders sets the necessary headers for the HTTP request.
@ -312,4 +327,3 @@ func groupCardsByElementRarityType(cards []*Card) map[string][]*Card {
return grouped return grouped
} }