-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
317 lines (283 loc) · 8.12 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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
package main
import (
"context"
"database/sql"
"errors"
"fmt"
"io/ioutil"
"log"
"os"
"strings"
"time"
"github.com/chromedp/cdproto/fetch"
"github.com/antchfx/htmlquery"
"golang.org/x/net/html"
_ "github.com/mattn/go-sqlite3"
"github.com/chromedp/cdproto/dom"
"github.com/chromedp/cdproto/input"
"github.com/chromedp/chromedp"
"github.com/chromedp/chromedp/device"
"github.com/mantyr/pricer"
)
func main() {
outputDirName := fmt.Sprintf("log/%d", time.Now().Unix())
itemDirPath := fmt.Sprintf("%s/shop_%s/item_%d", outputDirName, "shop", "item")
os.MkdirAll(itemDirPath, os.ModePerm)
// start()
// fmt.Printf("launch cron")
// c := cron.New()
// _, err := c.AddFunc("@every 6h", start)
// if err != nil {
// fmt.Errorf(err.Error())
// } else {
// c.Run()
// }
}
func start() {
fmt.Printf("\nok, let's go\n")
fmt.Printf("trying to connect to db\n")
db, err := sql.Open("sqlite3", "pricewatcher.db")
if err != nil {
log.Fatal(err)
}
defer db.Close()
fmt.Printf("receive shop items\n")
var shopItems = make(map[Shop][]Item)
shopItems, err = getShopItems(db)
if err != nil {
log.Fatal(err)
}
fmt.Printf("create directory for assets\n")
outputDirName := fmt.Sprintf("log/%d", time.Now().Unix())
fmt.Printf("shop items count: %d\n", len(shopItems))
fmt.Printf("process shop items\n")
for shop, items := range shopItems {
process(shop, items, outputDirName, db)
}
}
func process(shop Shop, items []Item, outputDirName string, db *sql.DB) {
fmt.Printf("process shop items\n")
var err error
for itemIndex := range items {
item := items[itemIndex]
err = receiveAndStoreItemPrice(shop, items[itemIndex], outputDirName, db)
if err != nil {
log.Printf("can't parse price from: %s,\n error: %v\n", item.title, err.Error())
}
}
}
func receiveAndStoreItemPrice(shop Shop, item Item, dir string, db *sql.DB) error {
fmt.Printf("receive price from shop:\n")
fmt.Printf("shop id: %d, item id: %d, item title: %s\n", shop.id, item.id, item.title)
itemDirPath, err, itemScreenshotPath, itemPagePath := createAssetDirs(shop, item, dir)
if err != nil {
fmt.Printf("can't create one of asset dir: %v\n", err)
} else {
pageContent, err := getPageContent(item.link)
if err == nil {
fmt.Printf("store page screenshot to %s\n", itemScreenshotPath)
err = ioutil.WriteFile(itemScreenshotPath, pageContent.screenshoot, 0644)
if err != nil {
fmt.Printf("Can't write page screenshot on disk %v\n", err)
}
fmt.Printf("store page html to %s\n", itemPagePath)
err = ioutil.WriteFile(itemPagePath, pageContent.content, 0644)
if err != nil {
fmt.Printf("Can't write page content on disk %v\n", err)
}
fmt.Printf("parse price \n")
var parsedPrice, err = parsePrice(pageContent, shop, item)
fmt.Printf("item: %s price: %s\n", item.title, parsedPrice)
if err == nil {
err = storePrice(parsedPrice, item, itemDirPath, err, db)
} else {
fmt.Printf("can't parse page. %v\n", err)
}
} else {
fmt.Printf("can't get page content: %v\n", err)
}
}
return err
}
func createAssetDirs(shop Shop, item Item, dir string) (string, error, string, string) {
itemDirPath := fmt.Sprintf("%s/shop_%s/item_%d", dir, shop.title, item.id)
err := os.MkdirAll(itemDirPath, os.ModePerm)
itemScreenshotPath := itemDirPath + "/screenshot.png"
itemPagePath := itemDirPath + "/page.html"
return itemDirPath, err, itemScreenshotPath, itemPagePath
}
func parsePrice(pageContent PageContent, shop Shop, item Item) (string, error) {
var parsedPrice string
var err error
doc, err := htmlquery.Parse(strings.NewReader(string(pageContent.content)))
if err == nil {
parsedPrice, err = getPrice(doc, shop, item)
} else {
fmt.Printf("can't parse page %v\n", err)
}
return parsedPrice, err
}
func storePrice(parsedPrice string, item Item, itemDirPath string, err error, db *sql.DB) error {
price := pricer.NewPrice()
price.SetDefaultType("UNKNOWN")
price.Parse(parsedPrice)
filteredPrice := price.Get()
currency := price.GetType()
stmt := fmt.Sprintf(
"insert into prices (item_id, parsed_price, filtered_price, timestamp, assets_path, currency) values(%d, \"%s\", \"%s\", %d, \"%s\", \"%s\")",
item.id,
parsedPrice,
filteredPrice,
time.Now().Unix(),
itemDirPath,
currency,
)
fmt.Printf("trying to execute: %s\n", stmt)
_, err = db.Exec(stmt)
return err
}
func getPrice(html *html.Node, shop Shop, item Item) (string, error) {
var err error
var price string
priceXPath := fmt.Sprintf("//%s", shop.priceXPath)
priceNode, err := htmlquery.Query(html, priceXPath)
if err == nil {
if priceNode != nil && priceNode.FirstChild != nil {
price = priceNode.FirstChild.Data
} else {
err = errors.New(fmt.Sprintf("can't parse %s", item.title))
}
} else {
log.Fatal(err)
}
return price, err
}
func getShops(db *sql.DB) ([]Shop, error) {
var shops []Shop
rows, err := db.Query("select * from shops")
if err != nil {
return shops, err
}
defer rows.Close()
for rows.Next() {
var id int
var title, titleXPath, pictureXPath, priceXPath string
err = rows.Scan(&id, &title, &titleXPath, &pictureXPath, &priceXPath)
if err != nil {
return shops, err
}
shops = append(shops, Shop{id, title, titleXPath, pictureXPath, priceXPath})
}
return shops, err
}
func getItemsByShop(db *sql.DB, shopId int) ([]Item, error) {
var items []Item
stmt, err := db.Query("select * from items where shop_id = $1", shopId)
if err != nil {
return items, err
}
defer stmt.Close()
for stmt.Next() {
var id, shopId int
var title, link string
err = stmt.Scan(&id, &title, &link, &shopId)
if err != nil {
return items, err
}
items = append(items, Item{id, title, link, shopId})
}
return items, err
}
func getShopItems(db *sql.DB) (map[Shop][]Item, error) {
var shopItems = make(map[Shop][]Item)
shops, err := getShops(db)
if err != nil {
return shopItems, err
}
for _, shop := range shops {
var items []Item
items, err = getItemsByShop(db, shop.id)
if err != nil {
return shopItems, err
}
shopItems[shop] = items
}
return shopItems, err
}
func getPageContent(url string) (PageContent, error) {
fmt.Printf("get page content: %s\n", url)
opts := append(chromedp.DefaultExecAllocatorOptions[:],
chromedp.ProxyServer("use your proxy server"),
)
ctx, cancel := chromedp.NewExecAllocator(context.Background(), opts...)
defer cancel()
ctx, cancel = chromedp.NewContext(ctx)
defer cancel()
lctx, lcancel := context.WithCancel(ctx)
chromedp.ListenTarget(lctx, func(ev interface{}) {
switch ev := ev.(type) {
case *fetch.EventRequestPaused:
go func() {
_ = chromedp.Run(ctx, fetch.ContinueRequest(ev.RequestID))
}()
case *fetch.EventAuthRequired:
if ev.AuthChallenge.Source == fetch.AuthChallengeSourceProxy {
go func() {
_ = chromedp.Run(ctx,
fetch.ContinueWithAuth(ev.RequestID, &fetch.AuthChallengeResponse{
Response: fetch.AuthChallengeResponseResponseProvideCredentials,
Username: "proxy user name",
Password: "prixy user password",
}),
// Chrome will remember the credential for the current instance,
// so we can disable the fetch domain once credential is provided.
// Please file an issue if Chrome does not work in this way.
fetch.Disable(),
)
// and cancel the event handler too.
lcancel()
}()
}
}
})
var pageContent string
var screenshot []byte
err := chromedp.Run(
ctx,
fetch.Enable().WithHandleAuthRequests(true),
chromedp.Emulate(device.IPhone11),
chromedp.Navigate(url),
chromedp.Sleep(5*time.Second),
chromedp.MouseEvent(input.MouseMoved, 15, 20),
chromedp.CaptureScreenshot(&screenshot),
chromedp.ActionFunc(func(ctx context.Context) error {
node, err := dom.GetDocument().Do(ctx)
if err != nil {
return err
}
pageContent, err =
dom.GetOuterHTML().WithNodeID(node.NodeID).Do(ctx)
return err
}),
)
return PageContent{[]byte(pageContent), screenshot}, err
}
// Data
type PageContent struct {
content []byte
screenshoot []byte
}
// Database entities
type Item struct {
id int
title string
link string
shop_id int
}
type Shop struct {
id int
title string
titleXPath string
pictureXPath string
priceXPath string
}