Import sets into database

This commit is contained in:
The Magician 2024-05-20 17:17:45 +01:00
parent 3f9d3a9dd6
commit 75b425c849
4 changed files with 99 additions and 54 deletions

View File

@ -0,0 +1,25 @@
package main
import (
"log"
"sevenkeys/database"
"sevenkeys/database/operations"
"sevenkeys/scryfall/methods"
)
func check(err error) {
if err != nil {
log.Fatal(err)
}
}
func main() {
sets, err := methods.GetSets()
check(err)
db := database.GetDatabaseFromConfig("config.json")
err = operations.InsertOrUpdateSets(db, sets)
check(err)
// Download icon files to cache/ directory
}

View File

@ -2,64 +2,15 @@ package operations
import ( import (
"database/sql" "database/sql"
"errors"
"log"
"sevenkeys/scryfall/types" "sevenkeys/scryfall/types"
"time" "time"
) )
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"])
}
}
func InsertOrUpdateCacheTimestampByType(db *sql.DB, cacheType string, stamp time.Time) error { func InsertOrUpdateCacheTimestampByType(db *sql.DB, cacheType string, stamp time.Time) error {
query := `INSERT INTO CacheTimestamps (CacheType, Stamp) VALUES (?, ?) ON DUPLICATE KEY UPDATE Stamp = ?;` query := `INSERT INTO CacheTimestamps (CacheType, Stamp)
VALUES (?, ?)
ON DUPLICATE KEY
UPDATE Stamp = ?;`
insertOrUpdate, err := db.Prepare(query) insertOrUpdate, err := db.Prepare(query)
defer insertOrUpdate.Close() defer insertOrUpdate.Close()
@ -74,3 +25,25 @@ func InsertOrUpdateCacheTimestampByType(db *sql.DB, cacheType string, stamp time
return nil return nil
} }
func InsertOrUpdateSets(db *sql.DB, sets []types.Set) error {
query := `INSERT INTO ExpansionSet (SetCode, Name, CardCount, IconSvgUri)
VALUES (?, ?, ?, ?)
ON DUPLICATE KEY
Update Name = ?, CardCount = ?, IconSvgUri = ?;`
insertOrUpdate, err := db.Prepare(query)
defer insertOrUpdate.Close()
if err != nil {
return err
}
for _, set := range sets {
_, err = insertOrUpdate.Exec(set.Code, set.Name, set.CardCount, set.IconSvgUri, set.Name, set.CardCount, set.IconSvgUri)
if err != nil {
return err
}
}
return nil
}

View File

@ -1,3 +1,36 @@
package methods package methods
func GetSets() {} import (
"encoding/json"
"errors"
"io"
"net/http"
"sevenkeys/scryfall/types"
)
const SETS_API_URL string = "https://api.scryfall.com/sets"
func GetSets() ([]types.Set, error) {
response, err := http.Get(SETS_API_URL)
if err != nil {
return []types.Set{}, nil
}
if response.StatusCode != http.StatusOK {
return []types.Set{}, errors.New("HTTP request failed with code: " + string(response.StatusCode))
}
defer response.Body.Close()
setsBytes, err := io.ReadAll(response.Body)
if err != nil {
return []types.Set{}, err
}
var setList types.SetList
err = json.Unmarshal(setsBytes, &setList)
if err != nil {
return []types.Set{}, err
}
return setList.Data, nil
}

View File

@ -0,0 +1,14 @@
package types
type SetList struct {
Object string `json:"object"`
HasMore bool `json:"has_more"`
Data []Set `json:"data"`
}
type Set struct {
Code string `json:"code"`
Name string `json:"name"`
CardCount int `json:"card_count"`
IconSvgUri string `json:"icon_svg_uri"`
}