aboutsummaryrefslogtreecommitdiff
path: root/tools/importer/ACDv2Reader.go
blob: f34dd15c398e2b671b6f72016d3fa1f8a5102715 (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package main

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

	"fagott.pw/grilist/models"
)

const failMsg = "Offenbar war meine Annahme bzgl. der Traits nicht ganz korrekt"

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 = models.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)
	}
	g.Gril.Traits = make([]models.Trait, 0)
	if len(jObj.Traits.Official) != len(jObj.Traits.Indexed) {
		panic(failMsg)
	}
	for i := 0; i < len(jObj.Traits.Official); i++ {
		if jObj.Traits.Official[i].Name != jObj.Traits.Indexed[i].Name {
			panic(failMsg)
		}
		t := models.Trait{
			Name:          jObj.Traits.Official[i].Name,
			Value:         jObj.Traits.Indexed[i].Value,
			OfficialValue: jObj.Traits.Official[i].Value,
		}
		g.Gril.Traits = append(g.Gril.Traits, t)
	}
	g.Gril.Series = make([]models.Series, 0)
	for _, v := range jObj.Assignments {
		s := models.Series{
			ID: v,
		}
		g.Gril.Series = append(g.Gril.Series, s)
	}
	return g
}

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