-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdetails.go
163 lines (144 loc) · 3.52 KB
/
details.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"regexp"
"strings"
"sync"
"time"
)
var wg sync.WaitGroup
var err error
var ch = make(chan struct{}, 20)
var client http.Client
var lk sync.Mutex
type retInfo struct {
dstInfo
TM string
APrice string
Discount string
Status string
Baitiao string
NeedYuyue string
YuyueTime string
}
type priceInfo struct {
P string `json:"p"`
Discount string `json:"discount"`
}
type stockInfo struct {
InfoStr string `json:"stockDesc"`
}
type baitiaoInfo struct {
InfoStr string `json:"marketingText"`
}
type yuyueInfo struct {
YuyueTime string `json:"yuyueTime"`
}
type goodsInfo struct {
Price priceInfo `json:"price"`
Stock stockInfo `json:"stockInfo"`
Baitiao string `json:"baitiaoPlanShowResVo"`
Yuyue yuyueInfo `json:"yuyueInfo"`
}
func download(item *dstInfo) {
timeNow := time.Now().Format("2006/01/02 15:04:05")
var req *http.Request
u := "https://item-soa.jd.com/getWareBusiness"
value := url.Values{
"callback": []string{"jQuery2817335"},
"skuId": []string{item.Uid},
//"cat": []string{"670,677,679"},
"area": []string{"17_1381_50713_53756"},
//"shopId": []string{"1000094211"},
//"venderId": []string{"1000094211"},
//"paramJson": []string{`{"platform2":"1","specialAttrStr":"p0pp1ppppppp2ppppppppppp","skuMarkStr":"00"}`},
//"num": []string{"1"},
}
req, err = http.NewRequest("GET", u, nil)
req.URL.RawQuery = value.Encode()
if err != nil {
log.Println("请求构造失败")
}
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.190 Safari/537.36")
rsp, err := client.Do(req)
if err != nil {
log.Println("请求失败")
return
}
defer rsp.Body.Close()
b, err := ioutil.ReadAll(rsp.Body)
if err != nil {
log.Println("read failed")
return
}
reg, err := regexp.Compile(`jQuery\d+\((.*)\)`)
if err != nil {
log.Println("reg失败")
return
}
ret := reg.FindSubmatch(b)
var g goodsInfo
err = json.Unmarshal(ret[1], &g)
if err != nil {
log.Println("json失败", err)
}
var bt baitiaoInfo
if g.Baitiao != "" {
json.Unmarshal([]byte(g.Baitiao), &bt)
}
status := "无货"
if strings.Contains(g.Stock.InfoStr, "有货") {
status = "有货"
}
needYuyue := "不需要"
if g.Yuyue != (yuyueInfo{}) {
needYuyue = "需要"
}
var info retInfo
info.TM = timeNow
info.Title = item.Title
info.Link = item.Link
info.EPrice = item.EPrice
info.APrice = g.Price.P
info.Discount = g.Price.Discount
info.Status = status
info.Baitiao = bt.InfoStr
info.NeedYuyue = needYuyue
info.YuyueTime = g.Yuyue.YuyueTime
lk.Lock()
fmt.Println(strings.Join([]string{info.TM, info.Title, info.APrice, info.EPrice, info.Discount, info.Status, info.Baitiao, info.NeedYuyue, info.YuyueTime, info.Link}, " "))
lk.Unlock()
}
// s := fmt.Sprintf(`时间:%s
//单价:%s
//折扣:%s
//状态:%s
//白条:%s
//需要预约:%s
//预约时间:%s`, timeNow, info.EPrice, info., g.Price.Discount, status, bt.InfoStr, needYuyue, g.Yuyue.YuyueTime)
// fmt.Println(s, len(retLis))
// tt = append(tt, 2)
//}
func getCurrent() {
for {
fmt.Println(strings.Join([]string{"当前时间", "商品名", "价格", "期望价格", "折扣", "状态", "白条", "预约", "预约时间", "链接"}, "|"))
for _, item := range items {
go func(itm *dstInfo) {
ch <- struct{}{}
wg.Add(1)
defer func() {
<-ch
wg.Done()
}()
download(itm)
}(item)
}
wg.Wait()
time.Sleep(time.Second * time.Duration(checkt))
}
}