import random import json import time from django_redis import get_redis_connection RANDOM_DATA_API_LIST = ['cinema/plays', 'play/seat-status', 'cinema/goods', 'ecard/ecard-levels'] def random_params(_user_info, _handle_data): if _user_info["api"] in RANDOM_DATA_API_LIST: if str(_handle_data['res']['status']) == '0': return False redis_key_api = f'dspt_api_{_user_info["user_ip"]}_{_user_info["env"]}_{_user_info["member_type"]}_{_user_info["pid"]}_{_user_info["cid"]}_{_user_info["api"]}_random' redis_conn = get_redis_connection() if redis_conn.get(redis_key_api): redis_conn.delete(redis_key_api) data = { 'api': _user_info["api"], 'member_type': _user_info["member_type"], 'format': _user_info["format"], 'timestamp': int(time.time() * 1000), } print('random_params_user_info["api"]', _user_info["api"]) if _user_info["api"] == 'play/seat-status': _user_data = get_ok_status_seat_list(_handle_data['res']['data']) elif _user_info["api"] == 'ecard/ecard-levels': print('ecard/ecard-levels') _user_data = _handle_data['res']['data']['ecardLevelData'] elif _user_info["api"] == 'cinema/goods': print('random_params', _handle_data['res']['data']) _user_data = [random_goods(_handle_data['res']['data'])] else: _user_data = _handle_data['res']['data'] print('random_params', _user_data) if len(_user_data) > 0: print('random_params', _user_data) if _user_info["api"] in ('play/seat-status', 'cinema/goods'): data['user_data'] = [[random.choice(_user_data)]] else: data['user_data'] = [random.choice(_user_data)] redis_conn.set(redis_key_api, json.dumps(data), 10 * 60 * 60) else: redis_conn.delete(redis_key_api) def get_ok_status_seat_list(_data): seat_list = [] for s in _data: if s['seatStatus'] == 'ok' and s['type'] in ['danren', 'zhendong', 'vip']: seat_list.append(s) return seat_list def random_goods(_data): goods = random.choice(_data) n = 1 while goods['type'] == 'package' and goods['packageType'] == '2' and n < 10: goods = random.choice(_data) n += 1 return goods