move refetch code to function, call on stale or no data

main
Zynh0722 2024-02-01 01:04:39 -08:00
parent cd2da0aa5c
commit 7aa82bead7
1 changed files with 16 additions and 13 deletions

29
main.go
View File

@ -43,19 +43,9 @@ func main() {
var id uint32 = 29932586 var id uint32 = 29932586
data, ok := cache[id] data, ok := cache[id]
if !ok { if !ok || time.Now().After(data.Time.Add(time.Minute*10)) {
log.Printf("Fetching fresh data for %d\n", id) cache[id] = refetch_data(id)
s := godestone.NewScraper(bingode.New(), godestone.EN) data = cache[id]
c, err := s.FetchCharacter(id)
if err != nil {
log.Fatalln(err)
}
raw_data, _ := json.Marshal(c.ClassJobs)
data = CacheRecord{Time: time.Now(), Data: string(raw_data)}
cache[id] = data
} }
cache_as_stored, err := json.Marshal(cache) cache_as_stored, err := json.Marshal(cache)
@ -69,3 +59,16 @@ func main() {
} }
fmt.Printf("%s\n", data.Data) fmt.Printf("%s\n", data.Data)
} }
func refetch_data(id uint32) CacheRecord {
log.Printf("Fetching fresh data for %d\n", id)
s := godestone.NewScraper(bingode.New(), godestone.EN)
c, err := s.FetchCharacter(id)
if err != nil {
log.Fatalln(err)
}
raw_data, _ := json.Marshal(c.ClassJobs)
return CacheRecord{Time: time.Now(), Data: string(raw_data)}
}