douyin/main.py

68 lines
2.3 KiB
Python
Raw Normal View History

2025-04-18 18:40:54 +08:00
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from fastapi import FastAPI, Request
from fastapi.responses import FileResponse
import uvicorn, os
from utils.music_analysis import music_analysis, chat_analysis
2025-04-17 10:22:39 +08:00
import requests
2025-04-18 18:40:54 +08:00
from utils.response import success_response, error_response
2025-04-17 10:22:39 +08:00
app = FastAPI(title="douyin文档", swagger_ui_parameters={
"defaultModelsExpandDepth": -1
})
2025-04-18 12:05:32 +08:00
ENVIRONMENT = os.getenv("ENVIRONMENT", "test")
2025-04-18 18:40:54 +08:00
host = 'http://127.0.0.1:9579'
2025-04-18 12:05:32 +08:00
if ENVIRONMENT == "pro":
host = 'http://douyin_tiktok_download_api:80'
2025-04-17 10:22:39 +08:00
2025-04-18 18:40:54 +08:00
@app.post('/douyin', tags=["抖音"], summary="分享链接获取抖音详细信息")
async def douyin(request: Request):
form_data = await request.form()
video_url = form_data.get('video_url', '').strip()
print(f'原始视频url为{video_url}')
2025-04-18 11:30:51 +08:00
if not video_url:
2025-04-18 18:40:54 +08:00
return error_response(400, data='video_url 参数缺失')
elif len(video_url) < 21:
return error_response(400, data='video_url 参数长度不够')
video_url = chat_analysis(video_url)
print(f'截取后视频url为{video_url}')
if not video_url:
return error_response(400, data='解析不到url')
elif len(video_url) < 21:
return error_response(400, data='解析到的url长度不够')
2025-04-18 11:30:51 +08:00
2025-04-18 18:40:54 +08:00
# 获取get请求参数
2025-04-18 12:05:32 +08:00
url = host + "/api/hybrid/video_data"
2025-04-18 18:40:54 +08:00
print(url)
2025-04-18 11:30:51 +08:00
querystring = {"url": video_url, "minimal": "true"}
response = requests.request("GET", url, params=querystring)
print(response.text)
try:
if response.json()['code'] == 200:
2025-04-18 12:05:32 +08:00
video_url = response.json()['data']['video_data']['wm_video_url']
video_music = response.json()['data']['music']['play_url']['url_list'][0]
content = music_analysis(video_music)
2025-04-18 18:40:54 +08:00
return success_response(200, data={"video_url": video_url, "video_music": video_music, "content": content
})
2025-04-17 10:22:39 +08:00
except Exception as e:
print(e)
2025-04-18 18:40:54 +08:00
return error_response(400, data='解析字段失败')
2025-04-17 10:22:39 +08:00
2025-04-18 18:40:54 +08:00
return error_response(500, data='其他异常')
2025-04-17 10:22:39 +08:00
2025-04-18 18:40:54 +08:00
@app.get('/')
def index():
2025-04-17 10:22:39 +08:00
try:
2025-04-18 18:40:54 +08:00
return FileResponse('templates/index.html')
except FileNotFoundError:
return {'error': 'index.html 文件未找到'}, 404
2025-04-17 10:22:39 +08:00
if __name__ == '__main__':
uvicorn.run(app='main:app', host='0.0.0.0', port=1314, reload=True)