ai_admin/WebAdmin/task.py
2024-09-20 04:29:09 +00:00

107 lines
4.3 KiB
Python
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from django.shortcuts import get_object_or_404
from django.http import JsonResponse
import requests
from django.views.decorators.csrf import csrf_exempt
from .models import VideoGeneration
@csrf_exempt
def update_video_status(request, pid, query_type='revid'):
"""
查询视频生成状态接口支持不同的生成接口查询revid 或 runwayml
:param pid: 视频生成的 UUID (pid)
:param query_type: 'revid' 表示查询之前接口,'runwayml' 表示新的接口
"""
if query_type == 'revid':
url = f"https://www.revid.ai/api/public/v2/status?pid={pid}"
headers = {
"key": "c4c0f516-33c0-4f5c-9d29-3b1d4e6a93c3",
"Content-Type": "text/plain"
}
elif query_type == 'runwayml':
url = f"https://api.aivideoapi.com/status?uuid={pid}" # 假设 runwayml 的接口类似,实际接口需替换
headers = {
"Authorization": "12e76710fad2047db8c0cc6b25987e2a2", # 替换为你的真实授权密钥
"Content-Type": "application/json"
}
else:
return JsonResponse({'code': 400, 'message': '无效的查询类型'})
try:
response = requests.get(url, headers=headers)
response.raise_for_status() # Raises an HTTPError for bad responses
data = response.json()
print(f"查询类型: {query_type}")
print(f"API响应内容: {data}")
# 获取视频生成记录
video_generation = get_object_or_404(VideoGeneration, pid=pid)
if query_type == 'runwayml':
# RunwayML接口返回的数据
status = data.get('status')
progress = data.get('progress', None)
video_url = data.get('url', None)
gif_url = data.get('gif_url', None)
# 根据不同状态更新数据库
if status == 'success':
video_generation.status = 'completed'
video_generation.video_url = video_url # 更新视频 URL
elif status in ['in queue', 'submitted']:
video_generation.status = 'pending'
elif status == 'submitted' and progress is not None:
video_generation.status = 'in_progress'
else:
video_generation.status = 'failed'
video_generation.description = 'Video generation failed due to an error.'
# 如果视频的gif_url存在,可以记录下来
if gif_url:
video_generation.gif_url = gif_url
video_generation.save()
return JsonResponse({
'code': 200,
'message': 'RunwayML 状态更新成功',
'data': {
'status': video_generation.status,
'video_url': video_generation.video_url,
'progress': progress,
'gif_url': gif_url
}
})
else:
# Revid 接口的处理(保持原来的逻辑)
success = data.get('success')
status = data.get('status')
video_url = data.get('videoUrl', None)
error_message = data.get('error', '')
if video_generation.video_url and video_generation.status == 'completed':
return JsonResponse({'code': 200, 'message': '视频已生成,状态不需更新'})
if success == 1:
if status == 'ready':
video_generation.status = 'completed'
if video_url:
video_generation.video_url = video_url
elif status == 'building':
video_generation.status = 'pending'
elif status == 'error':
video_generation.status = 'failed'
video_generation.description = error_message
video_generation.save()
return JsonResponse({
'code': 200,
'message': '状态更新成功',
'data': {'status': video_generation.status, 'video_url': video_url}
})
except requests.exceptions.RequestException as e:
print(f"发生错误: {e}")
return JsonResponse({'code': 500, 'message': str(e)})