package main import ( "encoding/json" "fmt" "io/ioutil" "os" "regexp" "strconv" "strings" "fagott.pw/grilist/modules/grils" ) var tagRegex = regexp.MustCompile(`\(.+)\<\/a\>`) type ACDReader struct{} func ifErrExit(err error) { if err != nil { fmt.Fprintf(os.Stderr, "%v\n", err) os.Exit(1) } } func toInt(s string) int { i, err := strconv.ParseInt(s, 10, 32) ifErrExit(err) return int(i) } func (r *ACDReader) Read(path string) WrappedGril { g := WrappedGril{} g.Gril = grils.Gril{} data, err := ioutil.ReadFile(path) ifErrExit(err) var jObj interface{} err = json.Unmarshal(data, &jObj) ifErrExit(err) for k, v := range jObj.(map[string]interface{}) { switch k { case "Character ID": g.Gril.ID = toInt(v.(string)) case "Romaji Name": g.Gril.RomajiName = v.(string) case "Japanese Name": g.Gril.KanjiName = v.(string) case "Aliases": g.Gril.OtherNames = make([]string, 0) for _, n := range strings.Split(v.(string), ",") { if n == "" { continue } g.Gril.OtherNames = append( g.Gril.OtherNames, strings.TrimSpace(n)) } case "Tagged": g.Gril.Tags = make([]string, 0) for _, n := range strings.Split(v.(string), ",") { if n == "" { continue } tag := strings.TrimSpace(n) tag = tagRegex.FindStringSubmatch(tag)[1] g.Gril.Tags = append( g.Gril.Tags, tag) } case "__img": g.Image = v.(string) case "__thumb": g.Thumb = v.(string) } } return g } func (r *ACDReader) ID() int { return int(grils.DataSourceACD) }