Download card data from Scryfall

This commit is contained in:
The Magician 2024-04-28 15:13:47 +01:00
parent b491315cd0
commit 9c566e997f
9 changed files with 119 additions and 49 deletions

View File

@ -3,5 +3,5 @@ removedb:
createdb: createdb:
mysql --user=root --password=$(shell pass show sevenkeys/mysql) <sql/createdb.sql mysql --user=root --password=$(shell pass show sevenkeys/mysql) <sql/createdb.sql
importdata: createdb importcards: createdb
go run cmd/importdata/main.go go run cmd/importcards/main.go

View File

@ -0,0 +1,13 @@
package main
import (
"sevenkeys/database"
"sevenkeys/database/operations"
"sevenkeys/scryfall/methods"
)
func main() {
db := database.GetDatabaseFromConfig("config.json")
allCards := methods.GetAllCards()
operations.InsertCards(db, allCards)
}

View File

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

View File

@ -1,9 +1,8 @@
package database package entities
type CardPrinting struct { type CardPrinting struct {
Id int Id int
GamepieceId int GamepieceId int
SetId [6]rune SetId string
FrontFaceImageUrl [2048]rune ImageUrl string
BackFaceImageUrl [2048]rune
} }

View File

@ -1,4 +1,4 @@
package database package entities
type Gamepiece struct { type Gamepiece struct {
Id int Id int

View File

@ -0,0 +1,58 @@
package operations
import (
"database/sql"
"errors"
"log"
"sevenkeys/scryfall/types"
)
var ErrGamepieceExists error = errors.New("Gamepiece already exists in database")
func insertGamepiece(db *sql.DB, cardName string) (int, error) {
gamepieceInsertQuery := "INSERT INTO Gamepiece (Name) VALUES (?);"
gamepieceInsert, err := db.Prepare(gamepieceInsertQuery)
defer gamepieceInsert.Close()
if err != nil {
log.Fatal(err)
}
existingGamepiece, err := GetGamepieceByName(db, cardName)
if err != sql.ErrNoRows {
return existingGamepiece.Id, ErrGamepieceExists
}
result, err := gamepieceInsert.Exec(cardName)
if err != nil {
log.Fatal(err)
}
gamepieceId, err := result.LastInsertId()
if err != nil {
log.Fatal(err)
}
return int(gamepieceId), nil
}
func insertCardPrinting(db *sql.DB, gamepieceId int, setCode string, imageUrl string) {
cardPrintingInsertQuery := "INSERT INTO CardPrinting (GamepieceId, SetCode, ImageUrl) VALUES (?, ?, ?);"
cardPrintingInsert, err := db.Prepare(cardPrintingInsertQuery)
defer cardPrintingInsert.Close()
if err != nil {
log.Fatal(err)
}
_, err = cardPrintingInsert.Exec(gamepieceId, setCode, imageUrl)
if err != nil {
log.Fatal(err)
}
}
func InsertCards(db *sql.DB, cards []types.Card) {
for index := range cards {
card := cards[index]
gamepieceId, _ := insertGamepiece(db, card.Name)
insertCardPrinting(db, gamepieceId, card.Set, card.ImageUris["png"])
}
}

View File

@ -0,0 +1,15 @@
package operations
import (
"database/sql"
"sevenkeys/database/entities"
)
func GetGamepieceByName(db *sql.DB, name string) (entities.Gamepiece, error) {
var gamepiece entities.Gamepiece
query := "SELECT Id, Name FROM Gamepiece WHERE Name = ?;"
err := db.QueryRow(query, name).Scan(&gamepiece.Id, &gamepiece.Name)
return gamepiece, err
}

View File

@ -20,7 +20,7 @@ type CardFace struct {
Defense string `json:"defense"` Defense string `json:"defense"`
FlavorText string `json:"flavor_text"` FlavorText string `json:"flavor_text"`
IllustrationId string `json:"illustration_id"` IllustrationId string `json:"illustration_id"`
ImageUris interface{} `json:"image_uris"` // TODO: Find out the structure of this object ImageUris map[string]string `json:"image_uris"` // TODO: Find out the structure of this object
Layout string `json:"layout"` Layout string `json:"layout"`
Loyalty string `json:"loyalty"` Loyalty string `json:"loyalty"`
ManaCost string `json:"mana_cost"` ManaCost string `json:"mana_cost"`
@ -98,7 +98,7 @@ type Card struct {
HighresImage bool `json:"highres_image"` HighresImage bool `json:"highres_image"`
IllustrationId string `json:"illustration_id"` IllustrationId string `json:"illustration_id"`
ImageStatus string `json:"image_status"` ImageStatus string `json:"image_status"`
ImageUris interface{} `json:"image_uris"` // TODO: Find out shape of object ImageUris map[string]string `json:"image_uris"` // TODO: Find out shape of object
Oversized bool `json:"oversized"` Oversized bool `json:"oversized"`
Prices map[string]string `json:"prices"` Prices map[string]string `json:"prices"`
PrintedName string `json:"printed_name"` PrintedName string `json:"printed_name"`

View File

@ -11,7 +11,6 @@ CREATE TABLE IF NOT EXISTS CardPrinting (
Id INT AUTO_INCREMENT PRIMARY KEY, Id INT AUTO_INCREMENT PRIMARY KEY,
GamepieceId INT NOT NULL, GamepieceId INT NOT NULL,
FOREIGN KEY (GamepieceId) REFERENCES Gamepiece(Id), FOREIGN KEY (GamepieceId) REFERENCES Gamepiece(Id),
SetId VARCHAR(6) NOT NULL, SetCode VARCHAR(6) NOT NULL,
FrontFaceImageUrl VARCHAR(2048) NOT NULL, ImageUrl VARCHAR(2048) NOT NULL
BackFaceImageUrl VARCHAR(2048) NOT NULL
); );