🚑: Fix Douyin API and Add Douyin Live API

This commit is contained in:
Evil0ctal 2022-12-22 12:00:17 -08:00
parent ea03bc9a72
commit ef7f3dd708
2 changed files with 71 additions and 10 deletions

View File

@ -2,8 +2,8 @@
# -*- 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/12/19 # @Update: 2022/12/22
# @Version: 3.1.7 # @Version: 3.1.8
# @Function: # @Function:
# 核心代码估值1块(๑•̀ㅂ•́)و✧ # 核心代码估值1块(๑•̀ㅂ•́)و✧
# 用于爬取Douyin/TikTok数据并以字典形式返回。 # 用于爬取Douyin/TikTok数据并以字典形式返回。
@ -211,6 +211,7 @@ class Scraper:
# 直播页 # 直播页
elif 'live.douyin' in video_url: elif 'live.douyin' in video_url:
# https://live.douyin.com/1000000000000000000 # https://live.douyin.com/1000000000000000000
video_url = video_url.split('?')[0] if '?' in video_url else video_url
key = video_url.replace('https://live.douyin.com/', '') key = video_url.replace('https://live.douyin.com/', '')
print('获取到的抖音直播ID为: {}'.format(key)) print('获取到的抖音直播ID为: {}'.format(key))
return key return key
@ -234,14 +235,18 @@ class Scraper:
print('正在获取抖音视频数据...') print('正在获取抖音视频数据...')
try: try:
# 构造访问链接/Construct the access link # 构造访问链接/Construct the access link
"""
旧API已失效(2022年12月21日)请大家且用且珍惜
api_url = f"https://www.iesdouyin.com/web/api/v2/aweme/iteminfo/?item_ids={video_id}" api_url = f"https://www.iesdouyin.com/web/api/v2/aweme/iteminfo/?item_ids={video_id}"
"""
api_url = f"https://www.iesdouyin.com/aweme/v1/web/aweme/detail/?aweme_id={video_id}"
# 访问API/Access API # 访问API/Access API
print("正在获取视频数据API: {}".format(api_url)) print("正在获取视频数据API: {}".format(api_url))
async with aiohttp.ClientSession() as session: async with aiohttp.ClientSession() as session:
async with session.get(api_url, headers=self.headers, proxy=self.proxies, timeout=10) as response: async with session.get(api_url, headers=self.headers, proxy=self.proxies, timeout=10) as response:
response = await response.json() response = await response.json()
# 获取视频数据/Get video data # 获取视频数据/Get video data
video_data = response['item_list'][0] video_data = response['aweme_detail']
print('获取视频数据成功!') print('获取视频数据成功!')
# print("抖音API返回数据: {}".format(video_data)) # print("抖音API返回数据: {}".format(video_data))
return video_data return video_data
@ -261,10 +266,9 @@ class Scraper:
async with aiohttp.ClientSession() as session: async with aiohttp.ClientSession() as session:
async with session.get(api_url, headers=self.douyin_cookies, proxy=self.proxies, timeout=10) as response: async with session.get(api_url, headers=self.douyin_cookies, proxy=self.proxies, timeout=10) as response:
response = await response.json() response = await response.json()
# 获取返回的json数据/Get the returned json data
data = orjson.loads(response.text)
# 获取视频数据/Get video data # 获取视频数据/Get video data
video_data = data['data'] video_data = response['data']
print(video_data)
print('获取视频数据成功!') print('获取视频数据成功!')
# print("抖音API返回数据: {}".format(video_data)) # print("抖音API返回数据: {}".format(video_data))
return video_data return video_data
@ -382,7 +386,7 @@ class Scraper:
'official_api_url': 'official_api_url':
{ {
"User-Agent": self.headers["User-Agent"], "User-Agent": self.headers["User-Agent"],
"api_url": f"https://www.iesdouyin.com/web/api/v2/aweme/iteminfo/?item_ids={video_id}" "api_url": f"https://www.iesdouyin.com/aweme/v1/web/aweme/detail/?aweme_id={video_id}"
} if url_platform == 'douyin' } if url_platform == 'douyin'
else else
{ {

View File

@ -2,8 +2,8 @@
# -*- 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/11/09 # @Update: 2022/12/22
# @Version: 3.1.0 # @Version: 3.1.1
# @Function: # @Function:
# 创建一个接受提交参数的FastAPi应用程序。 # 创建一个接受提交参数的FastAPi应用程序。
# 将scraper.py返回的内容以JSON格式返回。 # 将scraper.py返回的内容以JSON格式返回。
@ -369,6 +369,63 @@ async def get_douyin_video_data(request: Request, douyin_video_url: str = None,
return ORJSONResponse(result) return ORJSONResponse(result)
@app.get("/douyin_live_video_data/", response_model=API_Video_Response, tags=["Douyin"])
@limiter.limit(Rate_Limit)
async def get_douyin_live_video_data(request: Request, douyin_live_video_url: str = None, web_rid: str = None):
if web_rid is None or web_rid == '':
# 获取视频ID
web_rid = await api.get_douyin_video_id(douyin_live_video_url)
if web_rid is None:
result = {
"status": "failed",
"platform": "douyin",
"message": "web_rid获取失败/Failed to get web_rid",
}
return ORJSONResponse(result)
if web_rid is not None and web_rid != '':
# 开始时间
start_time = time.time()
print('获取到的web_rid:{}'.format(web_rid))
if web_rid is not None:
video_data = await api.get_douyin_live_video_data(web_rid=web_rid)
if video_data is None:
result = {
"status": "failed",
"platform": "douyin",
"endpoint": "/douyin_live_video_data/",
"message": "直播视频API数据获取失败/Failed to get live video API data",
}
return ORJSONResponse(result)
# print('获取到的video_data:{}'.format(video_data))
# 记录API调用
await api_logs(start_time=start_time,
input_data={'douyin_video_url': douyin_live_video_url, 'web_rid': web_rid},
endpoint='douyin_live_video_data')
# 结束时间
total_time = float(format(time.time() - start_time, '.4f'))
# 返回数据
result = {
"status": "success",
"platform": "douyin",
"endpoint": "/douyin_live_video_data/",
"message": "获取直播视频数据成功/Got live video data successfully",
"total_time": total_time,
"aweme_list": [video_data]
}
return ORJSONResponse(result)
else:
print('获取抖音video_id失败')
result = {
"status": "failed",
"platform": "douyin",
"endpoint": "/douyin_live_video_data/",
"message": "获取直播视频ID失败/Failed to get live video ID",
"total_time": 0,
"aweme_list": []
}
return ORJSONResponse(result)
""" ________________________⬇TikTok视频解析端点(TikTok video parsing endpoint)⬇________________________""" """ ________________________⬇TikTok视频解析端点(TikTok video parsing endpoint)⬇________________________"""