aboutsummaryrefslogtreecommitdiff
path: root/tools/importer/ACDReader.go
blob: 694087ef09b63d571405488f01f9909184543ebe (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
package main

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"os"
	"regexp"
	"strconv"
	"strings"

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

var tagRegex = regexp.MustCompile(`\<a.*?\>(.+)\<\/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)
}