aboutsummaryrefslogtreecommitdiff
path: root/tools
diff options
context:
space:
mode:
authorrtz12 <koenig@fagott.pw>2016-10-09 01:32:58 (UTC)
committerrtz12 <koenig@fagott.pw>2016-10-09 01:32:58 (UTC)
commitdb4bd8185f7283420b3f60fc561b1b8e4f97b3b9 (patch)
tree8d2668571d5391ae4f202e6b97d1de7d61f548ad /tools
parent5997487fc5cc2126fb5afa12b75b67f3985e11d2 (diff)
Traits in die Datenbank importieren
Diffstat (limited to 'tools')
-rw-r--r--tools/importer/ACDv2Reader.go17
-rw-r--r--tools/importer/filter.go13
-rw-r--r--tools/importer/main.go54
3 files changed, 83 insertions, 1 deletions
diff --git a/tools/importer/ACDv2Reader.go b/tools/importer/ACDv2Reader.go
index 2ddca4d..974da52 100644
--- a/tools/importer/ACDv2Reader.go
+++ b/tools/importer/ACDv2Reader.go
@@ -9,6 +9,8 @@ import (
9 "fagott.pw/grilist/modules/grils" 9 "fagott.pw/grilist/modules/grils"
10) 10)
11 11
12const failMsg = "Offenbar war meine Annahme bzgl. der Traits nicht ganz korrekt"
13
12type ACDv2Reader struct{} 14type ACDv2Reader struct{}
13 15
14type ACDv2NameValue struct { 16type ACDv2NameValue struct {
@@ -58,6 +60,21 @@ func (r *ACDv2Reader) Read(path string) WrappedGril {
58 for _, v := range jObj.Tags { 60 for _, v := range jObj.Tags {
59 g.Gril.Tags = append(g.Gril.Tags, v.Name) 61 g.Gril.Tags = append(g.Gril.Tags, v.Name)
60 } 62 }
63 g.Gril.Traits = make([]grils.Trait, 0)
64 if len(jObj.Traits.Official) != len(jObj.Traits.Indexed) {
65 panic(failMsg)
66 }
67 for i := 0; i < len(jObj.Traits.Official); i++ {
68 if jObj.Traits.Official[i].Name != jObj.Traits.Indexed[i].Name {
69 panic(failMsg)
70 }
71 t := grils.Trait{
72 Name: jObj.Traits.Official[i].Name,
73 Value: jObj.Traits.Indexed[i].Value,
74 OfficialValue: jObj.Traits.Official[i].Value,
75 }
76 g.Gril.Traits = append(g.Gril.Traits, t)
77 }
61 return g 78 return g
62} 79}
63 80
diff --git a/tools/importer/filter.go b/tools/importer/filter.go
new file mode 100644
index 0000000..5a6c315
--- /dev/null
+++ b/tools/importer/filter.go
@@ -0,0 +1,13 @@
1package main
2
3func filterTags(tag string) string {
4 return tag
5}
6
7func filterTraits(trait string) string {
8 switch trait {
9 case "ID":
10 return ""
11 }
12 return trait
13}
diff --git a/tools/importer/main.go b/tools/importer/main.go
index ba98fb5..13ed0cb 100644
--- a/tools/importer/main.go
+++ b/tools/importer/main.go
@@ -108,7 +108,8 @@ func InsertGril(g WrappedGril) int {
108 108
109func InsertTags(dbID int, g WrappedGril, taglist map[string]int) { 109func InsertTags(dbID int, g WrappedGril, taglist map[string]int) {
110 for _, v := range g.Gril.Tags { 110 for _, v := range g.Gril.Tags {
111 if _, ok := taglist[v]; ok { 111 v := filterTags(v)
112 if _, ok := taglist[v]; ok || v == "" {
112 continue 113 continue
113 } 114 }
114 var id int 115 var id int
@@ -126,16 +127,58 @@ func InsertTags(dbID int, g WrappedGril, taglist map[string]int) {
126 existingTags[tagID] = true 127 existingTags[tagID] = true
127 } 128 }
128 for _, v := range g.Gril.Tags { 129 for _, v := range g.Gril.Tags {
130 v = filterTags(v)
131 if v == "" {
132 continue
133 }
129 tagID := taglist[v] 134 tagID := taglist[v]
130 if _, ok := existingTags[tagID]; ok { 135 if _, ok := existingTags[tagID]; ok {
131 continue 136 continue
132 } 137 }
133 _, err := db.Exec(`INSERT INTO grilist.grils_tags (gril_id, tag_id) VALUES ($1, $2);`, dbID, tagID) 138 _, err := db.Exec(`INSERT INTO grilist.grils_tags (gril_id, tag_id) VALUES ($1, $2);`, dbID, tagID)
139 existingTags[tagID] = true
134 fmt.Printf("Assigned tag %s to %s\n", v, g.Gril.RomajiName) 140 fmt.Printf("Assigned tag %s to %s\n", v, g.Gril.RomajiName)
135 LogErr(err) 141 LogErr(err)
136 } 142 }
137} 143}
138 144
145func InsertTraits(dbID int, g WrappedGril, traitlist map[string]int) {
146 for _, v := range g.Gril.Traits {
147 v.Name = filterTraits(v.Name)
148 if _, ok := traitlist[v.Name]; ok || v.Name == "" {
149 continue
150 }
151 var id int
152 row := db.QueryRow("INSERT INTO grilist.traits (name) VALUES ($1) RETURNING id;", v.Name)
153 row.Scan(&id)
154 traitlist[v.Name] = id
155 fmt.Printf("Inserted trait %s as %d\n", v.Name, id)
156 }
157 rows, err := db.Query("SELECT trait_id FROM grils_traits WHERE gril_id = $1;", dbID)
158 LogErr(err)
159 existingTraits := make(map[int]bool, 0)
160 for rows.Next() {
161 var traitID int
162 rows.Scan(&traitID)
163 existingTraits[traitID] = true
164 }
165 for _, v := range g.Gril.Traits {
166 v.Name = filterTraits(v.Name)
167 if v.Name == "" {
168 continue
169 }
170 traitID := traitlist[v.Name]
171 if _, ok := existingTraits[traitID]; ok {
172 continue
173 }
174 _, err := db.Exec(`INSERT INTO grilist.grils_traits (gril_id,
175 trait_id, value, official) VALUES ($1, $2, $3, $4);`, dbID, traitID,
176 v.Value, v.OfficialValue)
177 existingTraits[traitID] = true
178 fmt.Printf("Assigned trait %s to %s\n", v, g.Gril.RomajiName)
179 LogErr(err)
180 }
181}
139func main() { 182func main() {
140 flag.StringVar(&path, "path", "", "path of the source files") 183 flag.StringVar(&path, "path", "", "path of the source files")
141 flag.StringVar(&sourceType, "type", "", "type of the files (ACD, AniDB)") 184 flag.StringVar(&sourceType, "type", "", "type of the files (ACD, AniDB)")
@@ -180,6 +223,14 @@ func main() {
180 rows.Scan(&id, &name) 223 rows.Scan(&id, &name)
181 taglist[name] = id 224 taglist[name] = id
182 } 225 }
226 traitlist := make(map[string]int)
227 rows, _ = db.Query("SELECT id, name FROM grilist.traits;")
228 for rows.Next() {
229 var id int
230 var name string
231 rows.Scan(&id, &name)
232 traitlist[name] = id
233 }
183 idmap := make(map[int]int) 234 idmap := make(map[int]int)
184 rows, _ = db.Query("SELECT source_id, gril_id FROM grilist.grils_id_mappings WHERE source = $1;", r.ID()) 235 rows, _ = db.Query("SELECT source_id, gril_id FROM grilist.grils_id_mappings WHERE source = $1;", r.ID())
185 for rows.Next() { 236 for rows.Next() {
@@ -200,6 +251,7 @@ func main() {
200 dbID = InsertGril(g) 251 dbID = InsertGril(g)
201 } 252 }
202 InsertTags(dbID, g, taglist) 253 InsertTags(dbID, g, taglist)
254 InsertTraits(dbID, g, traitlist)
203 255
204 return nil 256 return nil
205 }) 257 })