This repository has been archived by the owner on Feb 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
listen_jd_v2.go
197 lines (186 loc) · 4.92 KB
/
listen_jd_v2.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strings"
"time"
browser "github.com/EDDYCJY/fake-useragent"
)
// ListenMaskV2 - Listen Mask V2
type ListenMaskV2 struct {
listenClient *http.Client
skuNum string
listenURL string
reqStocks *http.Request
skuState map[string]bool
skuLimitState map[string]bool
doAction func(string)
stocks map[string]map[string]interface{}
randomUA string
}
func (lm *ListenMaskV2) initHTTPClient() {
logger.Infoln("[+] [V2] Init Http Client and Cookies")
lm.listenClient = &http.Client{
Transport: tr,
}
}
func (lm *ListenMaskV2) getSkuNumStr() {
logger.Infoln("[+] [V2] Make SkuNum")
skuIds := []string{}
for skuid := range skuMetas {
skuIds = append(skuIds, skuid)
}
lm.skuNum = strings.Join(skuIds, ",")
}
func (lm *ListenMaskV2) genListenReq() {
// Stocks
stockURL, err := url.Parse("https://c0.3.cn/stocks")
if err != nil {
return
}
params := url.Values{}
params.Set("type", "getstocks")
params.Set("skuIds", lm.skuNum)
params.Set("area", areaNext)
params.Set("callback", getCallback())
params.Set("_", getRandomTs())
stockURL.RawQuery = params.Encode()
lm.listenURL = stockURL.String()
logger.Infoln("[+] StockURL", lm.listenURL)
lm.reqStocks, err = http.NewRequest("GET", lm.listenURL, nil)
if err != nil {
logger.Errorln("[-] GLR::NewRequest::Stocks", err.Error())
}
}
func (lm *ListenMaskV2) listenStocks() {
// lm.subListenMask()
// defer wg.Done()
lm.reqStocks.Header.Set("User-Agent", lm.randomUA)
resp, err := lm.listenClient.Do(lm.reqStocks)
if err != nil {
logger.Errorln("[-] LS::DoRequest", err.Error())
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
logger.Errorln("[-] LS::ReadBody", err.Error())
return
}
body = getCallbackBody(body, "LS")
lm.stocks = make(map[string]map[string]interface{})
err = json.Unmarshal(body, &lm.stocks)
if err != nil {
logger.Errorln("[-] LS::UnmarshalBody", err.Error())
logger.Warnln(string(body))
}
}
func (lm *ListenMaskV2) listenLimit() {
data := url.Values{}
data.Set("skus", lm.skuNum)
reqLimit, err := http.NewRequest("POST", "https://cart.jd.com/getLimitInfo.action", strings.NewReader(data.Encode()))
if err != nil {
logger.Errorln("[-] GLR::NewRequest::Listen", err.Error())
}
reqLimit.Header.Set("User-Agent", lm.randomUA)
reqLimit.Header.Add("Content-Type", "application/x-www-form-urlencoded")
resp, err := lm.listenClient.Do(reqLimit)
if err != nil {
logger.Errorln("[-] LL::DoRequest", err.Error())
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
logger.Errorln("[-] LL::ReadBody", err.Error())
return
}
result := make(map[string]interface{})
err = json.Unmarshal(body, &result)
if err != nil {
logger.Errorln("[-] LL::UnmarshalBody", err.Error())
logger.Infoln(string(body))
return
}
if result["limitResult"] == nil {
fmt.Println(result)
return
}
limit := result["limitResult"].(string)
limitMsg := make([]string, 0)
for skuid := range lm.skuState {
if strings.Contains(limit, skuid) {
lm.skuLimitState[skuid] = false
limitMsg = append(limitMsg, skuid)
} else {
lm.skuLimitState[skuid] = true
}
}
logger.Infoln("[!] Limit:", strings.Join(limitMsg, ","))
}
func (lm *ListenMaskV2) subListenMask() {
for skuid, stock := range lm.stocks {
// Not onsell or already doAction
if !lm.skuState[skuid] || !lm.skuLimitState[skuid] {
continue
}
// StockState 库存状态编号 33 = 现货,39,40,36,34 = 无货
// skuState 上柜状态编号 0 = 下柜, 1 = 上柜
if stock["StockState"].(float64) == 33 && stock["skuState"].(float64) == 1 {
lm.skuState[skuid] = false
// Notice
go func(skuid string) {
msg := fmt.Sprintf("[ABML] Congratulation!!! https://item.jd.com/%s.html 有货!", skuid)
sendBotMsg(msg)
logger.Infoln("[+]", msg)
}(skuid)
// 下单相关
lm.doAction(skuid)
time.Sleep(1 * time.Second)
// Reset State
go func(skuid string) {
time.Sleep(time.Duration(waittime) * time.Second)
lm.skuState[skuid] = true
}(skuid)
}
}
}
func (lm *ListenMaskV2) listenMask() {
lm.getSkuNumStr()
lm.genListenReq()
i := 0
for {
if stop {
break
}
if i%(60*100) == 0 {
go sendBotMsg("[ABML] I am listening...")
}
if i%50 == 0 || i == 0 {
logger.Infoln("[+] httpGetForListen ===== 第", i, "次 =====")
lm.listenLimit()
time.Sleep(1 * time.Second)
}
i++
lm.randomUA = browser.Random()
lm.listenStocks()
lm.subListenMask()
time.Sleep(time.Duration(speed) * time.Second)
}
logger.Infoln("[+] Stop Listener")
}
// RunListenMaskV2 - Run Listen Mask V2
func RunListenMaskV2() *ListenMaskV2 {
lm := &ListenMaskV2{}
lm.initHTTPClient()
lm.skuState = make(map[string]bool, len(skuMetas))
lm.skuLimitState = make(map[string]bool, len(skuMetas))
for skuid := range skuMetas {
lm.skuState[skuid] = true
lm.skuLimitState[skuid] = false
}
return lm
}