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