Finish bulk json caching feature

This commit is contained in:
The Magician 2024-05-19 17:11:42 +01:00
parent e5b87bb8ae
commit af27b138bc
1 changed files with 14 additions and 13 deletions

View File

@ -1,7 +1,9 @@
package main
import (
"encoding/json"
"io"
"io/ioutil"
"log"
"net/http"
"os"
@ -19,6 +21,9 @@ func check(err error) {
}
}
const CacheDir string = "cache"
const AllCardsCacheFilename = CacheDir + "/all-cards.json"
func main() {
log.Printf("Getting bulk data listing...")
allCardsBulkData, err := methods.GetBulkDataByType(types.BulkDataTypeAllCards)
@ -37,15 +42,12 @@ func main() {
check(err)
if updatedAtTimestamp.After(cachedFileTimestamp) {
log.Printf("Cached time: ", cachedFileTimestamp)
log.Printf("Update time: ", updatedAtTimestamp)
log.Printf("Bulk data has been updated since last cache, redownloading.")
cacheDir := "cache"
err = os.RemoveAll(cacheDir)
err = os.RemoveAll(CacheDir)
check(err)
err := os.Mkdir(cacheDir, os.ModePerm)
err := os.Mkdir(CacheDir, os.ModePerm)
check(err)
log.Printf("Downloading bulk card data...")
@ -54,8 +56,7 @@ func main() {
log.Printf("Downloaded bulk card data.")
log.Printf("Writing card data to cache file...")
cacheFilename := cacheDir + "/all-cards.json"
cacheFile, err := os.Create(cacheFilename)
cacheFile, err := os.Create(AllCardsCacheFilename)
check(err)
defer bulkCardsResponse.Body.Close()
@ -71,12 +72,12 @@ func main() {
log.Printf("Bulk data has not been updated. Skipping download.")
}
// Marshal the bulk cards json from the cache file into a struct
// Import the json into the database
var allCards []types.Card
allCardsBytes, err := ioutil.ReadFile(AllCardsCacheFilename)
check(err)
err = json.Unmarshal(allCardsBytes, &allCards)
check(err)
/* OLD CODE
db := database.GetDatabaseFromConfig("config.json")
allCards := methods.GetAllCards()
operations.InsertCards(db, allCards)
*/
}