Compare commits

..

2 commits

Author SHA1 Message Date
cd2da0aa5c add basic caching 2024-02-01 00:52:42 -08:00
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

86
main.go
View file

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