You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
109 lines
4.5 KiB
109 lines
4.5 KiB
from suds.client import Client |
|
import xml_to_dict |
|
from lib.gzip_base64 import ungzip_base64_dh2cmc |
|
from dh2ec.config import global_data |
|
import datetime |
|
import time |
|
import json |
|
import random |
|
|
|
|
|
class LockSeats: |
|
def __init__(self, data): |
|
self.ws_client = Client(data['basic_data']['url']) |
|
self.user_name = data['basic_data']['user_name'] # 固定写死的参数 |
|
self.password = data['basic_data']['password'] # 固定写死的参数 |
|
self.cinema_id = data['basic_data']['cinema_id'] |
|
self.response_data = data['response_data'] |
|
self.user_data = data['user_data'] |
|
self.xml_parse = xml_to_dict.XMLtoDict() |
|
|
|
def prepare(self, buy_seats_num): |
|
buy_seats_num = int(buy_seats_num) |
|
# 生成临时订单号 |
|
temp_booking_id_int = round(time.time() * 1000000) + random.randint(10, 99) |
|
temp_booking_id = 'mx' + str(temp_booking_id_int) |
|
self.user_data['temp_booking_id'] = temp_booking_id |
|
|
|
# 生成座位数据 |
|
seat_list = self.user_data['seat_list'] |
|
print(seat_list) |
|
select_seat_list = [] |
|
if len(seat_list) < buy_seats_num: |
|
print('无可用座位') |
|
exit() |
|
while buy_seats_num > 0: |
|
select_seat = random.choice(seat_list) |
|
if select_seat['seat_type'] not in ['L', 'R'] and select_seat not in select_seat_list: |
|
select_seat_list.append(select_seat) |
|
buy_seats_num -= 1 |
|
elif select_seat not in select_seat_list: |
|
if buy_seats_num > 1: |
|
row = select_seat['row_id'] |
|
col = int(select_seat['col_id']) |
|
seat_type = select_seat['seat_type'] |
|
for seat in seat_list: |
|
if seat['row_id'] == row and \ |
|
(seat['col_id'] == str(col - 1) or seat['col_id'] == str(col + 1)) and \ |
|
(seat['seat_type'] in ['L', 'R'] and seat['seat_type'] != seat_type): |
|
select_seat_list.append(select_seat) |
|
select_seat_list.append(seat) |
|
buy_seats_num -= 2 |
|
break |
|
self.user_data['select_seats'] = select_seat_list |
|
request_seat_list = [] |
|
for seat in select_seat_list: |
|
request_seat_list.append(f"{seat['row_id']}:{seat['col_id']}") |
|
return '|'.join(request_seat_list) |
|
|
|
def request(self, buy_seats_num=1): |
|
t = 10 |
|
r_status = '-1' |
|
while r_status != '0' and t > 0: |
|
# 准备请求数据 |
|
seat_str = self.prepare(buy_seats_num) |
|
|
|
print('-' * 100 + '\n' + '请求lockSeats接口') |
|
|
|
show_info = self.user_data['buy_show'] |
|
|
|
data = {'UserName': self.user_name, |
|
'Password': self.password, |
|
'CinemaId': self.cinema_id, |
|
'ShowDate': show_info['show_date'], |
|
'FilmId': show_info['film_id'], |
|
'ShowTime': show_info['show_time'], |
|
'HallId': show_info['hall_id'], |
|
'SectionId': '1', # SectionId 影厅场区(暂时无用) |
|
'LockUserId': '', # LockUserId 锁定座位用户名(暂时无用) |
|
'NumOfSeats': buy_seats_num, # NumOfSeats 锁定数量(暂时无用) |
|
'Seats': seat_str, |
|
'TempBookingId': self.user_data['temp_booking_id'], |
|
'WorkstationId': '', # WorkstationId 工作站(暂时无用) |
|
'ReleaseTime': '10', # ReleaseTime 释放时间(暂时无用) |
|
} |
|
print(data) |
|
result_xml = self.ws_client.service.lockSeats(**data) |
|
if 'xml' in result_xml: |
|
result_dict = self.xml_parse.parse(result_xml) |
|
else: |
|
print(f"-- gzip加密结果 --\n{result_xml}") |
|
result_xml = ungzip_base64_dh2cmc(result_xml) |
|
result_dict = self.xml_parse.parse(result_xml) |
|
print(f"-- xml结果 --\n{result_xml}") |
|
print("-- json结果 --") |
|
print(json.dumps(result_dict)) |
|
r_status = result_dict['data']['result'] |
|
t = t - 1 |
|
|
|
self.response_data['lock_seats'] = result_dict['data']['tempBookingId'] |
|
|
|
self.handle() |
|
|
|
def handle(self): |
|
self.user_data['lock_id'] = self.response_data['lock_seats'] |
|
|
|
|
|
if __name__ == "__main__": |
|
api_lock_seats = LockSeats(global_data) |
|
api_lock_seats.request()
|
|
|