forked from mindsgn-studio/amazon-scraper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
257 lines (211 loc) · 5.65 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
package main
import (
"context"
"fmt"
"net/url"
"os"
"regexp"
"strconv"
"time"
"github.com/gocolly/colly"
"github.com/joho/godotenv"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
var mongoClient *mongo.Client
var ctx context.Context
var totalPages int = 1
type Price struct {
ItemID string `bson:"ItemID"`
Date time.Time `bson:"date"`
Currency string `bson:"currency"`
Price float64 `bson:"price"`
}
func connectDatabase() error {
ctx = context.Background()
err := godotenv.Load()
if err != nil {
return fmt.Errorf("error loading .env file: %w", err)
}
mongoURI := os.Getenv("MONGODB_URI")
mongoClient, err = mongo.Connect(ctx, options.Client().ApplyURI(mongoURI))
if err != nil {
return fmt.Errorf("error connecting to MongoDB: %w", err)
}
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()
err = mongoClient.Ping(ctx, nil)
if err != nil {
return fmt.Errorf("error pinging MongoDB: %w", err)
}
fmt.Println("Connected to MongoDB successfully")
return nil
}
func saveItemPrice(price float64, title string, link string) {
db := mongoClient.Database("snapprice")
itemCollection := db.Collection("items")
pricesCollection := db.Collection("prices")
twelveHoursAgo := time.Now().Add(-12 * time.Hour)
filter := map[string]interface{}{
"title": title,
"link": link,
}
var result map[string]interface{}
err := itemCollection.FindOne(ctx, filter).Decode(&result)
if err != nil {
return
}
if id, ok := result["_id"].(primitive.ObjectID); ok {
itemID := id.Hex()
filter := map[string]interface{}{
"itemID": itemID,
"date": map[string]interface{}{"$gt": twelveHoursAgo},
}
var result map[string]interface{}
err := pricesCollection.FindOne(ctx, filter).Decode(&result)
if err != nil {
newPrice := &Price{
ItemID: itemID,
Date: time.Now(),
Currency: "zar",
Price: price,
}
_, err := pricesCollection.InsertOne(ctx, newPrice)
if err != nil {
fmt.Println("failed to save Price")
return
}
}
}
return
}
func extractPrice(text string) (float64, error) {
re := regexp.MustCompile(`\d+\.\d+`)
match := re.FindString(text)
price, err := strconv.ParseFloat(match, 64)
if err != nil {
return 0, fmt.Errorf("Error parsing price")
}
return price, nil
}
func saveItemData(title string, images []string, link string, id string) {
db := mongoClient.Database("snapprice")
collection := db.Collection("items")
var filter = map[string]interface{}{
"sources.id": id,
}
var update = map[string]interface{}{
"$set": map[string]interface{}{
"title": title,
"images": images,
"link": link,
"updated": time.Now(),
"sources": map[string]interface{}{
"id": id,
"source": "amazon",
},
},
}
upsert := true
_, err := collection.UpdateOne(ctx, filter, update, &options.UpdateOptions{Upsert: &upsert})
if err != nil {
fmt.Println(err)
return
}
}
func getPage(brand string, page int) {
collyClient := colly.NewCollector()
var link = fmt.Sprintf("https://www.amazon.co.za/s?k=%s&page=%d", url.QueryEscape(brand), page)
collyClient.OnHTML("div.s-result-list.s-search-results.sg-row", func(h *colly.HTMLElement) {
h.ForEach("div.a-section.a-spacing-base", func(_ int, cardElement *colly.HTMLElement) {
var name string
var itemLink string
var itemID string = "3232"
var images []string
var price float64
h.ForEach("div.sg-col-4-of-24.sg-col-4-of-12.s-result-item.s-asin.sg-col-4-of-16.sg-col.s-widget-spacing-small.sg-col-4-of-20", func(_ int, cardParent *colly.HTMLElement) {
itemID = cardParent.Attr("data-asin")
})
name = cardElement.ChildText("span.a-size-base-plus.a-color-base.a-text-normal")
text := cardElement.ChildText("span.a-offscreen")
price, err := extractPrice(text)
if err != nil {
return
}
cardElement.ForEach("a.a-link-normal.s-no-outline", func(_ int, h *colly.HTMLElement) {
itemLink = "https://www.amazon.co.za/" + h.Attr("href")
})
cardElement.ForEach("img.s-image", func(_ int, h *colly.HTMLElement) {
images = append(images, h.Attr("src"))
})
saveItemData(name, images, itemLink, itemID)
saveItemPrice(price, name, itemLink)
})
h.ForEach("span.s-pagination-item.s-pagination-disabled", func(_ int, h *colly.HTMLElement) {
if h.Text != "Previous" {
number, err := strconv.Atoi(h.Text)
if err != nil {
return
}
if page == 1 {
totalPages = number
}
}
})
})
collyClient.Visit(link)
collyClient.Wait()
if page >= totalPages {
totalPages = 1
getBrand()
} else {
page++
getPage(brand, page)
}
}
func getBrand() error {
db := mongoClient.Database("snapprice")
collection := db.Collection("items")
pipeline := mongo.Pipeline{
{{"$group", bson.D{
{"_id", "$brand"},
{"count", bson.D{{"$sum", 1}}},
}}},
{{"$project", bson.D{
{"_id", 0},
{"brand", "$_id"},
{"count", 1},
}}},
{{"$sample", bson.D{{"size", 1}}}},
}
cursor, err := collection.Aggregate(ctx, pipeline)
if err != nil {
return fmt.Errorf("error creating aggregation cursor: %w", err)
}
defer func() {
if err := cursor.Close(ctx); err != nil {
fmt.Printf("error closing cursor: %v\n", err)
}
}()
for cursor.Next(ctx) {
var result bson.M
if err := cursor.Decode(&result); err != nil {
return fmt.Errorf("error decoding document: %w", err)
}
brand := result["brand"].(string)
getPage(brand, 1)
}
if err := cursor.Err(); err != nil {
return fmt.Errorf("cursor error: %w", err)
}
return nil
}
func main() {
err := connectDatabase()
if err != nil {
panic(err)
}
getBrand()
}