-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
212 lines (191 loc) · 5.85 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
212
package main
import (
"encoding/json"
"fmt"
"math/rand"
"strconv"
"strings"
"time"
_ "image/gif"
_ "image/jpeg"
_ "image/png"
)
var (
SubmitCaptchaStr = make(chan string)
login = &Login{}
ticket = &TicketQueryInfo{
SubmitCaptchaStr: make(chan string),
P1: &PassengerOrder{},
P2: &PassengerOrder{},
P3: &PassengerOrder{},
P4: &PassengerOrder{},
P5: &PassengerOrder{},
}
mapPassengers = make(map[string]Passenger)
)
func main() {
createUI()
}
//查询
func (t *TicketQueryInfo) Order(cdn string) {
if tickets := t.queryLeftTicket(cdn); tickets != nil { //获取车次
for _, trainCode := range t.Trians { //要预订的车次
trainCode = strings.ToUpper(trainCode)
for _, data := range tickets.Data { //每个车次
//查询到的车次
tkt := data.Ticket
if tkt.StationTrainCode == strings.ToUpper(trainCode) { //是预订的车次
//获取余票信息
ticketNum := GetTicketNum(tkt.YpInfo, tkt.YpEx)
if validateNum(ticketNum, t.NumOfSeatType) { //想要预订席别的余票大于等于订票人的人数
Info(cdn, "开始订票", t.TrainDate.Format("2006-01-02"), "车次", tkt.StationTrainCode, "余票", fmt.Sprintf("%v", ticketNum))
order = &Order{
CDN: cdn,
PassengerTicketStr: t.PassengerTicketStr,
OldPassengerStr: t.OldPassengerStr,
Ticket: tkt,
SecretStr: data.SecretStr,
SubmitCaptchaStr: make(chan string),
TrainDate: t.TrainDate,
SeatType: getSeatType(t.NumOfSeatType),
}
var err error
go checkUser(Conf.CDN[0])
err = order.submitOrderRequest()
if err != nil {
Error(err)
return
}
// leftTicketInit(Conf.CDN[0])
// t.queryLeftTicket(cdn)
// order.inits()
err = order.initDc()
if err != nil {
Error(err)
return
}
dyQueryJs(Conf.CDN[0])
// go ticket.queryLeftTicket(order.CDN)
go order.getPassCodeNew()
break
} else {
Info("车次", tkt.StationTrainCode, "余票不足!!!剩余票:", fmt.Sprintf("%v", ticketNum), "订购的票:", fmt.Sprintf("%v", t.NumOfSeatType))
}
} else { //不是预订的车次
//Debug(tkt.StationTrainCode, "余票", fmt.Sprintf("%v", getTicketNum(tkt.YpInfo, tkt.YpEx)))
}
}
}
} else {
Error(cdn, "余票查询错误", tickets)
}
}
func getSeatType(seatTypeNum map[string]int) (t string) {
t = "3"
max, name := 0, "硬卧"
for k, v := range seatTypeNum {
if v > max {
max, name = v, k
}
}
t = SeatTypeNameToV[name]
return
}
func validateNum(ticketNum, seatTypeNum map[string]int) (b bool) {
b = true
for k, v := range seatTypeNum {
Info(k, v, ticketNum[k])
if ticketNum[k] < v {
b = false
break
}
}
return
}
//查询余票
func (t *TicketQueryInfo) queryLeftTicket(cdn string) *QueryLeftNewDTO {
fr := t.FromStations
to := t.ToStations
leftTicketUrl := ""
leftTicketUrl += "leftTicketDTO.train_date=" + t.TrainDate.Format("2006-01-02") + "&"
leftTicketUrl += "leftTicketDTO.from_station=" + StationMap[fr[rand.Intn(len(fr))]] + "&"
leftTicketUrl += "leftTicketDTO.to_station=" + StationMap[to[rand.Intn(len(to))]] + "&"
leftTicketUrl += "purpose_codes=ADULT"
Info("queryLeftTicket url:", leftTicketUrl)
go DoForWardRequest(cdn, "GET", URLQueryLog+leftTicketUrl, nil)
h := map[string]string{"If-Modified-Since": time.Now().Local().Format(time.RFC1123Z),
"If-None-Match": strconv.FormatInt(time.Now().UnixNano(), 10)}
body, err := DoForWardRequestHeader(cdn, "GET", URLQuery+leftTicketUrl, nil, h)
if err != nil {
Error("queryLeftTicket DoForWardRequest error:", err)
return nil
}
Debug("queryLeftTicket body:", body)
if !strings.Contains(body, "queryLeftNewDTO") {
Error("查询余票出错,返回:", body, "查询链接:", leftTicketUrl)
//删除废弃的CDN
// if len(availableCDN) > 5 {
// delete(availableCDN, cdn)
// }
return nil
}
leftTicket := &QueryLeftNewDTO{}
if err := json.Unmarshal([]byte(body), &leftTicket); err != nil {
Error("queryLeftTicket", cdn, err)
return nil
} else {
Info(cdn, "获取成功!")
}
return leftTicket
}
func parseTicket() {
ticket.FromStations = parseStrings(ticket.FromStationsStr)
ticket.ToStations = parseStrings(ticket.ToStationsStr)
ticket.Trians = parseStrings(ticket.TriansStr)
o, n := parseStranger(ticket)
ticket.OldPassengerStr = o
ticket.PassengerTicketStr = n[:len(n)-1]
ticket.Order(Conf.CDN[0])
}
func plusNum(num int, oStr, nStr string, p *PassengerOrder, m map[string]int) (int, string, string, map[string]int) {
if strings.Trim(p.Name, " ") != "" {
name := SeatTypeValueToN[p.SeatType]
m[name] = m[name] + 1
nStr += p.SeatType + ",0," + p.TicketType + "," + p.Name + "," + p.PassengerIdTypeCode + "," + p.PassengerIdNo + ",,N_"
oStr += p.Name + "," + p.PassengerIdTypeCode + "," + p.PassengerIdNo + "," + p.TicketType + "_"
return num + 1, oStr, nStr, m
}
return num, oStr, nStr, m
}
func parseStranger(ticket *TicketQueryInfo) (oStr, nStr string) {
num := 0
m := make(map[string]int)
num, oStr, nStr, m = plusNum(num, oStr, nStr, ticket.P1, m)
num, oStr, nStr, m = plusNum(num, oStr, nStr, ticket.P2, m)
num, oStr, nStr, m = plusNum(num, oStr, nStr, ticket.P3, m)
num, oStr, nStr, m = plusNum(num, oStr, nStr, ticket.P4, m)
num, oStr, nStr, m = plusNum(num, oStr, nStr, ticket.P5, m)
ticket.NumOfPassenger = num
ticket.NumOfSeatType = m
return
}
func parseStrings(str string) (s []string) {
if strings.ContainsRune(str, rune(',')) {
for _, v := range strings.Split(str, ",") {
if v != "" {
s = append(s, v)
}
}
}
if strings.ContainsRune(str, rune(',')) {
for _, v := range strings.Split(str, ",") {
if v != "" {
s = append(s, v)
}
}
}
if len(s) == 0 {
s = append(s, str)
}
return
}