-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
67 lines (52 loc) · 1.41 KB
/
client.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
package amazon_scraper
import (
"fmt"
"strings"
"github.com/gocolly/colly"
)
type Client struct{}
func New() *Client {
return &Client{}
}
func (c Client) GetAppInfo(asin string) (*AppInfo, error) {
app := &AppInfo{
ID: asin,
}
scraper := colly.NewCollector()
scraper.OnHTML("#btAsinTitle", func(e *colly.HTMLElement) {
app.Title = formatRawText(e.Text)
})
scraper.OnHTML("#mas-product-description", func(e *colly.HTMLElement) {
app.Description = e.Text
app.Description = strings.Replace(app.Description, "Product description", "", -1)
app.Description = formatRawText(app.Description)
})
scraper.OnHTML("#brand", func(e *colly.HTMLElement) {
app.Developer = formatRawText(e.Text)
})
scraper.OnHTML("#js-masrw-main-image", func(e *colly.HTMLElement) {
app.Icon = e.Attr("src")
})
scraper.OnHTML(".masrw-screenshot", func(e *colly.HTMLElement) {
src := e.Attr("src")
if src != "" {
app.Screenshots = append(app.Screenshots, src)
}
})
scraper.OnHTML("[data-hook=\"rating-out-of-text\"]", func(e *colly.HTMLElement) {
app.Rating = e.Text
})
err := scraper.Visit(fmt.Sprintf("http://www.amazon.com/dp/%s", asin))
if err != nil {
switch {
case strings.Contains(err.Error(), "Not Found"):
return nil, ErrNotFound
default:
return nil, ErrUnknown
}
}
return app, nil
}
func formatRawText(raw string) string {
return strings.TrimSpace(strings.Replace(raw, "\n", "", -1))
}