Skip to content

Commit

Permalink
Upgraded SDK V1.12.0 Add Support V5.1.9 API
Browse files Browse the repository at this point in the history
  • Loading branch information
Evil0ctal committed Jul 27, 2024
1 parent 78147b4 commit cf9ffa7
Show file tree
Hide file tree
Showing 10 changed files with 159 additions and 3 deletions.
Binary file added dist/tikhub-1.12.0-py3-none-any.whl
Binary file not shown.
Binary file added dist/tikhub-1.12.0.tar.gz
Binary file not shown.
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
httpx~=0.27.0
rich~=13.7.1
websockets~=12.0
setuptools~=68.2.0
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
description="A Python SDK for TikHub RESTful API",
long_description=long_description,
long_description_content_type="text/markdown",
url="https://github.com/TikHubIO/TikHub-API-SDK-Python",
url="https://github.com/TikHubIO/TikHub-API-Python-SDK",
packages=find_packages(),
classifiers=[
"Programming Language :: Python :: 3",
Expand All @@ -31,5 +31,6 @@
install_requires=[
"httpx>=0.27.0",
"rich~=13.7.1",
"websockets~=12.0",
],
)
12 changes: 12 additions & 0 deletions tikhub/api/v1/endpoints/douyin/app/douyin_app_v3.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,18 @@ async def fetch_douyin_video_qrcode(self, object_id: str):
data = await self.client.fetch_get_json(f"{endpoint}?object_id={object_id}")
return data

# 用户粉丝列表 (User Fans List)
async def fetch_user_fans_list(self, sec_user_id: str, max_time: str = '0', count: int = 20):
endpoint = f"/api/v1/douyin/app/v3/fetch_user_fans_list"
data = await self.client.fetch_get_json(f"{endpoint}?sec_user_id={sec_user_id}&max_time={max_time}&count={count}")
return data

# 用户关注列表 (User Following List)
async def fetch_user_following_list(self, sec_user_id: str, max_time: str = '0', count: int = 20):
endpoint = f"/api/v1/douyin/app/v3/fetch_user_following_list"
data = await self.client.fetch_get_json(f"{endpoint}?sec_user_id={sec_user_id}&max_time={max_time}&count={count}")
return data


if __name__ == "__main__":
import asyncio
Expand Down
76 changes: 76 additions & 0 deletions tikhub/api/v1/endpoints/douyin/web/douyin_web.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# 导入API SDK Client类
import json

import websockets

from tikhub.http_client.api_client import APIClient


Expand Down Expand Up @@ -417,6 +419,80 @@ async def get_all_webcast_id(self, url: list):
data = await self.client.fetch_post_json(f"{endpoint}", data=json.dumps(url))
return data

# 提取直播间弹幕 - HTTP | Extract webcast danmaku - HTTP
async def douyin_live_room(self, live_room_url: str, danmaku_type: str):
"""
提取直播间弹幕 - HTTP | Extract webcast danmaku - HTTP
:param live_room_url: 直播间链接 | Room link
:param danmaku_type: 弹幕类型 | Danmaku type
:return: 弹幕数据 | Danmaku data
"""
endpoint = "/api/v1/douyin/web/douyin_live_room"
data = await self.client.fetch_get_json(f"{endpoint}?live_room_url={live_room_url}&danmaku_type={danmaku_type}")
return data

# 提取直播间弹幕 - WebSocket | Extract webcast danmaku - WebSocket
async def douyin_live_room_ws(self, live_room_url: str, danmaku_type: str):
"""
提取直播间弹幕 - WebSocket | Extract webcast danmaku - WebSocket
:param live_room_url: 直播间链接 | Room link
:param danmaku_type: 弹幕类型 | Danmaku type
:return: 弹幕数据 | Danmaku data
"""
endpoint = await self.douyin_live_room(live_room_url, danmaku_type)
# $.data.ws_url
wss_url = endpoint["data"]["ws_url"]
# 连接 WebSocket
try:
async with websockets.connect(wss_url, ping_interval=10, ping_timeout=5) as websocket:
# 持续接收消息
while True:
response = await websocket.recv()
print(f"Received from server: {response}")

# 你可以在这里处理接收到的消息 | You can process the received message here

except Exception as e:
print(f"Failed to connect: {e}")

# 首页Feed (Home Feed)
async def fetch_home_feed(self, count: int = 10, refresh_index = 0):
"""
首页Feed (Home Feed)
:param count: 数量 | Number
:param refresh_index: 刷新索引 | Refresh index
:return: Feed数据 | Feed data
"""
endpoint = "/api/v1/douyin/web/fetch_home_feed"
data = await self.client.fetch_get_json(f"{endpoint}?count={count}&refresh_index={refresh_index}")
return data

# 用户粉丝列表 (User Fans List)
async def fetch_user_fans_list(self, sec_user_id: str, max_time: str = '0', count: int = 20):
"""
用户粉丝列表 (User Fans List)
:param sec_user_id: 用户sec_user_id | User sec_user_id
:param max_time: 最大时间 | Maximum time
:param count: 数量 | Number
:return: 粉丝列表 | Fans list
"""
endpoint = "/api/v1/douyin/web/fetch_user_fans_list"
data = await self.client.fetch_get_json(f"{endpoint}?sec_user_id={sec_user_id}&max_time={max_time}&count={count}")
return data

