Move loop to main package

This commit is contained in:
The Magician 2024-05-20 17:33:40 +01:00
parent c4f87dc451
commit 5f7f07edea
3 changed files with 23 additions and 20 deletions

View File

@ -21,8 +21,8 @@ func check(err error) {
}
}
const CacheDir string = "cache"
const AllCardsCacheFilename = CacheDir + "/all-cards.json"
const CACHE_DIR string = "cache"
const ALL_CARDS_CACHE_FILENAME = CACHE_DIR + "/all-cards.json"
func main() {
log.Printf("Getting bulk data listing...")
@ -44,10 +44,10 @@ func main() {
if updatedAtTimestamp.After(cachedFileTimestamp) {
log.Printf("Bulk data has been updated since last cache, redownloading.")
err = os.RemoveAll(CacheDir)
err = os.RemoveAll(CACHE_DIR)
check(err)
err := os.Mkdir(CacheDir, os.ModePerm)
err := os.Mkdir(CACHE_DIR, os.ModePerm)
check(err)
log.Printf("Downloading bulk card data...")
@ -56,7 +56,7 @@ func main() {
log.Printf("Downloaded bulk card data.")
log.Printf("Writing card data to cache file...")
cacheFile, err := os.Create(AllCardsCacheFilename)
cacheFile, err := os.Create(ALL_CARDS_CACHE_FILENAME)
check(err)
defer bulkCardsResponse.Body.Close()
@ -73,7 +73,7 @@ func main() {
}
var allCards []types.Card
allCardsBytes, err := ioutil.ReadFile(AllCardsCacheFilename)
allCardsBytes, err := ioutil.ReadFile(ALL_CARDS_CACHE_FILENAME)
check(err)
err = json.Unmarshal(allCardsBytes, &allCards)

View File

@ -13,13 +13,24 @@ func check(err error) {
}
}
const CACHE_DIR string = "cache"
const ICON_CACHE_DIR string = CACHE_DIR + "/seticons/"
func main() {
sets, err := methods.GetSets()
check(err)
db := database.GetDatabaseFromConfig("config.json")
err = operations.InsertOrUpdateSets(db, sets)
check(err)
// 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
if set.Digital {
continue
}
err = operations.InsertOrUpdateSet(db, set)
check(err)
}
}

View File

@ -26,7 +26,7 @@ func InsertOrUpdateCacheTimestampByType(db *sql.DB, cacheType string, stamp time
return nil
}
func InsertOrUpdateSets(db *sql.DB, sets []types.Set) error {
func InsertOrUpdateSet(db *sql.DB, set types.Set) error {
query := `INSERT INTO ExpansionSet (SetCode, Name, CardCount, IconSvgUri)
VALUES (?, ?, ?, ?)
ON DUPLICATE KEY
@ -38,17 +38,9 @@ func InsertOrUpdateSets(db *sql.DB, sets []types.Set) error {
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 = insertOrUpdate.Exec(set.Code, set.Name, set.CardCount, set.IconSvgUri, set.Name, set.CardCount, set.IconSvgUri)
if err != nil {
return err
}
_, err = insertOrUpdate.Exec(set.Code, set.Name, set.CardCount, set.IconSvgUri, set.Name, set.CardCount, set.IconSvgUri)
if err != nil {
return err
}
return nil