-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathmain.go
211 lines (175 loc) · 5.71 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
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
package main
import (
"compress/gzip"
"encoding/csv"
"encoding/json"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"math/rand"
"net/http"
"os"
"regexp"
"strconv"
"github.com/fatih/color"
"github.com/gosuri/uiprogress"
)
var (
g = color.New(color.FgHiGreen)
y = color.New(color.FgHiYellow)
r = color.New(color.FgHiRed)
)
func main() {
// Init progress bar
uiprogress.Start()
// Flags
concurency := flag.Int("concurency", 5, "the number of goroutines that are allowed to run concurrently")
limit := flag.Int("limit", 100, "number of keywords to collect")
keywordToUse := flag.String("keyword", "", "keyword to use")
flag.Parse()
if *keywordToUse == "" {
r.Println("KeyWord is missing. To view help enter: akrt -help")
os.Exit(1)
}
g.Printf("Collect %d relevant keywords to the keyword '%s' \n", *limit, *keywordToUse)
// Keyword Collector progress bar
keywordBar := uiprogress.AddBar(*limit).AppendCompleted().PrependElapsed()
keywordBar.PrependFunc(func(b *uiprogress.Bar) string {
return fmt.Sprintf("Keywords (%d/%d)", b.Current(), *limit)
})
// Limiting concurent requests to collect keywords
concurrentGoroutines := make(chan struct{}, *concurency)
keyword := Keyword{*keywordToUse, 0}
keyWordList := make(map[string]Keyword)
keyChannel := make(chan Keyword)
go requestKeyWords(keyChannel, keyword)
toLongKeys := 0
for item := range keyChannel {
if len(keyWordList) >= *limit {
break
}
if toLongKeys > 10 {
break
}
if item.Keyword == "" {
toLongKeys++
} else {
if _, ok := keyWordList[item.Keyword]; !ok {
keywordBar.Incr()
keyWordList[item.Keyword] = item
go func(item Keyword) {
concurrentGoroutines <- struct{}{}
requestKeyWords(keyChannel, item)
<-concurrentGoroutines
}(item)
}
}
}
// Limiting concurent requests to collect number of products per keyword
concurrentGoroutinesProductCount := make(chan struct{}, 5)
totalResultCount := make(chan Keyword)
// Product Count Collector progress bar
productCountBar := uiprogress.AddBar(len(keyWordList)).AppendCompleted().PrependElapsed()
productCountBar.PrependFunc(func(b *uiprogress.Bar) string {
return fmt.Sprintf("Product Count (%d/%d)", b.Current(), len(keyWordList))
})
for key := range keyWordList {
go func(item Keyword) {
concurrentGoroutinesProductCount <- struct{}{}
keywordMetadata(totalResultCount, item)
<-concurrentGoroutinesProductCount
}(keyWordList[key])
}
products := 0
productCountBar.Incr()
for item := range totalResultCount {
productCountBar.Incr()
products++
keyWordList[item.Keyword] = item
if products >= len(keyWordList) {
close(totalResultCount)
}
}
// Saving result to the CSV file
records := [][]string{
{"#", "key_words", "total_products"},
}
csvFile, err := os.Create(*keywordToUse + ".csv")
if err != nil {
log.Fatalf("Failed creating file: %s", err)
}
csvwriter := csv.NewWriter(csvFile)
count := 1
for key := range keyWordList {
totalProducts := strconv.FormatInt(keyWordList[key].TotalResultCount, 10)
records = append(records, []string{strconv.Itoa(count), keyWordList[key].Keyword, totalProducts})
count++
}
csvwriter.WriteAll(records)
y.Printf("Collected %d keywords: '%s.csv' \n", len(keyWordList), *keywordToUse)
}
func requestKeyWords(keyChannel chan Keyword, keyword Keyword) {
client := http.Client{}
req, _ := http.NewRequest("GET", "https://completion.amazon.com/api/2017/suggestions", nil)
req.Header.Set("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_"+strconv.Itoa(rand.Intn(15-9)+9)+"_1) AppleWebKit/531.36 (KHTML, like Gecko) Chrome/"+strconv.Itoa(rand.Intn(79-70)+70)+".0.3945.130 Safari/531.36")
q := req.URL.Query()
q.Add("mid", "ATVPDKIKX0DER")
q.Add("alias", "aps")
q.Add("fresh", "0")
q.Add("ks", "88")
q.Add("prefix", keyword.Keyword)
q.Add("event", "onKeyPress")
q.Add("limit", "11")
req.URL.RawQuery = q.Encode()
resp, err := client.Do(req)
if err != nil {
log.Fatalln(err)
}
var result KeywordSuggestions
json.NewDecoder(resp.Body).Decode(&result)
if len(result.Suggestions) == 0 {
log.Fatalln("No keywords found")
}
if len(result.Suggestions) == 1 {
keyChannel <- Keyword{"", 0}
} else {
for _, item := range result.Suggestions {
keyChannel <- Keyword{item.Value, 0}
}
}
}
func keywordMetadata(totalResultCount chan Keyword, keyword Keyword) {
client := http.Client{}
req, _ := http.NewRequest("GET", "https://www.amazon.com/s", nil)
req.Header.Set("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_"+strconv.Itoa(rand.Intn(15-9)+9)+"_1) AppleWebKit/531.36 (KHTML, like Gecko) Chrome/"+strconv.Itoa(rand.Intn(79-70)+70)+".0.3945.130 Safari/531.36")
req.Header.Set("Origin", "https://www.amazon.com")
req.Header.Set("Referer", "https://www.amazon.com/")
req.Header.Set("Accept-Encoding", "gzip")
req.Header.Set("Accept-Language", "en-US,en;q=0.9,ru;q=0.8")
req.Header.Set("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9")
q := req.URL.Query()
q.Add("i", "aps")
q.Add("k", keyword.Keyword)
q.Add("ref", "nb_sb_noss")
q.Add("url", "search-alias=aps")
req.URL.RawQuery = q.Encode()
r, err := client.Do(req)
if err != nil {
log.Fatalln(err)
}
var reader io.ReadCloser
reader, _ = gzip.NewReader(r.Body)
dataInBytes, _ := ioutil.ReadAll(reader)
pageContent := string(dataInBytes)
reTotalCount := regexp.MustCompile(`(\w*"totalResultCount":\w*)(.[0-9])`)
res := reTotalCount.FindAllString(string(pageContent), -1)
var total int64 = 0
if len(res) > 0 {
reCount := regexp.MustCompile(`[-]?\d[\d,]*[\.]?[\d{2}]*`)
submatchall := reCount.FindAllString(res[0], -1)
total, _ = strconv.ParseInt(submatchall[0], 0, 64)
}
totalResultCount <- Keyword{keyword.Keyword, total}
}