1. 增加随机数据的逻辑

2. 增加时间戳的概念, 用来判断推荐数据的新旧
main
RogerWork 3 months ago
parent b25e499da7
commit 888452e7a1
  1. 6
      dspt_api/urls.py
  2. 20
      dspt_api/util/api/cinema_goods_pic.py
  3. 14
      dspt_api/util/api/cinema_hall_seats.py
  4. 11
      dspt_api/util/api/cinema_plays_increment.py
  5. 14
      dspt_api/util/api/play_info.py
  6. 19
      dspt_api/util/api/play_period_changed_seats.py
  7. 14
      dspt_api/util/api/play_seat_overview.py
  8. 20
      dspt_api/util/api/play_seat_status.py
  9. 29
      dspt_api/util/api/seat_lock.py
  10. 12
      dspt_api/util/general/handle_redis.py
  11. 21
      dspt_api/util/random_params.py
  12. 27
      dspt_api/util/suggest_params.py
  13. 27
      dspt_api/views.py

@ -1,6 +1,9 @@
from dspt_api import views
from django.urls import path
urlpatterns = [
path('get_channel', views.EcChannelViewSet.as_view({'get': 'list'}), name='get_ec_channel'),
path('get_env', views.EcEnvViewSet.as_view({'get': 'list'}), name='get_ec_env'),
@ -10,9 +13,10 @@ urlpatterns = [
path('get_request_log', views.EcRequestLogViewSet.as_view({'get': 'list'}), name='get_ec_request_log'),
path('get_api_group', views.EcApiGroupViewSet.as_view({'get': 'list'}), name='get_ec_api_group'),
path('get_params_by_type', views.get_api_params_by_api_type),
path('get_url', views.general_api_url),
path('get_url', views.general_api_url),
path('send_request', views.send_request),
path('get_suggest_params', views.get_suggest_params_by_api),
path('get_suggest_params_timestamp', views.get_suggest_params_timestamp_by_api),
path('set_user_data', views.set_user_select_data),
path('clear_user_data', views.clear_user_select_data),
]

@ -13,16 +13,26 @@ class ApiCinemaGoodsPic(object):
# play_id play_update_time
request_api = {'name': '3.2.1 获取卖品列表', 'path': 'cinema/goods'}
redis_key_api = f'dspt_api_{self.ip}_{self.member_type}_{request_api["path"]}'
result, _format, redis_data = get_data_from_redis(redis_key_api)
result, _format, redis_data, _timestamp = get_data_from_redis(redis_key_api)
if result:
goods_list = get_goods_list(redis_data, _format)
goods = random.choice(goods_list)
goods_id = goods['id']
goods_type = goods['type']
return [{'param': 'goods_id', 'value': goods_id, 'is_checked': True, 'result': True},
{'param': 'type', 'value': goods_type, 'is_checked': True, 'result': True}]
return [
{'param': 'goods_id', 'value': goods_id, 'is_checked': True, 'result': True, 'timestamp': _timestamp},
{'param': 'type', 'value': goods_type, 'is_checked': True, 'result': True, 'timestamp': _timestamp}]
else:
# 返回推荐参数应该包含参数名,参数值,和是否勾选的状态
return [
{'param': 'goods_id', 'value': redis_data + request_api["name"], 'is_checked': True, 'result': False},
{'param': 'type', 'value': redis_data + request_api["name"], 'is_checked': True, 'result': False}]
{'param': 'goods_id', 'value': redis_data + request_api["name"], 'is_checked': True, 'result': False,
'timestamp': _timestamp},
{'param': 'type', 'value': redis_data + request_api["name"], 'is_checked': True, 'result': False,
'timestamp': _timestamp}]
def get_timestamp(self):
# play_id play_update_time
request_api = {'name': '3.2.1 获取卖品列表', 'path': 'cinema/goods'}
redis_key_api = f'dspt_api_{self.ip}_{self.member_type}_{request_api["path"]}'
result, _format, redis_data, _timestamp = get_data_from_redis(redis_key_api)
return _timestamp if result else 0

