From 978ca34df22d9120eccc9dbfe1d2befab98c69c5 Mon Sep 17 00:00:00 2001 From: xfalcon Date: Tue, 2 Oct 2018 20:31:11 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=A0=B9=E6=8D=AE=E6=97=B6?= =?UTF-8?q?=E9=97=B4=E5=8C=BA=E9=97=B4=E8=B4=AD=E7=A5=A8=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/configCommon.py | 10 +++++++++ config/ticket_config.yaml | 31 ++++++++++++++++++++----- init/select_ticket_info.py | 41 ++++++++++++++++++++++++--------- inter/Query.py | 46 ++++++++++++++++++++++++++++++++------ utils/__init__.py | 0 utils/timeUtil.py | 14 ++++++++++++ 6 files changed, 119 insertions(+), 23 deletions(-) create mode 100644 utils/__init__.py create mode 100644 utils/timeUtil.py diff --git a/config/configCommon.py b/config/configCommon.py index 2861638..3c40963 100755 --- a/config/configCommon.py +++ b/config/configCommon.py @@ -16,6 +16,16 @@ RS_TIMEOUT = 1 RS_JSON_ERROR = 2 RS_OTHER_ERROR = 3 +seat_conf = {'商务座': 32, + '一等座': 31, + '二等座': 30, + '特等座': 25, + '软卧': 23, + '硬卧': 28, + '硬座': 29, + '无座': 26, + } +seat_conf_2 = dict([(v,k) for (k,v) in seat_conf.iteritems()]) def getNowTimestamp(): return time.time() diff --git a/config/ticket_config.yaml b/config/ticket_config.yaml index ac42908..c8e8e4c 100755 --- a/config/ticket_config.yaml +++ b/config/ticket_config.yaml @@ -5,13 +5,32 @@ set: # - 2018-01-06 # - 2018-01-07 station_dates: - - "2018-09-28" + - "2018-09-28" + + # 是否根据时间范围 和 乘车类型 购票 + # 否则将需要手动填写车次 + is_by_time: False + + # 列车类型: 高铁 G 动车 D 其它火车 O + train_types: [G,D,O] + + # 可接受最早出发时间 格式ex: + # departure_time: "8:00" + departure_time: "00:00" + + # 可接受最晚抵达时间 格式ex: + # arrival_time: "16:00" + arrival_time: "24:00" + + # 可接受最长旅途时间 格式ex: + # take_time: "24:00" + take_time: "24:00" # 过滤车次(list),格式ex: # - "G1353" # - "G1329" station_trains: - - "" + - "" # 出发城市,比如深圳北,就填深圳就搜得到 @@ -24,7 +43,7 @@ set: # - "二等座" # - "一等座" set_type: - - "" + - "" # 当余票小于乘车人,如果选择优先提交,则删减联系人和余票数一致在提交 is_more_ticket: False @@ -33,11 +52,11 @@ set: # - "张三" # - "李四" ticke_peoples: - - "" + - "" # 12306登录账号(list) 12306account: - - user: "" - - pwd: "" + - user: "" + - pwd: "" # 加入小黑屋时间,此功能为了防止僵尸票导致一直下单不成功错过正常的票 ticket_black_list_time: 5 diff --git a/init/select_ticket_info.py b/init/select_ticket_info.py index 00a70be..ef01315 100755 --- a/init/select_ticket_info.py +++ b/init/select_ticket_info.py @@ -3,13 +3,15 @@ import datetime import random import socket import sys -import threading import time + import wrapcache from agency.cdn_utils import CDNProxy from config import urlConf from config.TicketEnmu import ticket +from config.configCommon import seat_conf +from config.configCommon import seat_conf_2 from config.ticketConf import _get_yaml from init.login import GoLogin from inter.AutoSubmitOrderRequest import autoSubmitOrderRequest @@ -24,6 +26,7 @@ from myException.ticketConfigException import ticketConfigException from myException.ticketIsExitsException import ticketIsExitsException from myException.ticketNumOutException import ticketNumOutException from myUrllib.httpUtils import HTTPClient +from utils.timeUtil import time_to_minutes,minutes_to_time reload(sys) sys.setdefaultencoding('utf-8') @@ -37,7 +40,9 @@ class select: def __init__(self): self.from_station, self.to_station, self.station_dates, self._station_seat, self.is_more_ticket, \ self.ticke_peoples, self.station_trains, self.ticket_black_list_time, \ - self.order_type = self.get_ticket_info() + self.order_type, self.is_by_time, self.train_types, self.departure_time, \ + self.arrival_time, self.take_time = self.get_ticket_info() + self.is_auto_code = _get_yaml()["is_auto_code"] self.auto_code_type = _get_yaml()["auto_code_type"] self.is_cdn = _get_yaml()["is_cdn"] @@ -58,29 +63,43 @@ class select: from_station = ticket_info_config["set"]["from_station"].encode("utf8") to_station = ticket_info_config["set"]["to_station"].encode("utf8") station_dates = ticket_info_config["set"]["station_dates"] - set_type = ticket_info_config["set"]["set_type"] + set_names = ticket_info_config["set"]["set_type"] + set_type = [seat_conf[x.encode("utf8")] for x in ticket_info_config["set"]["set_type"]] is_more_ticket = ticket_info_config["set"]["is_more_ticket"] ticke_peoples = ticket_info_config["set"]["ticke_peoples"] station_trains = ticket_info_config["set"]["station_trains"] ticket_black_list_time = ticket_info_config["ticket_black_list_time"] order_type = ticket_info_config["order_type"] + + # by time + is_by_time = ticket_info_config["set"]["is_by_time"] + train_types = ticket_info_config["set"]["train_types"] + departure_time = time_to_minutes(ticket_info_config["set"]["departure_time"]) + arrival_time = time_to_minutes(ticket_info_config["set"]["arrival_time"]) + take_time = time_to_minutes(ticket_info_config["set"]["take_time"]) + print u"*" * 20 print u"12306刷票小助手,最后更新于2018.9.21,请勿作为商业用途,交流群号:286271084" + if is_by_time: + method_notie="购票方式:根据时间区间购票\n可接受最早出发时间:{0}\n可接受最晚抵达时间:{1}\n可接受最长旅途时间:{2}\n可接受列车类型:{3}\n"\ + .format(minutes_to_time(departure_time),minutes_to_time(arrival_time),minutes_to_time(take_time)," , ".join(train_types)) + else: + method_notie="购票方式:根据候选车次购买\n候选购买车次:{0}".format(",".join(station_trains)) print u"当前配置:\n出发站:{0}\n到达站:{1}\n乘车日期:{2}\n坐席:{3}\n是否有票优先提交:{4}\n乘车人:{5}\n" \ - u"刷新间隔:随机(1-3S)\n候选购买车次:{6}\n僵尸票关小黑屋时长:{7}\n 下单接口:{8}\n".format \ + u"刷新间隔:随机(1-3S)\n{6}\n僵尸票关小黑屋时长:{7}\n 下单接口:{8}\n".format \ ( from_station, to_station, station_dates, - ",".join(set_type), + ",".join(set_names), is_more_ticket, ",".join(ticke_peoples), - ",".join(station_trains), + method_notie, ticket_black_list_time, order_type, ) print u"*" * 20 - return from_station, to_station, station_dates, set_type, is_more_ticket, ticke_peoples, station_trains, ticket_black_list_time, order_type + return from_station, to_station, station_dates, set_type, is_more_ticket, ticke_peoples, station_trains, ticket_black_list_time, order_type, is_by_time, train_types, departure_time, arrival_time, take_time def station_table(self, from_station, to_station): """ @@ -155,13 +174,14 @@ class select: l = liftTicketInit(self) l.reqLiftTicketInit() self.call_login() - checkUser(self).sendCheckUser() + check_user = checkUser(self) + check_user.sendCheckUser() from_station, to_station = self.station_table(self.from_station, self.to_station) num = 0 while 1: try: num += 1 - checkUser(self).sendCheckUser() + check_user.sendCheckUser() if time.strftime('%H:%M:%S', time.localtime(time.time())) > "23:00:00" or time.strftime('%H:%M:%S', time.localtime( time.time())) < "06:00:00": @@ -200,7 +220,8 @@ class select: print(ticket.QUEUE_WARNING_MSG.format(train_no)) else: # 获取联系人 - s = getPassengerDTOs(session=self, ticket_peoples=self.ticke_peoples, set_type=seat, is_more_ticket_num=is_more_ticket_num) + s = getPassengerDTOs(session=self, ticket_peoples=self.ticke_peoples, set_type=seat_conf_2[seat], + is_more_ticket_num=is_more_ticket_num) getPassengerDTOsResult = s.getPassengerTicketStrListAndOldPassengerStr() if getPassengerDTOsResult.get("status", False): self.passengerTicketStrList = getPassengerDTOsResult.get("passengerTicketStrList", "") diff --git a/inter/Query.py b/inter/Query.py index 958813a..8bd8426 100644 --- a/inter/Query.py +++ b/inter/Query.py @@ -8,7 +8,8 @@ import wrapcache from config import urlConf from config.TicketEnmu import ticket from myUrllib.httpUtils import HTTPClient - +from config.configCommon import seat_conf_2 +from utils.timeUtil import time_to_minutes class query: """ @@ -30,6 +31,14 @@ class query: self.ticket_black_list = dict() self.ticke_peoples_num = ticke_peoples_num + #by time + self.is_by_time = session.is_by_time + self.train_types = session.train_types + self.departure_time = session.departure_time + self.arrival_time = session.arrival_time + self.take_time = session.take_time + + @classmethod def station_seat(self, index): """ 获取车票对应坐席 @@ -46,6 +55,25 @@ class query: } return seat[index] + def check_time_interval(self,ticket_info): + return self.departure_time <= time_to_minutes(ticket_info[8]) and \ + time_to_minutes(ticket_info[9]) <= self.arrival_time and \ + time_to_minutes(ticket_info[10]) <= self.take_time + + def check_train_types(self,train): + train_type = train[0] + if train_type!="G" and train_type!="D":train_type="O" + if train_type in self.train_types: + return True + else: + return False + + def check_is_need_train(self,ticket_info): + if self.is_by_time: + return self.check_train_types(ticket_info[3]) and self.check_time_interval(ticket_info) + else: + return ticket_info[3] in self.station_trains + def sendQuery(self): """ 查询 @@ -64,9 +92,9 @@ class query: for i in value['result']: ticket_info = i.split('|') if ticket_info[11] == "Y" and ticket_info[1].encode("utf8") == "预订": # 筛选未在开始时间内的车次 - for j in xrange(len(self._station_seat)): - is_ticket_pass = ticket_info[self.station_seat(self._station_seat[j].encode("utf8"))] - if is_ticket_pass != '' and is_ticket_pass != '无' and ticket_info[3] in self.station_trains and is_ticket_pass != '*': # 过滤有效目标车次 + for j in self._station_seat: + is_ticket_pass = ticket_info[j] + if is_ticket_pass != '' and is_ticket_pass != '无' and is_ticket_pass != '*' and self.check_is_need_train(ticket_info): # 过滤有效目标车次 secretStr = ticket_info[0] train_no = ticket_info[2] query_from_station_name = ticket_info[6] @@ -74,15 +102,19 @@ class query: train_location = ticket_info[15] stationTrainCode = ticket_info[3] leftTicket = ticket_info[12] - seat = self._station_seat[j].encode("utf8") + start_time=ticket_info[8] + arrival_time=ticket_info[9] + distance_time=ticket_info[10] + print start_time,arrival_time,distance_time + seat = j try: - ticket_num = int(ticket_info[self.station_seat(self._station_seat[j].encode("utf8"))]) + ticket_num = int(ticket_info[j]) except ValueError: ticket_num = "有" print (u'车次: {0} 始发车站: {1} 终点站: {2} {3}: {4}'.format(ticket_info[3], self.from_station_h, self.to_station_h, - self._station_seat[j].encode("utf8"), + seat_conf_2[j], ticket_num)) if wrapcache.get(train_no): print(ticket.QUERY_IN_BLACK_LIST.format(train_no)) diff --git a/utils/__init__.py b/utils/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/utils/timeUtil.py b/utils/timeUtil.py new file mode 100644 index 0000000..4925807 --- /dev/null +++ b/utils/timeUtil.py @@ -0,0 +1,14 @@ +def time_to_minutes(time_str): + s = time_str.split(":") + a = int(s[0]) * 60 + int(s[1]) + return a + + +def minutes_to_time(minutes): + m = minutes % 60 + if m<10: + return str(minutes / 60) + ":" + str("0"+str(m)) + else: + return str(minutes / 60) + ":" + str(m) + +