-
Notifications
You must be signed in to change notification settings - Fork 0
/
homing.sh
executable file
·278 lines (222 loc) · 8.07 KB
/
homing.sh
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
#!/bin/bash
# author: nicephil@gmail.com
# new version: http://github.com/nicephil/homing.git
# Dependency: curl, feh, ocrad, sed/awk, bash, elinks
# UTF-8 Encoding can generated from http://tool.chinaz.com/Tools/URLEncode.aspx
#Zhengzhou=%e9%83%91%e5%b7%9e
#Hangzhou=%e6%9d%ad%e5%b7%9e
#########################################################################
user_name="XXXX" #12306 username
password="****" #password for 12306
train_date="2012-01-26"
round_train_date="2012-01-18"
from_station_telecode="ZZF" # you can find it from station_name.list generated by this script later
from_station_name="%e9%83%91%e5%b7%9e" #Zhengzhou UTF-8 encode
to_station_telecode="HZH" # you can find it from station_name.list
to_station_name="%e6%9d%ad%e5%b7%9e" #Hangzhou UTF-8 encode
station_train_code="K658" # your trainnumber
passenger1_name="%e7%8e%8b%e4%b8%89" # Wang San
passenger1_id="411xxxxxxxxxxx" #id card
passenger1_phone="139xxxxxxxx" #mobile phone
passenger1_seat="3" #1 - yingzuo 2 - ruanwo 3 - yingwo
passenger2_name="%e5%a8%84%e5%9b%9b" # Lou Si
passenger2_id="410XXXXXXXX" #id card
passenger2_phone="189XXXXXXX" #mobile phone
passenger2_seat="3" #1 - yingzuo 2 - ruanwo 3 - yingwo
# the last form only supports two passengers, if need more, or less,
# have to modified the last form at line 233
#passenger3_name="xxxxxxxxxxx" #name
#passenger3_id="411xxxxxxxxxxxx" #id card
#passenger3_phone="189xxxxxxxx" #mobile phone
#passenger3_seat="x" #1 - yingzuo 2 - ruanwo 3 - yingwo
#########################################################################33
ua="Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
cookie=cookie.txt
page=page.html
jpg=randcode.jpg
station_name_list=station_name.list
train_info_detail=train_info.detail
while [[ ! -e $cookie || $1 != "" ]]
do
rm -rvf "$cookie" "$page" "$jpg"
# get randcode picture
url="https://dynamic.12306.cn/otsweb/passCodeAction.do?rand=lrand"
refer="https://dynamic.12306.cn/otsweb/loginAction.do?method=init"
curl -k -A "$ua" -D "$cookie" -b "$cookie" -o "$jpg" -e "$refer" "$url"
feh "$jpg"&
randcode=`djpeg -greyscale -pnm "$jpg" | ocrad --charset=ascii --scale=60`
echo "randcode=${randcode}"
read -t 10 -p "Please correct rancode: " randcode1
echo ""
if [[ "$randcode1" != "" ]]
then
randcode=$randcode1
fi
killall feh
echo "randcode=${randcode}"
# login system
url="https://dynamic.12306.cn/otsweb/loginAction.do?method=login"
form="loginUser.user_name=${user_name}&nameErrorFocus=&user.password=${password}&passwordErrorFocus=&randCode=${randcode}&randErrorFocus="
curl -k -L -A "$ua" -b "$cookie" -o $page -d "${form}" -e "$refer" "$url"
if (( $? != 0 ))
then
exit -1
fi
grep "2012" $page
errcode=$?
if (( $errcode != 0 ))
then
echo "=======>LOGIN FAILED"
rm $cookie $page
tail -n 20 $page
else
echo "=======>LOGIN OK!"
cp $cookie ${cookie}_bak
break
fi
done
# station name list code
if [[ -e ${station_name_list} ]]
then
echo "${station_name_list} is here already."
else
refer="https://dynamic.12306.cn/otsweb/order/querySingleAction.do?method=init"
url="https://dynamic.12306.cn/otsweb/js/common/station_name.js?version=2.1"
curl -k -L -A "$ua" -b "$cookie" -G -o ${station_name_list} -e "$refer" "$url"
fi
#elinks ${station_name_list}
#exit
while (( 1 ))
do
# queryLeftTicket"
refer="https://dynamic.12306.cn/otsweb/order/querySingleAction.do?method=init"
url="https://dynamic.12306.cn/otsweb/order/querySingleAction.do?method=queryLeftTicket"
form="orderRequest.train_date=${train_date}&\
orderRequest.from_station_telecode=${from_station_telecode}&\
orderRequest.to_station_telecode=${to_station_telecode}&\
orderRequest.train_no=&trainPassType=QB&\
trainClass=QB%23D%23Z%23T%23K%23QT%23&\
includeStudent=00&\
seatTypeAndNum=&\
orderRequest.start_time_str=00%3A00--24%3A00"
curl -k -L -A "$ua" -b "$cookie" -G -d "$form" -o $page -e "$refer" "$url"
# get TainInfoDetail
sed -n 's/\(getSelected\)/\n\1/gp' $page | sed -n "s/^getSelected('\([^)']*\)').*$/\1/pg" > ${train_info_detail}_bak
cat ${train_info_detail}_bak
sed -n "/${station_train_code}/p" ${train_info_detail}_bak > ${train_info_detail}
lishi=`awk -F"#" '{print $2}' ${train_info_detail}`
start_time=`awk -F"#" '{print $3}' ${train_info_detail}`
trainno=`awk -F"#" '{print $4}' ${train_info_detail}`
end_time=`awk -F"#" '{print $7}' ${train_info_detail}`
ypInfoDetail=`awk -F"#" '{print $10}' ${train_info_detail}`
echo ""
echo "lishi=${lishi}"
echo "start_time=${start_time}"
echo "trainno=${trainno}"
echo "end_time=${end_time}"
echo "ypInfoDetail=${ypInfoDetail}"
if [[ $ypInfoDetail == "" ]]
then
echo "the train you specified is wrong!!!"
exit
fi
#elinks $page
#exit
# submutOrderRequest
refer="https://dynamic.12306.cn/otsweb/order/querySingleAction.do?method=init"
url="https://dynamic.12306.cn/otsweb/order/querySingleAction.do?method=submutOrderRequest"
form="station_train_code=${station_train_code}&\
train_date=${train_date}&\
seattype_num=&\
from_station_telecode=${from_station_telecode}&\
to_station_telecode=${to_station_telecode}&\
include_student=00&\
from_station_telecode_name=${from_station_name}&\
to_station_telecode_name=${to_station_name}&\
round_train_date=${round_train_date}&\
round_start_time_str=00:00--24:00&\
single_round_type=1&\
train_pass_type=QB&\
train_class_arr=QB#D#Z#T#K#QT#&\
start_time_str=00:00--24:00&\
lishi=${lishi}&\
train_start_time=${start_time}&\
trainno=${trainno}&\
arrive_time=${end_time}&\
from_station_name=${from_station_name}&\
to_station_name=${to_station_name}&\
ypInfoDetail=${ypInfoDetail}"
curl -k -L -A "$ua" -b "$cookie" -d "$form" -o $page -e "$refer" "$url"
token=`sed -n '/TOKEN/p' $page | sed -n 's/^.*value="\([^ "]*\)".*$/\1/gp'`
echo "token=$token"
#elinks $page
#exit
# get randcode picture
url="https://dynamic.12306.cn/otsweb/passCodeAction.do?rand=randp"
refer="https://dynamic.12306.cn/otsweb/order/confirmPassengerAction.do?method=init"
curl -k -A "$ua" -b "$cookie" -o "$jpg" -e "$refer" "$url"
feh "$jpg"&
randcode=`djpeg -greyscale -pnm "$jpg" | ocrad --charset=ascii --scale=60`
echo "randcode=${randcode}"
read -t 10 -p "Please correct rancode: " randcode1
echo ""
if [[ "$randcode1" != "" ]]
then
randcode=$randcode1
fi
killall feh
echo "randcode=${randcode}"
# confirmPassengerAction
refer="https://dynamic.12306.cn/otsweb/order/confirmPassengerAction.do?method=init"
url="https://dynamic.12306.cn/otsweb/order/confirmPassengerAction.do?method=confirmPassengerInfoSingle"
form="org.apache.struts.taglib.html.TOKEN=${token}&\
textfield=&\
checkbox0=0&\
checkbox1=1&\
orderRequest.train_date=${train_date}&\
orderRequest.train_no=${trainno}&\
orderRequest.station_train_code=${station_train_code}&\
orderRequest.from_station_telecode=${from_station_telecode}&\
orderRequest.to_station_telecode=${to_station_telecode}&\
orderRequest.seat_type_code=&\
orderRequest.ticket_type_order_num=&\
orderRequest.bed_level_order_num=000000000000000000000000000000&\
orderRequest.start_time=${start_time}&\
orderRequest.end_time=${end_time}&\
orderRequest.from_station_name=${from_station_name}&\
orderRequest.to_station_name=${to_station_name}&\
orderRequest.cancel_flag=1&\
orderRequest.id_mode=Y&\
passengerTickets=${passenger1_seat},1,${passenger1_name},1,${passenger1_id},${passenger1_phone},Y&\
oldPassengers=${passenger1_name},1,${passenger1_id}&\
passenger_1_seat=${passenger1_seat}&\
passenger_1_ticket=1&\
passenger_1_name=${passenger1_name}&\
passenger_1_cardtype=1&\
passenger_1_cardno=${passenger1_id}&\
passenger_1_mobileno=${passenger1_phone}&\
checkbox9=Y&\
passengerTickets=${passenger2_seat},1,${passenger2_name},1,${passenger2_id},${passenger2_phone},Y&\
oldPassengers=${passenger2_name},1,${passenger2_id}&\
passenger_2_seat=${passenger2_seat}&\
passenger_2_ticket=1&\
passenger_2_name=${passenger2_name}&\
passenger_2_cardtype=1&\
passenger_2_cardno=${passenger2_id}&\
passenger_2_mobileno=${passenger2_phone}&\
checkbox9=Y&\
oldPassengers=&\
checkbox9=Y&\
oldPassengers=&\
checkbox9=Y&\
oldPassengers=&\
checkbox9=Y&\
randCode=${randcode}&\
orderRequest.reserve_flag=A"
curl -k -L -A "$ua" -b "$cookie" -o $page -d "$form" -e "$refer" "$url"
#elinks $page
#exit
tail -n 20 $page
cp $page ${page}_bak
done
# commit