@ -12,14 +12,20 @@ class ApiCinemaHallSeats:
# hall_id
request_api = {'name': '3.1.3 获取影厅列表', 'path': 'cinema/halls'}
redis_key_api = f'dspt_api_{self.ip}_{self.member_type}_{request_api["path"]}'
result, _format, redis_data = get_data_from_redis(redis_key_api)
result, _format, redis_data, _timestamp = get_data_from_redis(redis_key_api)
if result:
hall_data = random.choice(redis_data)
hall_id = hall_data['id']
return [{'param': 'hall_id', 'value': hall_id, 'is_checked': True, 'result': True}]
return [{'param': 'hall_id', 'value': hall_id, 'is_checked': True, 'result': True, 'timestamp': _timestamp}]
else:
# 返回推荐参数应该包含参数名,参数值,和是否勾选的状态
return [
{'param': 'hall_id', 'value': redis_data + request_api["name"], 'is_checked': True, 'result': False}]
{'param': 'hall_id', 'value': redis_data + request_api["name"], 'is_checked': True, 'result': False,
'timestamp': _timestamp}]
def get_timestamp(self):
# hall_id
request_api = {'name': '3.1.3 获取影厅列表', 'path': 'cinema/halls'}
redis_key_api = f'dspt_api_{self.ip}_{self.member_type}_{request_api["path"]}'
result, _format, redis_data, _timestamp = get_data_from_redis(redis_key_api)
return _timestamp if result else 0

@ -12,8 +12,15 @@ class ApiCinemaPlaysIncrement:
# start
request_api = {'name': '3.1.6 获取放映计划列表(增量)', 'path': 'cinema/plays-increment'}
redis_key_api = f'dspt_api_{self.ip}_{self.member_type}_{request_api["path"]}'
result, _format, redis_data = get_data_from_redis(redis_key_api)
result, _format, redis_data, _timestamp = get_data_from_redis(redis_key_api)
# 生成默认值
start = redis_data['nextSyncStartTime'] if result else datetime.datetime.strftime(datetime.datetime.now(),
'%Y-%m-%d') + ' 06:00:00'
return [{'param': 'start', 'value': start, 'is_checked': True, 'result': True}]
return [{'param': 'start', 'value': start, 'is_checked': True, 'result': True, 'timestamp': _timestamp}]
def get_timestamp(self):
# start
request_api = {'name': '3.1.6 获取放映计划列表(增量)', 'path': 'cinema/plays-increment'}
redis_key_api = f'dspt_api_{self.ip}_{self.member_type}_{request_api["path"]}'
result, _format, redis_data, _timestamp = get_data_from_redis(redis_key_api)
return _timestamp if result else 0

@ -13,14 +13,20 @@ class ApiPlayInfo:
# play_id
request_api = {'name': '3.1.5 获取放映计划列表', 'path': 'cinema/plays'}
redis_key_api = f'dspt_api_{self.ip}_{self.member_type}_{request_api["path"]}'
result, _format, redis_data = get_data_from_redis(redis_key_api)
result, _format, redis_data, _timestamp = get_data_from_redis(redis_key_api)
if result:
play_data = random.choice(redis_data)
play_id = play_data['id']
return [{'param': 'play_id', 'value': play_id, 'is_checked': True, 'result': True}]
return [{'param': 'play_id', 'value': play_id, 'is_checked': True, 'result': True, 'timestamp': _timestamp}]
else:
# 返回推荐参数应该包含参数名,参数值,和是否勾选的状态
return [
{'param': 'play_id', 'value': redis_data + request_api["name"], 'is_checked': True, 'result': True}]
{'param': 'play_id', 'value': redis_data + request_api["name"], 'is_checked': True, 'result': True,
'timestamp': _timestamp}]
def get_timestamp(self):
# play_id
request_api = {'name': '3.1.5 获取放映计划列表', 'path': 'cinema/plays'}
redis_key_api = f'dspt_api_{self.ip}_{self.member_type}_{request_api["path"]}'
result, _format, redis_data, _timestamp = get_data_from_redis(redis_key_api)
return _timestamp if result else 0

