TheMathemagicians/sevenkeys/cmd/importcards/main.go

81 lines
2.1 KiB
Go

package main
import (
"encoding/json"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"sevenkeys/database"
"sevenkeys/database/entities"
"sevenkeys/database/operations"
"sevenkeys/scryfall/methods"
"sevenkeys/scryfall/types"
"time"
)
func check(err error) {
if err != nil {
log.Fatal(err)
}
}
func main() {
log.Printf("Getting bulk data listing...")
allCardsBulkData, err := methods.GetBulkDataByType(types.BulkDataTypeAllCards)
check(err)
log.Printf("Got bulk data listing.")
log.Printf("Checking if we need to download bulk data...")
updatedAtTimestamp, err := time.Parse(types.ScryfallTimestampFormat, allCardsBulkData.UpdatedAt)
check(err)
// Remove the fractional seconds component from the downloaded timestamp because it isn't saved in the cache table
updatedAtTimestamp = updatedAtTimestamp.Truncate(time.Second)
db := database.GetDatabaseFromConfig("config.json")
cachedFileTimestamp, err := operations.GetCacheTimestampByType(db, entities.CacheTypeAllCardsBulkData)
check(err)
if updatedAtTimestamp.After(cachedFileTimestamp) {
log.Printf("Bulk data has been updated since last cache, redownloading.")
err = os.RemoveAll(CACHE_DIR)
check(err)
err := os.Mkdir(CACHE_DIR, os.ModePerm)
check(err)
log.Printf("Downloading bulk card data...")
bulkCardsResponse, err := http.Get(allCardsBulkData.DownloadUri)
check(err)
log.Printf("Downloaded bulk card data.")
log.Printf("Writing card data to cache file...")
cacheFile, err := os.Create(ALL_CARDS_CACHE_FILENAME)
check(err)
defer bulkCardsResponse.Body.Close()
defer cacheFile.Close()
io.Copy(cacheFile, bulkCardsResponse.Body)
log.Printf("Cache file written.")
log.Printf("Recording timestamp...")
err = operations.InsertOrUpdateCacheTimestampByType(db, entities.CacheTypeAllCardsBulkData, updatedAtTimestamp)
check(err)
log.Printf("Timestamp recorded.")
} else {
log.Printf("Bulk data has not been updated. Skipping download.")
}
var allCards []types.Card
allCardsBytes, err := ioutil.ReadFile(ALL_CARDS_CACHE_FILENAME)
check(err)
err = json.Unmarshal(allCardsBytes, &allCards)
check(err)
operations.InsertCards(db, allCards)
}