-
Notifications
You must be signed in to change notification settings - Fork 0
/
yp.py
executable file
·106 lines (94 loc) · 2.93 KB
/
yp.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
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
#!/usr/bin/python2
# vim: set fileencoding=utf-8:
import cgi
import datetime
import email.header
import json
import os
import traceback
from trainquery import TrainQuery, TrainQueryError
def getdj(traincode):
dj = {u'G': u'高速',
u'C': u'城际',
u'D': u'动车',
u'Z': u'直达',
u'T': u'特快',
u'K': u'快速',
u'Y': u'旅游',
u'L': u'临客',}
dj.update([(unicode(i), u'普快') for i in xrange(1, 6)])
dj.update([(unicode(i), u'普客') for i in xrange(6, 9)])
return dj.get(traincode[0], u'--')
form = cgi.FieldStorage()
fz = form.getvalue('fz').strip()
dz = form.getvalue('dz').strip()
train = form.getvalue('train', '').strip()
date = form.getvalue('date', str(datetime.date.today()))
jsoncallback = form.getvalue('jsoncallback')
year, month, day = [int(s) for s in date.split('-')]
date = datetime.date(year, month, day)
print 'Content-Type: application/javascript; charset=utf-8'
print 'Cache-Control: private'
if not fz or not dz or not jsoncallback:
print 'Status: 400 Bad Request'
print
exit()
if os.getenv('REQUEST_METHOD') == 'HEAD':
print 'Connection: close'
print
exit()
try:
tq = TrainQuery(fz, dz, traincode=train, date=date)
result = tq.query()
except TrainQueryError, ex:
result = {0: [u'--', fz.decode('utf-8'), dz.decode('utf-8')] + [u'--'] * 12 + [ex.message, u'--', u''],}
except Exception, ex:
if isinstance(ex.message, str):
message = ex.message.decode('utf-8')
else:
message = unicode(ex.message)
message = email.header.Header(message).encode()
print 'Status: 500 Internal Server Error'
print 'Content-Type: text/plain'
print
print '/*\n%s */' % traceback.format_exc()
exit()
data = u''
for i in sorted(result.keys()):
r = result[i]
other = u''
rz = r[13] if r[13] != u'--' else r[8]
yz = r[14] if r[14] != u'--' else r[9]
if r[6] != u'--':
other += u'商务:%s ' % r[6]
if r[7] != u'--':
other += u'特等:%s ' % r[7]
if r[10] != u'--':
other += u'高软:%s ' % r[10]
if r[16] != u'--':
other += u'其他:%s ' % r[16]
if r[17]:
other += r[17]
# 日期,#,车次,发站,到站,出发,到达,历时,软卧,硬卧,软座,硬座,其他,无座,等级
data += u'%s,%d,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s|' % (
date, # 日期
i, # #
r[0], # 车次
r[1], # 发站
r[2], # 到站
r[3], # 出发
r[4], # 到达
r[5], # 历时
r[11], # 软卧
r[12], # 硬卧
rz, # 软座
yz, # 硬座
other if other else u'--', # 其他
r[15], # 无座
getdj(r[0]), # 等级
)
data = data[:-1].encode('utf-8')
response = '%s(%s)' % (jsoncallback, json.dumps({'data': data}))
print 'Content-Length: %d' % len(response)
print
print response