diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 6e3c3a6..00e749f 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,7 +2,9 @@ + + @@ -39,7 +42,7 @@ - + @@ -51,8 +54,8 @@ - - + + @@ -85,8 +88,8 @@ - - + + @@ -97,9 +100,14 @@ - - - + + + + + + + + @@ -135,9 +143,6 @@ - SNH - DTO - \u4e0a\u6d77 长沙 _json_att myurllib2 @@ -165,10 +170,19 @@ 尝试提交订单... 恭喜您订票成功 checkRandCodeAnsyn + ticketIsExitsException(submitResult['messages'][0]) + ticketIsExitsException + 该车次小于上次提交时间 + @@ -297,6 +311,119 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -807,44 +934,72 @@ - - - - - - - - - - - - - - - - @@ -1094,10 +1234,31 @@ + + + + + + + + + + + + + + + + + + + + + - - + + @@ -1106,7 +1267,7 @@ - + @@ -1116,8 +1277,8 @@ - - + + diff --git a/config/ticket_config.yaml b/config/ticket_config.yaml index 26d4e62..fc15670 100644 --- a/config/ticket_config.yaml +++ b/config/ticket_config.yaml @@ -23,9 +23,10 @@ #damatu:打码兔账号,用于自动登录 #is_aotu_code是否自动打码,如果选择Ture,则调用打码兔打码,默认不使用打码兔 set: - station_date: "2018-02-09" + station_date: "2018-02-10" station_trains: - - "K575" + - "T17" + - "T297" # - "G2365" # - "G1371" # - "G1377" @@ -36,13 +37,14 @@ set: # - "G1373" # - "G1363" # - "G4933" - from_station: "上海" - to_station: "邵阳" + from_station: "北京" + to_station: "牡丹江" set_type: - "硬卧" is_more_ticket: True ticke_peoples: - - "文贤平" + - "彭淑杰" + - "金宏博" 12306count: # - uesr: "13828728396" # - pwd: "649823049lilymin" diff --git a/init/select_ticket_info.py b/init/select_ticket_info.py index a9d9e84..d0a1593 100644 --- a/init/select_ticket_info.py +++ b/init/select_ticket_info.py @@ -7,6 +7,7 @@ import threading import urllib import sys import time +import Queue from collections import OrderedDict from config.ticketConf import _get_yaml @@ -32,6 +33,8 @@ class select: self.user_info = "" self.secretStr = "" self.ticket_black_list = dict() + self.submitQueue = Queue.Queue(5) + self.ticket_skip_time = dict() def get_ticket_info(self): """ @@ -50,7 +53,7 @@ class select: expect_refresh_interval = ticket_info_config["expect_refresh_interval"] ticket_black_list_time = ticket_info_config["ticket_black_list_time"] print "*"*20 - print "当前配置:出发站:{0}\n到达站:{1}\n乘车日期:{2}\n坐席:{3}\n是否有票自动提交:{4}\n乘车人:{5}\n刷新间隔:{6}\n候选购买车次:{7}\n未开始刷票间隔时间:{8}\n僵尸票关小黑屋时长:{9}\n".format\ + print "当前配置:出发站:{0}\n到达站:{1}\n乘车日期:{2}\n坐席:{3}\n是否有票自动提交:{4}\n乘车人:{5}\n刷新间隔:{6}\n候选购买车次:{7}\n未开始刷票间隔时间:{8}\n僵尸票关小黑屋时长:{9}".format\ ( from_station, to_station, @@ -231,15 +234,13 @@ class select: if self.ticket_black_list.has_key(train_no) and (datetime.datetime.now() - self.ticket_black_list[train_no]).seconds/60 < int(self.ticket_black_list_time): print("该车次{} 正在被关小黑屋,跳过此车次".format(train_no)) break + if self.ticket_skip_time.has_key(train_no) and (datetime.datetime.now() - self.ticket_skip_time[train_no]).seconds < 10: + # 判断离上次车次订票时间,如果间隔太小,则跳过,如果想不停地疯狂提交,那请设置99999,就无等待了 + break else: + self.submitQueue.put({"obj": self, "train_no": train_no, "seat": self._station_seat[j].encode("utf8")}) print ('正在尝试提交订票...') - if self.check_user(): - self.submit_station() - self.getPassengerTicketStr(self._station_seat[j].encode("utf8")) - self.getRepeatSubmitToken() - self.user_info = self.getPassengerDTOs() - if self.checkOrderInfo(train_no, self._station_seat[j].encode("utf8")): - break + self.ticket_skip_time[train_no] = datetime.datetime.now() else: pass else: @@ -368,11 +369,11 @@ class select: data['REPEAT_SUBMIT_TOKEN'] = self.token checkOrderInfo = json.loads(myurllib2.Post(checkOrderInfoUrl, data, )) if 'data' in checkOrderInfo: - if checkOrderInfo["data"]["ifShowPassCode"] == "Y": + if "ifShowPassCode" in checkOrderInfo["data"] and checkOrderInfo["data"]["ifShowPassCode"] == "Y": is_need_code = True if self.getQueueCount(train_no, set_type, is_need_code): return True - if checkOrderInfo['data']['submitStatus'] is True: + if "ifShowPassCode" in checkOrderInfo["data"] and checkOrderInfo['data']['submitStatus'] is True: print ('车票提交通过,正在尝试排队') is_need_code = False if self.getQueueCount(train_no, set_type, is_need_code): @@ -380,7 +381,6 @@ class select: else: if "errMsg" in checkOrderInfo['data'] and checkOrderInfo['data']["errMsg"]: print checkOrderInfo['data']["errMsg"] - else: print checkOrderInfo elif 'messages' in checkOrderInfo and checkOrderInfo['messages']: @@ -448,7 +448,6 @@ class select: 模拟提交订单是确认按钮,参数获取方法还是get_ticketInfoForPassengerForm 中获取 :return: """ - passengerTicketStrList, oldPassengerStr = self.getPassengerTicketStrListAndOldPassengerStr() checkQueueOrderUrl = "https://kyfw.12306.cn/otn/confirmPassenger/confirmSingleForQueue" data = { @@ -547,7 +546,7 @@ class select: elif "messages" in queryOrderWaitTimeResult and queryOrderWaitTimeResult["messages"]: print("排队等待失败: " + queryOrderWaitTimeResult["messages"]) else: - print("第{}排队中,请耐心等待".format(num)) + print("第{}次排队中,请耐心等待".format(num)) else: print("排队中") time.sleep(2) @@ -610,56 +609,82 @@ class select: def main(self): from_station, to_station = self.station_table(self.from_station, self.to_station) if self.leftTicketLog(from_station, to_station): + submitOrderConsumer("daemon1", self.submitQueue).start() num = 1 + runedTime = 0 while 1: + num += 1 + sleepTime = self.select_refresh_interval*1000-runedTime + if sleepTime > 0: + time.sleep(sleepTime/1000.0) + if time.strftime('%H:%M:%S', time.localtime(time.time())) > "23:00:00": + print "12306休息时间,本程序自动停止,明天早上七点运行" + break + start_time = datetime.datetime.now() try: - num += 1 - time.sleep(self.select_refresh_interval) - if time.strftime('%H:%M:%S', time.localtime(time.time())) > "23:00:00": - print "12306休息时间,本程序自动停止,明天早上七点运行" - break - start_time = datetime.datetime.now() self.submitOrderRequest(from_station, to_station) - print "正在第{0}次查询 乘车日期: {1} 车次{2} 查询无票 代理设置 无 总耗时{3}ms".format(num, self.station_date, ",".join(self.station_trains), (datetime.datetime.now()-start_time).microseconds/1000) - except PassengerUserException as e: - print e.message - break - except ticketConfigException as e: - print e.message - break - except ticketIsExitsException as e: - print e.message - break - except ticketNumOutException as e: - print e.message - break except ValueError as e: if e.message == "No JSON object could be decoded": print("12306接口无响应,正在重试") else: print(e.message) + runedTime = (datetime.datetime.now()-start_time).microseconds/1000 + print "正在第{0}次查询 乘车日期: {1} 车次{2} 查询无票 代理设置 无 总耗时{3}ms".format(num, self.station_date, ",".join(self.station_trains), runedTime) -# class selectProducer(threading.Thread): -# """刷票队列""" -# def __init__(self, t_name, queue): -# threading.Thread.__init__(self, name=t_name) -# self.data = queue -# print "{0} 正在运行".format(t_name) -# -# def run(self): -# pass -# -# -# class submitOrderConsumer(threading.Thread): -# """订单队列""" -# def __init__(self, t_name, queue): -# threading.Thread.__init__(self, name=t_name) -# self.data = queue -# print "{0} 正在运行".format(t_name) -# -# def run(self): -# pass +class selectProducer(threading.Thread): + """刷票队列""" + def __init__(self, t_name, data): + self.t_name = t_name + self.data = data + threading.Thread.__init__(self, name=self.t_name) + print "{0} 正在运行".format(self.t_name) + + def run(self): + self.worker(self.data) + + def worker(self, data): + obj = data['obj'] + try: + if obj.check_user(): + obj.submit_station() + obj.getPassengerTicketStr(data['seat']) + obj.getRepeatSubmitToken() + obj.user_info = obj.getPassengerDTOs() + if obj.checkOrderInfo(data['train_no'], data['seat']): + return + obj.submitQueue.task_done() + except PassengerUserException as e: + print e.message + except ticketConfigException as e: + print e.message + except ticketIsExitsException as e: + print e.message + except ticketNumOutException as e: + print e.message + except ValueError as e: + if e.message == "No JSON object could be decoded": + print("12306接口无响应,正在重试") + else: + print(e.message) + print("{0} 线程运行结束".format(self.t_name)) + + +class submitOrderConsumer(threading.Thread): + """订单队列""" + def __init__(self, t_name, queue): + threading.Thread.__init__(self, name=t_name) + self.data = queue + print "{0} 正在运行".format(t_name) + + def run(self): + while 1: + if not self.data.empty(): + taskData = self.data.get() + selectProducer(taskData['train_no']+str(random.random()), taskData).start() + else: + time.sleep(1.5) + if __name__ == '__main__': diff --git a/tkcode b/tkcode index f7c06e7..e88bdc2 100644 Binary files a/tkcode and b/tkcode differ