aboutsummaryrefslogtreecommitdiff
path: root/tools/importer/main.go
diff options
context:
space:
mode:
authorrtz12 <koenig@fagott.pw>2015-12-28 18:56:16 (UTC)
committerrtz12 <koenig@fagott.pw>2015-12-28 18:56:16 (UTC)
commit23f73a60a4cf4621dd5951bfe6601a8c154cad07 (patch)
tree91604c8fb001c93f0a3e021624d1b2ad22d7e24f /tools/importer/main.go
parent95d733307cc87610101469953e61435dbcbeb052 (diff)
Grilimporter hinzugefuegt
Diffstat (limited to 'tools/importer/main.go')
-rw-r--r--tools/importer/main.go152
1 files changed, 152 insertions, 0 deletions
diff --git a/tools/importer/main.go b/tools/importer/main.go
new file mode 100644
index 0000000..41733a1
--- /dev/null
+++ b/tools/importer/main.go
@@ -0,0 +1,152 @@
1package main
2
3import (
4 "database/sql"
5 "flag"
6 "fmt"
7 "os"
8 "path/filepath"
9
10 "fagott.pw/grilist/grilist"
11 "fagott.pw/grilist/modules/grils"
12
13 _ "github.com/lib/pq"
14)
15
16type WrappedGril struct {
17 Gril grils.Gril
18 Image string
19 Thumb string
20}
21
22type GrilReader interface {
23 Read(path string) WrappedGril
24 ID() int
25}
26
27func GetGrilReader(rType string) GrilReader {
28 switch rType {
29 case "ACD":
30 return &ACDReader{}
31 }
32 return nil
33}
34
35func main() {
36 var path string
37 var sourceType string
38 var imgUrlExportFile string
39 flag.StringVar(&path, "path", "", "path of the source files")
40 flag.StringVar(&sourceType, "type", "", "type of the files (ACD, AniDB)")
41 flag.StringVar(&imgUrlExportFile, "imageexportfile", "", "image URLs will be exported to this file, THIS WILL OVERWRITE EVERYTHING IN THE FILE!")
42 flag.Parse()
43 var exportFile *os.File
44 if imgUrlExportFile != "" {
45 var err error
46 if exportFile, err = os.Create(imgUrlExportFile); err != nil {
47 fmt.Fprintf(os.Stderr, "%s\n", "error while accessing \"%s\"")
48 fmt.Fprintf(os.Stderr, "%v\n", err)
49 os.Exit(1)
50 }
51 }
52 if path == "" {
53 fmt.Fprintf(os.Stderr, "%s\n", "\"path\" must be specified!")
54 flag.Usage()
55 os.Exit(1)
56 }
57 r := GetGrilReader(sourceType)
58 if r == nil {
59 fmt.Fprintf(os.Stderr, "%s\n", "\"type\" must be specified and a valid gril source!")
60 flag.Usage()
61 os.Exit(1)
62 }
63 config := grilist.LoadConfig()
64 db, err := sql.Open("postgres", config.DBConnectionString())
65 if err == nil {
66 err = db.Ping()
67 }
68 if err != nil {
69 fmt.Fprintf(os.Stderr, "%s\n", "error with database connection")
70 fmt.Fprintf(os.Stderr, "%v\n", err)
71 os.Exit(1)
72 }
73 taglist := make(map[string]int)
74 rows, _ := db.Query("SELECT id, name FROM grilist.tags;")
75 for rows.Next() {
76 var id int
77 var name string
78 rows.Scan(&id)
79 rows.Scan(&name)
80 taglist[name] = id
81 }
82 idmap := make(map[int]int)
83 rows, _ = db.Query("SELECT source_id, gril_id FROM grilist.grils_id_mappings WHERE source = $1;", r.ID())
84 for rows.Next() {
85 var sourceID int
86 var grilID int
87 rows.Scan(&sourceID)
88 rows.Scan(&grilID)
89 idmap[sourceID] = grilID
90 }
91 filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
92 if info.IsDir() {
93 return nil
94 }
95 g := r.Read(path)
96 for _, v := range g.Gril.Tags {
97 if _, ok := taglist[v]; ok {
98 continue
99 }
100 var id int
101 row := db.QueryRow("INSERT INTO grilist.tags (name) VALUES ($1) RETURNING id;", v)
102 row.Scan(&id)
103 taglist[v] = id
104 }
105 if _, ok := idmap[g.Gril.ID]; !ok {
106 row := db.QueryRow("INSERT INTO grilist.grils (age) VALUES (NULL) RETURNING id;")
107 var dbID int
108 row.Scan(&dbID)
109 for _, v := range g.Gril.Tags {
110 tagID := taglist[v]
111 db.Exec("INSERT INTO grilist.grils_tags (gril_id, tag_id) VALUES ($1, $2);", dbID, tagID)
112 }
113 db.Exec(`INSERT INTO grilist.grils_id_mappings (gril_id,
114 source, source_id) VALUES ($1, $2, $3);`,
115 dbID, r.ID(), g.Gril.ID)
116 if g.Gril.KanjiName != "" {
117 db.Exec(`INSERT INTO grilist.gril_names
118 (gril_id, name, name_type) VALUES ($1, $2,
119 $3);`, dbID, g.Gril.KanjiName, 0)
120 }
121 if g.Gril.RomajiName != "" {
122 db.Exec(`INSERT INTO grilist.gril_names
123 (gril_id, name, name_type) VALUES ($1, $2,
124 $3);`, dbID, g.Gril.RomajiName, 1)
125 }
126 for _, v := range g.Gril.OtherNames {
127 db.Exec(`INSERT INTO grilist.gril_names
128 (gril_id, name, name_type) VALUES ($1, $2,
129 $3);`, dbID, v, 2)
130 }
131 if exportFile != nil {
132 if g.Image != "" {
133 fmt.Fprintf(
134 exportFile,
135 "%d %d %s\n",
136 dbID,
137 0,
138 g.Image)
139 }
140 if g.Thumb != "" {
141 fmt.Fprintf(
142 exportFile,
143 "%d %d %s\n",
144 dbID,
145 1,
146 g.Thumb)
147 }
148 }
149 }
150 return nil
151 })
152}