def check_passcode(self, rand_code, module, rand="sjrand"): data = dict(randCode=rand_code, rand=rand) ret = common.parse_json(self._session.post("https://kyfw.12306.cn/otn/passcodeNew/checkRandCodeAnsyn", data=data).text) if ret.get("data", "N") == "Y": signals.passcode_checked.send(sender=self, module=module, value=True) else: self.new_passcode(module=module, rand=rand)
def query_trains(self, from_station, to_station, date, sender=None): if not from_station or not to_station or not date: signals.error_excepted.send(sender=self, type_="query", info="请输入完整信息") return if sender is None: sender = self _data = OrderedDict() _data['purpose_codes'] = "ADULT" _data['queryDate'] = date _data['from_station'] = from_station _data['to_station'] = to_station url = "https://kyfw.12306.cn/otn/lcxxcx/query" ret = common.parse_json(self._session.get(url, params=_data).text) if ret == -1: signals.error_excepted.send(sender=self, type_="query", info="查询错误, 请重试") return data = ret.get("data", {}) flag = data.get("flag", False) if flag: signals.query_trains_completed.send(sender=sender, data=data.get("datas")) else: message = data.get("message", "") signals.error_excepted.send(sender=self, type_="query", info=message)
def query_tickets(self, from_station, to_station, date, sender=None, sendError=True): if not from_station or not to_station or not date: signals.error_excepted.send(sender=self, type_="query", info="请输入完整信息") return if sender is None: sender = self url = "https://kyfw.12306.cn/otn/leftTicket/query" data = OrderedDict() data['leftTicketDTO.train_date'] = date data['leftTicketDTO.from_station'] = from_station data['leftTicketDTO.to_station'] = to_station data['purpose_codes'] = "ADULT" ret = common.parse_json(self._session.get(url, params=data).text) if ret == -1: signals.error_excepted.send(sender=self, type_="query", info="查询错误, 请重试") return ret_data = ret.get("data", []) if not ret_data: ret_data = None if sendError: msg = "".join(ret.get("messages", [])) if not msg.strip(): msg = "没有查询到结果, 请注意购票日期" signals.error_excepted.send(sender=self, type_="query", info=msg) signals.query_tickets_completed.send(sender=sender, data=ret_data)
def get_quque_count(self): url = "https://kyfw.12306.cn/otn/confirmPassenger/getQueueCount" train = self._order_infos['train'] data = OrderedDict() data['train_date'] = self._order_infos['dateStr'] data['train_no'] = train.trainNo data['stationTrainCode'] = train.stationTrainCode data['seatType'] = self._order_infos['seat_type'] data['fromStationTelecode'] = train.fromStationTelecode data['toStationTelecode'] = train.toStationTelecode data['leftTicket'] = train.ypInfo data['purpose_codes'] = "00" data['_json_att'] = "" data['REPEAT_SUBMIT_TOKEN'] = self._order_infos['REPEAT_SUBMIT_TOKEN'] ret = common.parse_json(self._session.post(url, data=data).text) status = ret.get("status", False) if not status: signals.error_excepted.send(sender=self, type_="order", info="".join(ret.get("messages", []))) return False ret_data = ret.get("data", {}) ticket = ret_data.get("ticket", "") if not ticket: message = "前面已有{0}人在排队".format(ret_data.get("count", 0)) signals.error_excepted.send(sender=self, type_="order", info=message) self._order_infos['leftTicketStr'] = ticket return bool(ticket)
def request_passengers(self): ret = common.parse_json(self._session.post("https://kyfw.12306.cn/otn/confirmPassenger/getPassengerDTOs").text) status = ret.get("status", False) if status: passengers = ret.get("data", {}).get("normal_passengers", []) logger.info("-- received {0} passengers".format(len(passengers))) signals.passengers_received.send(sender=self, passengers=passengers) signals.error_excepted.send(sender=self, type_="query", info="联系人信息已经刷新")
def confirm_order(self): url = "https://kyfw.12306.cn/otn/confirmPassenger/confirmSingleForQueue" data = OrderedDict() data['passengerTicketStr'] = self._order_infos["passengerTicketStr"] data['oldPassengerStr'] = self._order_infos['oldPassengerStr'] data['randCode'] = self._order_infos['randCode'] data['purpose_codes'] = "00" data['key_check_isChange'] = self._order_infos['key_check_isChange'] data['leftTicketStr'] = self._order_infos['leftTicketStr'] data['train_location'] = self._order_infos["train"].locationCode data['REPEAT_SUBMIT_TOKEN'] = self._order_infos["REPEAT_SUBMIT_TOKEN"] ret = common.parse_json(self._session.post(url, data=data).text) submit_status = ret.get('data', {}).get("submitStatus", False) if not submit_status: signals.error_excepted.send(sender=self, type_="order", info="确认订单失败: " + "".join(ret.get("messages", []))) return submit_status
def login(self, rand_code): data = {"loginUserDTO.user_name": self._username, "userDTO.password" : self._passwd, "randCode" : rand_code} ret = common.parse_json(self._session.post("https://kyfw.12306.cn/otn/login/loginAysnSuggest", data=data).text) login_check = ret.get("data", {}).get("loginCheck", "N") if login_check == "Y": logger.info("-- account {0} login success [requests]".format(self._username)) logger.debug("-- save cookies to file") self._session.cookies.save(ignore_discard=True, ignore_expires=True) # emit login_successed signals. signals.login_successed.send(sender=self, username=self._username, password=self._passwd) return True message = ";".join(ret.get("messages", [])) logger.info("-- login failed!!! {0}".format(message)) signals.error_excepted.send(sender=self, type_="login", info=message)
def submit_order_request(self, train): url = "https://kyfw.12306.cn/otn/leftTicket/submitOrderRequest" data = OrderedDict() data['secretStr'] = unquote(train.secretStr) data['train_date'] = train.startTrainDate data['back_train_date'] = train.startTrainDate data['tour_flag'] = "dc" data['purpose_codes'] = "ADULT" data['query_from_station_name'] = train.fromStationName data['query_to_station_name'] = train.toStationName data['undefined'] = "" ret = common.parse_json(self._session.post(url, data=data).text) status = ret.get("status", False) if not status: messages = "提交订单请求失败: {0}".format("".join(ret.get("messages", []))) signals.error_excepted.send(sender=self, type_="order", info=messages) return status
def submit_order_request(self, train): url = "https://kyfw.12306.cn/otn/leftTicket/submitOrderRequest" data = OrderedDict() data['secretStr'] = unquote(train.secretStr.encode("utf-8")) data['train_date'] = train.startTrainDate data['back_train_date'] = train.startTrainDate data['tour_flag'] = "dc" data['purpose_codes'] = "ADULT" data['query_from_station_name'] = train.fromStationName data['query_to_station_name'] = train.toStationName data['undefined'] = "" ret = common.parse_json(self._session.post(url, data=data).text) status = ret.get("status", False) if not status: messages = "提交订单请求失败: {0}".format("".join(ret.get("messages", []))) signals.error_excepted.send(sender=self, type_="order", info=messages) return status
def check_order_info(self): url = "https://kyfw.12306.cn/otn/confirmPassenger/checkOrderInfo" data = OrderedDict() data['cancel_flag'] = 2 data['bed_level_order_num'] = "000000000000000000000000000000" data['passengerTicketStr'] = self._order_infos['passengerTicketStr'] data['oldPassengerStr'] = self._order_infos['oldPassengerStr'] data['tour_flag'] = "dc" data['randCode'] = self._order_infos['randCode'] data['_json_att'] = "" data['REPEAT_SUBMIT_TOKEN'] = self._order_infos["REPEAT_SUBMIT_TOKEN"] ret = common.parse_json(self._session.post(url, data=data).text) ret_data = ret.get('data', {}) submit_status = ret_data.get("submitStatus", False) if not submit_status: err_msg = ret_data.get("errMsg", None) if not err_msg: err_msg = "检测订单信息: " + "".join(ret.get("messages", [])) signals.error_excepted.send(sender=self, type_="order", info=err_msg) return submit_status
def user_logined(self): data = {"_json_att" : ""} ret = common.parse_json(self._session.post("https://kyfw.12306.cn/otn/login/checkUser", data=data).text) return ret.get("data", {}).get("flag", False)