-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
99 lines (85 loc) · 3.18 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
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
#!/usr/bin/env python3
import os
import re
import time
import json
import yaml
import common
import requests
import logging
from datetime import datetime
H = '''Accept: */*
Accept-Encoding: gzip, deflate, br
Accept-Language: en,en-US;q=0.9,zh-CN;q=0.8,zh;q=0.7,zh-TW;q=0.6,ja;q=0.5
Cache-Control: no-cache
Connection: keep-alive
Cookie: {0}
Host: kyfw.12306.cn
If-Modified-Since: 0
Referer: {1}
Sec-Fetch-Site: same-origin
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.135 Safari/537.36
X-Requested-With: XMLHttpRequest'''.format(os.environ['TRAIN_COOKIE'],
os.environ['TRAIN_REFERER'])
API_COLUMN = [
'', '按钮', '列车号', '车次', '起始站代码', '到达站代码', '出发站', '到达站', '出发时间', '到达时间', '历时',
'是否可购买', 'yp_info', '出发日期', 'train_seat_feature', 'location_code',
'from_station_no', 'to_station_no', 'is_support_code',
'controlled_train_flag', 'gg_num', '高级软卧', '其他', '软卧', '软座', '特等座', '无座',
'yb_num', '硬卧', '硬座', '二等座', '一等座', '商务座', '动卧', 'yp_ex', 'seat_type',
'exchange_train_flag', '候补标记', '候补座位限制'
]
QUERIES = yaml.safe_load(os.environ['TRAIN_QUERY'])
def send_msg(msg):
common.send_msg('Train', msg)
def request(sess, url, start_time2tickets):
try:
r = sess.get(url)
r.raise_for_status()
resp = r.json()
except Exception as e:
send_msg(f'Requests Error: {e}')
return
try:
code_to_station = resp['data']['map']
results = resp['data']['result']
for result in results:
trains = result.split('|')
from_station = code_to_station[trains[6]]
to_station = code_to_station[trains[7]]
start_time = trains[8]
end_time = trains[9]
msg = f'{from_station}({start_time}) -> {to_station}({end_time})\n'
tickets = {}
if '*' in start_time2tickets:
tickets = start_time2tickets['*']
elif start_time in start_time2tickets:
tickets = start_time2tickets[start_time]
else:
continue
need_send = False
for seat in tickets.get('seats'):
seat_index = API_COLUMN.index(seat)
msg += f'{seat}: {trains[seat_index]}\n'
if trains[seat_index] not in tickets.get('unwant_tickets'):
need_send = True
logging.info(msg)
if need_send:
send_msg(msg)
except Exception as e:
send_msg(f'Parse Error: {e}')
def main():
headers = common.get_header(H)
with requests.session() as sess:
sess.headers.update(headers)
now = datetime.utcnow()
logging.info(now)
if now.minute < 2:
send_msg('Alive')
for query in QUERIES:
if query['url'] != '':
request(sess, query['url'], query['start_times'])
if __name__ == '__main__':
logging.basicConfig(
format = '%(asctime)s %(levelname)s %(message)s', level = 'INFO')
main()