TheMathemagicians/sevenkeys/logic/update.go

204 lines
4.3 KiB
Go
Raw Normal View History

2024-05-28 14:29:13 +00:00
package logic
import (
"database/sql"
2024-05-29 19:55:38 +00:00
"encoding/json"
2024-05-28 14:38:30 +00:00
"fmt"
2024-05-29 19:55:38 +00:00
"io"
"io/ioutil"
"log"
"net/http"
"os"
2024-05-28 14:29:13 +00:00
"sevenkeys/database"
"sevenkeys/logic/scryfall"
2024-05-28 14:38:30 +00:00
"strings"
2024-05-28 14:29:13 +00:00
"time"
2024-05-29 19:55:38 +00:00
sqlerr "github.com/go-mysql/errors"
2024-05-28 14:29:13 +00:00
)
2024-05-29 19:55:38 +00:00
const GAME_PAPER = "paper"
func CheckForUpdates(db *sql.DB, bulkData scryfall.BulkData) (bool, error) {
// TODO: We also want to update if:
// - there is no cached version of the all-cards.json file or it is empty
// - The set icon files are missing
2024-05-28 14:29:13 +00:00
cachedFileTimestampStr, err := database.GetCacheTimestampByType(db, database.CacheTypeAllCardsBulkData)
if err == sql.ErrNoRows {
return true, nil
} else if err != nil {
return false, err
}
cachedFileTimestamp, err := time.Parse("2006-01-02 15:04:05", cachedFileTimestampStr)
2024-05-29 19:55:38 +00:00
return bulkData.UpdatedAtTime.After(cachedFileTimestamp), nil
}
func ConfirmUpdate() bool {
fmt.Print("Run update? (y/N) ")
var response string
fmt.Scan(&response)
return strings.ToUpper(response) == "Y"
}
func UpdateSets(db *sql.DB) error {
sets, err := scryfall.GetAllSets()
2024-05-28 14:29:13 +00:00
if err != nil {
2024-05-29 19:55:38 +00:00
return err
}
for _, set := range sets {
// We're only interested in paper cards, so skip importing
// any sets that were only released in a video game
if set.Digital {
continue
}
err := database.InsertSet(db, set)
// If we already have this set in the database, then we can just skip it
if ok, insertErr := sqlerr.Error(err); ok {
if insertErr == sqlerr.ErrDupeKey {
continue
}
}
if err != nil {
return err
}
response, err := http.Get(set.IconSvgUri)
defer response.Body.Close()
if err != nil {
return nil
}
iconFilename := SET_ICON_CACHE_DIR + set.Code + SET_ICON_FILE_EXTENSION
iconFile, err := os.Create(iconFilename)
if err != nil {
return err
}
io.Copy(iconFile, response.Body)
2024-05-28 14:29:13 +00:00
}
2024-05-29 19:55:38 +00:00
fmt.Println("Sets updated.")
return nil
}
func cacheBulkCardsFile(db *sql.DB, bulkData scryfall.BulkData) error {
bulkCardsResponse, err := http.Get(bulkData.DownloadUri)
defer bulkCardsResponse.Body.Close()
2024-05-28 14:29:13 +00:00
if err != nil {
2024-05-29 19:55:38 +00:00
return err
}
cacheFile, err := os.Create(ALL_CARDS_CACHE_FILENAME)
defer cacheFile.Close()
if err != nil {
return err
}
io.Copy(cacheFile, bulkCardsResponse.Body)
err = database.InsertOrUpdateCacheTimestampByType(db, database.CacheTypeAllCardsBulkData, bulkData.UpdatedAtTime)
if err != nil {
return err
2024-05-28 14:29:13 +00:00
}
2024-05-29 19:55:38 +00:00
return nil
2024-05-28 14:29:13 +00:00
}
2024-05-28 14:38:30 +00:00
2024-05-29 19:55:38 +00:00
func isPaper(card scryfall.Card) bool {
var paper bool = false
2024-05-28 14:38:30 +00:00
2024-05-29 19:55:38 +00:00
for _, game := range card.Games {
if game == GAME_PAPER {
paper = true
}
}
return paper
}
func getCardPrintings(card scryfall.Card) []database.CardPrinting {
var printings []database.CardPrinting
if card.Foil {
printings = append(printings, database.CardPrinting{
Name: card.Name,
SetCode: card.Set,
IsFoil: true,
IsPromo: card.Promo,
CollectorNumber: card.CollectorNumber,
Language: card.Language,
})
}
if card.NonFoil {
printings = append(printings, database.CardPrinting{
Name: card.Name,
SetCode: card.Set,
IsFoil: false,
IsPromo: card.Promo,
CollectorNumber: card.CollectorNumber,
Language: card.Language,
})
}
return printings
}
func UpdateCards(db *sql.DB, bulkData scryfall.BulkData) error {
log.Println("Caching bulk cards file")
err := cacheBulkCardsFile(db, bulkData)
if err != nil {
return err
}
log.Println("Cached bulk cards file")
log.Println("Reading cached file")
cardsBytes, err := ioutil.ReadFile(ALL_CARDS_CACHE_FILENAME)
if err != nil {
return err
}
log.Println("Read cached file")
log.Println("Unmarshaling JSON")
var cards []scryfall.Card
err = json.Unmarshal(cardsBytes, &cards)
if err != nil {
return err
}
log.Println("Unmarshaled JSON")
log.Println("INSERTing cards")
for _, card := range cards {
// We're only interested in paper cards, so skip cards or printings of a card which
// aren't available in paper
if !isPaper(card) {
continue
}
cardPrintings := getCardPrintings(card)
for _, cardPrinting := range cardPrintings {
err := database.InsertCardPrinting(db, cardPrinting)
// If we already have this card in the database, then we can just skip it
if ok, insertErr := sqlerr.Error(err); ok {
if insertErr == sqlerr.ErrDupeKey {
continue
}
}
}
if err != nil {
return err
}
}
log.Println("INSERTed cards")
return nil
2024-05-28 14:38:30 +00:00
}