woo
This commit is contained in:
parent
3b568e5722
commit
694a93b788
File diff suppressed because it is too large
Load Diff
65
main.go
65
main.go
|
@ -10,11 +10,11 @@ import (
|
|||
|
||||
// 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"`
|
||||
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"`
|
||||
}
|
||||
|
@ -60,11 +60,11 @@ type Variant struct {
|
|||
|
||||
// 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"`
|
||||
ID string `json:"id"`
|
||||
Slug string `json:"slug"`
|
||||
SetID string `json:"setId"`
|
||||
CardID string `json:"cardId"`
|
||||
Meta MetaData `json:"meta"`
|
||||
SetDetails SetDetails `json:"set"`
|
||||
}
|
||||
|
||||
|
@ -89,8 +89,8 @@ type MetaData struct {
|
|||
|
||||
// SetDetails represents the structure of set details in the set card.
|
||||
type SetDetails struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
ReleaseDate string `json:"releaseDate"`
|
||||
}
|
||||
|
||||
|
@ -195,22 +195,45 @@ func fetchCards(limit, cursor int) ([]*Card, error) {
|
|||
}
|
||||
|
||||
func main() {
|
||||
// Example usage
|
||||
limit := 35
|
||||
cursor := 35
|
||||
const limit = 100
|
||||
var allCards []*Card
|
||||
cursor := 0
|
||||
|
||||
cards, err := fetchCards(limit, cursor)
|
||||
if err != nil {
|
||||
logErrorAndExit(err)
|
||||
for {
|
||||
cards, err := fetchCards(limit, cursor)
|
||||
if err != nil {
|
||||
logErrorAndExit(err)
|
||||
}
|
||||
|
||||
allCards = append(allCards, cards...)
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
// Output the cards as pretty JSON
|
||||
prettyData, err := json.MarshalIndent(cards, "", " ")
|
||||
// Write all cards to cards.json in the current working directory
|
||||
file, err := os.Create("cards.json")
|
||||
if err != nil {
|
||||
logErrorAndExit(fmt.Errorf("failed to create cards.json: %w", err))
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
prettyData, err := json.MarshalIndent(allCards, "", " ")
|
||||
if err != nil {
|
||||
logErrorAndExit(fmt.Errorf("failed to marshal pretty JSON: %w", err))
|
||||
}
|
||||
|
||||
fmt.Println(string(prettyData))
|
||||
_, 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")
|
||||
}
|
||||
|
||||
// setHeaders sets the necessary headers for the HTTP request.
|
||||
|
|
Loading…
Reference in New Issue