forked from kpawlik/exportms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
236 lines (217 loc) · 5.46 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package main
import (
"flag"
"fmt"
"io"
"log"
"sync"
_ "github.com/go-sql-driver/mysql"
"github.com/kpawlik/exportms/gratka"
// "github.com/kpawlik/exportms/otodom"
_ "github.com/go-sql-driver/mysql"
"os"
"path/filepath"
"runtime"
"strings"
"time"
"github.com/kpawlik/exportms/utils"
)
const (
// DateFormat main date format
DateFormat = "2006_01_02_15"
//GratkaExportType default export type
GratkaExportType = "full"
// CreadentalsFile name of file with credentials
CreadentalsFile = "enc"
)
var (
wg sync.WaitGroup
workDir string
noOfWorkers int
sendOnly bool
exportTypes string
tmpPrefix string
testMode bool
// DbUser database access user name
DbUser string
//DbPass database access passworld
DbPass string
// DbHost database host name
DbHost string
// DbName name of database
DbName string
// Domain database Domain
Domain string
// OfflineCode offline code
OfflineCode string
// FTPHost ftp host name
FTPHost string
// FTPLogin ftp login
FTPLogin string
// FTPPass ftp password
FTPPass string
credentials utils.Credentials
settings *config
//
exports map[string]exportFunc
)
type config struct {
workDir, tmpPrefix string
sendOnly bool
exports string
noOfWorkers int
testMode bool
}
type exportFunc func(string, *config) error
func init() {
var (
err error
)
runtime.GOMAXPROCS(runtime.NumCPU())
flag.StringVar(&workDir, "o", "", "")
flag.IntVar(&noOfWorkers, "n", 1, "")
flag.BoolVar(&sendOnly, "s", false, "")
flag.BoolVar(&testMode, "tm", false, "")
flag.StringVar(&exportTypes, "e", "", "gratka, otodom")
flag.Parse()
workDir, err = filepath.Abs(strings.ToLower(workDir))
utils.LogErr(err, "Dest folder path")
if len(workDir) == 0 {
fmt.Println("Set out forlder path")
os.Exit(0)
}
if runtime.GOOS == "windows" {
tmpPrefix = `c:\tmp`
} else {
tmpPrefix = `/tmp/`
}
f, err := os.Create(`ms-expot.log`)
if err != nil {
panic(err)
}
wr := io.MultiWriter(f, os.Stdout)
log.SetOutput(wr)
settings = &config{workDir: workDir,
noOfWorkers: noOfWorkers,
tmpPrefix: tmpPrefix,
sendOnly: sendOnly,
exports: exportTypes,
testMode: testMode}
log.Println("Settings:")
log.Printf("No of concurrent workers: %d\n", noOfWorkers)
log.Printf("Work dir: %s\n", workDir)
exports = map[string]exportFunc{
"gratka": exportGratka,
"otodom": exportGratka}
}
// validPaths validates path to folders, if necessery then
// it will remover and recreate folders
func validPaths(conf *config) (err error) {
workDir := conf.workDir
tmpPrefix := conf.tmpPrefix
removeFirst := !conf.sendOnly
_, err = os.Stat(workDir)
dirExists := err == nil
if removeFirst && dirExists && !strings.HasPrefix(workDir, tmpPrefix) {
return utils.Errorf("Dest folder '%s' already exists is not in tmp folder so need to be removed manually\n", workDir)
}
if removeFirst && dirExists {
if err = os.RemoveAll(workDir); err != nil {
return
}
}
if err = os.MkdirAll(workDir, os.ModePerm); err != nil {
return
}
return os.MkdirAll(filepath.Join(workDir, "images"), os.ModePerm)
}
// setCredentials sets login/pass/ftp etc data for db acceess and exprot to ftp
func setCredentials(exportType string) error {
var (
err error
)
if credentials == nil {
if credentials, err = utils.GetCredentials(CreadentalsFile); err != nil {
return err
}
}
if exportType == "db" {
dbData := credentials[exportType]
DbUser = dbData["user"]
DbPass = dbData["pass"]
DbHost = dbData["host"]
DbName = dbData["name"]
return nil
}
data := credentials[exportType]
Domain = data["domain"]
OfflineCode = data["offlineCode"]
FTPHost = data["ftpHost"]
FTPLogin = data["ftpLogin"]
FTPPass = data["ftpPass"]
return nil
}
func exportGratka(name string, conf *config) (err error) {
var (
gratkaDir string
zipPath string
ftp *utils.FTP
)
if err = setCredentials(name); err != nil {
log.Printf("Incorrect password for %s\n", name)
return
}
zipPath = gratka.ZipPath(conf.workDir, Domain)
if !conf.sendOnly {
startTime := time.Now()
if gratkaDir, err = gratka.Convert(workDir, OfflineCode, Domain); err != nil {
return
}
log.Printf("Convertion time: %s\n", time.Now().Sub(startTime))
if zipPath, err = gratka.Zip(workDir, gratkaDir, Domain); err != nil {
return
}
}
log.Printf("--- Send to '%s'---\n", name)
ftp = utils.NewFTP(FTPHost, FTPLogin, FTPPass, conf.testMode)
return gratka.SendToFtp(Domain, zipPath, ftp)
}
func main() {
var (
err error
offersNo int
types []string
)
if len(settings.exports) == 0 {
types = []string{"gratka", "otodom"}
} else {
types = strings.Split(settings.exports, ",")
for _, typ := range types {
if _, ok := exports[typ]; !ok {
log.Fatalf("Unsupported export name %s", typ)
}
}
}
if err = setCredentials("db"); err != nil {
log.Printf("Incorrect password for DB\n")
return
}
startTime := time.Now()
// validate paths, remove / create directory
if err = validPaths(settings); err != nil {
utils.LogErrf(err, "Settings paths")
}
if !settings.sendOnly {
// dump to XML
log.Printf("Start getting data from DB\n")
offersNo, err = dumpAsXML(settings)
utils.LogErr(err, "Dump XML")
log.Printf("Downloaded %d offers from DB (in %v)\n", offersNo, time.Now().Sub(startTime))
}
for _, name := range types {
if err = exports[name](name, settings); err != nil {
utils.LogErrf(err, "Error durning process %s", name)
}
}
log.Printf("All done in %v\n", time.Now().Sub(startTime))
}