@ -13,15 +13,24 @@ class ApiPlayPeriodChangedSeats:
# play_id play_update_time
request_api = {'name': '3.1.5 获取放映计划列表', 'path': 'cinema/plays'}
redis_key_api = f'dspt_api_{self.ip}_{self.member_type}_{request_api["path"]}'
result, _format, redis_data = get_data_from_redis(redis_key_api)
result, _format, redis_data, _timestamp = get_data_from_redis(redis_key_api)
if result:
play_data = random.choice(redis_data)
play_id = play_data['id']
start = datetime.datetime.strftime(datetime.datetime.now(), '%Y-%m-%d') + ' 06:00:00'
return [{'param': 'play_id', 'value': play_id, 'is_checked': True, 'result': True},
{'param': 'start', 'value': start, 'is_checked': True, 'result': True}]
return [{'param': 'play_id', 'value': play_id, 'is_checked': True, 'result': True, 'timestamp': _timestamp},
{'param': 'start', 'value': start, 'is_checked': True, 'result': True, 'timestamp': _timestamp}]
else:
# 返回推荐参数应该包含参数名,参数值,和是否勾选的状态
return [
{'param': 'play_id', 'value': redis_data + request_api["name"], 'is_checked': True, 'result': True},
{'param': 'start', 'value': redis_data + request_api["name"], 'is_checked': True, 'result': True}]
{'param': 'play_id', 'value': redis_data + request_api["name"], 'is_checked': True, 'result': True,
'timestamp': _timestamp},
{'param': 'start', 'value': redis_data + request_api["name"], 'is_checked': True, 'result': True,
'timestamp': _timestamp}]
def get_timestamp(self):
# play_id play_update_time
request_api = {'name': '3.1.5 获取放映计划列表', 'path': 'cinema/plays'}
redis_key_api = f'dspt_api_{self.ip}_{self.member_type}_{request_api["path"]}'
result, _format, redis_data, _timestamp = get_data_from_redis(redis_key_api)
return _timestamp if result else 0

@ -12,12 +12,20 @@ class ApiPlaySeatOverview:
# play_id play_update_time
request_api = {'name': '3.1.5 获取放映计划列表', 'path': 'cinema/plays'}
redis_key_api = f'dspt_api_{self.ip}_{self.member_type}_{request_api["path"]}'
result, _format, redis_data = get_data_from_redis(redis_key_api)
result, _format, redis_data, _timestamp = get_data_from_redis(redis_key_api)
if result:
play_data = random.choice(redis_data)
play_id = play_data['id']
return [{'param': 'play_id', 'value': play_id, 'is_checked': True}]
return [{'param': 'play_id', 'value': play_id, 'is_checked': True, 'timestamp': _timestamp}]
else:
# 返回推荐参数应该包含参数名,参数值,和是否勾选的状态
return [
{'param': 'play_id', 'value': redis_data + request_api["name"], 'is_checked': True, 'result': True}]
{'param': 'play_id', 'value': redis_data + request_api["name"], 'is_checked': True, 'result': True,
'timestamp': _timestamp}]
def get_timestamp(self):
# play_id play_update_time
request_api = {'name': '3.1.5 获取放映计划列表', 'path': 'cinema/plays'}
redis_key_api = f'dspt_api_{self.ip}_{self.member_type}_{request_api["path"]}'
result, _format, redis_data, _timestamp = get_data_from_redis(redis_key_api)
return _timestamp if result else 0

@ -12,15 +12,25 @@ class ApiPlaySeatStatus:
# play_id play_update_time
request_api = {'name': '3.1.5 获取放映计划列表', 'path': 'cinema/plays'}
redis_key_api = f'dspt_api_{self.ip}_{self.member_type}_{request_api["path"]}'
result, _format, redis_data = get_data_from_redis(redis_key_api)
result, _format, redis_data, _timestamp = get_data_from_redis(redis_key_api)
if result:
play_data = random.choice(redis_data)
play_id = play_data['id']
play_update_time = play_data['cineUpdateTime']
return [{'param': 'play_id', 'value': play_id, 'is_checked': True, 'result': True},
{'param': 'play_update_time', 'value': play_update_time, 'is_checked': True, 'result': True}]
return [{'param': 'play_id', 'value': play_id, 'is_checked': True, 'result': True, 'timestamp': _timestamp},
{'param': 'play_update_time', 'value': play_update_time, 'is_checked': True, 'result': True,
'timestamp': _timestamp}]
else:
# 返回推荐参数应该包含参数名,参数值,和是否勾选的状态
return [
{'param': 'play_id', 'value': redis_data + request_api["name"], 'is_checked': True, 'result': True},
{'param': 'play_update_time', 'value': redis_data + request_api["name"], 'is_checked': True, 'result': True}]
{'param': 'play_id', 'value': redis_data + request_api["name"], 'is_checked': True, 'result': True,
'timestamp': _timestamp},
{'param': 'play_update_time', 'value': redis_data + request_api["name"], 'is_checked': True,
'result': True, 'timestamp': _timestamp}]
def get_timestamp(self):
# play_id play_update_time
request_api = {'name': '3.1.5 获取放映计划列表', 'path': 'cinema/plays'}
redis_key_api = f'dspt_api_{self.ip}_{self.member_type}_{request_api["path"]}'
result, _format, redis_data, _timestamp = get_data_from_redis(redis_key_api)
return _timestamp if result else 0

