Exemple #1
0
def simple_order(date, src, dst, names, seat, train, seat_detail='0'):
    # 1. 获取站点信息
    station_res = StationResource()
    stations = station_res.process()
    if isinstance(src, unicode):
        src = src.encode('utf8')
    if isinstance(dst, unicode):
        dst = dst.encode('utf8')
    if isinstance(seat, unicode):
        seat = seat.encode('utf8')
    src = stations[src]
    dst = stations[dst]
    seat_type = SEAT_TYPE[seat]

    # 2. 获取乘客信息
    passenager_res = PassenagerResource()
    passengers = passenager_res.process()

    # 3. 初始化乘客数据
    passengers = [
        passenger for passenger in passengers
        if passenger.passenger_name in names
    ]

    for passenger in passengers:
        passenger.seat_type = seat_type
        passenger.seat_detail = seat_detail
        passenger.ticket_type = passenger.passenger_type
        passenger.card_type = passenger.passenger_id_type_code
    newstr, oldstr = build_passenger_str(passengers)
    num = len(passengers)

    # 4. 开始刷票
    while True:
        try:
            # 5. 获取余票信息并选择列车
            ticket_res = TicketResource(date, src, dst)
            tickets = ticket_res.process()
            ticket = select_specific_ticket(tickets, train, seat, num)
        except Exception:
            logger.exception('query ticket')
            time.sleep(1)
            continue
        try:
            # 6. 提交订单
            submit_order(ticket, date, newstr, oldstr)
            return
        except Exception:
            logger.exception('submit order error')
            time.sleep(1)
Exemple #2
0
def get_ticket(date, src, dst, train, seat, num=1, retry_times=10000):
    # deprecated!
    for i in xrange(retry_times):
        logger.debug('try select ticket: %d', i + 1)
        ticket_res = TicketResource(date, src, dst)
        tickets = ticket_res.process()
        try:
            ticket = select_specific_ticket(tickets, train, seat, num)
            return ticket
        except TicketSoldOut:
            logger.info('sold out!!')
            time.sleep(1)
    else:
        raise TicketSoldOut('try times[%d]' % retry_times)
Exemple #3
0
def get_ticket(date, src, dst, train, seat, num=1, retry_times=10000):
    # deprecated!
    for i in xrange(retry_times):
        logger.debug('try select ticket: %d', i + 1)
        ticket_res = TicketResource(date, src, dst)
        tickets = ticket_res.process()
        try:
            ticket = select_specific_ticket(tickets, train, seat, num)
            return ticket
        except TicketSoldOut:
            logger.info('sold out!!')
            time.sleep(1)
    else:
        raise TicketSoldOut('try times[%d]' % retry_times)
Exemple #4
0
def simple_order(date, src, dst, names, seat, train, seat_detail='0'):
    # 1. 获取站点信息
    station_res = StationResource()
    stations = station_res.process()
    if isinstance(src, unicode):
        src = src.encode('utf8')
    if isinstance(dst, unicode):
        dst = dst.encode('utf8')
    if isinstance(seat, unicode):
        seat = seat.encode('utf8')
    src = stations[src]
    dst = stations[dst]
    seat_type = SEAT_TYPE[seat]

    # 2. 获取乘客信息
    passenager_res = PassenagerResource()
    passengers = passenager_res.process()

    # 3. 初始化乘客数据
    passengers = [passenger for passenger in passengers if passenger.passenger_name in names]

    for passenger in passengers:
        passenger.seat_type = seat_type
        passenger.seat_detail = seat_detail
        passenger.ticket_type = passenger.passenger_type
        passenger.card_type = passenger.passenger_id_type_code
    newstr, oldstr = build_passenger_str(passengers)
    num = len(passengers)

    # 4. 开始刷票
    while True:
        try:
            # 5. 获取余票信息并选择列车
            ticket_res = TicketResource(date, src, dst)
            tickets = ticket_res.process()
            ticket = select_specific_ticket(tickets, train, seat, num)
        except Exception:
            logger.exception('query ticket')
            time.sleep(1)
            continue
        try:
            # 6. 提交订单
            submit_order(ticket, date, newstr, oldstr)
            return
        except Exception:
            logger.exception('submit order error')
            time.sleep(1)