TheMathemagicians/sevenkeys/cmd/importsets/main.go

47 lines
1.0 KiB
Go

package main
import (
"io"
"log"
"net/http"
"os"
"sevenkeys/database"
"sevenkeys/database/operations"
"sevenkeys/scryfall/methods"
)
func main() {
log.Println("Downloading set data from Scryfall...")
sets, err := methods.GetSets()
check(err)
log.Println("Downloaded set data.")
log.Println("Connecting to database...")
db := database.GetDatabaseFromConfig("config.json")
log.Println("Connected.")
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: " + set.Name)
err = operations.InsertOrUpdateSet(db, set)
check(err)
response, err := http.Get(set.IconSvgUri)
log.Println("Downloading logo for set: " + set.Name)
defer response.Body.Close()
check(err)
iconFilename := ICON_CACHE_DIR + set.Code + FILE_EXTENSION_SVG
iconFile, err := os.Create(iconFilename)
check(err)
io.Copy(iconFile, response.Body)
log.Println("Finished importing set: " + set.Name)
}
}