2024-06-05 05:25:27 +08:00
|
|
|
import base64
|
|
|
|
import json
|
|
|
|
import logging
|
|
|
|
import os
|
|
|
|
from tencentcloud.common import credential
|
|
|
|
from tencentcloud.common.profile.client_profile import ClientProfile
|
|
|
|
from tencentcloud.common.profile.http_profile import HttpProfile
|
|
|
|
|
|
|
|
from tencentcloud.asr.v20190614 import asr_client, models
|
|
|
|
from django.http import JsonResponse
|
|
|
|
from Crypto.Cipher import AES
|
|
|
|
import base64
|
|
|
|
|
|
|
|
from .API_Log import get_logger
|
|
|
|
from .models import TranscriptionTask
|
|
|
|
|
|
|
|
from .views import update_usage_count
|
|
|
|
# 使用示例
|
|
|
|
log_file = '文案提取日志.log'
|
|
|
|
logger = get_logger('文案提取日志', log_file, when='midnight', backup_count=7)
|
|
|
|
|
|
|
|
def decrypt_param(x, t):
|
|
|
|
# 创建AES解密器对象
|
|
|
|
key = b'qw5w6SFE2D1jmxyd'
|
|
|
|
iv = b'345GDFED433223DF'
|
|
|
|
|
|
|
|
# 检查 x 和 t 是否为空
|
|
|
|
if not x or not t:
|
|
|
|
return False
|
|
|
|
|
|
|
|
try:
|
|
|
|
cipher = AES.new(key, AES.MODE_CBC, iv)
|
|
|
|
# 将密文进行Base64解码
|
|
|
|
ciphertext_bytes = base64.b64decode(x)
|
|
|
|
# 使用解密器解密密文
|
|
|
|
plaintext_bytes = cipher.decrypt(ciphertext_bytes)
|
|
|
|
# 删除填充字符
|
|
|
|
plaintext = plaintext_bytes.rstrip(b'\0').decode('utf-8')
|
|
|
|
# 比较解密后的明文和 t
|
|
|
|
if plaintext.rstrip('\x03') == t.rstrip('\x03'):
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
except Exception as e:
|
|
|
|
print(f"解密过程出现错误: {e}")
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
# 数据库操作函数
|
|
|
|
def get_task_from_db(video_url):
|
|
|
|
try:
|
|
|
|
return TranscriptionTask.objects.get(video_url=video_url)
|
|
|
|
except TranscriptionTask.DoesNotExist:
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
def save_task_to_db(uuid, video_url, task_id, result=None):
|
|
|
|
task = TranscriptionTask(uuid=uuid, video_url=video_url, task_id=task_id, result=result)
|
|
|
|
task.save()
|
|
|
|
|
|
|
|
|
|
|
|
def get_task_result_from_db(task_id):
|
|
|
|
try:
|
|
|
|
task = TranscriptionTask.objects.get(task_id=task_id)
|
|
|
|
return task.result
|
|
|
|
except TranscriptionTask.DoesNotExist:
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
def update_task_result_in_db(task_id, result):
|
|
|
|
try:
|
|
|
|
task = TranscriptionTask.objects.get(task_id=task_id)
|
|
|
|
task.result = result
|
|
|
|
task.save()
|
|
|
|
except TranscriptionTask.DoesNotExist:
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
def transcribe_audio(request):
|
|
|
|
data = json.loads(request.body)
|
|
|
|
video_url = data.get('url')
|
|
|
|
uuid = data.get('uuid')
|
|
|
|
openid = data.get('openid')
|
|
|
|
x = request.headers.get('X', '')
|
|
|
|
t = request.headers.get('T', '')
|
|
|
|
|
|
|
|
if not uuid or not video_url:
|
|
|
|
logger.error(
|
|
|
|
f'API 文案提取: 【缺少必要参数】\n headers={request.headers}\n data={data} \n ---------------------------------')
|
|
|
|
return JsonResponse({'error': '缺少必要参数'})
|
|
|
|
|
|
|
|
if decrypt_param(x, t):
|
|
|
|
existing_task = get_task_from_db(video_url)
|
|
|
|
if existing_task:
|
|
|
|
if existing_task.result:
|
|
|
|
return JsonResponse({'result': existing_task.result})
|
|
|
|
else:
|
|
|
|
return JsonResponse({'task_id': existing_task.task_id})
|
|
|
|
else:
|
2024-10-18 21:17:49 +08:00
|
|
|
increment = -2 # 假设每次生成视频需要消耗一次使用次数
|
2024-06-05 05:25:27 +08:00
|
|
|
function_type = 'transcribe_audio'
|
|
|
|
result = update_usage_count(openid, increment, function_type)
|
|
|
|
if not result['success']:
|
|
|
|
return JsonResponse(result)
|
|
|
|
|
|
|
|
cred = credential.Credential("AKIDb6YsAMQL4hBjVGybN6gzLeu7FUegcins", "MpYQQbp8UxQ2pbIKXrCX5d2hjx9Th2Or")
|
|
|
|
http_profile = HttpProfile()
|
|
|
|
http_profile.endpoint = "asr.tencentcloudapi.com"
|
|
|
|
client_profile = ClientProfile()
|
|
|
|
client_profile.httpProfile = http_profile
|
|
|
|
client = asr_client.AsrClient(cred, "ap-guangzhou", client_profile)
|
|
|
|
req = models.CreateRecTaskRequest()
|
|
|
|
req.EngineModelType = "16k_zh"
|
|
|
|
req.ChannelNum = 1
|
|
|
|
req.ResTextFormat = 0
|
|
|
|
req.SourceType = 0
|
|
|
|
req.Url = video_url
|
|
|
|
resp = client.CreateRecTask(req)
|
|
|
|
resp_dict = json.loads(resp.to_json_string())
|
|
|
|
|
|
|
|
task_id = resp_dict['Data']['TaskId']
|
|
|
|
save_task_to_db(uuid, video_url, task_id)
|
|
|
|
|
|
|
|
logger.info(
|
|
|
|
f'API 文案提取: 【{uuid}--{video_url} 提交任务成功】\n 返回结果={resp}\n---------------------------------')
|
|
|
|
return JsonResponse(resp_dict, safe=False)
|
|
|
|
else:
|
|
|
|
logger.error(
|
|
|
|
f'API 文案提取: 【非法参数】\n headers={request.headers}\n data={data} \n ---------------------------------')
|
|
|
|
return JsonResponse({'error': '非法参数'})
|
|
|
|
|
|
|
|
def query_task_status(request):
|
|
|
|
data = json.loads(request.body)
|
|
|
|
logger.info(
|
|
|
|
f'API 文案提取任务查询: {data} \n ---------------------------------')
|
|
|
|
task_id = data.get('task_id')
|
|
|
|
uuid = data.get('uuid')
|
|
|
|
x = request.headers.get('X', '')
|
|
|
|
t = request.headers.get('T', '')
|
|
|
|
|
|
|
|
if not uuid or not task_id:
|
|
|
|
logger.error(
|
|
|
|
f'API 文案提取任务查询: 【缺少必要参数】\n headers={request.headers}\n data={data} \n ---------------------------------')
|
|
|
|
return JsonResponse({'error': '缺少必要参数'})
|
|
|
|
|
|
|
|
if decrypt_param(x, t):
|
|
|
|
cached_result = get_task_result_from_db(task_id)
|
|
|
|
if cached_result:
|
|
|
|
return JsonResponse({'result': cached_result})
|
|
|
|
|
|
|
|
cred = credential.Credential("AKIDb6YsAMQL4hBjVGybN6gzLeu7FUegcins", "MpYQQbp8UxQ2pbIKXrCX5d2hjx9Th2Or")
|
|
|
|
http_profile = HttpProfile()
|
|
|
|
http_profile.endpoint = "asr.tencentcloudapi.com"
|
|
|
|
clientProfile = ClientProfile()
|
|
|
|
clientProfile.httpProfile = http_profile
|
|
|
|
client = asr_client.AsrClient(cred, "ap-guangzhou", clientProfile)
|
|
|
|
req = models.DescribeTaskStatusRequest()
|
|
|
|
params = {
|
|
|
|
"TaskId": int(task_id)
|
|
|
|
}
|
|
|
|
req.from_json_string(json.dumps(params))
|
|
|
|
|
|
|
|
resp = client.DescribeTaskStatus(req)
|
|
|
|
resp_dict = json.loads(resp.to_json_string())
|
|
|
|
|
|
|
|
if resp_dict['Data']['Status'] == 2:
|
|
|
|
result = resp_dict['Data']['Result']
|
|
|
|
update_task_result_in_db(task_id, result)
|
|
|
|
return JsonResponse({'result': result})
|
|
|
|
|
|
|
|
logger.info(
|
|
|
|
f'API 文案提取任务查询: 【{uuid}--{task_id} 进度查询】\n 返回结果={resp}\n---------------------------------')
|
|
|
|
return JsonResponse(resp_dict, safe=False)
|
|
|
|
else:
|
|
|
|
logger.error(
|
|
|
|
f'API 文案提取任务查询: 【非法参数】\n headers={request.headers}\n data={data} \n ---------------------------------')
|
|
|
|
return JsonResponse({'error': '非法参数'})
|