Move filesystem constants to main.go

This commit is contained in:
The Magician 2024-05-21 12:19:01 +01:00
parent a6463b0b25
commit e98b0ec72f
3 changed files with 27 additions and 13 deletions

View File

@ -21,9 +21,6 @@ func check(err error) {
}
}
const CACHE_DIR string = "cache"
const ALL_CARDS_CACHE_FILENAME = CACHE_DIR + "/all-cards.json"
func main() {
log.Printf("Getting bulk data listing...")
allCardsBulkData, err := methods.GetBulkDataByType(types.BulkDataTypeAllCards)

View File

@ -1,28 +1,25 @@
package main
import (
"io"
"log"
"net/http"
"os"
"sevenkeys/database"
"sevenkeys/database/operations"
"sevenkeys/scryfall/methods"
)
func check(err error) {
if err != nil {
log.Fatal(err)
}
}
const CACHE_DIR string = "cache"
const ICON_CACHE_DIR string = CACHE_DIR + "/seticons/"
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.")
// Download icon files to cache/ directory
for _, set := range sets {
// We're only interested in paper cards, so skip importing
// any sets that were only released in a video game
@ -30,7 +27,20 @@ func main() {
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)
}
}

View File

@ -5,6 +5,13 @@ import (
"log"
)
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"
func check(err error) {
if err != nil {
log.Fatal(err)