From db4bd8185f7283420b3f60fc561b1b8e4f97b3b9 Mon Sep 17 00:00:00 2001 From: rtz12 Date: Sun, 9 Oct 2016 03:32:58 +0200 Subject: Traits in die Datenbank importieren diff --git a/modules/grils/gril.go b/modules/grils/gril.go index f78d31f..edec05a 100644 --- a/modules/grils/gril.go +++ b/modules/grils/gril.go @@ -19,6 +19,12 @@ const ( DataSourceAnilist ) +type Trait struct { + Name string + Value string + OfficialValue string +} + type Gril struct { ID int KanjiName string @@ -27,6 +33,7 @@ type Gril struct { Age sql.NullInt64 Birthday pq.NullTime Tags []string + Traits []Trait ForeignIDs map[DataSource]int UpdatedAt time.Time Lists []int 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 ( "fagott.pw/grilist/modules/grils" ) +const failMsg = "Offenbar war meine Annahme bzgl. der Traits nicht ganz korrekt" + type ACDv2Reader struct{} type ACDv2NameValue struct { @@ -58,6 +60,21 @@ func (r *ACDv2Reader) Read(path string) WrappedGril { for _, v := range jObj.Tags { g.Gril.Tags = append(g.Gril.Tags, v.Name) } + g.Gril.Traits = make([]grils.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 := grils.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) + } return g } 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 @@ +package main + +func filterTags(tag string) string { + return tag +} + +func filterTraits(trait string) string { + switch trait { + case "ID": + return "" + } + return trait +} 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 { func InsertTags(dbID int, g WrappedGril, taglist map[string]int) { for _, v := range g.Gril.Tags { - if _, ok := taglist[v]; ok { + v := filterTags(v) + if _, ok := taglist[v]; ok || v == "" { continue } var id int @@ -126,16 +127,58 @@ func InsertTags(dbID int, g WrappedGril, taglist map[string]int) { existingTags[tagID] = true } for _, v := range g.Gril.Tags { + v = filterTags(v) + if v == "" { + continue + } tagID := taglist[v] if _, ok := existingTags[tagID]; ok { continue } _, err := db.Exec(`INSERT INTO grilist.grils_tags (gril_id, tag_id) VALUES ($1, $2);`, dbID, tagID) + existingTags[tagID] = true fmt.Printf("Assigned tag %s to %s\n", v, g.Gril.RomajiName) LogErr(err) } } +func InsertTraits(dbID int, g WrappedGril, traitlist map[string]int) { + for _, v := range g.Gril.Traits { + v.Name = filterTraits(v.Name) + if _, ok := traitlist[v.Name]; ok || v.Name == "" { + continue + } + var id int + row := db.QueryRow("INSERT INTO grilist.traits (name) VALUES ($1) RETURNING id;", v.Name) + row.Scan(&id) + traitlist[v.Name] = id + fmt.Printf("Inserted trait %s as %d\n", v.Name, id) + } + rows, err := db.Query("SELECT trait_id FROM grils_traits WHERE gril_id = $1;", dbID) + LogErr(err) + existingTraits := make(map[int]bool, 0) + for rows.Next() { + var traitID int + rows.Scan(&traitID) + existingTraits[traitID] = true + } + for _, v := range g.Gril.Traits { + v.Name = filterTraits(v.Name) + if v.Name == "" { + continue + } + traitID := traitlist[v.Name] + if _, ok := existingTraits[traitID]; ok { + continue + } + _, err := db.Exec(`INSERT INTO grilist.grils_traits (gril_id, + trait_id, value, official) VALUES ($1, $2, $3, $4);`, dbID, traitID, + v.Value, v.OfficialValue) + existingTraits[traitID] = true + fmt.Printf("Assigned trait %s to %s\n", v, g.Gril.RomajiName) + LogErr(err) + } +} func main() { flag.StringVar(&path, "path", "", "path of the source files") flag.StringVar(&sourceType, "type", "", "type of the files (ACD, AniDB)") @@ -180,6 +223,14 @@ func main() { rows.Scan(&id, &name) taglist[name] = id } + traitlist := make(map[string]int) + rows, _ = db.Query("SELECT id, name FROM grilist.traits;") + for rows.Next() { + var id int + var name string + rows.Scan(&id, &name) + traitlist[name] = id + } idmap := make(map[int]int) rows, _ = db.Query("SELECT source_id, gril_id FROM grilist.grils_id_mappings WHERE source = $1;", r.ID()) for rows.Next() { @@ -200,6 +251,7 @@ func main() { dbID = InsertGril(g) } InsertTags(dbID, g, taglist) + InsertTraits(dbID, g, traitlist) return nil }) -- cgit v0.10.1