#!/usr/bin/env python
# -*- encoding:utf-8 -*-
'''
Created on 2014年12月18日
@author: liujichao
'''
import datetime
import json
import re
import sys
import time
import Image
import PyV8
import requests
import tools.email_helper as emailHelper
reload(sys)
sys.setdefaultencoding('utf-8')# @UndefinedVariable
reqSingle= requests.Session()
attCheCi=["G655","G6741","G67","G491"]#关注的车次
dateList=["2015-02-18"]#关注的日期
username="12306登录用户名"
password="登录密码"
#这个是需要手动提交订单后f12自己找的,挨个post请求去找,参数名为:oldPassengerStr 格式如下
oldPassengerStr="姓名,1,130434199802036011,1_姓名2,1,130434199204238069,1_"
#这个是需要手动提交订单后f12自己找的,挨个post请求去找,参数名为:passengerTicketStr 格式如下
passengerTicketStr="O,0,1,姓名,1,130434199802036011,13683456789,N_O,0,1,姓名2,1,130434199204238069,13683456789,N"
header={
"Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"Accept-Encoding":"gzip, deflate",
"Accept-Language":"zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3",
"Connection":"keep-alive",
"Host":"kyfw.12306.cn",
"Referer":"https://kyfw.12306.cn/otn/safeguard/init",
"User-Agent":"Mozilla/5.0 (Windows NT 5.1; rv:34.0) Gecko/20100101 Firefox/34.0"
}
##定火车票
def orderTicket(fromStation,toStation,trainDate,secretStr):
header["Referer"]="https://kyfw.12306.cn/otn/leftTicket/init"
orderInitReq= reqSingle.get("https://kyfw.12306.cn/otn/leftTicket/init",headers=header)
header["Referer"]="https://kyfw.12306.cn/otn/leftTicket/init"
aryKV=extractKey(orderInitReq.content,header)
print aryKV
#初始化订票
header["Referer"]="https://kyfw.12306.cn/otn/leftTicket/init"
orderInitReq= reqSingle.post("https://kyfw.12306.cn/otn/leftTicket/submitOrderRequest",data={
aryKV[0]:aryKV[1],
"train_date":trainDate,
"myversion":"undefined",
"purpose_codes":"ADULT",
"query_from_station_name":fromStation,
"query_to_station_name":toStation,
"secretStr":secretStr,
"tour_flag":"dc",
"back_train_date":time.strftime('%Y-%m-%d',time.localtime(time.time())),
"undefined":""
},headers=header)
print orderInitReq.content
orderInitJson=orderInitReq.json()
if orderInitJson.get("status")==False or orderInitJson.get("httpstatus")!=200:
raise Exception("订票出现错误")
initDcReq= reqSingle.post("https://kyfw.12306.cn/otn/confirmPassenger/initDc", data={"_json_att":""},headers=header)
header["Ref