Compare commits

...

2 Commits

Author SHA1 Message Date
Zynh0722 cd2da0aa5c add basic caching 2024-02-01 00:52:42 -08:00
Zynh0722 5419a77c48 print json output for id 2024-01-31 23:48:09 -08:00
3 changed files with 54 additions and 36 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
.cache/

3
clear_cache.sh Executable file
View File

@ -0,0 +1,3 @@
#!/bin/sh
rm -rf .cache

82
main.go
View File

@ -1,57 +1,71 @@
package main package main
import ( import (
"encoding/json"
"errors"
"fmt"
"log" "log"
"slices" "os"
"path"
"time"
"github.com/karashiiro/bingode" "github.com/karashiiro/bingode"
"github.com/xivapi/godestone/v2" "github.com/xivapi/godestone/v2"
) )
const CachePath = "./.cache"
type CacheRecord struct {
Time time.Time `json:"time"`
Data string `json:"data"`
}
type Cache map[uint32]CacheRecord
func main() { func main() {
ignore := []string{ err := os.MkdirAll(CachePath, 0755)
"Carpenter", if err != nil {
"Blacksmith", log.Fatal(err)
"Armorer",
"Goldsmith",
"Leatherworker",
"Weaver",
"Alchemist",
"Culinarian",
"Miner",
"Botanist",
"Fisher",
} }
base_levels := make(map[string]int) raw_cache, err := os.ReadFile(path.Join(CachePath, "cache.json"))
var cache Cache
base_levels["Dark Knight"] = 30 if errors.Is(err, os.ErrNotExist) {
base_levels["Astrologian"] = 30 cache = make(Cache)
base_levels["Machinist"] = 30 } else if err == nil {
base_levels["Red Mage"] = 50 err := json.Unmarshal(raw_cache, &cache)
base_levels["Samurai"] = 50 if err != nil {
base_levels["Gunbreaker"] = 60 log.Fatalln(err)
base_levels["Dancer"] = 60 }
base_levels["Reaper"] = 70 } else {
base_levels["Sage"] = 70 log.Fatalln(err)
}
var id uint32 = 29932586
data, ok := cache[id]
if !ok {
log.Printf("Fetching fresh data for %d\n", id)
s := godestone.NewScraper(bingode.New(), godestone.EN) s := godestone.NewScraper(bingode.New(), godestone.EN)
c, err := s.FetchCharacter(29932586) c, err := s.FetchCharacter(id)
if err != nil { if err != nil {
log.Fatalln(err) log.Fatalln(err)
} }
total := 0 raw_data, _ := json.Marshal(c.ClassJobs)
for _, v := range c.ClassJobs { data = CacheRecord{Time: time.Now(), Data: string(raw_data)}
if v.Name != "Scholar" && !slices.Contains(ignore[:], v.Name) {
base_level, ok := base_levels[v.Name] cache[id] = data
if !ok {
base_level = 0
}
total += int(v.Level) - base_level
}
} }
println(total) cache_as_stored, err := json.Marshal(cache)
if err != nil {
log.Fatalln(err)
}
err = os.WriteFile(path.Join(CachePath, "cache.json"), cache_as_stored, 0755)
if err != nil {
log.Fatalln(err)
}
fmt.Printf("%s\n", data.Data)
} }