Get all cards from bulk data endpoint

This commit is contained in:
The Magician 2024-04-24 14:29:38 +01:00
parent d6e558f9e3
commit b93df37bd4
6 changed files with 113 additions and 73 deletions

View File

@ -2,10 +2,13 @@ package main
import ( import (
"fmt" "fmt"
"sevenkeys/database" "sevenkeys/scryfall/methods"
) )
func main() { func main() {
db := database.GetDatabaseFromConfig("config.json") //db := database.GetDatabaseFromConfig("config.json")
fmt.Println(db) allCards := methods.GetAllCards()
fmt.Println(allCards[0])
//cards := scryfall.GetCards()
//database.InsertCards(cards)
} }

View File

@ -0,0 +1,79 @@
package methods
import (
"encoding/json"
"errors"
"io"
"log"
"net/http"
"sevenkeys/scryfall/types"
)
var BULK_DATA_URI = "https://api.scryfall.com/bulk-data"
func GetBulkData() types.BulkDataList {
response, err := http.Get(BULK_DATA_URI)
if err != nil {
log.Fatal(err)
}
if response.StatusCode != http.StatusOK {
log.Fatal(response.StatusCode)
}
defer response.Body.Close()
bulkDataBytes, err := io.ReadAll(response.Body)
if err != nil {
log.Fatal(err)
}
var bulkDataList types.BulkDataList
err = json.Unmarshal(bulkDataBytes, &bulkDataList)
if err != nil {
log.Fatal(err)
}
return bulkDataList
}
func getBulkDownloadUri(bulkType string) (string, error) {
bulkDataList := GetBulkData()
for index := range bulkDataList.Data {
bulkData := bulkDataList.Data[index]
if bulkData.Type == bulkType {
return bulkData.DownloadUri, nil
}
}
return "", errors.New("No bulk data of type " + bulkType + " found.")
}
func GetAllCards() []types.Card {
allCardsUri, err := getBulkDownloadUri("all_cards")
if err != nil {
log.Fatal(err)
}
response, err := http.Get(allCardsUri)
if err != nil {
log.Fatal(err)
}
if response.StatusCode != http.StatusOK {
log.Fatal(response.StatusCode)
}
defer response.Body.Close()
allCardsBytes, err := io.ReadAll(response.Body)
if err != nil {
log.Fatal(err)
}
var allCards []types.Card
err = json.Unmarshal(allCardsBytes, &allCards)
if err != nil {
log.Fatal(err)
}
return allCards
}

View File

@ -0,0 +1,3 @@
package methods
func GetSets() {}

View File

@ -1,65 +0,0 @@
package scryfall
import (
"encoding/json"
"errors"
"io"
"net/http"
)
type Set struct {
Object string `json:"object"`
Id string `json:"id"` // Use UUID package?
Code string `json:"code"`
MtgoCode string `json:"mtgo_code"`
ArenaCode string `json:"arena_code"`
TcgplayerId int `json:"tcgplayer_id"`
Name string `json:"name"`
SetType string `json:"set_type"`
ReleasedAt string `json:"released_at"`
BlockCode string `json:"block_code"`
Block string `json:"block"`
ParentSetCode string `json:"parent_set_code"`
CardCount int `json:"card_count"`
PrintedSize int `json:"printed_size"`
Digital bool `json:"digital"`
FoilOnly bool `json:"foil_only"`
NonfoilOnly bool `json:"nonfoil_only"`
ScryfallUri string `json:"scryfall_uri"`
Uri string `json:"uri"`
IconSvgUri string `json:"icon_svg_uri"`
SearchUri string `json:"search_uri"`
}
type SetList struct {
Object string `json:"object"`
HasMore bool `json:"has_more"`
Data []Set `json:"data"`
}
var SETS_API string = "https://api.scryfall.com/sets"
func GetSets() (SetList, error) {
response, err := http.Get(SETS_API)
if err != nil {
return SetList{}, err
}
defer response.Body.Close()
if response.StatusCode == http.StatusOK {
setsBytes, err := io.ReadAll(response.Body)
if err != nil {
return SetList{}, err
}
var setList SetList
err = json.Unmarshal(setsBytes, &setList)
if err != nil {
return SetList{}, err
}
return setList, nil
}
return SetList{}, errors.New("scryfall: failed to get all sets")
}

View File

@ -0,0 +1,20 @@
package types
type BulkData struct {
Id string `json:"id"`
Uri string `json:"uri"`
Type string `json:"type"`
Name string `json:"name"`
Description string `json:"description"`
DownloadUri string `json:"download_uri"`
UpdatedAt string `json:"updated_at"`
Size int `json:"size"`
ContentType string `json:"content_type"`
ContentEncoding string `json:"content_encoding"`
}
type BulkDataList struct {
Object string `json:"object"`
HasMore bool `json:"has_more"`
Data []BulkData `json:"data"`
}

View File

@ -1,6 +1,6 @@
package scryfall package types
type Colors []rune type Colors []string
type RelatedCard struct { type RelatedCard struct {
Id string `json:"id"` Id string `json:"id"`
@ -92,7 +92,7 @@ type Card struct {
FlavorName string `json:"flavor_name"` FlavorName string `json:"flavor_name"`
FlavorText string `json:"flavor_text"` FlavorText string `json:"flavor_text"`
FrameEffects []string `json:"frame_effects"` FrameEffects []string `json:"frame_effects"`
Frame []string `json:"frame"` Frame string `json:"frame"`
FullArt bool `json:"full_art"` FullArt bool `json:"full_art"`
Games []string `json:"games"` Games []string `json:"games"`
HighresImage bool `json:"highres_image"` HighresImage bool `json:"highres_image"`
@ -105,7 +105,7 @@ type Card struct {
PrintedText string `json:"printed_text"` PrintedText string `json:"printed_text"`
PrintedTypeLine string `json:"printed_type_line"` PrintedTypeLine string `json:"printed_type_line"`
Promo bool `json:"promo"` Promo bool `json:"promo"`
PromoTypes bool `json:"promo_types"` PromoTypes []string `json:"promo_types"`
PurchaseUris interface{} `json:"purchase_uris"` // TODO: Find out shape of object PurchaseUris interface{} `json:"purchase_uris"` // TODO: Find out shape of object
Rarity string `json:"rarity"` Rarity string `json:"rarity"`
RelatedUris interface{} `json:"related_uris"` // TODO: Find out shape of object RelatedUris interface{} `json:"related_uris"` // TODO: Find out shape of object
@ -118,7 +118,7 @@ type Card struct {
SetUri string `json:"set_uri"` SetUri string `json:"set_uri"`
Set string `json:"set"` Set string `json:"set"`
SetId string `json:"set_id"` SetId string `json:"set_id"`
StorySpotlight string `json:"story_spotlight"` StorySpotlight bool `json:"story_spotlight"`
Textless bool `json:"textless"` Textless bool `json:"textless"`
Variation bool `json:"variation"` Variation bool `json:"variation"`
VariationOf string `json:"variation_of"` VariationOf string `json:"variation_of"`