Last active
January 6, 2017 08:10
-
-
Save xiaotangyuan/811e827530c12a5b344c63be3a39fbc2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# encoding=utf8 | |
import sys | |
reload(sys) | |
sys.setdefaultencoding('utf-8') | |
import requests | |
STATION_TRAIN_CODE = 'G1952' | |
TRAIN_DATE = '2017-01-17' | |
subject = u'火车票余票提醒' | |
smtpserver = 'smtp.163.com' | |
username = '[email protected]' | |
send_from = username | |
pwd = '' | |
send_to = ['[email protected]'] | |
GET_DATA_SLEEP_SECONDS = 60 | |
def get_url(train_date): | |
base_url = 'https://kyfw.12306.cn/otn/leftTicket/queryA?leftTicketDTO.train_date=%s&leftTicketDTO.from_station=SHH&leftTicketDTO.to_station=KFF&purpose_codes=ADULT' | |
return base_url % train_date | |
def get_need_train_info(all_train_info_list, station_train_code): | |
for item in all_train_info_list: | |
if item['queryLeftNewDTO'].get('station_train_code') == station_train_code: | |
return item['queryLeftNewDTO'] | |
return None | |
# {"train_no":"5l000G195200", | |
# "station_train_code":"G1952", | |
# "start_station_telecode":"AOH", | |
# "start_station_name":"上海虹桥", | |
# "end_station_telecode":"TNV","end_station_name":"太原南", | |
# "from_station_telecode":"AOH","from_station_name":"上海虹桥", | |
# "to_station_telecode":"KBF","to_station_name":"开封北", | |
# "start_time":"08:16","arrive_time":"12:47","day_difference":"0", | |
# "train_class_name":"","lishi":"04:31","canWebBuy":"N","lishiValue":"271" | |
# ,"yp_info":"1TTzgiSp8yjQoKNO7cKClG%2BSD86iRpNeEVUqylXlyXI0mC0V", | |
# "control_train_day":"20300303","start_train_date":"20170117", | |
# "seat_feature":"O3M393","yp_ex":"O0M090","train_seat_feature":"3", | |
# "seat_types":"OM9","location_code":"HY","from_station_no":"01", | |
# "to_station_no":"10","control_day":29,"sale_time":"1330", | |
# "is_support_card":"0","controlled_train_flag":"0", | |
# "controlled_train_message":"正常车次,不受控", | |
# "train_type_code":"2","start_province_code":"33", | |
# "start_city_code":"0712","end_province_code":"02", | |
# "end_city_code":"0405", | |
# "yz_num":"--","rz_num":"--","yw_num":"--","rw_num":"--","gr_num":"--","zy_num":"无","ze_num":"无","tz_num":"--", | |
# "gg_num":"--","yb_num":"--","wz_num":"--","qt_num":"--","swz_num":"无"}, | |
# "secretStr":"","buttonTextInfo":"预订"} | |
import datetime | |
def log_fo_file(info): | |
f=open('train.text','a+') | |
dt = datetime.datetime.now() | |
print >>f, '%s_%s' % (dt, info) | |
def get_info(station_train_code, train_date): | |
url = get_url(train_date) | |
r = requests.get(url, verify=False) | |
all_train_info_list = r.json()['data'] | |
train_info = get_need_train_info(all_train_info_list, station_train_code) | |
yidengzuo = train_info.get('zy_num') | |
erdengzuo = train_info.get('ze_num') | |
info_y = u'车次 %s 一等座数量:%s' % (STATION_TRAIN_CODE, yidengzuo) | |
info_e = u'车次 %s 二等座数量:%s' % (STATION_TRAIN_CODE, erdengzuo) | |
log_fo_file(info_y) | |
log_fo_file(info_e) | |
data = { | |
'yidengzuo': yidengzuo, | |
'erdengzuo': erdengzuo, | |
} | |
return data, info_y, info_e | |
def is_should_notify(data, info_y, info_e): | |
if data['erdengzuo'] not in [u'无', u"--"]: | |
return True | |
else: | |
return False | |
import sys | |
import os | |
import smtplib | |
from email.mime.application import MIMEApplication | |
from email.mime.multipart import MIMEMultipart | |
from email.mime.text import MIMEText | |
from email.utils import COMMASPACE, formatdate | |
def send_mail(send_from, send_to, subject, text, files, | |
smtpserver, sender_username, sender_password): | |
assert isinstance(send_to, list) | |
msg = MIMEMultipart() | |
msg['From'] = send_from | |
msg['To'] = COMMASPACE.join(send_to) | |
msg['Date'] = formatdate(localtime=True) | |
msg['Subject'] = subject | |
msg.attach(MIMEText(text)) | |
for f in files or []: | |
with open(f, "rb") as fil: | |
part = MIMEApplication( | |
fil.read(), | |
Name=os.path.basename(f) | |
) | |
part['Content-Disposition'] = 'attachment; filename="%s"' % os.path.basename(f) | |
msg.attach(part) | |
smtp = smtplib.SMTP_SSL(smtpserver, 465) | |
smtp.ehlo() | |
smtp.login(sender_username, sender_password) | |
smtp.sendmail(send_from, send_to, msg.as_string()) | |
smtp.close() | |
def notify_use_mail(send_to, text): | |
text = str(text) | |
send_mail(send_from=send_from, send_to=send_to, subject=subject, text=text, smtpserver=smtpserver, sender_username=username, sender_password=pwd, files=None) | |
def main(): | |
try: | |
data, info_y, info_e = get_info(STATION_TRAIN_CODE, TRAIN_DATE) | |
if is_should_notify(data, info_y, info_e): | |
text = '%s, %s' % (info_y, info_e) | |
notify_use_mail(send_to, text) | |
except Exception as e: | |
msg = '%s' % e | |
log_fo_file(msg) | |
notify_use_mail(send_to, msg) | |
if __name__ == '__main__': | |
import time | |
while True: | |
main() | |
time.sleep(GET_DATA_SLEEP_SECONDS) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment