Download card data from Scryfall
This commit is contained in:
parent
b491315cd0
commit
9c566e997f
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
}
|
|
@ -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)
|
||||
}
|
|
@ -1,9 +1,8 @@
|
|||
package database
|
||||
package entities
|
||||
|
||||
type CardPrinting struct {
|
||||
Id int
|
||||
GamepieceId int
|
||||
SetId [6]rune
|
||||
FrontFaceImageUrl [2048]rune
|
||||
BackFaceImageUrl [2048]rune
|
||||
SetId string
|
||||
ImageUrl string
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package database
|
||||
package entities
|
||||
|
||||
type Gamepiece struct {
|
||||
Id int
|
||||
|
|
|
@ -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"])
|
||||
}
|
||||
}
|
|
@ -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
|
||||
}
|
|
@ -20,7 +20,7 @@ type CardFace struct {
|
|||
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
|
||||
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"`
|
||||
|
@ -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"`
|
||||
|
|
|
@ -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
|
||||
);
|
||||
|
|
Loading…
Reference in New Issue