def execute(self): url = "https://kyfw.12306.cn/otn/leftTicket/query" params = self.__get_query_params() json = webrequest.get_json(url, params=params) try: json_data = json["data"] except RequestError as ex: if ex.args[0] == "选择的查询日期不在预售日期范围内": raise DateOutOfRangeError() from ex raise logger.debug("Got train list from {0} to {1} on {2}".format( self.departure_station.name, self.destination_station.name, timeconverter.date_to_str(self.date))) train_list = [] station_list = StationList.instance() for train_data in json_data: query_data = train_data["queryLeftNewDTO"] departure_station = station_list.get_by_id(query_data["from_station_telecode"]) destination_station = station_list.get_by_id(query_data["to_station_telecode"]) if self.exact_departure_station and departure_station != self.departure_station: continue if self.exact_destination_station and destination_station != self.destination_station: continue train = Train(train_data, departure_station, destination_station, self.pricing, self.date) train_list.append(train) return train_list
def __get_substations(self, train): # Gets stations in (train.departure, train.destination] url = "https://kyfw.12306.cn/otn/czxx/queryByTrainNo" params = self.__get_train_data_query_params(train) json = webrequest.get_json(url, params=params) json_station_list = json["data"]["data"] logger.debug("Fetched station data for train " + train.name) istart = None iend = len(json_station_list) for i in range(len(json_station_list)): is_in_path = json_station_list[i]["isEnabled"] if is_in_path and istart is None: istart = i elif not is_in_path and istart is not None: iend = i break assert istart is not None assert json_station_list[istart]["station_name"] == train.departure_station.name assert json_station_list[iend-1]["station_name"] == train.destination_station.name available_stations = [] station_list = StationList.instance() for i in range(istart+1, iend-1): station_name = json_station_list[i]["station_name"] if self.station_blacklist[station_name]: continue station = station_list.get_by_name(station_name) available_stations.append(station) available_stations.append(train.destination_station) return available_stations
def refresh_ticket_prices(self): url = "https://kyfw.12306.cn/otn/leftTicket/queryTicketPrice" params = self.__get_price_query_params() json = webrequest.get_json(url, params=params) for key, value in json["data"].items(): if not isinstance(value, str): continue if value[0] == "¥" and value[-2] == ".": num_value = float(value[1:]) lookup = TicketType.REVERSE_ID2_LOOKUP else: try: num_value = int(value)/10.0 lookup = TicketType.REVERSE_ID_LOOKUP except ValueError: continue ticket_type = lookup.get(key) if ticket_type is None: continue if self.tickets[ticket_type].status == TicketStatus.NOT_APPLICABLE: continue self.tickets[ticket_type].price = num_value self.ticket_prices_fetched = True logger.debug("Fetched ticket prices for train " + self.name)
def execute(self): url = "https://kyfw.12306.cn/otn/leftTicketPrice/query" params = self.__get_query_params() json = webrequest.get_json(url, params=params, cookies=self.cookies) # TODO: Complete
def execute(self): if self.__data__ is None: self.load_list() url = "https://kyfw.12306.cn/otn/queryTrainInfo/query" params = self.__get_query_params() json = webrequest.get_json(url, params=params, cookies=self.cookies)