-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
279 lines (234 loc) · 6.37 KB
/
main.go
File metadata and controls
279 lines (234 loc) · 6.37 KB
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
package main
import (
"context"
"fmt"
"io/ioutil"
"math/rand"
"net/url"
"os"
"regexp"
"strconv"
"strings"
"time"
"github.com/gocolly/colly"
"github.com/joho/godotenv"
"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
var total uint64 = 0
var item uint64 = 0
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, 30*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 randomSleep() {
seconds := rand.Intn(10) + 1
time.Sleep(time.Duration(seconds) * time.Second)
}
func saveItemPrice(price float64, title string, link string) {
db := mongoClient.Database("snapprice")
itemCollection := db.Collection("items")
pricesCollection := db.Collection("prices")
twoHoursAgo := time.Now().Add(-2 * 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": twoHoursAgo},
}
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(`R[ \xA0]?([\d \xA0]+,\d{2})`)
match := re.FindStringSubmatch(text)
if len(match) < 2 {
return 0, fmt.Errorf("No price found")
}
// Remove spaces/non-breaking spaces
clean := strings.ReplaceAll(match[1], " ", "")
clean = strings.ReplaceAll(clean, "\u00A0", "")
// Replace comma with dot
clean = strings.Replace(clean, ",", ".", 1)
price, err := strconv.ParseFloat(clean, 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()
collyClient.UserAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
var link = fmt.Sprintf("https://www.amazon.co.za/s?k=%s&page=%d", url.QueryEscape(brand), page)
/*
collyClient.OnRequest(func(r *colly.Request) {
fmt.Println("Visiting", r.URL.String())
})
collyClient.OnError(func(r *colly.Response, err error) {
fmt.Printf("Request to %s failed: %v\n", r.Request.URL, err)
})
*/
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("h2.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"))
})
item++
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()
randomSleep()
fmt.Print("items: ", item, "\tpage: ", page, "/", totalPages)
fmt.Print("\n")
if page >= totalPages {
totalPages = 1
total = 0
item = 0
fmt.Print("\n")
fmt.Print("\n")
getBrand()
} else {
page++
item = 0
getPage(brand, page)
}
}
func getBrand() {
data, err := ioutil.ReadFile("brand.txt")
if err != nil {
fmt.Println("Error reading brand.txt:", err)
return
}
// Split by comma and trim spaces
rawBrands := strings.Split(string(data), ",")
var brands []string
for _, b := range rawBrands {
trimmed := strings.TrimSpace(b)
if trimmed != "" {
brands = append(brands, trimmed)
}
}
if len(brands) == 0 {
fmt.Println("No brands found in brand.txt")
return
}
rand.Seed(time.Now().UnixNano())
brand := brands[rand.Intn(len(brands))]
fmt.Println("================================")
fmt.Println(brand)
fmt.Println("================================")
getPage(brand, 1)
}
func main() {
err := connectDatabase()
if err != nil {
panic(err)
}
getBrand()
}