diff options
Diffstat (limited to 'tools/importer/main.go')
-rw-r--r-- | tools/importer/main.go | 54 |
1 files changed, 53 insertions, 1 deletions
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 | ||
109 | func InsertTags(dbID int, g WrappedGril, taglist map[string]int) { | 109 | func 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 | ||
145 | func 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 | } | ||
139 | func main() { | 182 | func 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 | }) |