-
Notifications
You must be signed in to change notification settings - Fork 8
/
otp.py
executable file
·48 lines (36 loc) · 1.1 KB
/
otp.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
#!/usr/bin/env python3
import datetime
import requests
from util import repl
def station_encode(s: str) -> str:
return '-' + '-'.join(
'%02x' % byte
for byte in s.encode('utf-8')
)
def get_status(train, station, kind) -> requests.Response:
url = 'http://dynamic.12306.cn/map_zwdcx/cx.jsp'
params = {
'cz': station,
'cc': train,
'cxlx': int(kind),
'rq': datetime.date.today().isoformat(),
'czEn': station_encode(station),
}
ua = {'User-Agent': 'Mozilla/5.0'}
return requests.get(url, params, headers=ua)
def print_status(response: requests.Response):
if response.status_code == 200:
print('|', response.text.strip())
else:
print('X %d error' % response.status_code)
def main(options: str):
options = options.split()
if len(options) < 2:
print('# usage: train stations')
return False
train, stations = options[0], options[1:]
for station in stations:
for kind in [0, 1]:
print_status(get_status(train, station, kind))
if __name__ == '__main__':
repl(main)