-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
executable file
·74 lines (62 loc) · 1.96 KB
/
train.py
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
#!/usr/bin/python2
# vim: set fileencoding=utf-8:
import cgi
import json
import os
import datetime
import requests
import trainquery.station_name as station_name
import trainquery.train_list as train_list
form = cgi.FieldStorage()
train = form.getvalue('train', '').strip()
date = form.getvalue('date', str(datetime.date.today()))
print 'Content-Type: application/javascript; charset=utf-8'
print 'Cache-Control: private'
if not train:
print 'Status: 400 Bad Request'
print
exit()
if os.getenv('REQUEST_METHOD') == 'HEAD':
print 'Connection: close'
print
exit()
base = 'https://kyfw.12306.cn/otn/czxx/queryByTrainNo'
s = requests.session()
s.headers.update({
'Referer': 'https://kyfw.12306.cn/otn/czxx/init',
'X-Requested-With': 'XMLHttpRequest',
'User-Agent': 'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)',})
try:
train_no = train_list[date][train]
except KeyError:
#print 'Status: 400 Bad Request'
print
print json.dumps({'stop': {}, 'stops': ['车次不存在,或日期不在预售期内']}).encode('utf-8')
exit()
payload = [('train_no', train_list[date][train]),
('from_station_telecode', 'XXX'),
('to_station_telecode', 'XXX'),
('depart_date', date),]
r = s.get(base, params=payload)
t = r.json()['data']['data']
stop = {}
stops = []
station_no = 1
extra_day = 0
time_last = datetime.datetime.strptime(t[0]['start_time'], '%H:%M')
for data in t:
time_this = datetime.datetime.strptime(data['start_time'], '%H:%M')
dep_h, dep_m = [int(x) for x in data['start_time'].split(':')]
dep_time = dep_h * 60 + dep_m
extra_day += -(time_this - time_last).days
time_last = time_this
stops += [data['station_name']]
stop[data['station_name']] = {
'seq': station_no,
'extra_day': extra_day,
'dep_time': dep_time
}
station_no += 1
train = {'stops': stops, 'stop': stop}
print
print json.dumps(train).encode('utf-8')