diff options
Diffstat (limited to 'dataimport')
-rw-r--r-- | dataimport/converter.go | 56 | ||||
-rw-r--r-- | dataimport/dataimport.go | 9 | ||||
-rw-r--r-- | dataimport/finder.go | 87 | ||||
-rw-r--r-- | dataimport/importer.go | 52 | ||||
-rw-r--r-- | dataimport/types.go | 23 |
5 files changed, 227 insertions, 0 deletions
diff --git a/dataimport/converter.go b/dataimport/converter.go new file mode 100644 index 0000000..6a44384 --- /dev/null +++ b/dataimport/converter.go | |||
@@ -0,0 +1,56 @@ | |||
1 | package dataimport | ||
2 | |||
3 | import ( | ||
4 | "fmt" | ||
5 | "strings" | ||
6 | |||
7 | "fagott.pw/goanilist" | ||
8 | "fagott.pw/grilist/models" | ||
9 | ) | ||
10 | |||
11 | func ConvertGrilAnilist(char *goanilist.CharacterPageResult) *ImporterGril { | ||
12 | gril := &ImporterGril{} | ||
13 | gril.ID = char.ID | ||
14 | gril.KanjiName = char.JapaneseName | ||
15 | gril.RomajiName = strings.TrimSpace(fmt.Sprintf("%s %s", char.GivenName, char.FamilyName)) | ||
16 | gril.Order = char.Order | ||
17 | if char.AlternativeName != "" { | ||
18 | gril.OtherNames = append(gril.OtherNames, char.AlternativeName) | ||
19 | } | ||
20 | if char.ImageURL != "" { | ||
21 | gril.Images = append(gril.Images, Image{ | ||
22 | Type: ImageTypeFull, | ||
23 | URL: char.ImageURL, | ||
24 | }) | ||
25 | } | ||
26 | if char.ThumbURL != "" { | ||
27 | gril.Images = append(gril.Images, Image{ | ||
28 | Type: ImageTypeThumbnail, | ||
29 | URL: char.ThumbURL, | ||
30 | }) | ||
31 | } | ||
32 | for _, anime := range char.Anime { | ||
33 | gril.Series = append(gril.Series, ConvertSeriesAnilist(anime.SeriesResult)) | ||
34 | } | ||
35 | for _, manga := range char.Manga { | ||
36 | gril.Series = append(gril.Series, ConvertSeriesAnilist(manga.SeriesResult)) | ||
37 | } | ||
38 | return gril | ||
39 | } | ||
40 | |||
41 | func ConvertSeriesTypeAnilist(seriesType string) models.SeriesType { | ||
42 | switch seriesType { | ||
43 | } | ||
44 | return models.SeriesTypeUnknown | ||
45 | } | ||
46 | |||
47 | func ConvertSeriesAnilist(series goanilist.SeriesResult) models.Series { | ||
48 | return models.Series{ | ||
49 | ID: series.ID, | ||
50 | Type: ConvertSeriesTypeAnilist(series.Type), | ||
51 | Name: series.TitleEnglish, | ||
52 | KanjiName: series.TitleJapanese, | ||
53 | RomajiName: series.TitleRomaji, | ||
54 | OtherNames: series.Synonyms, | ||
55 | } | ||
56 | } | ||
diff --git a/dataimport/dataimport.go b/dataimport/dataimport.go new file mode 100644 index 0000000..4966e25 --- /dev/null +++ b/dataimport/dataimport.go | |||
@@ -0,0 +1,9 @@ | |||
1 | package dataimport | ||
2 | |||
3 | import "database/sql" | ||
4 | |||
5 | var database *sql.DB | ||
6 | |||
7 | func Init(db *sql.DB) { | ||
8 | database = db | ||
9 | } | ||
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 @@ | |||
1 | package dataimport | ||
2 | |||
3 | import ( | ||
4 | "strings" | ||
5 | |||
6 | "fagott.pw/grilist/models" | ||
7 | "fagott.pw/grilist/util" | ||
8 | ) | ||
9 | |||
10 | func 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 | |||
16 | func 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 | |||
23 | func 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 | } | ||
diff --git a/dataimport/importer.go b/dataimport/importer.go new file mode 100644 index 0000000..0d121e5 --- /dev/null +++ b/dataimport/importer.go | |||
@@ -0,0 +1,52 @@ | |||
1 | package dataimport | ||
2 | |||
3 | import ( | ||
4 | "log" | ||
5 | |||
6 | "fagott.pw/grilist/models" | ||
7 | ) | ||
8 | |||
9 | func logErr(err error) { | ||
10 | if err != nil { | ||
11 | log.Printf("IMPORT ERROR: %v\n", err) | ||
12 | } | ||
13 | } | ||
14 | |||
15 | func AssignGril(foreign int, id int, dataSource models.DataSource) error { | ||
16 | _, err := database.Exec("INSERT INTO grilist.grils_id_mappings (gril_id, source, source_id) VALUES ($1, $2, $3);", id, models.DataSourceAnilist, foreign) | ||
17 | if err != nil { | ||
18 | return err | ||
19 | } | ||
20 | return nil | ||
21 | } | ||
22 | |||
23 | func InsertGril(g *ImporterGril, dataSource models.DataSource) (int, error) { | ||
24 | var dbID int | ||
25 | if err := database.QueryRow("INSERT INTO grilist.grils (age) VALUES (NULL) RETURNING id;").Scan(&dbID); err != nil { | ||
26 | return 0, err | ||
27 | } | ||
28 | if err := AssignGril(g.ID, dbID, dataSource); err != nil { | ||
29 | return 0, err | ||
30 | } | ||
31 | if g.Gril.KanjiName != "" { | ||
32 | _, err := database.Exec(`INSERT INTO | ||
33 | grilist.gril_names (gril_id, name, name_type) | ||
34 | VALUES ($1, $2, $3);`, dbID, g.KanjiName, | ||
35 | 0) | ||
36 | logErr(err) | ||
37 | } | ||
38 | if g.Gril.RomajiName != "" { | ||
39 | _, err := database.Exec(`INSERT INTO | ||
40 | grilist.gril_names (gril_id, name, name_type) | ||
41 | VALUES ($1, $2, $3);`, dbID, g.RomajiName, | ||
42 | 1) | ||
43 | logErr(err) | ||
44 | } | ||
45 | for _, v := range g.OtherNames { | ||
46 | _, err := database.Exec(`INSERT INTO | ||
47 | grilist.gril_names (gril_id, name, name_type) | ||
48 | VALUES ($1, $2, $3);`, dbID, v, 2) | ||
49 | logErr(err) | ||
50 | } | ||
51 | return dbID, nil | ||
52 | } | ||
diff --git a/dataimport/types.go b/dataimport/types.go new file mode 100644 index 0000000..82e3b9a --- /dev/null +++ b/dataimport/types.go | |||
@@ -0,0 +1,23 @@ | |||
1 | package dataimport | ||
2 | |||
3 | import "fagott.pw/grilist/models" | ||
4 | |||
5 | type ImporterGril struct { | ||
6 | models.Gril | ||
7 | Order int | ||
8 | Images []Image | ||
9 | } | ||
10 | |||
11 | type ImageType int | ||
12 | |||
13 | const ( | ||
14 | ImageTypeThumbnail ImageType = iota | ||
15 | ImageTypeThumbnailSmall | ||
16 | ImageTypeFull | ||
17 | ImageTypeBanner | ||
18 | ) | ||
19 | |||
20 | type Image struct { | ||
21 | URL string | ||
22 | Type ImageType | ||
23 | } | ||