TheMathemagicians/sevenkeys/cmd/importsets/main.go

47 lines
1.0 KiB
Go
Raw Normal View History

2024-05-20 16:17:45 +00:00
package main
import (
2024-05-21 11:19:01 +00:00
"io"
2024-05-20 16:17:45 +00:00
"log"
2024-05-21 11:19:01 +00:00
"net/http"
"os"
2024-05-20 16:17:45 +00:00
"sevenkeys/database"
"sevenkeys/database/operations"
"sevenkeys/scryfall/methods"
)
func main() {
2024-05-21 11:19:01 +00:00
log.Println("Downloading set data from Scryfall...")
2024-05-20 16:17:45 +00:00
sets, err := methods.GetSets()
check(err)
2024-05-21 11:19:01 +00:00
log.Println("Downloaded set data.")
2024-05-20 16:17:45 +00:00
2024-05-21 11:19:01 +00:00
log.Println("Connecting to database...")
2024-05-20 16:17:45 +00:00
db := database.GetDatabaseFromConfig("config.json")
2024-05-21 11:19:01 +00:00
log.Println("Connected.")
2024-05-20 16:17:45 +00:00
2024-05-20 16:33:40 +00:00
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
}
2024-05-21 11:19:01 +00:00
log.Println("Importing set: " + set.Name)
2024-05-20 16:33:40 +00:00
err = operations.InsertOrUpdateSet(db, set)
check(err)
2024-05-21 11:19:01 +00:00
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)
2024-05-20 16:33:40 +00:00
}
2024-05-20 16:17:45 +00:00
}