mirror of
https://github.com/Evil0ctal/Douyin_TikTok_Download_API.git
synced 2025-04-22 19:48:57 +08:00
🈲设置了API开关
This commit is contained in:
parent
656369fc86
commit
029111d768
211
API/web_api.py
211
API/web_api.py
@ -16,7 +16,6 @@ from scraper import Scraper
|
||||
from werkzeug.urls import url_quote
|
||||
from flask import Flask, request, jsonify, make_response
|
||||
|
||||
|
||||
app = Flask(__name__)
|
||||
headers = {
|
||||
'user-agent': 'Mozilla/5.0 (Linux; Android 8.0; Pixel 2 Build/OPD3.170816.012) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Mobile Safari/537.36 Edg/87.0.664.66'
|
||||
@ -91,116 +90,128 @@ def webapi():
|
||||
@app.route("/video", methods=["POST", "GET"])
|
||||
def download_video():
|
||||
# 用于返回视频下载请求(返回MP4文件下载请求,面对大量请求时非常吃服务器内存,容易崩,慎用。)
|
||||
api = Scraper()
|
||||
content = request.args.get("url")
|
||||
if content == '':
|
||||
return jsonify(status='failed', reason='url value cannot be empty', function='download_music()', value=content)
|
||||
else:
|
||||
post_content = find_url(content)[0]
|
||||
try:
|
||||
if 'douyin.com' in post_content:
|
||||
# 获取视频信息
|
||||
result = api.douyin(post_content)
|
||||
# 视频链接
|
||||
video_url = result['nwm_video_url']
|
||||
# 视频标题
|
||||
video_title = result['video_title']
|
||||
# 作者昵称
|
||||
video_author = result['video_author']
|
||||
# 清理文件名
|
||||
file_name = clean_filename(video_title, video_author)
|
||||
elif 'tiktok.com' in post_content:
|
||||
# 获取视频信息
|
||||
result = api.tiktok(post_content)
|
||||
# 无水印地址
|
||||
video_url = result['nwm_video_url']
|
||||
# 视频标题
|
||||
video_title = result['video_title']
|
||||
# 作者昵称
|
||||
video_author = result['video_author']
|
||||
# 清理文件名
|
||||
file_name = clean_filename(video_title, video_author)
|
||||
else:
|
||||
return jsonify(Status='Failed', Reason='Check submitted parameters!')
|
||||
# 获取视频文件字节流
|
||||
video_mp4 = requests.get(video_url, headers).content
|
||||
# 将字节流封装成返回对象
|
||||
response = make_response(video_mp4)
|
||||
# 添加响应头部信息
|
||||
response.headers['Content-Type'] = "video/mp4"
|
||||
# 他妈的,费了我老大劲才解决文件中文名的问题
|
||||
# 将api_switch的值设定为False可关闭该API
|
||||
api_switch = True
|
||||
if api_switch:
|
||||
api = Scraper()
|
||||
content = request.args.get("url")
|
||||
if content == '':
|
||||
return jsonify(status='failed', reason='url value cannot be empty', function='download_music()',
|
||||
value=content)
|
||||
else:
|
||||
post_content = find_url(content)[0]
|
||||
try:
|
||||
filename = file_name.encode('latin-1')
|
||||
except UnicodeEncodeError:
|
||||
filenames = {
|
||||
'filename': unicodedata.normalize('NFKD', file_name).encode('latin-1', 'ignore'),
|
||||
'filename*': "UTF-8''{}".format(url_quote(file_name) + '.mp4'),
|
||||
}
|
||||
else:
|
||||
filenames = {'filename': file_name + '.mp4'}
|
||||
# attachment表示以附件形式下载
|
||||
response.headers.set('Content-Disposition', 'attachment', **filenames)
|
||||
return response
|
||||
except Exception as e:
|
||||
return jsonify(status='failed', reason=str(e), function='download_video()', value=content)
|
||||
|
||||
if 'douyin.com' in post_content:
|
||||
# 获取视频信息
|
||||
result = api.douyin(post_content)
|
||||
# 视频链接
|
||||
video_url = result['nwm_video_url']
|
||||
# 视频标题
|
||||
video_title = result['video_title']
|
||||
# 作者昵称
|
||||
video_author = result['video_author']
|
||||
# 清理文件名
|
||||
file_name = clean_filename(video_title, video_author)
|
||||
elif 'tiktok.com' in post_content:
|
||||
# 获取视频信息
|
||||
result = api.tiktok(post_content)
|
||||
# 无水印地址
|
||||
video_url = result['nwm_video_url']
|
||||
# 视频标题
|
||||
video_title = result['video_title']
|
||||
# 作者昵称
|
||||
video_author = result['video_author']
|
||||
# 清理文件名
|
||||
file_name = clean_filename(video_title, video_author)
|
||||
else:
|
||||
return jsonify(Status='Failed', Reason='Check submitted parameters!')
|
||||
# 获取视频文件字节流
|
||||
video_mp4 = requests.get(video_url, headers).content
|
||||
# 将字节流封装成返回对象
|
||||
response = make_response(video_mp4)
|
||||
# 添加响应头部信息
|
||||
response.headers['Content-Type'] = "video/mp4"
|
||||
# 他妈的,费了我老大劲才解决文件中文名的问题
|
||||
try:
|
||||
filename = file_name.encode('latin-1')
|
||||
except UnicodeEncodeError:
|
||||
filenames = {
|
||||
'filename': unicodedata.normalize('NFKD', file_name).encode('latin-1', 'ignore'),
|
||||
'filename*': "UTF-8''{}".format(url_quote(file_name) + '.mp4'),
|
||||
}
|
||||
else:
|
||||
filenames = {'filename': file_name + '.mp4'}
|
||||
# attachment表示以附件形式下载
|
||||
response.headers.set('Content-Disposition', 'attachment', **filenames)
|
||||
return response
|
||||
except Exception as e:
|
||||
return jsonify(status='failed', reason=str(e), function='download_video()', value=content)
|
||||
else:
|
||||
return jsonify(Status='Failed', Reason='This API is disabled. To enable it, set the value of "api_switch" to True.')
|
||||
|
||||
|
||||
@app.route("/music", methods=["POST", "GET"])
|
||||
def download_music():
|
||||
# 用于返回视频下载请求(返回MP3文件下载请求,面对大量请求时非常吃服务器内存,容易崩,慎用。)
|
||||
api = Scraper()
|
||||
content = request.args.get("url")
|
||||
if content == '':
|
||||
return jsonify(status='failed', reason='url value cannot be empty', function='download_music()', value=content)
|
||||
else:
|
||||
post_content = find_url(content)[0]
|
||||
try:
|
||||
if 'douyin.com' in post_content:
|
||||
# 获取视频信息
|
||||
result = api.douyin(post_content)
|
||||
bgm_url = result['video_music']
|
||||
if bgm_url == "None":
|
||||
return jsonify(Status='Failed', Reason='This link has no music to get!')
|
||||
else:
|
||||
# 将api_switch的值设定为False可关闭该API
|
||||
api_switch = True
|
||||
if api_switch:
|
||||
api = Scraper()
|
||||
content = request.args.get("url")
|
||||
if content == '':
|
||||
return jsonify(status='failed', reason='url value cannot be empty', function='download_music()',
|
||||
value=content)
|
||||
else:
|
||||
post_content = find_url(content)[0]
|
||||
try:
|
||||
if 'douyin.com' in post_content:
|
||||
# 获取视频信息
|
||||
result = api.douyin(post_content)
|
||||
bgm_url = result['video_music']
|
||||
if bgm_url == "None":
|
||||
return jsonify(Status='Failed', Reason='This link has no music to get!')
|
||||
else:
|
||||
# 视频标题
|
||||
bgm_title = result['video_music_title']
|
||||
# 作者昵称
|
||||
author_name = result['video_music_author']
|
||||
# 清理文件名
|
||||
file_name = clean_filename(bgm_title, author_name)
|
||||
elif 'tiktok.com' in post_content:
|
||||
# 获取视频信息
|
||||
result = api.douyin(post_content)
|
||||
# BGM链接
|
||||
bgm_url = result['video_music']
|
||||
# 视频标题
|
||||
bgm_title = result['video_music_title']
|
||||
# 作者昵称
|
||||
author_name = result['video_music_author']
|
||||
# 清理文件名
|
||||
file_name = clean_filename(bgm_title, author_name)
|
||||
elif 'tiktok.com' in post_content:
|
||||
# 获取视频信息
|
||||
result = api.douyin(post_content)
|
||||
# BGM链接
|
||||
bgm_url = result['video_music']
|
||||
# 视频标题
|
||||
bgm_title = result['video_music_title']
|
||||
# 作者昵称
|
||||
author_name = result['video_music_author']
|
||||
# 清理文件名
|
||||
file_name = clean_filename(bgm_title, author_name)
|
||||
else:
|
||||
return jsonify(Status='Failed', Reason='This link has no music to get!')
|
||||
video_bgm = requests.get(bgm_url, headers).content
|
||||
# 将bgm字节流封装成response对象
|
||||
response = make_response(video_bgm)
|
||||
# 添加响应头部信息
|
||||
response.headers['Content-Type'] = "video/mp3"
|
||||
# 他妈的,费了我老大劲才解决文件中文名的问题
|
||||
try:
|
||||
filename = file_name.encode('latin-1')
|
||||
except UnicodeEncodeError:
|
||||
filenames = {
|
||||
'filename': unicodedata.normalize('NFKD', file_name).encode('latin-1', 'ignore'),
|
||||
'filename*': "UTF-8''{}".format(url_quote(file_name) + '.mp3'),
|
||||
}
|
||||
else:
|
||||
filenames = {'filename': file_name + '.mp3'}
|
||||
# attachment表示以附件形式下载
|
||||
response.headers.set('Content-Disposition', 'attachment', **filenames)
|
||||
return response
|
||||
except Exception as e:
|
||||
return jsonify(status='failed', reason=str(e), function='download_music()', value=content)
|
||||
else:
|
||||
return jsonify(Status='Failed', Reason='This link has no music to get!')
|
||||
video_bgm = requests.get(bgm_url, headers).content
|
||||
# 将bgm字节流封装成response对象
|
||||
response = make_response(video_bgm)
|
||||
# 添加响应头部信息
|
||||
response.headers['Content-Type'] = "video/mp3"
|
||||
# 他妈的,费了我老大劲才解决文件中文名的问题
|
||||
try:
|
||||
filename = file_name.encode('latin-1')
|
||||
except UnicodeEncodeError:
|
||||
filenames = {
|
||||
'filename': unicodedata.normalize('NFKD', file_name).encode('latin-1', 'ignore'),
|
||||
'filename*': "UTF-8''{}".format(url_quote(file_name) + '.mp3'),
|
||||
}
|
||||
else:
|
||||
filenames = {'filename': file_name + '.mp3'}
|
||||
# attachment表示以附件形式下载
|
||||
response.headers.set('Content-Disposition', 'attachment', **filenames)
|
||||
return response
|
||||
except Exception as e:
|
||||
return jsonify(status='failed', reason=str(e), function='download_music()', value=content)
|
||||
else:
|
||||
return jsonify(Status='Failed', Reason='This API is disabled. To enable it, set the value of "api_switch" to True.')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
Loading…
x
Reference in New Issue
Block a user