-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathall_products.go
415 lines (364 loc) · 12 KB
/
all_products.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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
package main
import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
"regexp"
"sync"
"time"
discordwebhook "github.com/bensch777/discord-webhook-golang"
"github.com/rs/zerolog"
"gopkg.in/yaml.v2"
)
const (
HomeURL = "https://store.ui.com/us/en"
ProductsFile = "products.json"
)
var DiscordWebhookURL string
var logger = zerolog.New(
zerolog.ConsoleWriter{
Out: os.Stderr,
TimeFormat: time.RFC3339,
FormatLevel: func(i interface{}) string {
return fmt.Sprintf("[%-6s]", i) // Custom level format in square brackets
},
},
).Level(zerolog.TraceLevel).With().Timestamp().Caller().Logger()
type UnifiStore struct {
BaseURL string
Headers map[string]string
Categories []string
KnownProductIDs map[string]bool
KnownProducts map[string]Product
Mutex sync.Mutex
Initialized bool
}
type Product struct {
ID string `json:"id"`
Title string `json:"title"`
ShortDescription string `json:"shortDescription"`
Slug string `json:"slug"`
Thumbnail Thumbnail `json:"thumbnail"`
Variants []Variant `json:"variants"`
}
type Thumbnail struct {
URL string `json:"url"`
}
type Variant struct {
ID string `json:"id"`
DisplayPrice struct {
Amount int `json:"amount"`
Currency string `json:"currency"`
} `json:"displayPrice"`
}
type PageProps struct {
SubCategories []struct {
Products []Product `json:"products"`
} `json:"subCategories"`
}
type Response struct {
PageProps PageProps `json:"pageProps"`
}
type Config struct {
DiscordWebhookURL string `yaml:"discord_webhook_url"`
}
func CreateUnifiStore() *UnifiStore {
store := &UnifiStore{
Headers: map[string]string{
"accept": "*/*",
"user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36",
},
Categories: []string{
"all-switching",
"all-unifi-cloud-gateways",
"all-wifi",
"all-cameras-nvrs",
"all-door-access",
"all-cloud-keys-gateways",
"all-power-tech",
"all-integrations",
"accessories-cables-dacs",
},
KnownProductIDs: make(map[string]bool),
KnownProducts: make(map[string]Product),
Initialized: false,
}
store.loadKnownProducts()
return store
}
// loadKnownProducts reads the products.json file and loads all known products into the store's maps.
// If the file does not exist, it will be created.
// If the file is malformed, an error is logged and the store is not initialized.
func (store *UnifiStore) loadKnownProducts() {
logger.Info().Msg("Loading known products...")
file, err := os.Open(ProductsFile)
if err != nil {
if os.IsNotExist(err) {
logger.Info().Msg("Products.json file not found, creating new file")
_, err = os.Create(ProductsFile)
if err != nil {
logger.Error().Err(err).Msg("Failed to create products.json file")
return
}
store.Initialized = false
return
}
logger.Error().Err(err).Msg("Failed to load products.json file")
return
}
defer file.Close()
fileInfo, err := file.Stat()
if err != nil {
logger.Error().Err(err).Msg("Failed to get file info")
return
}
if fileInfo.Size() == 0 {
return
}
var products []Product
if err := json.NewDecoder(file).Decode(&products); err != nil {
logger.Error().Err(err).Msg("Failed to decode products.json file")
return
}
for _, product := range products {
store.KnownProductIDs[product.ID] = true
store.KnownProducts[product.ID] = product
}
logger.Info().Msg(fmt.Sprintf("Loaded %d known products", len(store.KnownProductIDs)))
store.Initialized = true
}
// saveKnownProducts writes the current known products to a JSON file.
// It locks the store's mutex to ensure thread safety while accessing
// the KnownProducts map, encodes the products into JSON format, and
// saves them to the file specified by ProductsFile. If an error occurs
// during file creation or encoding, it logs the error.
func (store *UnifiStore) saveKnownProducts(filename string, newProducts []Product) error {
logger.Info().Msg("Saving known products...")
store.Mutex.Lock()
defer store.Mutex.Unlock()
// Read existing products from the file
var existingProducts []Product
file, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE, 0644)
if err != nil {
return fmt.Errorf("failed to open known products file: %v", err)
}
defer file.Close()
if err := json.NewDecoder(file).Decode(&existingProducts); err != nil && err != io.EOF {
return fmt.Errorf("failed to decode existing products: %v", err)
}
// Append new products to the existing products
existingProducts = append(existingProducts, newProducts...)
// Write the combined list back to the file
file.Seek(0, 0)
file.Truncate(0)
if err := json.NewEncoder(file).Encode(existingProducts); err != nil {
return fmt.Errorf("failed to encode products: %v", err)
}
return nil
}
// fetchBuildID attempts to retrieve the build ID from the Unifi store homepage.
// It sends a GET request to the HomeURL and searches the response body for a
// build ID using a regex pattern. If successful, it sets the BaseURL of the
// UnifiStore with the extracted build ID. If the build ID cannot be extracted,
// it logs an error and returns an error.
func (store *UnifiStore) fetchBuildID() error {
logger.Info().Msg("Fetching build ID...")
req, err := http.NewRequest("GET", HomeURL, nil)
if err != nil {
logger.Error().Err(err).Msg("Failed to fetch build ID")
return err
}
for key, value := range store.Headers {
req.Header.Set(key, value)
}
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
logger.Error().Err(err).Msg("Failed to fetch build ID")
return err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
logger.Error().Err(err).Msg("Failed to Read Response")
return err
}
soup := string(body)
regexPattern := `https://assets\.ecomm\.ui\.com/_next/static/([a-zA-Z0-9]+)/_buildManifest\.js`
re := regexp.MustCompile(regexPattern)
matches := re.FindStringSubmatch(soup)
if len(matches) > 1 {
buildID := matches[1]
store.BaseURL = fmt.Sprintf("https://store.ui.com/_next/data/%s/us/en.json", buildID)
logger.Info().Msg(fmt.Sprintf("Extracted build ID: %s", buildID))
return nil
}
return fmt.Errorf("failed to extract build ID")
}
// fetchProducts fetches the products for a given category from the Unifi store.
//
// It takes a category as a string and returns a slice of Product objects and an error.
// If the error is not nil, it should be logged and the program should retry the call.
//
// The products are fetched from the Unifi store with a GET request to the URL
// <BaseURL>?category=<category>&store=us&language=en.
//
// The response is unmarshaled into a struct with a field "pageProps" which contains
// a slice of structs with a field "subCategories" which contains a slice of structs
// with a field "products". The latter is the slice of Product objects that is returned.
//
// The function logs an info message with the category and an error message with the
// error if it occurs.
func (store *UnifiStore) fetchProducts(category string) ([]Product, error) {
// logger.Info().Msg(fmt.Sprintf("Fetching products for category: %s", category))
url := fmt.Sprintf("%s?category=%s&store=us&language=en", store.BaseURL, category)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, fmt.Errorf("failed to create request: %v", err)
}
for k, v := range store.Headers {
req.Header.Set(k, v)
}
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("failed to fetch products: %v", err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("failed to read response body: %v", err)
}
var response Response
if err := json.Unmarshal(body, &response); err != nil {
return nil, fmt.Errorf("failed to unmarshal JSON: %v", err)
}
var products []Product
for _, subCategory := range response.PageProps.SubCategories {
products = append(products, subCategory.Products...)
}
return products, nil
}
func (store *UnifiStore) sendToDiscord(product Product) {
if !store.Initialized {
return
}
fmt.Println("Sending to Discord...")
embed := discordwebhook.Embed{
Title: product.Title,
Color: 15277667,
Url: fmt.Sprintf("https://store.ui.com/us/en/products/%s", product.Slug),
Timestamp: time.Now(),
Thumbnail: discordwebhook.Thumbnail{
Url: product.Thumbnail.URL,
},
Author: discordwebhook.Author{
Name: "🎉 **New Product Alert!** 🎉",
Icon_URL: "https://tse3.mm.bing.net/th?id=OIP.RadjPrUUrLwqfVTEI5YqmwHaIV&pid=Api&P=0&w=300&h=300",
},
Description: fmt.Sprintf("%s\n", product.ShortDescription),
Fields: []discordwebhook.Field{
discordwebhook.Field{
Name: "Variant",
Value: product.Variants[0].ID,
Inline: true,
},
discordwebhook.Field{
Name: "Price",
Value: fmt.Sprintf("$%d.%02d", product.Variants[0].DisplayPrice.Amount/100, product.Variants[0].DisplayPrice.Amount%100),
Inline: true,
},
},
Footer: discordwebhook.Footer{
Text: "Unifi Store Monitor",
Icon_url: "https://tse3.mm.bing.net/th?id=OIP.RadjPrUUrLwqfVTEI5YqmwHaIV&pid=Api&P=0&w=300&h=300",
},
}
hook := discordwebhook.Hook{
Username: "Unifi Store Monitor",
Avatar_url: "https://tse3.mm.bing.net/th?id=OIP.RadjPrUUrLwqfVTEI5YqmwHaIV&pid=Api&P=0&w=300&h=300",
Embeds: []discordwebhook.Embed{embed},
}
payload, err := json.Marshal(hook)
if err != nil {
log.Fatal(err)
}
err = discordwebhook.ExecuteWebhook(DiscordWebhookURL, payload)
if err != nil {
fmt.Printf("Failed to send to Discord: %v\n", err)
}
}
func readEnv(key, defaultValue string) string {
value, exists := os.LookupEnv(key)
if exists {
return value
}
configFile := "/etc/unifi-monitor.yml"
data, err := os.ReadFile(configFile)
if err == nil {
var config Config
if err := yaml.Unmarshal(data, &config); err == nil {
if key == "DISCORD_WEBHOOK_URL" {
return config.DiscordWebhookURL
}
}
}
return defaultValue
}
// Start begins an infinite loop to monitor and fetch new products from the Unifi store.
// It iterates through each category in the store, fetching products and checking if
// they are new by comparing against known product IDs. If a new product is found, it
// is added to the store's known products and a log message is generated. The loop
// sleeps for 30 seconds between each complete iteration. In case of an error while
// fetching products, it logs the error and retries after a 30-second delay.
func (store *UnifiStore) Start() {
logger.Info().Msg("Starting Monitor")
for {
// Need to fetch the build ID in loop in case it changes
if err := store.fetchBuildID(); err != nil {
logger.Fatal().Err(err).Msg("Failed to fetch build ID")
}
for _, category := range store.Categories {
products, err := store.fetchProducts(category)
if err != nil {
logger.Error().Err(err).Msg("Failed to fetch products")
time.Sleep(30 * time.Second)
continue
}
var newProducts []Product
store.Mutex.Lock()
for _, product := range products {
if !store.KnownProductIDs[product.ID] {
store.KnownProductIDs[product.ID] = true
store.KnownProducts[product.ID] = product
logger.Info().Msg(fmt.Sprintf("New Product Alert! ID: %s, Title: %s", product.ID, product.Title))
newProducts = append(newProducts, product)
store.sendToDiscord(product)
}
}
store.Mutex.Unlock()
if len(newProducts) > 0 {
if err := store.saveKnownProducts(ProductsFile, newProducts); err != nil {
logger.Error().Err(err).Msg("Failed to save known products")
}
}
// logger.Info().Msg(fmt.Sprintf("Fetched %d products for category: %s", len(products), category))
}
logger.Info().Msg("Sleeping for 30 seconds...")
time.Sleep(30 * time.Second)
}
}
func main() {
logger.Info().Msg("Initializing...")
DiscordWebhookURL = readEnv("DISCORD_WEBHOOK_URL", "")
if DiscordWebhookURL == "" {
logger.Fatal().Msg("DISCORD_WEBHOOK_URL is not set. Please set it in the environment or in the config file.")
}
store := CreateUnifiStore()
go store.Start()
// Keep the main thread alive
select {}
}