diff --git a/scraper.py b/scraper.py index fdf2293..1ad2975 100644 --- a/scraper.py +++ b/scraper.py @@ -12,6 +12,7 @@ import re import os +import time import aiohttp import platform import asyncio @@ -280,6 +281,53 @@ class Scraper: # return None raise e + # 获取单个抖音视频数据/Get single Douyin video data + @retry(stop=stop_after_attempt(4), wait=wait_fixed(7)) + async def get_douyin_user_profile_videos(self, profile_url: str, tikhub_token: str) -> Union[dict, None]: + try: + api_url = f"https://api.tikhub.io/douyin_profile_videos/?douyin_profile_url={profile_url}&cursor=0&count=20" + _headers = {"Authorization": f"Bearer {tikhub_token}"} + async with aiohttp.ClientSession() as session: + async with session.get(api_url, headers=_headers, proxy=self.proxies, timeout=10) as response: + response = await response.json() + return response + except Exception as e: + print('获取抖音视频数据失败!原因:{}'.format(e)) + # return None + raise e + + + # 获取抖音主页点赞视频数据/Get Douyin profile like video data + @retry(stop=stop_after_attempt(4), wait=wait_fixed(7)) + async def get_douyin_profile_liked_data(self, profile_url: str, tikhub_token: str) -> Union[dict, None]: + try: + api_url = f"https://api.tikhub.io/douyin_profile_liked_videos/?douyin_profile_url={profile_url}&cursor=0&count=20" + _headers = {"Authorization": f"Bearer {tikhub_token}"} + async with aiohttp.ClientSession() as session: + async with session.get(api_url, headers=_headers, proxy=self.proxies, timeout=10) as response: + response = await response.json() + return response + except Exception as e: + print('获取抖音视频数据失败!原因:{}'.format(e)) + # return None + raise e + + # 获取抖音视频评论数据/Get Douyin video comment data + @retry(stop=stop_after_attempt(4), wait=wait_fixed(7)) + async def get_douyin_video_comments(self, video_url: str, tikhub_token: str) -> Union[dict, None]: + try: + api_url = f"https://api.tikhub.io/douyin_video_comments/?douyin_video_url={video_url}&cursor=0&count=20" + _headers = {"Authorization": f"Bearer {tikhub_token}"} + async with aiohttp.ClientSession() as session: + async with session.get(api_url, headers=_headers, proxy=self.proxies, timeout=10) as response: + response = await response.json() + return response + except Exception as e: + print('获取抖音视频数据失败!原因:{}'.format(e)) + # return None + raise e + + """__________________________________________⬇️TikTok methods(TikTok方法)⬇️______________________________________""" # 获取TikTok视频ID/Get TikTok video ID @@ -327,6 +375,35 @@ class Scraper: # return None raise e + @retry(stop=stop_after_attempt(4), wait=wait_fixed(7)) + async def get_tiktok_user_profile_videos(self, tiktok_video_url: str, tikhub_token: str) -> Union[dict, None]: + try: + api_url = f"https://api.tikhub.io/tiktok_profile_videos/?tiktok_video_url={tiktok_video_url}&cursor=0&count=20" + _headers = {"Authorization": f"Bearer {tikhub_token}"} + async with aiohttp.ClientSession() as session: + async with session.get(api_url, headers=_headers, proxy=self.proxies, timeout=10) as response: + response = await response.json() + return response + except Exception as e: + print('获取抖音视频数据失败!原因:{}'.format(e)) + # return None + raise e + + @retry(stop=stop_after_attempt(4), wait=wait_fixed(7)) + async def get_tiktok_user_profile_liked_videos(self, tiktok_video_url: str, tikhub_token: str) -> Union[dict, None]: + try: + api_url = f"https://api.tikhub.io/tiktok_profile_liked_videos/?tiktok_video_url={tiktok_video_url}&cursor=0&count=20" + _headers = {"Authorization": f"Bearer {tikhub_token}"} + async with aiohttp.ClientSession() as session: + async with session.get(api_url, headers=_headers, proxy=self.proxies, timeout=10) as response: + response = await response.json() + return response + except Exception as e: + print('获取抖音视频数据失败!原因:{}'.format(e)) + # return None + raise e + + """__________________________________________⬇️Hybrid methods(混合方法)⬇️______________________________________""" diff --git a/web_api.py b/web_api.py index 138a025..0613b55 100644 --- a/web_api.py +++ b/web_api.py @@ -3,7 +3,7 @@ # @Author: https://github.com/Evil0ctal/ # @Time: 2021/11/06 # @Update: 2023/02/19 -# @Version: 3.1.3 +# @Version: 3.1.5 # @Function: # 创建一个接受提交参数的FastAPi应用程序。 # 将scraper.py返回的内容以JSON格式返回。 @@ -212,7 +212,7 @@ async def api_logs(start_time, input_data, endpoint, error_data: dict = None): # Root端点 -@app.get("/", response_model=APIRoot, tags=["Root"]) +@app.get("/", response_class=ORJSONResponse, response_model=APIRoot, tags=["Root"]) async def root(): """ Root path info. @@ -235,7 +235,7 @@ async def root(): # 混合解析端点,自动判断输入链接返回精简后的数据 # Hybrid parsing endpoint, automatically determine the input link and return the simplified data. -@app.get("/api", tags=["API"], response_model=API_Hybrid_Response) +@app.get("/api", tags=["API"], response_class=ORJSONResponse, response_model=API_Hybrid_Response) @limiter.limit(Rate_Limit) async def hybrid_parsing(request: Request, url: str, minimal: bool = False): """ @@ -289,7 +289,7 @@ async def hybrid_parsing(request: Request, url: str, minimal: bool = False): # 获取抖音单个视频数据/Get Douyin single video data -@app.get("/douyin_video_data/", response_model=API_Video_Response, tags=["Douyin"]) +@app.get("/douyin_video_data/", response_class=ORJSONResponse, response_model=API_Video_Response, tags=["Douyin"]) @limiter.limit(Rate_Limit) async def get_douyin_video_data(request: Request, douyin_video_url: str = None, video_id: str = None): """ @@ -369,7 +369,7 @@ async def get_douyin_video_data(request: Request, douyin_video_url: str = None, return ORJSONResponse(result) -@app.get("/douyin_live_video_data/", response_model=API_Video_Response, tags=["Douyin"]) +@app.get("/douyin_live_video_data/", response_class=ORJSONResponse, 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): """ @@ -432,6 +432,45 @@ async def get_douyin_live_video_data(request: Request, douyin_live_video_url: st return ORJSONResponse(result) +@app.get("/douyin_profile_videos/", response_class=ORJSONResponse, response_model=None, tags=["Douyin"]) +async def get_douyin_user_profile_videos(tikhub_token: str, douyin_user_url: str = None): + """ + ## 用途/Usage + - 获取抖音用户主页数据,参数是用户链接|ID + - Get the data of a Douyin user profile, the parameter is the user link or ID. + ## 参数/Parameter + tikhub_token: https://api.tikhub.io/#/Authorization/login_for_access_token_user_login_post + """ + response = await api.get_douyin_user_profile_videos(tikhub_token=tikhub_token, profile_url=douyin_user_url) + return response + + +@app.get("/douyin_profile_liked_videos/", response_class=ORJSONResponse, response_model=None, tags=["Douyin"]) +async def get_douyin_user_profile_liked_videos(tikhub_token: str, douyin_user_url: str = None): + """ + ## 用途/Usage + - 获取抖音用户喜欢的视频数据,参数是用户链接|ID + - Get the data of a Douyin user profile liked videos, the parameter is the user link or ID. + ## 参数/Parameter + tikhub_token: https://api.tikhub.io/#/Authorization/login_for_access_token_user_login_post + """ + response = await api.get_douyin_profile_liked_data(tikhub_token=tikhub_token, profile_url=douyin_user_url) + return response + + +@app.get("/douyin_video_comments/", response_class=ORJSONResponse, response_model=None, tags=["Douyin"]) +async def get_douyin_video_comments(tikhub_token: str, douyin_video_url: str = None): + """ + ## 用途/Usage + - 获取抖音视频评论数据,参数是视频链接|分享口令 + - Get the data of a Douyin video comments, the parameter is the video link. + ## 参数/Parameter + tikhub_token: https://api.tikhub.io/#/Authorization/login_for_access_token_user_login_post + """ + response = await api.get_douyin_video_comments(tikhub_token=tikhub_token, video_url=douyin_video_url) + return response + + """ ________________________⬇️TikTok视频解析端点(TikTok video parsing endpoint)⬇️________________________""" @@ -510,6 +549,34 @@ async def get_tiktok_video_data(request: Request, tiktok_video_url: str = None, return ORJSONResponse(result) +# 获取TikTok用户视频数据/Get TikTok user video data +@app.get("/tiktok_profile_videos/", response_class=ORJSONResponse, response_model=None, tags=["TikTok"]) +async def get_tiktok_profile_videos(tikhub_token: str, tiktok_video_url: str = None): + """ + ## 用途/Usage + - 获取抖音用户主页数据,参数是用户链接|ID + - Get the data of a Douyin user profile, the parameter is the user link or ID. + ## 参数/Parameter + tikhub_token: https://api.tikhub.io/#/Authorization/login_for_access_token_user_login_post + """ + response = await api.get_tiktok_user_profile_videos(tikhub_token=tikhub_token, tiktok_video_url=tiktok_video_url) + return response + + +# 获取TikTok用户主页点赞视频数据/Get TikTok user profile liked video data +@app.get("/tiktok_profile_liked_videos/", response_class=ORJSONResponse, response_model=None, tags=["TikTok"]) +async def get_tiktok_profile_liked_videos(tikhub_token: str, tiktok_video_url: str = None): + """ + ## 用途/Usage + - 获取抖音用户主页点赞视频数据,参数是用户链接|ID + - Get the data of a Douyin user profile liked video, the parameter is the user link or ID. + ## 参数/Parameter + tikhub_token: https://api.tikhub.io/#/Authorization/login_for_access_token_user_login_post + """ + response = await api.get_tiktok_user_profile_liked_videos(tikhub_token=tikhub_token, tiktok_video_url=tiktok_video_url) + return response + + """ ________________________⬇️iOS快捷指令更新端点(iOS Shortcut update endpoint)⬇️________________________"""