@ -0,0 +1,29 @@
from dspt_api.util.general.handle_redis import get_data_from_redis
import random
class ApiSeatLock:
def __init__(self, **kwargs):
self.member_type = kwargs.get('member_type')
self.api = kwargs.get('api')
self.ip = kwargs.get('ip')
# def get_suggestion(self):
# # play_id
# request_api_play = {'name': '3.1.5 获取放映计划列表', 'path': 'cinema/plays'}
# redis_key_api_play = f'dspt_api_{self.ip}_{self.member_type}_{request_api_play["path"]}'
# result_play, _format_play, redis_data_play = get_data_from_redis(redis_key_api_play)
# # seat_id
# request_api_seat = {'name': '3.1.8 获取某场次座位状态', 'path': 'play/seat-status'}
# redis_key_api_seat = f'dspt_api_{self.ip}_{self.member_type}_{request_api_seat["path"]}'
# result_seat, _format_seat, redis_data_seat = get_data_from_redis(redis_key_api_seat)
# if result:
# play_data = random.choice(redis_data)
# play_id = play_data['id']
# return [{'param': 'play_id', 'value': play_id, 'is_checked': True, 'result': True}]
# else:
# # 返回推荐参数应该包含参数名,参数值,和是否勾选的状态
# return [
# {'param': 'play_id', 'value': redis_data + request_api["name"], 'is_checked': True, 'result': True}]

@ -12,7 +12,13 @@ def get_data_from_redis(redis_key):
redis_data = json.loads(redis_conn.get(redis_key + '_user_data'))
user_data = redis_data['user_data']
print('user_data', json.dumps(user_data))
return True, redis_data['format'], user_data
return True, redis_data['format'], user_data, redis_data['timestamp']
# 如果没用户数据时优先获取随机数据
if redis_conn.exists(redis_key + '_random'):
redis_data = json.loads(redis_conn.get(redis_key + '_random'))
user_data = redis_data['user_data']
print('user_data', json.dumps(user_data))
return True, redis_data['format'], user_data, redis_data['timestamp']
# 没有用户数据时,随机选择数据
if redis_conn.exists(redis_key):
redis_data = json.loads(redis_conn.get(redis_key))
@ -26,5 +32,5 @@ def get_data_from_redis(redis_key):
return False, redis_data['format'], '请检查接口返回值或手动输入参数:'
if len(resp_data) == 0:
return False, redis_data['format'], '接口返回数据为空,请手动输入参数:'
return True, redis_data['format'], resp_data
return False, 'json', '请手动输入参数,或先请求接口:'
return True, redis_data['format'], resp_data, redis_data['timestamp']
return False, 'json', '请手动输入参数,或先请求接口:', 0

@ -0,0 +1,21 @@
import random
import json
import time
from django_redis import get_redis_connection
def random_params(_user_info, _handle_data):
print('_user_info', _user_info)
print('_handle_data', _handle_data)
redis_key_api = f'dspt_api_{_user_info["user_ip"]}_{_user_info["member_type"]}_{_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),
'user_data': [random.choice(_handle_data['res']['data'])],
}
redis_conn.set(redis_key_api, json.dumps(data))

