def queryOrderWaitTime(self): """ 排队获取订单等待信息,每隔3秒请求一次,最高请求次数为20次! :return: """ queryOrderWaitTimeUrl = "https://kyfw.12306.cn/otn/confirmPassenger/queryOrderWaitTime" data = { "random": "149545481029" + str(random.randint(1, 9)), "tourFlag": "dc", "REPEAT_SUBMIT_TOKEN": self.get_token(), } num = 1 while True: num += 1 if num > 20: print("超出排队时间,自动放弃,正在重新刷票") break try: queryOrderWaitTimeResult = json.loads( myurllib2.Post(queryOrderWaitTimeUrl, data)) except ValueError: pass if "status" in queryOrderWaitTimeResult and queryOrderWaitTimeResult[ "status"]: if "orderId" in queryOrderWaitTimeResult[ "data"] and queryOrderWaitTimeResult["data"][ "orderId"] != "null": self.initNoComplete() orderId = self.queryMyOrderNoComplete() if orderId: raise ticketIsExitsException(( "恭喜您订票成功,订单号为:{0}, 请立即打开浏览器登录12306,访问‘未完成订单’,在30分钟内完成支付!" .format(orderId))) else: print("等待出票中...") continue elif "msg" in queryOrderWaitTimeResult[ "data"] and queryOrderWaitTimeResult["data"]["msg"]: orderId = self.queryMyOrderNoComplete() if orderId: raise ticketIsExitsException(( "恭喜您订票成功,订单号为:{0}, 请立即打开浏览器登录12306,访问‘未完成订单’,在30分钟内完成支付!" .format(orderId))) else: break elif "messages" in queryOrderWaitTimeResult and queryOrderWaitTimeResult[ "messages"]: print("订单提交失败: " + queryOrderWaitTimeResult["messages"]) orderId = self.queryMyOrderNoComplete() if orderId: raise ticketIsExitsException(( "恭喜您订票成功,订单号为:{0}, 请立即打开浏览器登录12306,访问‘未完成订单’,在30分钟内完成支付!" .format(orderId))) else: break else: print("订单提交中,请耐心等待") time.sleep(3)
def queryOrderWaitTime(self): """ 排队获取订单等待信息,每隔3秒请求一次,最高请求次数为20次! :return: """ # queryOrderWaitTimeUrl = "https://kyfw.12306.cn/otn/confirmPassenger/queryOrderWaitTime" # data = { # "random": "{0}{1}".format(int(time.time()), random.randint(1, 9)), # "tourFlag": "dc", # "REPEAT_SUBMIT_TOKEN": self.get_token(), # } num = 1 while True: _random = int(round(time.time() * 1000)) num += 1 if num > 30: print("超出排队时间,自动放弃,正在重新刷票") break try: # queryOrderWaitTimeUrl = "https://kyfw.12306.cn/otn/confirmPassenger/queryOrderWaitTime?random={0}&tourFlag=dc&_json_att=&REPEAT_SUBMIT_TOKEN={1}".format(_random, self.get_token()) data = {"random": _random, "tourFlag": "dc"} queryOrderWaitTimeUrl = "https://kyfw.12306.cn/otn/confirmPassenger/queryOrderWaitTime" queryOrderWaitTimeResult = self.s.post(queryOrderWaitTimeUrl, data=data, verify=False).json() except ValueError: queryOrderWaitTimeResult = {} if queryOrderWaitTimeResult: if "status" in queryOrderWaitTimeResult and queryOrderWaitTimeResult["status"]: if "orderId" in queryOrderWaitTimeResult["data"] and queryOrderWaitTimeResult["data"]["orderId"] is not None: email("恭喜您订票成功,订单号为:{0}, 请立即打开浏览器登录12306,访问‘未完成订单’,在30分钟内完成支付!".format(queryOrderWaitTimeResult["data"]["orderId"]),self.ticket_config).sendEmail() raise ticketIsExitsException("恭喜您订票成功,订单号为:{0}, 请立即打开浏览器登录12306,访问‘未完成订单’,在30分钟内完成支付!".format(queryOrderWaitTimeResult["data"]["orderId"])) elif "msg" in queryOrderWaitTimeResult["data"] and queryOrderWaitTimeResult["data"]["msg"]: print(queryOrderWaitTimeResult["data"]["msg"]) break elif "waitTime"in queryOrderWaitTimeResult["data"] and queryOrderWaitTimeResult["data"]["waitTime"]: print("排队等待时间预计还剩 {0} ms".format(0-queryOrderWaitTimeResult["data"]["waitTime"])) else: print ("正在等待中") elif "messages" in queryOrderWaitTimeResult and queryOrderWaitTimeResult["messages"]: print("排队等待失败: " + queryOrderWaitTimeResult["messages"]) else: print("第{}次排队中,请耐心等待".format(num)) else: print("排队中") time.sleep(1) order_id = self.queryMyOrderNoComplete() # 尝试查看订单列表,如果有订单,则判断成功,不过一般可能性不大 if order_id: email("恭喜您订票成功,订单号为:{0}, 请立即打开浏览器登录12306,访问‘未完成订单’,在30分钟内完成支付!".format(order_id),self.ticket_config).sendEmail() raise ticketIsExitsException("恭喜您订票成功,订单号为:{0}, 请立即打开浏览器登录12306,访问‘未完成订单’,在30分钟内完成支付!".format(order_id)) else: print(ticketNumOutException("订单提交失败!,正在重新刷票"))
def submit_station(self): """ 提交车次 预定的请求参数,注意参数顺序 注意这里为了防止secretStr被urllib.parse过度编码,在这里进行一次解码 否则调用HttpTester类的post方法将会将secretStr编码成为无效码,造成提交预定请求失败 :param self: :param secretStr: 提交车次加密 :return: """ submit_station_url = 'https://kyfw.12306.cn/otn/leftTicket/submitOrderRequest' data = [ ('secretStr', urllib.unquote(self.secretStr)), # 字符串加密 ('train_date', self.time()), # 出发时间 ('back_train_date', self.time()), # 返程时间 ('tour_flag', 'dc'), # 旅途类型 ('purpose_codes', 'ADULT'), # 成人票还是学生票 ('query_from_station_name', self.from_station), # 起始车站 ('query_to_station_name', self.to_station), # 终点车站 ] submitResult = json.loads(myurllib2.Post(submit_station_url, data), encoding='utf-8') if 'data' in submitResult and submitResult['data']: if submitResult['data'] == 'N': print('出票成功') else: print('出票失败') elif 'messages' in submitResult and submitResult['messages']: raise ticketIsExitsException(submitResult['messages'][0])
def submit_station(self): """ 提交车次 预定的请求参数,注意参数顺序 注意这里为了防止secretStr被urllib.parse过度编码,在这里进行一次解码 否则调用HttpTester类的post方法将会将secretStr编码成为无效码,造成提交预定请求失败 :param self: :param secretStr: 提交车次加密 :return: """ submit_station_url = self.confUrl["submit_station_url"]["req_url"] data = [('secretStr', urllib.unquote(self.secretStr)), # 字符串加密 ('train_date', self.time()), # 出发时间 ('back_train_date', self.time()), # 返程时间 ('tour_flag', 'dc'), # 旅途类型 ('purpose_codes', 'ADULT'), # 成人票还是学生票 ('query_from_station_name', self.from_station), # 起始车站 ('query_to_station_name', self.to_station), # 终点车站 ] submitResult = self.httpClint.send(submit_station_url, data) if 'data' in submitResult and submitResult['data']: if submitResult['data'] == 'N': print ('出票成功') else: print ('出票失败') elif 'messages' in submitResult and submitResult['messages']: raise ticketIsExitsException(submitResult['messages'][0])
def queryOrderWaitTime(self): """ 排队获取订单等待信息,每隔3秒请求一次,最高请求次数为20次! :return: """ num = 1 while True: _random = int(round(time.time() * 1000)) num += 1 if num > 30: print(u"超出排队时间,自动放弃,正在重新刷票") order_id = self.queryMyOrderNoComplete() # 排队失败,自动取消排队订单 if order_id: try: self.cancelNoCompleteMyOrder(order_id) except: print("取消未完成的订单失败") break try: data = {"random": _random, "tourFlag": "dc"} queryOrderWaitTimeUrl = self.confUrl["queryOrderWaitTimeUrl"] queryOrderWaitTimeResult = self.httpClint.send( queryOrderWaitTimeUrl, data) except ValueError: queryOrderWaitTimeResult = {} if queryOrderWaitTimeResult: if "status" in queryOrderWaitTimeResult and queryOrderWaitTimeResult[ "status"]: if "orderId" in queryOrderWaitTimeResult["data"] and \ queryOrderWaitTimeResult["data"]["orderId"] is not None: sendEmail( u"恭喜您订票成功,订单号为:{0}, 请立即打开浏览器登录12306," u"访问‘未完成订单’,在30分钟内完成支付!".format( queryOrderWaitTimeResult["data"]["orderId"])) raise ticketIsExitsException( u"恭喜您订票成功,订单号为:{0}, " u"请立即打开浏览器登录12306,访问‘未完成订单’,在30分钟内完成支付!".format( queryOrderWaitTimeResult["data"]["orderId"])) elif "msg" in queryOrderWaitTimeResult[ "data"] and queryOrderWaitTimeResult["data"]["msg"]: print queryOrderWaitTimeResult["data"]["msg"] break elif "waitTime" in queryOrderWaitTimeResult[ "data"] and queryOrderWaitTimeResult["data"][ "waitTime"]: print(u"排队等待时间预计还剩 {0} ms".format( 0 - queryOrderWaitTimeResult["data"]["waitTime"])) else: print("正在等待中") elif "messages" in queryOrderWaitTimeResult and queryOrderWaitTimeResult[ "messages"]: print(u"排队等待失败: " + queryOrderWaitTimeResult["messages"]) else: print(u"第{}次排队中,请耐心等待".format(num + 1)) else: print(u"排队中") time.sleep(2) else: print(ticketNumOutException(u"订单提交失败!,正在重新刷票"))
def sendSubmitOrderRequest(self, log_info=[]): """ 提交车次 预定的请求参数,注意参数顺序 注意这里为了防止secretStr被urllib.parse过度编码,在这里进行一次解码 否则调用HttpTester类的post方法将会将secretStr编码成为无效码,造成提交预定请求失败 :param self: :param secretStr: 提交车次加密 :return: """ submit_station_url = self.session.urls["submit_station_url"] submitResult = self.session.httpClint.send(submit_station_url, self.data_apr()) if 'data' in submitResult and submitResult['data']: if submitResult['data'] == 'N': print(u'出票成功') log_info.append(u'出票成功') coi = checkOrderInfo(self.session, self.train_no, self.set_type, self.passengerTicketStrList, self.oldPassengerStr, self.train_date, self.ticke_peoples) coi.sendCheckOrderInfo(log_info) else: print(u'出票失败') log_info.append(u'出票失败') elif 'messages' in submitResult and submitResult['messages']: raise ticketIsExitsException(submitResult['messages'][0])
def submit_station(self): """ 提交车次 预定的请求参数,注意参数顺序 注意这里为了防止secretStr被urllib.parse过度编码,在这里进行一次解码 否则调用HttpTester类的post方法将会将secretStr编码成为无效码,造成提交预定请求失败 :param self: :param secretStr: 提交车次加密 :return: """ submit_station_url = self.confUrl["submit_station_url"] data = [('secretStr', urllib.unquote(self.secretStr)), # 字符串加密 ('train_date', self.station_dates[0]), # 出发时间 ('back_train_date', self.time()), # 返程时间 ('tour_flag', 'dc'), # 旅途类型 ('purpose_codes', 'ADULT'), # 成人票还是学生票 ('query_from_station_name', self.from_station), # 起始车站 ('query_to_station_name', self.to_station), # 终点车站 ] submitResult = self.httpClint.send(submit_station_url, data) if 'data' in submitResult and submitResult['data']: if submitResult['data'] == 'N': print (u'出票成功') else: print (u'出票失败') elif 'messages' in submitResult and submitResult['messages']: raise ticketIsExitsException(submitResult['messages'][0])
def sendQueryOrderWaitTime(self): """ 排队获取订单等待信息,每隔3秒请求一次,最高请求次数为20次! :return: """ num = 1 while True: num += 1 if num > ticket.OUT_NUM: print(ticket.WAIT_OUT_NUM) order_id = self.queryMyOrderNoComplete() # 排队失败,自动取消排队订单 if order_id: self.cancelNoCompleteMyOrder(order_id) break try: queryOrderWaitTimeUrl = copy.deepcopy( self.session.urls["queryOrderWaitTimeUrl"]) queryOrderWaitTimeUrl["req_url"] = queryOrderWaitTimeUrl[ "req_url"].format(int(round(time.time() * 1000))) queryOrderWaitTimeResult = self.session.httpClint.send( queryOrderWaitTimeUrl) except ValueError: queryOrderWaitTimeResult = {} if queryOrderWaitTimeResult: if queryOrderWaitTimeResult.get("status", False): data = queryOrderWaitTimeResult.get("data", False) if data and data.get("orderId", ""): sendIFTTT( ticket.WAIT_ORDER_SUCCESS.format( data.get("orderId", ""))) sendEmail( ticket.WAIT_ORDER_SUCCESS.format( data.get("orderId", ""))) sendPushBear( ticket.WAIT_ORDER_SUCCESS.format( data.get("orderId", ""))) raise ticketIsExitsException( ticket.WAIT_ORDER_SUCCESS.format( data.get("orderId"))) elif data.get("msg", False): print(data.get("msg", "")) break elif data.get("waitTime", False): print( ticket.WAIT_ORDER_CONTINUE.format( 0 - data.get("waitTime", False))) else: pass elif queryOrderWaitTimeResult.get("messages", False): print( ticket.WAIT_ORDER_FAIL.format( queryOrderWaitTimeResult.get("messages", ""))) else: print(ticket.WAIT_ORDER_NUM.format(num + 1)) else: pass time.sleep(2) else: print(ticketNumOutException(ticket.WAIT_ORDER_SUB_FAIL))
def sendQueryQueueByAfterNate(self): for i in range(10): queryQueueByAfterNateRsp = self.session.httpClint.send(urls.get("queryQueue")) if not queryQueueByAfterNateRsp.get("status"): print("".join(queryQueueByAfterNateRsp.get("messages")) or queryQueueByAfterNateRsp.get("validateMessages")) time.sleep(1) else: sendEmail(ticket.WAIT_ORDER_SUCCESS) sendPushBear(ticket.WAIT_ORDER_SUCCESS) sendServerChan(ticket.WAIT_ORDER_SUCCESS) raise ticketIsExitsException(ticket.WAIT_AFTER_NATE_SUCCESS)
def sendQueryOrderWaitTime(self): """ 排队获取订单等待信息,每隔3秒请求一次,最高请求次数为20次! :return: """ num = 1 while True: num += 1 if num > ticket.OUT_NUM: print(ticket.WAIT_OUT_NUM) order_id = self.queryMyOrderNoComplete() # 排队失败,自动取消排队订单 if order_id: self.cancelNoCompleteMyOrder(order_id) break try: queryOrderWaitTimeUrl = copy.deepcopy(self.session.urls["queryOrderWaitTimeUrl"]) queryOrderWaitTimeUrl["req_url"] = queryOrderWaitTimeUrl["req_url"].format(int(round(time.time() * 1000))) queryOrderWaitTimeResult = self.session.httpClint.send(queryOrderWaitTimeUrl) except ValueError: queryOrderWaitTimeResult = {} if queryOrderWaitTimeResult: if queryOrderWaitTimeResult.get("status", False): data = queryOrderWaitTimeResult.get("data", False) if data and data.get("orderId", ""): sendEmail(ticket.WAIT_ORDER_SUCCESS.format( data.get("orderId", ""))) raise ticketIsExitsException(ticket.WAIT_ORDER_SUCCESS.format( data.get("orderId"))) elif data.get("msg", False): print(data.get("msg", "")) break elif data.get("waitTime", False): print(ticket.WAIT_ORDER_CONTINUE.format(0 - data.get("waitTime", False))) else: pass elif queryOrderWaitTimeResult.get("messages", False): print(ticket.WAIT_ORDER_FAIL.format(queryOrderWaitTimeResult.get("messages", ""))) else: print(ticket.WAIT_ORDER_NUM.format(num + 1)) else: pass time.sleep(2) else: print(ticketNumOutException(ticket.WAIT_ORDER_SUB_FAIL))
def queryOrderWaitTime(self): """ 排队获取订单等待信息,每隔3秒请求一次,最高请求次数为20次! :return: """ num = 1 while True: _random = int(round(time.time() * 1000)) num += 1 if num > 30: print(u"超出排队时间,自动放弃,正在重新刷票") order_id = self.queryMyOrderNoComplete() # 排队失败,自动取消排队订单 if order_id: self.cancelNoCompleteMyOrder(order_id) break try: data = {"random": _random, "tourFlag": "dc"} queryOrderWaitTimeUrl = self.confUrl["queryOrderWaitTimeUrl"] queryOrderWaitTimeResult = self.httpClint.send(queryOrderWaitTimeUrl, data) except ValueError: queryOrderWaitTimeResult = {} if queryOrderWaitTimeResult: if "status" in queryOrderWaitTimeResult and queryOrderWaitTimeResult["status"]: if "orderId" in queryOrderWaitTimeResult["data"] and queryOrderWaitTimeResult["data"]["orderId"] is not None: sendEmail(u"恭喜您订票成功,订单号为:{0}, 请立即打开浏览器登录12306,访问‘未完成订单’,在30分钟内完成支付!".format(queryOrderWaitTimeResult["data"]["orderId"])) raise ticketIsExitsException(u"恭喜您订票成功,订单号为:{0}, 请立即打开浏览器登录12306,访问‘未完成订单’,在30分钟内完成支付!".format(queryOrderWaitTimeResult["data"]["orderId"])) elif "msg" in queryOrderWaitTimeResult["data"] and queryOrderWaitTimeResult["data"]["msg"]: print queryOrderWaitTimeResult["data"]["msg"] break elif "waitTime"in queryOrderWaitTimeResult["data"] and queryOrderWaitTimeResult["data"]["waitTime"]: print(u"排队等待时间预计还剩 {0} ms".format(0-queryOrderWaitTimeResult["data"]["waitTime"])) else: print ("正在等待中") elif "messages" in queryOrderWaitTimeResult and queryOrderWaitTimeResult["messages"]: print(u"排队等待失败: " + queryOrderWaitTimeResult["messages"]) else: print(u"第{}次排队中,请耐心等待".format(num+1)) else: print(u"排队中") time.sleep(2) else: print(ticketNumOutException(u"订单提交失败!,正在重新刷票"))
def sendSubmitOrderRequest(self): """ 提交车次 预定的请求参数,注意参数顺序 注意这里为了防止secretStr被urllib.parse过度编码,在这里进行一次解码 否则调用HttpTester类的post方法将会将secretStr编码成为无效码,造成提交预定请求失败 :param secretStr: 提交车次加密 :return: """ submit_station_url = self.session.urls["submit_station_url"] submitResult = self.session.httpClint.send(submit_station_url, self.data_apr()) if 'data' in submitResult and submitResult['data']: if submitResult['data'] == 'N': coi = checkOrderInfo(self.session, self.train_no, self.set_type, self.passengerTicketStrList, self.oldPassengerStr, self.train_date, self.ticke_peoples) coi.sendCheckOrderInfo() else: print (u'出票失败') elif 'messages' in submitResult and submitResult['messages']: raise ticketIsExitsException(submitResult['messages'][0])