# 用户关注列表 (User Following List)
async def fetch_user_following_list(self, sec_user_id: str, max_time: str = '0', count: int = 20):
"""
用户关注列表 (User Following List)
:param sec_user_id: 用户sec_user_id | User sec_user_id
:param max_time: 最大时间 | Maximum time
:param count: 数量 | Number
:return: 关注列表 | Following list
"""
endpoint = "/api/v1/douyin/web/fetch_user_following_list"
data = await self.client.fetch_get_json(f"{endpoint}?sec_user_id={sec_user_id}&max_time={max_time}&count={count}")
return data


if __name__ == "__main__":
import asyncio
Expand Down
38 changes: 38 additions & 0 deletions tikhub/api/v1/endpoints/tiktok/web/tiktok_web.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# 导入API SDK Client类
import json

import websockets

from tikhub.http_client.api_client import APIClient


Expand Down Expand Up @@ -172,6 +174,42 @@ async def get_all_unique_id(self, url: list):
data = await self.client.fetch_post_json(endpoint, data={"url": url})
return data

# 根据直播间链接提取直播间ID | Extract live room ID based on live room link
async def get_live_room_id(self, live_room_url: str):
endpoint = "/api/v1/tiktok/web/get_live_room_id"
data = await self.client.fetch_get_json(f"{endpoint}?live_room_url={live_room_url}")
return data

# 提取直播间弹幕 - HTTP | Extract live room barrage - HTTP
async def tiktok_live_room(self, live_room_url: str, danmaku_type: str):
endpoint = "/api/v1/tiktok/web/tiktok_live_room"
data = await self.client.fetch_get_json(f"{endpoint}?live_room_url={live_room_url}&danmaku_type={danmaku_type}")
return data

# 提取直播间弹幕 - WebSocket | Extract live room barrage - WebSocket
async def tiktok_live_room_ws(self, live_room_url: str, danmaku_type: str):
"""
提取直播间弹幕 - WebSocket | Extract webcast danmaku - WebSocket
:param live_room_url: 直播间链接 | Room link
:param danmaku_type: 弹幕类型 | Danmaku type
:return: 弹幕数据 | Danmaku data
"""
endpoint = await self.tiktok_live_room(live_room_url, danmaku_type)
# $.data.ws_url
wss_url = endpoint["data"]["ws_url"]
# 连接 WebSocket
try:
async with websockets.connect(wss_url, ping_interval=10, ping_timeout=5) as websocket:
# 持续接收消息
while True:
response = await websocket.recv()
print(f"Received from server: {response}")

# 你可以在这里处理接收到的消息 | You can process the received message here

except Exception as e:
print(f"Failed to connect: {e}")


if __name__ == "__main__":
import asyncio
Expand Down
18 changes: 18 additions & 0 deletions tikhub/api/v1/endpoints/xiaohongshu/web/xiaohongshu_web.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,21 @@ async def get_user_notes(self, user_id: str, lastCursor: str = None):
endpoint = "/api/v1/xiaohongshu/web/get_user_notes"
data = await self.client.fetch_get_json(f"{endpoint}?user_id={user_id}&lastCursor={lastCursor}")
return data

# 获取笔记评论
async def get_note_comments(self, note_id: str, lastCursor: str = None):
endpoint = "/api/v1/xiaohongshu/web/get_note_comments"
data = await self.client.fetch_get_json(f"{endpoint}?note_id={note_id}&lastCursor={lastCursor}")
return data

# 获取笔记评论回复
async def get_note_comment_replies(self, note_id: str, comment_id: str, lastCursor: str = None):
endpoint = "/api/v1/xiaohongshu/web/get_note_comment_replies"
data = await self.client.fetch_get_json(f"{endpoint}?note_id={note_id}&comment_id={comment_id}&lastCursor={lastCursor}")
return data

# 搜索用户
async def search_users(self, keyword: str, page: int = 1):
endpoint = "/api/v1/xiaohongshu/web/search_users"
data = await self.client.fetch_get_json(f"{endpoint}?keyword={keyword}&page={page}")
return data
12 changes: 11 additions & 1 deletion tikhub/client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,15 @@ def __init__(self,
if not self.api_key:
raise RuntimeError("API Key is required to use the SDK. | 需要API Key才能使用SDK。")

# Version
self.version = str(version)

# API Client
self.client = APIClient(
base_url=self.base_url,
client_headers={
"User-Agent": f"TikHub-API-SDK-Python/{version}",
"User-Agent": f"TikHub-API-SDK-Python/{self.version}",
"X-SDK-Version": f"{self.version}",
"Authorization": f"Bearer {self.api_key}"
},
proxies=proxies,
Expand Down Expand Up @@ -124,3 +128,9 @@ def __init__(self,

# Hybrid Parsing
self.HybridParsing = HybridParsing(self.client)


if __name__ == '__main__':
# Example
api_key = "YOUR_API_KEY"
client = Client(api_key=api_key)
2 changes: 1 addition & 1 deletion tikhub/version.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# tikhub/version.py
version = "1.11.9"
version = "1.12.0"

0 comments on commit cf9ffa7

Please sign in to comment.