-
Notifications
You must be signed in to change notification settings - Fork 0
/
logic.go
173 lines (134 loc) Β· 4.67 KB
/
logic.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
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"net/smtp"
"os"
"strings"
"time"
"github.com/google/uuid"
"github.com/joho/godotenv"
"github.com/kristiyann/af1-spider/models"
)
type Logic interface {
HandleGet(params GetProductParams) error
}
type logicImpl struct{}
func NewLogic() Logic {
return &logicImpl{}
}
type GetProductParams struct {
Url string
Size string
}
func (l *logicImpl) HandleGet(params GetProductParams) error {
resp := getProductFromNikeApi(params)
defer resp.Body.Close()
// APPARENTLY json stucture varies between products so I guess I have to do this??
responseModel := decodeNikeProductResponse1(resp)
if responseModel == nil {
return handleNikeAlternate(params)
}
product := responseModel.Objects[0]
skuList := product.ProductInfo[0].Skus
var desiredSku models.NikeProductSku
for i := 0; i < len(skuList); i++ {
if skuList[i].NikeSize == params.Size {
desiredSku = skuList[i]
break
}
}
availableSkuList := product.ProductInfo[0].AvailableSkus
for i := 0; i < len(availableSkuList); i++ {
if availableSkuList[i].ID == desiredSku.ID {
fmt.Println("request-id: " + uuid.New().String())
fmt.Printf("product: %s \n", product.ProductInfo[0].ProductContent.FullTitle)
fmt.Printf("size: %s \n", desiredSku.NikeSize)
fmt.Printf("available: %t \n", availableSkuList[i].Available)
fmt.Println("request-time: " + time.Now().Local().String() + "\n")
if availableSkuList[i].Available {
sendEmail(product.ProductInfo[0].ProductContent.FullTitle, params.Size, params.Url)
} else {
time.Sleep(15 * time.Second)
l.HandleGet(params)
}
}
}
return nil
}
func handleNikeAlternate(params GetProductParams) error {
resp := getProductFromNikeApi(params)
defer resp.Body.Close()
responseModel := decodeNikeProductResponse2(resp)
if responseModel == nil {
log.Fatalln("error decoding json")
}
product := responseModel.Objects[0]
skuList := product.ProductInfo[0].Skus
var desiredSku models.NikeProductSku
for i := 0; i < len(skuList); i++ {
if skuList[i].NikeSize == params.Size {
desiredSku = skuList[i]
break
}
}
availableSkuList := product.ProductInfo[0].AvailableSkus
for i := 0; i < len(availableSkuList); i++ {
if availableSkuList[i].ID == desiredSku.ID {
fmt.Println("request-id: " + uuid.New().String())
fmt.Printf("product: %s \n", product.ProductInfo[0].ProductContent.FullTitle)
fmt.Printf("size: %s \n", desiredSku.NikeSize)
fmt.Printf("available: %t \n", availableSkuList[i].Available)
fmt.Println("request-time: " + time.Now().Local().String() + "\n")
if availableSkuList[i].Available {
sendEmail(product.ProductInfo[0].ProductContent.FullTitle, params.Size, params.Url)
} else {
time.Sleep(15 * time.Second)
handleNikeAlternate(params)
}
}
}
return nil
}
func sendEmail(productName string, size string, link string) {
err := godotenv.Load(".env")
if err != nil {
log.Fatal("Error loading .env file")
}
fromEmail := os.Getenv("FROM_EMAIL")
fromPassword := os.Getenv("FROM_PASS")
toEmail := os.Getenv("TO_EMAIL")
auth := smtp.PlainAuth("", fromEmail, fromPassword, "smtp.gmail.com")
body := "Subject: AF1SPIDER - Product Available\n" + productName + " is available in size " + size + " - " + "https://" + link
smtp.SendMail("smtp.gmail.com:587", auth, fromEmail, []string{toEmail}, []byte(body))
}
func decodeNikeProductResponse1(r *http.Response) *models.NikeProductResponse {
var responseModel models.NikeProductResponse
err := json.NewDecoder(r.Body).Decode(&responseModel)
if err != nil {
log.Println("error decoding json of type 1: " + err.Error() + " (moving on to option 2)")
}
return &responseModel
}
func decodeNikeProductResponse2(r *http.Response) *models.NikeProductResponse2 {
var responseModel models.NikeProductResponse2
err := json.NewDecoder(r.Body).Decode(&responseModel)
if err != nil {
log.Println("error decoding json: " + err.Error())
}
return &responseModel
}
func getProductFromNikeApi(params GetProductParams) *http.Response {
params.Url = strings.Replace(params.Url, "https://", "", -1)
strArr := strings.Split(params.Url, "/")
identifier := strArr[len(strArr)-1]
marketplace := strArr[1]
actualUrl := "https://api.nike.com/product_feed/threads/v2?filter=language(en-GB)&filter=marketplace(" + strings.ToUpper(marketplace) + ")&filter=channelId(d9a5bc42-4b9c-4976-858a-f159cf99c647)&filter=productInfo.merchProduct.styleColor(" + identifier + ")"
resp, err := http.Get(actualUrl)
if err != nil {
log.Fatalln(err.Error())
}
return resp
}