68 lines
2.3 KiB
Python
68 lines
2.3 KiB
Python
#!/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
|
||
import requests
|
||
from utils.response import success_response, error_response
|
||
|
||
app = FastAPI(title="douyin文档", swagger_ui_parameters={
|
||
"defaultModelsExpandDepth": -1
|
||
})
|
||
|
||
ENVIRONMENT = os.getenv("ENVIRONMENT", "test")
|
||
host = 'http://127.0.0.1:9579'
|
||
if ENVIRONMENT == "pro":
|
||
host = 'http://douyin_tiktok_download_api:80'
|
||
|
||
|
||
@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}')
|
||
if not video_url:
|
||
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长度不够')
|
||
|
||
# 获取get请求参数
|
||
url = host + "/api/hybrid/video_data"
|
||
print(url)
|
||
|
||
querystring = {"url": video_url, "minimal": "true"}
|
||
response = requests.request("GET", url, params=querystring)
|
||
print(response.text)
|
||
|
||
try:
|
||
if response.json()['code'] == 200:
|
||
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)
|
||
return success_response(200, data={"video_url": video_url, "video_music": video_music, "content": content
|
||
})
|
||
except Exception as e:
|
||
print(e)
|
||
return error_response(400, data='解析字段失败')
|
||
|
||
return error_response(500, data='其他异常')
|
||
|
||
|
||
@app.get('/')
|
||
def index():
|
||
try:
|
||
return FileResponse('templates/index.html')
|
||
except FileNotFoundError:
|
||
return {'error': 'index.html 文件未找到'}, 404
|
||
|
||
|
||
if __name__ == '__main__':
|
||
uvicorn.run(app='main:app', host='0.0.0.0', port=1314, reload=True)
|