package dataimport import ( "strings" "fagott.pw/grilist/models" "fagott.pw/grilist/util" ) func FindGrilInDatabase(id int, dataSource models.DataSource) *int { var grilID *int _ = database.QueryRow("SELECT gril_id FROM grilist.grils_id_mappings WHERE source = $1 AND source_id = $2;", dataSource, id).Scan(&grilID) return grilID } func compareAnimeNames(a, b string) bool { normalize := func(str string) string { return util.NormalizeString(strings.TrimSpace(strings.Replace(strings.ToLower(str), " ", "", -1))) } return normalize(a) == normalize(b) } func FindSimilarGrils(iGril *ImporterGril, dataSource models.DataSource) ([]*models.Gril, error) { rows, err := database.Query("SELECT id, name, appearance_id, appearance_names, source_id FROM grilist.get_grils_for_merge($1, $2);", iGril.RomajiName, dataSource) if err != nil { return nil, err } defer rows.Close() grils := []*models.Gril{} var gril *models.Gril for rows.Next() { var id int var name string var aID *int var aNamesData *[]byte var sID *int if err := rows.Scan(&id, &name, &aID, &aNamesData, &sID); err != nil { return nil, err } if gril == nil || gril.ID != id { gril = &models.Gril{ ID: id, RomajiName: name, } grils = append(grils, gril) } if aID != nil && aNamesData != nil { series := models.Series{ ID: *aID, } aNames := util.PGArray(*aNamesData) series.RomajiName = aNames[0] series.OtherNames = aNames if sID != nil { series.ForeignIDs[dataSource] = *sID } gril.Series = append(gril.Series, series) } } mGrils := []*models.Gril{} for _, gril := range grils { matches := false for _, series := range gril.Series { gril.Series = append(gril.Series, series) for _, anime := range iGril.Series { if id, ok := series.ForeignIDs[dataSource]; ok && id == anime.ID { matches = true continue } for _, name := range series.OtherNames { matches = matches || compareAnimeNames(name, anime.RomajiName) matches = matches || compareAnimeNames(name, anime.KanjiName) matches = matches || compareAnimeNames(name, anime.FuriganaName) for _, syn := range anime.OtherNames { matches = matches || compareAnimeNames(name, syn) } } } } if matches { mGrils = append(mGrils, gril) } } return mGrils, nil }