TheMathemagicians/sevenkeys/cmd/importcards/main.go

81 lines
2.1 KiB
Go
Raw Normal View History

2024-04-28 14:13:47 +00:00
package main
import (
2024-05-19 16:11:42 +00:00
"encoding/json"
2024-05-19 14:01:18 +00:00
"io"
2024-05-19 16:11:42 +00:00
"io/ioutil"
2024-05-19 14:01:18 +00:00
"log"
"net/http"
"os"
2024-04-28 14:13:47 +00:00
"sevenkeys/database"
2024-05-19 14:01:18 +00:00
"sevenkeys/database/entities"
2024-04-28 14:13:47 +00:00
"sevenkeys/database/operations"
"sevenkeys/scryfall/methods"
2024-05-19 14:01:18 +00:00
"sevenkeys/scryfall/types"
"time"
2024-04-28 14:13:47 +00:00
)
2024-05-19 14:01:18 +00:00
func check(err error) {
if err != nil {
log.Fatal(err)
}
}
2024-04-28 14:13:47 +00:00
func main() {
2024-05-19 14:01:18 +00:00
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.")
2024-05-20 16:33:40 +00:00
err = os.RemoveAll(CACHE_DIR)
2024-05-19 14:01:18 +00:00
check(err)
2024-05-20 16:33:40 +00:00
err := os.Mkdir(CACHE_DIR, os.ModePerm)
2024-05-19 14:01:18 +00:00
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...")
2024-05-20 16:33:40 +00:00
cacheFile, err := os.Create(ALL_CARDS_CACHE_FILENAME)
2024-05-19 14:01:18 +00:00
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.")
}
2024-05-19 16:11:42 +00:00
var allCards []types.Card
2024-05-20 16:33:40 +00:00
allCardsBytes, err := ioutil.ReadFile(ALL_CARDS_CACHE_FILENAME)
2024-05-19 16:11:42 +00:00
check(err)
err = json.Unmarshal(allCardsBytes, &allCards)
check(err)
2024-05-19 14:01:18 +00:00
2024-04-28 14:13:47 +00:00
operations.InsertCards(db, allCards)
}