aboutsummaryrefslogtreecommitdiff
path: root/dataimport/finder.go
diff options
context:
space:
mode:
authorrtz12 <koenig@fagott.pw>2016-11-15 17:29:02 (UTC)
committerrtz12 <koenig@fagott.pw>2016-11-15 17:29:02 (UTC)
commit0ad174d7329e02b3f644614de0758789bb3677d4 (patch)
treee49a659c0b89bdec07cde778850b7fe002ac27ea /dataimport/finder.go
parente02ad854383265db216ed92b10c4f12408999d6a (diff)
Verrückter Anilist Import
Bestes Feature 2k16. Hashtag Make Grilist Great Again.
Diffstat (limited to 'dataimport/finder.go')
-rw-r--r--dataimport/finder.go87
1 files changed, 87 insertions, 0 deletions
diff --git a/dataimport/finder.go b/dataimport/finder.go
new file mode 100644
index 0000000..5b704a4
--- /dev/null
+++ b/dataimport/finder.go
@@ -0,0 +1,87 @@
1package dataimport
2
3import (
4 "strings"
5
6 "fagott.pw/grilist/models"
7 "fagott.pw/grilist/util"
8)
9
10func FindGrilInDatabase(id int, dataSource models.DataSource) *int {
11 var grilID *int
12 _ = database.QueryRow("SELECT gril_id FROM grilist.grils_id_mappings WHERE source = $1 AND source_id = $2;", dataSource, id).Scan(&grilID)
13 return grilID
14}
15
16func compareAnimeNames(a, b string) bool {
17 normalize := func(str string) string {
18 return util.NormalizeString(strings.TrimSpace(strings.Replace(strings.ToLower(str), " ", "", -1)))
19 }
20 return normalize(a) == normalize(b)
21}
22
23func FindSimilarGrils(iGril *ImporterGril, dataSource models.DataSource) ([]*models.Gril, error) {
24 rows, err := database.Query("SELECT id, name, appearance_id, appearance_names, source_id FROM grilist.get_grils_for_merge($1, $2);", iGril.RomajiName, dataSource)
25 if err != nil {
26 return nil, err
27 }
28 defer rows.Close()
29
30 grils := []*models.Gril{}
31 var gril *models.Gril
32
33 for rows.Next() {
34 var id int
35 var name string
36 var aID *int
37 var aNamesData *[]byte
38 var sID *int
39 if err := rows.Scan(&id, &name, &aID, &aNamesData, &sID); err != nil {
40 return nil, err
41 }
42 if gril == nil || gril.ID != id {
43 gril = &models.Gril{
44 ID: id,
45 RomajiName: name,
46 }
47 grils = append(grils, gril)
48 }
49 if aID != nil && aNamesData != nil {
50 series := models.Series{
51 ID: *aID,
52 }
53 aNames := util.PGArray(*aNamesData)
54 series.RomajiName = aNames[0]
55 series.OtherNames = aNames
56 if sID != nil {
57 series.ForeignIDs[dataSource] = *sID
58 }
59 gril.Series = append(gril.Series, series)
60 }
61 }
62 mGrils := []*models.Gril{}
63 for _, gril := range grils {
64 matches := false
65 for _, series := range gril.Series {
66 gril.Series = append(gril.Series, series)
67 for _, anime := range iGril.Series {
68 if id, ok := series.ForeignIDs[dataSource]; ok && id == anime.ID {
69 matches = true
70 continue
71 }
72 for _, name := range series.OtherNames {
73 matches = matches || compareAnimeNames(name, anime.RomajiName)
74 matches = matches || compareAnimeNames(name, anime.KanjiName)
75 matches = matches || compareAnimeNames(name, anime.FuriganaName)
76 for _, syn := range anime.OtherNames {
77 matches = matches || compareAnimeNames(name, syn)
78 }
79 }
80 }
81 }
82 if matches {
83 mGrils = append(mGrils, gril)
84 }
85 }
86 return mGrils, nil
87}