@ -35,3 +35,30 @@ def suggest_params(member_type, api, ip):
print('cinema/goods-pic')
params = ApiCinemaGoodsPic(**data).get_suggestion()
return params
def suggest_params_timestamp(member_type, api, ip):
data = {'member_type': member_type, 'api': api, 'ip': ip}
_timestamps = 0
# 非会员
if api == 'cinema/hall-seats' and member_type == 'nonmember':
print('cinema/hall-seats')
_timestamps = ApiCinemaHallSeats(**data).get_timestamp()
if api == 'cinema/plays-increment' and member_type == 'nonmember':
print('cinema/plays-increment')
_timestamps = ApiCinemaPlaysIncrement(**data).get_timestamp()
if api == 'play/info' and member_type == 'nonmember':
print('play/info')
_timestamps = ApiPlayInfo(**data).get_timestamp()
if api == 'play/seat-status' and member_type == 'nonmember':
print('play/seat-status')
_timestamps = ApiPlaySeatStatus(**data).get_timestamp()
if api == 'play/period-changed-seats' and member_type == 'nonmember':
print('play/period-changed-seats')
_timestamps = ApiPlayPeriodChangedSeats(**data).get_timestamp()
if api == 'play/seat-overview' and member_type == 'nonmember':
print('play/seat-overview')
_timestamps = ApiPlaySeatOverview(**data).get_timestamp()
if api == 'cinema/goods-pic' and member_type == 'nonmember':
print('cinema/goods-pic')
_timestamps = ApiCinemaGoodsPic(**data).get_timestamp()
return _timestamps

@ -1,6 +1,7 @@
import datetime
import json
import re
import time
import requests
import xml.etree.ElementTree
@ -14,14 +15,17 @@ from django.views.decorators.csrf import csrf_exempt
from django.http.response import JsonResponse, HttpResponse
# from rest_framework.response import Response
from django.utils import timezone
from dspt_api.util.random_params import random_params
from dspt_api.util.sign import Sign
from dspt_api.models import EcChannel, EcEnv, EcApi, EcApiParams, EcCinemaIds, EcRequestLog, EcApiGroup
from dspt_api.serializers import EcChannelSerializer, EcEnvSerializer, EcApiSerializer, EcApiParamsSerializer, \
EcCinemaIdsSerializer, EcRequestLogSerializer, EcApiGroupSerializer
from dspt_api.util.suggest_params import suggest_params
from dspt_api.util.suggest_params import suggest_params, suggest_params_timestamp
from dspt_api.util.general.handle_xml_resp import HandleXmlResp
from dspt_api.util.general.format_xml import format_xml
# Create your views here.
class EcChannelViewSet(viewsets.ModelViewSet):
queryset = EcChannel.objects.all()
@ -140,6 +144,17 @@ def get_suggest_params_by_api(request):
return JsonResponse(params, safe=False)
@csrf_exempt
def get_suggest_params_timestamp_by_api(request):
# 获取基础数据
member_type = request.GET.get('member_type')
api = request.GET.get('api')
user_ip = request.META.get('REMOTE_ADDR')
_ts = suggest_params_timestamp(member_type, api, user_ip)
print('timestamp', _ts)
return JsonResponse({'timestamp': _ts})
# 外部接口, 用于实时生成url并返回前端
@csrf_exempt
def general_api_url(request):
@ -210,6 +225,7 @@ def send_request(request):
'request_url': req.url,
'member_type': member_type,
'format': resp_format,
'timestamp': int(time.time() * 1000),
'params': params,
'response_data': response.text,
'handled_data': handled_data,
@ -226,6 +242,14 @@ def send_request(request):
}
print(db_data)
EcRequestLog.objects.create(**db_data)
# 写入随机值
user_data = {
'user_ip': user_ip,
'member_type': member_type,
'api': api,
'format': resp_format,
}
random_params(user_data, handled_data)
return JsonResponse({'format': resp_format, 'data': response_data, 'handled': json.dumps(handled_data)})
@ -256,6 +280,7 @@ def set_user_select_data(request):
'api': api,
'member_type': member_type,
'format': resp_format,
'timestamp': int(time.time() * 1000),
'user_data': [json.loads(user_data)],
}
print('set_user_select_data', data)

Loading…
Cancel
Save