aboutsummaryrefslogtreecommitdiff
path: root/tools/importer/ACDv2Reader.go
blob: 2ddca4df192b1bbfde324a325874aba44950fd87 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package main

import (
	"encoding/json"
	"io/ioutil"
	"path/filepath"
	"strings"

	"fagott.pw/grilist/modules/grils"
)

type ACDv2Reader struct{}

type ACDv2NameValue struct {
	Name  string `json:"name"`
	Value string `json:"value"`
}

type ACDv2Gril struct {
	Name struct {
		Romaji   string   `json:"romaji"`
		Japanese string   `json:"japanese"`
		Aliases  []string `json:"aliases"`
	} `json:"name"`
	Image struct {
		Thumbnail string `json:"thumb"`
		Full      string `json:"full"`
	} `json:"image"`
	Tags []struct {
		ID   int    `json:"id"`
		Name string `json:"name"`
	} `json:"tags"`
	Traits struct {
		Official []ACDv2NameValue `json:"official"`
		Indexed  []ACDv2NameValue `json:"indexed"`
	} `json:"traits"`
	Assignments            []int            `json:"assignments"`
	CharsWithSimilarTraits []int            `json:"chars_similar_traits"`
	Extra                  []ACDv2NameValue `json:"extra"`
	Role                   string           `json:"role"`
}

func (r *ACDv2Reader) Read(path string) WrappedGril {
	g := WrappedGril{}
	g.Gril = grils.Gril{}
	data, err := ioutil.ReadFile(path)
	ifErrExit(err)
	var jObj ACDv2Gril
	err = json.Unmarshal(data, &jObj)
	ifErrExit(err)
	g.Image = jObj.Image.Full
	g.Thumb = jObj.Image.Thumbnail
	g.Gril.ID = toInt(strings.TrimSuffix(filepath.Base(path), ".json"))
	g.Gril.KanjiName = jObj.Name.Japanese
	g.Gril.RomajiName = jObj.Name.Romaji
	g.Gril.OtherNames = jObj.Name.Aliases
	g.Gril.Tags = make([]string, 0)
	for _, v := range jObj.Tags {
		g.Gril.Tags = append(g.Gril.Tags, v.Name)
	}
	return g
}

func (r *ACDv2Reader) ID() int {
	return int(grils.DataSourceACD)
}