🗳修复Douyin部分链接因无背景音乐导致解析失败

This commit is contained in:
Evil0ctal 2022-04-06 14:23:58 -07:00 committed by GitHub
parent e68d96080f
commit 7e64e396da
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2,7 +2,7 @@
# -*- encoding: utf-8 -*- # -*- encoding: utf-8 -*-
# @Author: https://github.com/Evil0ctal/ # @Author: https://github.com/Evil0ctal/
# @Time: 2021/11/06 # @Time: 2021/11/06
# @Update: 2022/04/06 # @Update: 2022/04/05
# @Function: # @Function:
# 创建一个接受提交参数的Flask应用程序。 # 创建一个接受提交参数的Flask应用程序。
# 将scraper.py返回的内容以JSON格式返回。 # 将scraper.py返回的内容以JSON格式返回。
@ -147,54 +147,57 @@ def download_music():
# 用于返回视频下载请求(返回MP3文件下载请求面对大量请求时非常吃服务器内存容易崩慎用。) # 用于返回视频下载请求(返回MP3文件下载请求面对大量请求时非常吃服务器内存容易崩慎用。)
api = Scraper() api = Scraper()
content = request.args.get("url") content = request.args.get("url")
post_content = find_url(content)[0] if content == 'No BGM found':
try: return jsonify(status='failed', reason='No BGM found', function='download_music()', value=content)
if 'douyin.com' in post_content: else:
# 获取视频信息 post_content = find_url(content)[0]
result = api.douyin(post_content) try:
bgm_url = result['video_music'] if 'douyin.com' in post_content:
if bgm_url == "None": # 获取视频信息
return jsonify(Status='Failed', Reason='This link has no music to get!') result = api.douyin(post_content)
else: 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'] bgm_title = result['video_music_title']
# 作者昵称 # 作者昵称
author_name = result['video_music_author'] author_name = result['video_music_author']
# 清理文件名 # 清理文件名
file_name = clean_filename(bgm_title, author_name) file_name = clean_filename(bgm_title, author_name)
elif 'tiktok.com' in post_content: else:
# 获取视频信息 return jsonify(Status='Failed', Reason='This link has no music to get!')
result = api.douyin(post_content) video_bgm = requests.get(bgm_url, headers).content
# BGM链接 # 将bgm字节流封装成response对象
bgm_url = result['video_music'] response = make_response(video_bgm)
# 视频标题 # 添加响应头部信息
bgm_title = result['video_music_title'] response.headers['Content-Type'] = "video/mp3"
# 作者昵称 # 他妈的,费了我老大劲才解决文件中文名的问题
author_name = result['video_music_author'] try:
# 清理文件名 filename = file_name.encode('latin-1')
file_name = clean_filename(bgm_title, author_name) except UnicodeEncodeError:
else: filenames = {
return jsonify(Status='Failed', Reason='This link has no music to get!') 'filename': unicodedata.normalize('NFKD', file_name).encode('latin-1', 'ignore'),
video_bgm = requests.get(bgm_url, headers).content 'filename*': "UTF-8''{}".format(url_quote(file_name) + '.mp3'),
# 将bgm字节流封装成response对象 }
response = make_response(video_bgm) else:
# 添加响应头部信息 filenames = {'filename': file_name + '.mp3'}
response.headers['Content-Type'] = "video/mp3" # attachment表示以附件形式下载
# 他妈的,费了我老大劲才解决文件中文名的问题 response.headers.set('Content-Disposition', 'attachment', **filenames)
try: return response
filename = file_name.encode('latin-1') except Exception as e:
except UnicodeEncodeError: return jsonify(status='failed', reason=str(e), function='download_music()', value=content)
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)
if __name__ == '__main__': if __name__ == '__main__':