-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
106 lines (88 loc) · 2.38 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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strconv"
"strings"
"time"
"github.com/aiomonitors/godiscord"
)
type Products struct {
MainItems []MainItem `json:"products"`
}
type MainItem struct {
Title string `json:"title"`
Id int64 `json:"id"`
Handle string `json:"handle"`
Sizes []Size `json:"variants"`
Images []Image `json:"images"`
}
type Size struct {
Title string `json:"title"`
Sku string `json:"sku"`
Price string `json:"price"`
InStock bool `json:"available"`
}
type Image struct {
Src string `json:"src"`
}
var SentProductsIds = make([]int64, 0)
func ReleasesMonitor() {
for {
client := &http.Client{}
link := "https://www.a-ma-maniere.com/collections/releases/products.json"
req, reqerr := client.Get(link)
if reqerr != nil {
fmt.Printf("Could not send the request: %s", reqerr)
}
req.Header.Set("Content-Type", "application/json")
resp, resperr := ioutil.ReadAll(req.Body)
if resperr != nil {
fmt.Printf("Could not get the request response!: %s", resperr)
}
_ = req.Body.Close()
var products Products
err := json.Unmarshal(resp, &products)
if err != nil {
fmt.Printf("Could not unmarshal the body: %s", err)
}
for _, product := range products.MainItems {
// this checks if the current product has been sent in a webhook. if true, skip the current product, and check the next one.
if contains(SentProductsIds, product.Id) {
continue
} else {
sendWebhook(product, "webhook")
time.Sleep(time.Millisecond * 500)
SentProductsIds = append(SentProductsIds, product.Id)
}
}
time.Sleep(time.Second * 30)
SentProductsIds = nil
}
}
func sendWebhook(product MainItem, webhook string) {
emb := godiscord.NewEmbed(product.Title, "", fmt.Sprintf("https://www.a-ma-maniere.com/collections/releases/products/%s", product.Handle))
ImageString := product.Images[0].Src
ImageUrl := strings.Replace(ImageString, `\`, "", -1)
emb.SetThumbnail(ImageUrl)
emb.SetColor("#ffc0cb")
emb.SetFooter("@RixtiRobotics", "")
for _, size := range product.Sizes {
emb.AddField(fmt.Sprintf("Size[%s]", size.Title), fmt.Sprintln("STOCK: "+strconv.FormatBool(size.InStock)), true)
}
emb.Username = "A-ma-Maniere Monitor"
emb.SendToWebhook(webhook)
}
func main() {
ReleasesMonitor()
}
func contains(s []int64, e int64) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}