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.
 
 

137 lines
5.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 random
import json
class GetSeats:
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, show_code, show_datetime):
# 准备购买的场次信息
show_list = self.user_data['show_list']
if show_code is not None:
for show in show_list:
if show['show_code'] == show_code:
self.user_data['buy_show'] = show
break
elif show_datetime is not None:
try:
dt = datetime.datetime.strptime(show_datetime, "%Y-%m-%d %H:%M")
s_date = datetime.datetime.strftime(dt, "%Y-%m-%d")
s_time = datetime.datetime.strftime(dt, "%H%M")
s_time = s_time[1:] if s_time.startswith('0') else s_time
for show in show_list:
if show['show_date'] == s_date and show['show_time'] == s_time:
self.user_data['buy_show'] = show
break
except ValueError as e:
print(f"{e}: {show_datetime}")
else:
pass
# if 'buy_show' not in self.user_data.keys():
self.user_data['buy_show'] = random.choice(show_list)
print('全部场次:', show_list)
print('选中的场次:', self.user_data['buy_show'])
# getSeats(xs:string UserName, xs:string Password, xs:string CinemaId, xs:string ShowDate, xs:string FilmId, xs:string ShowTime, xs:string HallId, xs:string SectionId, xs:string LockUserId, )
"""
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header/>
<SOAP-ENV:Body>
<ns1:getSeats xmlns:ns1="wsdl">
<UserName>TUOWEI</UserName>
<Password>668CBE9A6BCF4ABB</Password>
<CinemaId>304</CinemaId>
<ShowDate>2021-01-14</ShowDate>
<FilmId/>
<ShowTime>1520</ShowTime>
<HallId>1</HallId>
<SectionId/>
<LockUserId/>
</ns1:getSeats>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
"""
def request(self, buy_seats_num, show_code=None, show_datetime=None):
# 准备请求数据
self.prepare(show_code, show_datetime)
print('-' * 100 + '\n' + '请求getSeats接口')
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'], # 影片ID
'ShowTime': show_info['show_time'], # 播放时间
'HallId': show_info['hall_id'], # 影厅Id
'SectionId': '',
'LockUserId': '',
}
print(json.dumps(data))
result_xml = self.ws_client.service.getSeats(**data)
print(result_xml)
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))
self.response_data['get_seats'] = {}
self.response_data['get_seats'] = result_dict['data']['seats']
# print(result_dict['data']['seats'])
return self.handle(buy_seats_num)
# return result_dict
def handle(self, buy_seats_num):
available_seat_list = []
seat_list = self.response_data['get_seats']['seat']
# print(self.response_data['get_seats'])
for seat in seat_list:
# H 保留, W 残疾, N 单人, B 包厢, V VIP, R 右情侣座, L 左情侣座,
# print('-'*100)
# print(seat)
# seatType ['W', 'N', 'B', 'V', 'R', 'L']
if seat['@damagedFlag'] == 'N' and seat['@available'] == 'Y' and seat['@status'] == 'F' and \
seat['@lockUserId'] == "" and seat['@seatType'] in ['W', 'N', 'B', 'V']:
# print(seat)
s = {'row_id': seat['@rowId'],
'col_id': seat['@colId'],
'area_id': seat['@sectionId'],
'seat_type': seat['@seatType']}
# print(s)
available_seat_list.append(s)
# print(available_seat_list)
self.user_data['seat_list'] = available_seat_list
print('判断可购买的座位数量')
if len(available_seat_list) >= buy_seats_num:
print('可购买座位数量充足')
return True
return False
if __name__ == "__main__":
api_get_seats = GetSeats(global_data)
api_get_seats.response_data(show_code='C304202102040020010', show_date='2021-02-04 09:45')