def main(self): l = liftTicketInit(self) l.reqLiftTicketInit() getDrvicesID(self) self.call_login() check_user = checkUser(self) t = threading.Thread(target=check_user.sendCheckUser) t.setDaemon(True) t.start() from_station, to_station = self.station_table( TickerConfig.FROM_STATION, TickerConfig.TO_STATION) num = 0 s = getPassengerDTOs(selectObj=self, ticket_peoples=TickerConfig.TICKET_PEOPLES) passenger = s.sendGetPassengerDTOs() wrapcache.set("user_info", passenger, timeout=9999999) now = datetime.datetime.now() if TickerConfig.ORDER_MODEL is 1: print( f"预售还未开始,阻塞中,预售时间为{TickerConfig.OPEN_TIME}, 当前时间为: {now.strftime('%H:%M:%S')}" ) sleep_time_s = 0.1 sleep_time_t = 0.3 # 测试了一下有微妙级的误差,应该不影响,测试结果:2019-01-02 22:30:00.004555,预售还是会受到前一次刷新的时间影响,暂时没想到好的解决方案 while now.strftime("%H:%M:%S") < TickerConfig.OPEN_TIME: now = datetime.datetime.now() time.sleep(0.0001) print(f"预售开始,开启时间为: {now.strftime('%H:%M:%S')}") else: sleep_time_s = TickerConfig.MIN_TIME sleep_time_t = TickerConfig.MAX_TIME while 1: try: num += 1 now = datetime.datetime.now() # 感谢群里大佬提供整点代码 configCommon.checkSleepTime(self) # 晚上到点休眠 q = query( selectObj=self, from_station=from_station, to_station=to_station, from_station_h=TickerConfig.FROM_STATION, to_station_h=TickerConfig.TO_STATION, _station_seat=self._station_seat, station_trains=TickerConfig.STATION_TRAINS, station_dates=TickerConfig.STATION_DATES, ticke_peoples_num=len(TickerConfig.TICKET_PEOPLES), ) queryResult = q.sendQuery() # 查询接口 if queryResult.get("status"): train_no = queryResult.get("train_no", "") train_date = queryResult.get("train_date", "") stationTrainCode = queryResult.get("stationTrainCode", "") secretStr = queryResult.get("secretStr", "") secretList = queryResult.get("secretList", "") seat = queryResult.get("seat", "") leftTicket = queryResult.get("leftTicket", "") query_from_station_name = queryResult.get( "query_from_station_name", "") query_to_station_name = queryResult.get( "query_to_station_name", "") is_more_ticket_num = queryResult.get( "is_more_ticket_num", len(TickerConfig.TICKET_PEOPLES)) if wrapcache.get(train_no): print(ticket.QUEUE_WARNING_MSG.format(train_no)) else: # 获取联系人 s = getPassengerDTOs( selectObj=self, ticket_peoples=TickerConfig.TICKET_PEOPLES, set_type="" if isinstance(seat, list) else seat_conf_2[seat], # 候补订单需要设置多个坐席 is_more_ticket_num=is_more_ticket_num) getPassengerDTOsResult = s.getPassengerTicketStrListAndOldPassengerStr( secretStr, secretList) if getPassengerDTOsResult.get("status", False): self.passengerTicketStrList = getPassengerDTOsResult.get( "passengerTicketStrList", "") self.passengerTicketStrByAfterLate = getPassengerDTOsResult.get( "passengerTicketStrByAfterLate", "") self.oldPassengerStr = getPassengerDTOsResult.get( "oldPassengerStr", "") self.set_type = getPassengerDTOsResult.get( "set_type", "") # 提交订单 # 订单分为两种,一种为抢单,一种为候补订单 if secretStr: # 正常下单 if TickerConfig.ORDER_TYPE == 1: # 快速下单 a = autoSubmitOrderRequest( selectObj=self, secretStr=secretStr, train_date=train_date, passengerTicketStr=self. passengerTicketStrList, oldPassengerStr=self.oldPassengerStr, train_no=train_no, stationTrainCode=stationTrainCode, leftTicket=leftTicket, set_type=self.set_type, query_from_station_name= query_from_station_name, query_to_station_name=query_to_station_name, ) a.sendAutoSubmitOrderRequest() elif TickerConfig.ORDER_TYPE == 2: # 普通下单 sor = submitOrderRequest( self, secretStr, from_station, to_station, train_no, self.set_type, self.passengerTicketStrList, self.oldPassengerStr, train_date, TickerConfig.TICKET_PEOPLES) sor.sendSubmitOrderRequest() elif secretList: # 候补订单 c = chechFace(self, secretList, train_no) c.sendChechFace() else: random_time = round( random.uniform(sleep_time_s, sleep_time_t), 2) nateMsg = ' 无候补机会' if TickerConfig.ORDER_TYPE == 2 else "" print( f"正在第{num}次查询 停留时间:{random_time} 乘车日期: {','.join(TickerConfig.STATION_DATES)} 车次:{','.join(TickerConfig.STATION_TRAINS) or '所有车次'} 下单无票{nateMsg} 耗时:{(datetime.datetime.now() - now).microseconds / 1000} {queryResult.get('cdn')}" ) time.sleep(random_time) except PassengerUserException as e: print(e) break except ticketConfigException as e: print(e) break except ticketIsExitsException as e: print(e) break except ticketNumOutException as e: print(e) break except UserPasswordException as e: print(e) break except ValueError as e: if e == "No JSON object could be decoded": print(u"12306接口无响应,正在重试") else: print(e) except KeyError as e: print(e) except TypeError as e: print(u"12306接口无响应,正在重试 {0}".format(e)) except socket.error as e: print(e)
def main(self): self.cdn_certification() l = liftTicketInit(session=self) l.reqLiftTicketInit() self.call_login() checkUser(self).sendCheckUser() from_station, to_station = self.station_table(self.from_station, self.to_station) num = 0 while 1: try: num += 1 checkUser(self).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": print(ticket.REST_TIME) # while 1: # time.sleep(1) # if "06:00:00" < time.strftime('%H:%M:%S', time.localtime(time.time())) < "23:00:00": # print(ticket.REST_TIME_PAST) # self.call_login() # break start_time = datetime.datetime.now() q = query(session=self, from_station=from_station, to_station=to_station, from_station_h=self.from_station, to_station_h=self.to_station, _station_seat=self._station_seat, station_trains=self.station_trains, station_dates=self.station_dates, ) queryResult = q.sendQuery() # 查询接口 # tmp = queryResult.get("status", False) # assert tmp == False if queryResult.get("status", False): train_no = queryResult.get("train_no", "") train_date = queryResult.get("train_date", "") stationTrainCode = queryResult.get("stationTrainCode", "") set_type = queryResult.get("set_type", "") secretStr = queryResult.get("secretStr", "") leftTicket = queryResult.get("leftTicket", "") query_from_station_name = queryResult.get("query_from_station_name", "") query_to_station_name = queryResult.get("query_to_station_name", "") if wrapcache.get(train_no): print(ticket.QUEUE_WARNING_MSG.format(train_no)) else: # 获取联系人 if not self.passengerTicketStrList and not self.oldPassengerStr: s = getPassengerDTOs(session=self, ticket_peoples=self.ticke_peoples, set_type=set_type) # getPassengerDTOsResult = s.getPassengerTicketStrListAndOldPassengerStr() if getPassengerDTOsResult.get("status", False): self.passengerTicketStrList = getPassengerDTOsResult.get("passengerTicketStrList", "") self.oldPassengerStr = getPassengerDTOsResult.get("oldPassengerStr", "") set_type = getPassengerDTOsResult.get("set_type", "") # 提交订单 if self.order_type == 1: # 快读下单 a = autoSubmitOrderRequest(session=self, secretStr=secretStr, train_date=train_date, passengerTicketStr=self.passengerTicketStrList, oldPassengerStr=self.oldPassengerStr, train_no=train_no, stationTrainCode=stationTrainCode, leftTicket=leftTicket, set_type=set_type, query_from_station_name=query_from_station_name, query_to_station_name=query_to_station_name, ) a.sendAutoSubmitOrderRequest() elif self.order_type == 2: # 普通下单 sor = submitOrderRequest(self, secretStr, from_station, to_station, train_no, set_type, self.passengerTicketStrList, self.oldPassengerStr, train_date, self.ticke_peoples) sor.sendSubmitOrderRequest() else: random_time = round(random.uniform(1, 4), 2) time.sleep(random_time) print u"正在第{0}次查询 随机停留时长:{6} 乘车日期: {1} 车次:{2} 查询无票 cdn轮询IP:{4}当前cdn总数:{5} 总耗时:{3}ms".format(num, ",".join( self.station_dates), ",".join( self.station_trains), (datetime.datetime.now() - start_time).microseconds / 1000, self.httpClint.cdn, len(self.cdn_list), random_time) 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 UserPasswordException as e: print e.message break except ValueError as e: if e.message == "No JSON object could be decoded": print(u"12306接口无响应,正在重试") else: print(e.message) except KeyError as e: print(e.message) except TypeError as e: print(u"12306接口无响应,正在重试 {0}".format(e.message)) except socket.error as e: print(e.message)
def main(self): # autoSynchroTime() # 同步时间 self.cdn_certification() l = liftTicketInit(self) l.reqLiftTicketInit() self.call_login() check_user = checkUser(self) t = threading.Thread(target=check_user.sendCheckUser) t.setDaemon(True) t.start() from_station, to_station = self.station_table(self.from_station, self.to_station) num = 0 s = getPassengerDTOs(session=self, ticket_peoples=self.ticke_peoples) passenger = s.sendGetPassengerDTOs() wrapcache.set("user_info", passenger, timeout=9999999) while 1: try: num += 1 now = datetime.datetime.now() # 感谢群里大佬提供整点代码 configCommon.checkSleepTime(self) # 晚上到点休眠 if self.order_model is 1: sleep_time_s = 0.5 sleep_time_t = 0.6 # 测试了一下有微妙级的误差,应该不影响,测试结果:2019-01-02 22:30:00.004555,预售还是会受到前一次刷新的时间影响,暂时没想到好的解决方案 while not now.strftime("%H:%M:%S") == self.open_time: now = datetime.datetime.now() if now.strftime("%H:%M:%S") > self.open_time: break time.sleep(0.0001) else: sleep_time_s = 0.5 sleep_time_t = 3 q = query( session=self, from_station=from_station, to_station=to_station, from_station_h=self.from_station, to_station_h=self.to_station, _station_seat=self._station_seat, station_trains=self.station_trains, station_dates=self.station_dates, ticke_peoples_num=len(self.ticke_peoples), ) queryResult = q.sendQuery() # 查询接口 if queryResult.get("status", False): train_no = queryResult.get("train_no", "") train_date = queryResult.get("train_date", "") stationTrainCode = queryResult.get("stationTrainCode", "") secretStr = queryResult.get("secretStr", "") seat = queryResult.get("seat", "") leftTicket = queryResult.get("leftTicket", "") query_from_station_name = queryResult.get( "query_from_station_name", "") query_to_station_name = queryResult.get( "query_to_station_name", "") is_more_ticket_num = queryResult.get( "is_more_ticket_num", len(self.ticke_peoples)) if wrapcache.get(train_no): print(ticket.QUEUE_WARNING_MSG.format(train_no)) else: # 获取联系人 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", "") self.oldPassengerStr = getPassengerDTOsResult.get( "oldPassengerStr", "") self.set_type = getPassengerDTOsResult.get( "set_type", "") # 提交订单 if self.order_type == 1: # 快读下单 a = autoSubmitOrderRequest( session=self, secretStr=secretStr, train_date=train_date, passengerTicketStr=self.passengerTicketStrList, oldPassengerStr=self.oldPassengerStr, train_no=train_no, stationTrainCode=stationTrainCode, leftTicket=leftTicket, set_type=self.set_type, query_from_station_name=query_from_station_name, query_to_station_name=query_to_station_name, ) a.sendAutoSubmitOrderRequest() elif self.order_type == 2: # 普通下单 sor = submitOrderRequest( self, secretStr, from_station, to_station, train_no, self.set_type, self.passengerTicketStrList, self.oldPassengerStr, train_date, self.ticke_peoples) sor.sendSubmitOrderRequest() else: random_time = round( random.uniform(sleep_time_s, sleep_time_t), 2) print( u"正在第{0}次查询 随机停留时长:{6} 乘车日期: {1} 车次:{2} 查询无票 cdn轮询IP:{4}当前cdn总数:{5} 总耗时:{3}ms" .format(num, ",".join(self.station_dates), ",".join(self.station_trains), (datetime.datetime.now() - now).microseconds / 1000, queryResult.get("cdn", None), len(self.cdn_list), random_time)) time.sleep(random_time) except PassengerUserException as e: print(e) break except ticketConfigException as e: print(e) break except ticketIsExitsException as e: print(e) break except ticketNumOutException as e: print(e) break except UserPasswordException as e: print(e) break except ValueError as e: if e == "No JSON object could be decoded": print(u"12306接口无响应,正在重试") else: print(e) except KeyError as e: print(e) except TypeError as e: print(u"12306接口无响应,正在重试 {0}".format(e)) except socket.error as e: print(e)
def go_qiangpiao(self, tricket_info, log_info=[], num=1): try: from_station_h = tricket_info['from_station'] to_station_h = tricket_info['to_station'] from_station, to_station = self.station_name_map[ from_station_h], self.station_name_map[to_station_h] except: return log_info.append(u'站点输入有误,请检查!') try: checkUser(self).sendCheckUser() now_time = time.strftime('%H:%M:%S', time.localtime(time.time())) while now_time > "23:00:00" or now_time < "06:00:00": time.sleep(5) now_time = time.strftime('%H:%M:%S', time.localtime(time.time())) if "06:00:00" < now_time < "23:00:00": log_info.append(ticket.REST_TIME_PAST) print(ticket.REST_TIME_PAST) self.call_login() break start_time = datetime.datetime.now() q = query(session=self, from_station=from_station, to_station=to_station, from_station_h=from_station_h, to_station_h=to_station_h, _station_seat=tricket_info['seat'], station_trains=tricket_info['trains'], station_dates=tricket_info['date']) queryResult = q.sendQuery(log_info) # 查询接口 # tmp = queryResult.get("status", False) # assert tmp == False if queryResult.get("status", False): train_no = queryResult.get("train_no", "") train_date = queryResult.get("train_date", "") stationTrainCode = queryResult.get("stationTrainCode", "") set_type = queryResult.get("set_type", "") secretStr = queryResult.get("secretStr", "") leftTicket = queryResult.get("leftTicket", "") query_from_station_name = queryResult.get( "query_from_station_name", "") query_to_station_name = queryResult.get( "query_to_station_name", "") if wrapcache.get(train_no): print(ticket.QUEUE_WARNING_MSG.format(train_no)) log_info.append(ticket.QUEUE_WARNING_MSG.format(train_no)) else: # 获取联系人 if not self.passengerTicketStrList and not self.oldPassengerStr: s = getPassengerDTOs( session=self, ticket_peoples=tricket_info['person_name'], set_type=set_type) # getPassengerDTOsResult = s.getPassengerTicketStrListAndOldPassengerStr( ) if getPassengerDTOsResult.get("status", False): self.passengerTicketStrList = getPassengerDTOsResult.get( "passengerTicketStrList", "") self.oldPassengerStr = getPassengerDTOsResult.get( "oldPassengerStr", "") set_type = getPassengerDTOsResult.get( "set_type", "") self.order_type = 2 # 提交订单 if self.order_type == 1: # 快读下单 a = autoSubmitOrderRequest( session=self, secretStr=secretStr, train_date=train_date, passengerTicketStr=self.passengerTicketStrList, oldPassengerStr=self.oldPassengerStr, train_no=train_no, stationTrainCode=stationTrainCode, leftTicket=leftTicket, set_type=set_type, query_from_station_name=query_from_station_name, query_to_station_name=query_to_station_name, ) a.sendAutoSubmitOrderRequest(log_info) elif self.order_type == 2: # 普通下单 sor = submitOrderRequest( self, secretStr, from_station, to_station, train_no, set_type, self.passengerTicketStrList, self.oldPassengerStr, train_date, self.ticke_peoples) sor.sendSubmitOrderRequest(log_info) else: random_time = round(random.uniform(1, 4), 2) time.sleep(random_time) mess = u"正在第{0}次查询 随机停留时长:{6}s 乘车日期: {1} 车次:{2} 查询无票 cdn轮询IP:{4}当前cdn总数:{5} 总耗时:{3}ms".format( num, ",".join(tricket_info['date']), tricket_info['trains'], (datetime.datetime.now() - start_time).microseconds / 1000, self.httpClint.cdn, len(self.cdn_list), random_time) print mess log_info.append(mess) except PassengerUserException as e: print e.message log_info.append(e.message) # break except ticketConfigException as e: print e.message log_info.append(e.message) # break except ticketIsExitsException as e: print e.message log_info.append(e.message) # break except ticketNumOutException as e: print e.message log_info.append(e.message) # break except UserPasswordException as e: print e.message log_info.append(e.message) # break except ValueError as e: if e.message == "No JSON object could be decoded": print(u"12306接口无响应,正在重试") log_info.append(u"12306接口无响应,正在重试") else: print(e.message) log_info.append(e.message) except KeyError as e: print(e.message) log_info.append(e.message) except TypeError as e: print(u"12306接口无响应,正在重试 {0}".format(e.message)) log_info.append(u"12306接口无响应,正在重试 {0}".format(e.message)) except socket.error as e: print e log_info.append(e.message)