2024-04-04 13:55:12 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-05-21 11:33:14 +00:00
|
|
|
"database/sql"
|
|
|
|
"io"
|
2024-05-21 11:17:50 +00:00
|
|
|
"log"
|
2024-05-21 11:33:14 +00:00
|
|
|
"net/http"
|
2024-05-21 11:28:08 +00:00
|
|
|
"os"
|
2024-05-21 11:24:16 +00:00
|
|
|
"sevenkeys/database"
|
2024-05-21 11:33:14 +00:00
|
|
|
"sevenkeys/database/operations"
|
2024-05-21 11:24:16 +00:00
|
|
|
"sevenkeys/scryfall/methods"
|
2024-05-21 11:33:14 +00:00
|
|
|
"sevenkeys/scryfall/types"
|
2024-04-04 13:55:12 +00:00
|
|
|
)
|
|
|
|
|
2024-05-21 11:19:01 +00:00
|
|
|
const CACHE_DIR string = "cache"
|
|
|
|
|
|
|
|
const SET_ICON_CACHE_DIR string = CACHE_DIR + "/seticons/"
|
|
|
|
const SET_ICON_FILE_EXTENSION string = ".svg"
|
|
|
|
|
|
|
|
const ALL_CARDS_CACHE_FILENAME = CACHE_DIR + "/all-cards.json"
|
|
|
|
|
2024-05-21 11:17:50 +00:00
|
|
|
func check(err error) {
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-21 11:28:08 +00:00
|
|
|
func createCacheDirectories() error {
|
|
|
|
err := os.Mkdir(CACHE_DIR, os.ModePerm)
|
|
|
|
if err != nil && !os.IsExist(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = os.Mkdir(SET_ICON_CACHE_DIR, os.ModePerm)
|
|
|
|
if err != nil && !os.IsExist(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-05-21 11:33:14 +00:00
|
|
|
func importSets(db *sql.DB, sets []types.Set) error {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Println("Importing " + set.Code)
|
2024-05-21 12:25:08 +00:00
|
|
|
err := operations.InsertOrUpdateSet(db, set)
|
2024-05-21 11:33:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Println("Downloading logo for " + set.Code)
|
|
|
|
response, err := http.Get(set.IconSvgUri)
|
|
|
|
defer response.Body.Close()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
log.Println("Finished importing " + set.Code)
|
|
|
|
}
|
2024-05-21 12:25:08 +00:00
|
|
|
|
|
|
|
return nil
|
2024-05-21 11:33:14 +00:00
|
|
|
}
|
|
|
|
|
2024-04-04 13:55:12 +00:00
|
|
|
func main() {
|
2024-05-21 11:24:16 +00:00
|
|
|
log.Println("Connecting to database...")
|
|
|
|
db := database.GetDatabaseFromConfig("config.json")
|
|
|
|
log.Println("Connected.")
|
|
|
|
|
2024-05-21 11:28:08 +00:00
|
|
|
log.Println("Creating cache directories...")
|
2024-05-21 12:25:08 +00:00
|
|
|
err := createCacheDirectories()
|
2024-05-21 11:28:08 +00:00
|
|
|
check(err)
|
|
|
|
log.Println("Created cache directories.")
|
2024-05-21 11:24:16 +00:00
|
|
|
|
2024-05-21 11:33:14 +00:00
|
|
|
log.Println("Downloading set data from Scryfall...")
|
|
|
|
sets, err := methods.GetSets()
|
|
|
|
check(err)
|
|
|
|
log.Println("Downloaded set data.")
|
|
|
|
|
|
|
|
log.Println("Importing set data...")
|
2024-05-21 12:25:08 +00:00
|
|
|
err = importSets(db, sets)
|
2024-05-21 11:33:14 +00:00
|
|
|
check(err)
|
|
|
|
log.Println("Imported sets.")
|
2024-05-21 11:24:16 +00:00
|
|
|
|
|
|
|
// Check whether card data has been updated since last download
|
|
|
|
// If yes, redownload and recache the bulk data
|
|
|
|
|
|
|
|
// Unmarshal cached file into Golang struct
|
|
|
|
// Import cards into database
|
2024-04-04 13:55:12 +00:00
|
|
|
}
|