-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathamazon.go
328 lines (300 loc) · 9.38 KB
/
amazon.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
package main
import (
"encoding/json"
"fmt"
query2 "github.com/goark/pa-api/query"
"strings"
"time"
)
var InitHeaders = map[string]string{
"host": "webservices.amazon.com",
"content-type": "application/json; charset=utf-8",
"x-amz-target": "com.amazon.paapi5.v1.ProductAdvertisingAPIv1.GetItems",
"content-encoding": "amz-1.0",
}
var reverse = false
func (apiCredential *ApiCredential) RequestData() {
asins := make([]string, 1)
apiCredential.Jobs.Range(func(key string, value []chan ProductData) bool {
asins = append(asins, key)
return true
})
query := query2.NewGetItems(
apiCredential.Client.Marketplace(),
apiCredential.Client.PartnerTag(),
apiCredential.Client.PartnerType()).ASINs(asins).EnableOffers().EnableItemInfo().EnableImages()
body, err := apiCredential.Client.Request(query)
if strings.Contains(string(body), "provided in the request is invalid.") {
go sendAsinError(apiCredential.PartnerTag, asins)
apiCredential.Jobs = JobsMap{}
return
}
if err != nil || strings.Contains(string(body), "Errors") {
fmt.Println(apiCredential.PartnerTag, "Erroneous response, cooling down")
go sendAccountError(apiCredential.PartnerTag, apiCredential.RequestsSinceError)
/*apiCredential.OnCooldown = true
time.AfterFunc(time.Hour, func() {
apiCredential.OnCooldown = false
})*/
apiCredential.Jobs = JobsMap{}
for _, asin := range asins {
channels, _ := apiCredential.Jobs.Load(asin)
for _, dataChannel := range channels {
assignDataRequest(asin, dataChannel)
}
}
apiCredential.RequestsSinceError = 0
return
}
var response AmazonResponse
json.Unmarshal(body, &response)
for _, item := range response.ItemsResult.Items {
channels, _ := apiCredential.Jobs.Load(item.ASIN)
for _, dataReceiver := range channels {
dataReceiver <- item
}
apiCredential.Jobs.Delete(item.ASIN)
}
apiCredential.RequestsSinceError++
}
func runRequestLoop(credential *ApiCredential) {
ticker := time.Tick(time.Second / time.Duration(credential.RequestsPerSecond))
for time := range ticker {
empty := true
credential.Jobs.Range(func(key string, value []chan ProductData) bool {
if key != "" {
empty = false
}
return false
})
if !empty {
fmt.Println(time, credential.PartnerTag, "Requesting data for the following ASINs: ", credential.Jobs)
credential.RequestData()
}
}
}
func assignDataRequest(asin string, dataChannel chan ProductData) {
last := len(apiCredentials) - 1
/*sort.Slice(apiCredentials, func(i, j int) bool {
return len(apiCredentials[i].Jobs) > len(apiCredentials[j].Jobs)
})*/
for i := range apiCredentials {
if _, ok := apiCredentials[i].Jobs.Load(asin); ok {
channels, _ := apiCredentials[i].Jobs.Load(asin)
apiCredentials[i].Jobs.Store(asin, append(channels, dataChannel))
reverse = !reverse
return
}
}
for i := range apiCredentials {
if reverse {
i = last - i
}
if getJobLength(apiCredentials[i].Jobs) <= 10 && !apiCredentials[i].OnCooldown {
channels, _ := apiCredentials[i].Jobs.Load(asin)
apiCredentials[i].Jobs.Store(asin, append(channels, dataChannel))
reverse = !reverse
return
}
}
time.AfterFunc(time.Millisecond*100, func() {
assignDataRequest(asin, dataChannel)
})
}
func getJobLength(jobsMap JobsMap) int {
count := 0
jobsMap.Range(func(key string, value []chan ProductData) bool {
count++
return true
})
return count
}
type AmazonResponse struct {
ItemsResult struct {
Items []ProductData `json:"Items"`
} `json:"ItemsResult"`
}
type ProductData struct {
ASIN string
Images struct {
Primary struct {
Large struct {
Height int `json:"Height"`
URL string `json:"URL"`
Width int `json:"Width"`
} `json:"Large"`
Medium struct {
Height int `json:"Height"`
URL string `json:"URL"`
Width int `json:"Width"`
} `json:"Medium"`
Small struct {
Height int `json:"Height"`
URL string `json:"URL"`
Width int `json:"Width"`
} `json:"Small"`
} `json:"Primary"`
Variants []struct {
Large struct {
Height int `json:"Height"`
URL string `json:"URL"`
Width int `json:"Width"`
} `json:"Large"`
Medium struct {
Height int `json:"Height"`
URL string `json:"URL"`
Width int `json:"Width"`
} `json:"Medium"`
Small struct {
Height int `json:"Height"`
URL string `json:"URL"`
Width int `json:"Width"`
} `json:"Small"`
} `json:"Variants"`
} `json:"Images"`
ItemInfo struct {
ByLineInfo struct {
Brand struct {
DisplayValue string `json:"DisplayValue"`
Label string `json:"Label"`
Locale string `json:"Locale"`
} `json:"Brand"`
Manufacturer struct {
DisplayValue string `json:"DisplayValue"`
Label string `json:"Label"`
Locale string `json:"Locale"`
} `json:"Manufacturer"`
} `json:"ByLineInfo"`
Classifications struct {
Binding struct {
DisplayValue string `json:"DisplayValue"`
Label string `json:"Label"`
Locale string `json:"Locale"`
} `json:"Binding"`
ProductGroup struct {
DisplayValue string `json:"DisplayValue"`
Label string `json:"Label"`
Locale string `json:"Locale"`
} `json:"ProductGroup"`
} `json:"Classifications"`
ExternalIds struct {
EANs struct {
DisplayValues []string `json:"DisplayValues"`
Label string `json:"Label"`
Locale string `json:"Locale"`
} `json:"EANs"`
UPCs struct {
DisplayValues []string `json:"DisplayValues"`
Label string `json:"Label"`
Locale string `json:"Locale"`
} `json:"UPCs"`
} `json:"ExternalIds"`
Features struct {
DisplayValues []string `json:"DisplayValues"`
Label string `json:"Label"`
Locale string `json:"Locale"`
} `json:"Features"`
ManufactureInfo struct {
ItemPartNumber struct {
DisplayValue string `json:"DisplayValue"`
Label string `json:"Label"`
Locale string `json:"Locale"`
} `json:"ItemPartNumber"`
Model struct {
DisplayValue string `json:"DisplayValue"`
Label string `json:"Label"`
Locale string `json:"Locale"`
} `json:"Model"`
} `json:"ManufactureInfo"`
ProductInfo struct {
IsAdultProduct struct {
DisplayValue bool `json:"DisplayValue"`
Label string `json:"Label"`
Locale string `json:"Locale"`
} `json:"IsAdultProduct"`
ItemDimensions struct {
Height struct {
DisplayValue int `json:"DisplayValue"`
Label string `json:"Label"`
Locale string `json:"Locale"`
Unit string `json:"Unit"`
} `json:"Height"`
Length struct {
DisplayValue float64 `json:"DisplayValue"`
Label string `json:"Label"`
Locale string `json:"Locale"`
Unit string `json:"Unit"`
} `json:"Length"`
Weight struct {
DisplayValue float64 `json:"DisplayValue"`
Label string `json:"Label"`
Locale string `json:"Locale"`
Unit string `json:"Unit"`
} `json:"Weight"`
Width struct {
DisplayValue float64 `json:"DisplayValue"`
Label string `json:"Label"`
Locale string `json:"Locale"`
Unit string `json:"Unit"`
} `json:"Width"`
} `json:"ItemDimensions"`
ReleaseDate struct {
DisplayValue time.Time `json:"DisplayValue"`
Label string `json:"Label"`
Locale string `json:"Locale"`
} `json:"ReleaseDate"`
} `json:"ProductInfo"`
Title struct {
DisplayValue string `json:"DisplayValue"`
Label string `json:"Label"`
Locale string `json:"Locale"`
} `json:"Title"`
} `json:"ItemInfo"`
Offers struct {
Listings []struct {
Availability struct {
Message string `json:"Message"`
MinOrderQuantity int `json:"MinOrderQuantity"`
Type string `json:"Type"`
} `json:"Availability"`
Condition struct {
SubCondition struct {
Value string `json:"Value"`
} `json:"SubCondition"`
Value string `json:"Value"`
} `json:"Condition"`
DeliveryInfo struct {
IsAmazonFulfilled bool `json:"IsAmazonFulfilled"`
IsFreeShippingEligible bool `json:"IsFreeShippingEligible"`
IsPrimeEligible bool `json:"IsPrimeEligible"`
} `json:"DeliveryInfo"`
ID string `json:"Id"`
Promotions []struct {
Type string `json:"Type"`
Amount float64 `json:"Amount"`
Currency string `json:"Currency"`
DiscountPercent int `json:"DiscountPercent"`
PricePerUnit float64 `json:"PricePerUnit"`
DisplayAmount string `json:"DisplayAmount"`
} `json:"Promotions"`
IsBuyBoxWinner bool `json:"IsBuyBoxWinner"`
MerchantInfo struct {
DefaultShippingCountry string `json:"DefaultShippingCountry"`
FeedbackCount int `json:"FeedbackCount"`
FeedbackRating float64 `json:"FeedbackRating"`
ID string `json:"Id"`
Name string `json:"Name"`
} `json:"MerchantInfo"`
Price struct {
Amount float64 `json:"Amount"`
Currency string `json:"Currency"`
DisplayAmount string `json:"DisplayAmount"`
} `json:"Price"`
ProgramEligibility struct {
IsPrimeExclusive bool `json:"IsPrimeExclusive"`
IsPrimePantry bool `json:"IsPrimePantry"`
} `json:"ProgramEligibility"`
ViolatesMAP bool `json:"ViolatesMAP"`
} `json:"Listings"`
} `json:"Offers"`
}