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:
mysql --user=root --password=$(shell pass show sevenkeys/mysql) <sql/createdb.sql
importdata: createdb
go run cmd/importdata/main.go
importcards: createdb
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 {
Id int
GamepieceId int
SetId [6]rune
FrontFaceImageUrl [2048]rune
BackFaceImageUrl [2048]rune
Id int
GamepieceId int
SetId string
ImageUrl string
}

View File

@ -1,4 +1,4 @@
package database
package entities
type Gamepiece struct {
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

@ -12,28 +12,28 @@ type RelatedCard struct {
}
type CardFace struct {
Artist string `json:"artist"`
ArtistId string `json:"artist_id"`
ManaValue float32 `json:"cmc"`
ColorIndicator Colors `json:"color_indicator"`
Colors Colors `json:"colors"`
Defense string `json:"defense"`
FlavorText string `json:"flavor_text"`
IllustrationId string `json:"illustration_id"`
ImageUris interface{} `json:"image_uris"` // TODO: Find out the structure of this object
Layout string `json:"layout"`
Loyalty string `json:"loyalty"`
ManaCost string `json:"mana_cost"`
Name string `json:"name"`
Object string `json:"object"`
OracleId string `json:"oracle_id"`
Power string `json:"power"`
PrintedName string `json:"printed_name"`
PrintedText string `json:"printed_text"`
PrintedTypeLine string `json:"printed_type_line"`
Toughness string `json:"toughness"`
TypeLine string `json:"type_line"`
Watermark string `json:"watermark"`
Artist string `json:"artist"`
ArtistId string `json:"artist_id"`
ManaValue float32 `json:"cmc"`
ColorIndicator Colors `json:"color_indicator"`
Colors Colors `json:"colors"`
Defense string `json:"defense"`
FlavorText string `json:"flavor_text"`
IllustrationId string `json:"illustration_id"`
ImageUris map[string]string `json:"image_uris"` // TODO: Find out the structure of this object
Layout string `json:"layout"`
Loyalty string `json:"loyalty"`
ManaCost string `json:"mana_cost"`
Name string `json:"name"`
Object string `json:"object"`
OracleId string `json:"oracle_id"`
Power string `json:"power"`
PrintedName string `json:"printed_name"`
PrintedText string `json:"printed_text"`
PrintedTypeLine string `json:"printed_type_line"`
Toughness string `json:"toughness"`
TypeLine string `json:"type_line"`
Watermark string `json:"watermark"`
}
type Card struct {
@ -98,7 +98,7 @@ type Card struct {
HighresImage bool `json:"highres_image"`
IllustrationId string `json:"illustration_id"`
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"`
Prices map[string]string `json:"prices"`
PrintedName string `json:"printed_name"`

View File

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