-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.__12go
245 lines (202 loc) · 5.49 KB
/
main.__12go
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
237
238
239
240
241
242
243
244
245
package main
import (
"encoding/xml"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
"time"
)
var (
WarningLogger *log.Logger
InfoLogger *log.Logger
ErrorLogger *log.Logger
)
type Priceru_feed struct {
XMLName xml.Name
Shop []Shop `xml:"shop"`
}
type Shop struct {
XMLName xml.Name `xml:"shop"`
Offers []Offers `xml:"offers"`
}
type Offers struct {
XMLName xml.Name `xml:"offers"`
Offer []Offer `xml:"offer"`
}
type Offer struct {
XMLName xml.Name `xml:"offer"`
Id string `xml:"id,attr"`
VendorCode string `xml:"vendorCode"`
Picture []string `xml:"picture"`
}
type xmlname struct {
name string
fileUrl string
path string
catalogPhoto string
}
func init() {
//InfoLogger.Println("Starting the application...")
//InfoLogger.Println("Something noteworthy happened")
//WarningLogger.Println("There is something you should know about")
//ErrorLogger.Println("Something went wrong")
file, err := os.OpenFile("logs2.txt", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)
if err != nil {
log.Fatal(err)
}
InfoLogger = log.New(file, "INFO: ", log.Ldate|log.Ltime|log.Lshortfile)
WarningLogger = log.New(file, "WARNING: ", log.Ldate|log.Ltime|log.Lshortfile)
ErrorLogger = log.New(file, "ERROR: ", log.Ldate|log.Ltime|log.Lshortfile)
}
func main() {
start := time.Now()
first := xmlname{
name: "priceru",
fileUrl: "https://loveyouhome.ua/index.php?route=extension/feed/unixml/priceru",
path: "C:\\PriceLove\\priceru.xml",
catalogPhoto: "C:\\PriceYUG\\photo\\",
//path: "./priceru.xml",
//catalogPhoto: "./photo/",
}
//second := xmlname{
// name: "tomasby",
// fileUrl: "https://loveyouhome.ua/index.php?route=extension/feed/unixml/tomasby",
// //path: "C:\\PriceLove\\tomasby.xml",
// //catalogPhoto: "C:\\PriceYUG\\photo\\",
// path: "./tomasby.xml",
// catalogPhoto: "./photo/",
//}
runParse(first)
InfoLogger.Println(first.name, "Done")
//runParse(second)
//InfoLogger.Println(second.name, "Done")
duration := time.Since(start)
fmt.Println("Время выполнения: ", duration)
}
func runParse(p xmlname) {
p.DownloadPrice()
p.ParseXml()
}
func (x *xmlname) ParseXml() {
results := make(map[string]string)
xmlFile, err := os.Open(x.path)
// if we os.Open returns an error then handle it
if err != nil {
ErrorLogger.Println(err)
}
fmt.Println("start...")
// defer the closing of our xmlFile so that we can parse it later on
defer xmlFile.Close()
// read our opened xmlFile as a byte array.
byteValue, _ := ioutil.ReadAll(xmlFile)
// we initialize our Users array
var users Priceru_feed
// we unmarshal our byteArray which contains our
// xmlFiles content into 'users' which we defined above
xml.Unmarshal(byteValue, &users)
// we iterate through every user within our users array and
// print out the user Type, their name, and their facebook url
// as just an example
start := time.Now()
for _, offer := range users.Shop[0].Offers[0].Offer {
if offer.VendorCode != "62021" {
con
}
replacer := strings.NewReplacer("\\", "", "/", "", ",", "", " ", "", ".", "")
out := replacer.Replace(offer.VendorCode)
catalog := x.catalogPhoto + out + "_L"
for _, picture := range offer.Picture {
InfoLogger.Println(offer.VendorCode + "_L")
results[picture] = catalog
}
}
duration := time.Since(start)
fmt.Println("Время выполнения: ", duration)
for picture, catalog := range results {
DownloadPhoto(picture, catalog)
}
}
// DownloadFile will download a url to a local file. It's efficient because it will
// write as it downloads and not load the whole file into memory.
func (x *xmlname) DownloadPrice() {
// Get the data
resp, err := http.Get(x.fileUrl)
if err != nil {
ErrorLogger.Println(err)
}
defer resp.Body.Close()
// Create the file
out, err := os.Create(x.path)
if err != nil {
ErrorLogger.Println(err)
}
defer out.Close()
// Write the body to file
_, err = io.Copy(out, resp.Body)
if err != nil {
ErrorLogger.Println(err)
}
}
func Exists(name string) bool {
if _, err := os.Stat(name); err != nil {
if os.IsNotExist(err) {
return false
}
}
return true
}
func replaceString(str string) string {
replacer := strings.NewReplacer("\\", "", "/", "", ",", "", " ", "", ".", "")
//out = replacer.Replace(v.Code)
lenname := len(strings.Split(str, ".")) - 1
splitName := strings.SplitN(str, ".", lenname)
if lenname == 1 {
return str
} else {
out := replacer.Replace(splitName[0])
return out + splitName[1]
}
}
func DownloadPhoto(url string, catalog string) {
//fileName := catalog + "/" + url[strings.LastIndex(url, "/")+1:]
fileName := catalog + "/" + replaceString(url[strings.LastIndex(url, "/")+1:])
fi, err := os.Stat(fileName)
if os.IsNotExist(err) {
os.Mkdir(catalog, 0755)
}
if Exists(fileName) {
if err == nil {
if fi.Size() < 1000 {
os.Remove(fileName)
}
}
} else {
//fi, err := os.Stat(fileName)
//if err != nil {
response, _ := http.Get(url)
defer response.Body.Close()
if response.StatusCode == 200 {
//Create a empty file
// разбить файл и реплейснуть
file, err := os.Create(fileName)
if err != nil {
fmt.Println(err)
}
defer file.Close()
//Write the bytes to the fiel
io.Copy(file, response.Body)
fi, err := file.Stat()
//fmt.Println(fmt.Sprintf("File: %s, size: %s", file, fi.Size()))
if err == nil {
if fi.Size() < 1000 && err == nil {
//file.Close()
os.Remove(fileName)
}
}
}
}
}