Skip to content

Commit cf9ffa7

Browse files
committed
Upgraded SDK V1.12.0 Add Support V5.1.9 API
1 parent 78147b4 commit cf9ffa7

File tree

10 files changed

+159
-3
lines changed

10 files changed

+159
-3
lines changed

dist/tikhub-1.12.0-py3-none-any.whl

67 KB
Binary file not shown.

dist/tikhub-1.12.0.tar.gz

45.4 KB
Binary file not shown.

requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
httpx~=0.27.0
22
rich~=13.7.1
3+
websockets~=12.0
34
setuptools~=68.2.0

setup.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
description="A Python SDK for TikHub RESTful API",
2121
long_description=long_description,
2222
long_description_content_type="text/markdown",
23-
url="https://github.com/TikHubIO/TikHub-API-SDK-Python",
23+
url="https://github.com/TikHubIO/TikHub-API-Python-SDK",
2424
packages=find_packages(),
2525
classifiers=[
2626
"Programming Language :: Python :: 3",
@@ -31,5 +31,6 @@
3131
install_requires=[
3232
"httpx>=0.27.0",
3333
"rich~=13.7.1",
34+
"websockets~=12.0",
3435
],
3536
)

tikhub/api/v1/endpoints/douyin/app/douyin_app_v3.py

+12
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,18 @@ async def fetch_douyin_video_qrcode(self, object_id: str):
181181
data = await self.client.fetch_get_json(f"{endpoint}?object_id={object_id}")
182182
return data
183183

184+
# 用户粉丝列表 (User Fans List)
185+
async def fetch_user_fans_list(self, sec_user_id: str, max_time: str = '0', count: int = 20):
186+
endpoint = f"/api/v1/douyin/app/v3/fetch_user_fans_list"
187+
data = await self.client.fetch_get_json(f"{endpoint}?sec_user_id={sec_user_id}&max_time={max_time}&count={count}")
188+
return data
189+
190+
# 用户关注列表 (User Following List)
191+
async def fetch_user_following_list(self, sec_user_id: str, max_time: str = '0', count: int = 20):
192+
endpoint = f"/api/v1/douyin/app/v3/fetch_user_following_list"
193+
data = await self.client.fetch_get_json(f"{endpoint}?sec_user_id={sec_user_id}&max_time={max_time}&count={count}")
194+
return data
195+
184196

185197
if __name__ == "__main__":
186198
import asyncio

tikhub/api/v1/endpoints/douyin/web/douyin_web.py

+76
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# 导入API SDK Client类
22
import json
33

4+
import websockets
5+
46
from tikhub.http_client.api_client import APIClient
57

68

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

422+
# 提取直播间弹幕 - HTTP | Extract webcast danmaku - HTTP
423+
async def douyin_live_room(self, live_room_url: str, danmaku_type: str):
424+
"""
425+
提取直播间弹幕 - HTTP | Extract webcast danmaku - HTTP
426+
:param live_room_url: 直播间链接 | Room link
427+
:param danmaku_type: 弹幕类型 | Danmaku type
428+
:return: 弹幕数据 | Danmaku data
429+
"""
430+
endpoint = "/api/v1/douyin/web/douyin_live_room"
431+
data = await self.client.fetch_get_json(f"{endpoint}?live_room_url={live_room_url}&danmaku_type={danmaku_type}")
432+
return data
433+
434+
# 提取直播间弹幕 - WebSocket | Extract webcast danmaku - WebSocket
435+
async def douyin_live_room_ws(self, live_room_url: str, danmaku_type: str):
436+
"""
437+
提取直播间弹幕 - WebSocket | Extract webcast danmaku - WebSocket
438+
:param live_room_url: 直播间链接 | Room link
439+
:param danmaku_type: 弹幕类型 | Danmaku type
440+
:return: 弹幕数据 | Danmaku data
441+
"""
442+
endpoint = await self.douyin_live_room(live_room_url, danmaku_type)
443+
# $.data.ws_url
444+
wss_url = endpoint["data"]["ws_url"]
445+
# 连接 WebSocket
446+
try:
447+
async with websockets.connect(wss_url, ping_interval=10, ping_timeout=5) as websocket:
448+
# 持续接收消息
449+
while True:
450+
response = await websocket.recv()
451+
print(f"Received from server: {response}")
452+
453+
# 你可以在这里处理接收到的消息 | You can process the received message here
454+
455+
except Exception as e:
456+
print(f"Failed to connect: {e}")
457+
458+
# 首页Feed (Home Feed)
459+
async def fetch_home_feed(self, count: int = 10, refresh_index = 0):
460+
"""
461+
首页Feed (Home Feed)
462+
:param count: 数量 | Number
463+
:param refresh_index: 刷新索引 | Refresh index
464+
:return: Feed数据 | Feed data
465+
"""
466+
endpoint = "/api/v1/douyin/web/fetch_home_feed"
467+
data = await self.client.fetch_get_json(f"{endpoint}?count={count}&refresh_index={refresh_index}")
468+
return data
469+
470+
# 用户粉丝列表 (User Fans List)
471+
async def fetch_user_fans_list(self, sec_user_id: str, max_time: str = '0', count: int = 20):
472+
"""
473+
用户粉丝列表 (User Fans List)
474+
:param sec_user_id: 用户sec_user_id | User sec_user_id
475+
:param max_time: 最大时间 | Maximum time
476+
:param count: 数量 | Number
477+
:return: 粉丝列表 | Fans list
478+
"""
479+
endpoint = "/api/v1/douyin/web/fetch_user_fans_list"
480+
data = await self.client.fetch_get_json(f"{endpoint}?sec_user_id={sec_user_id}&max_time={max_time}&count={count}")
481+
return data
482+
483+
# 用户关注列表 (User Following List)
484+
async def fetch_user_following_list(self, sec_user_id: str, max_time: str = '0', count: int = 20):
485+
"""
486+
用户关注列表 (User Following List)
487+
:param sec_user_id: 用户sec_user_id | User sec_user_id
488+
:param max_time: 最大时间 | Maximum time
489+
:param count: 数量 | Number
490+
:return: 关注列表 | Following list
491+
"""
492+
endpoint = "/api/v1/douyin/web/fetch_user_following_list"
493+
data = await self.client.fetch_get_json(f"{endpoint}?sec_user_id={sec_user_id}&max_time={max_time}&count={count}")
494+
return data
495+
420496

421497
if __name__ == "__main__":
422498
import asyncio

tikhub/api/v1/endpoints/tiktok/web/tiktok_web.py

+38
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# 导入API SDK Client类
22
import json
33

4+
import websockets
5+
46
from tikhub.http_client.api_client import APIClient
57

68

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

177+
# 根据直播间链接提取直播间ID | Extract live room ID based on live room link
178+
async def get_live_room_id(self, live_room_url: str):
179+
endpoint = "/api/v1/tiktok/web/get_live_room_id"
180+
data = await self.client.fetch_get_json(f"{endpoint}?live_room_url={live_room_url}")
181+
return data
182+
183+
# 提取直播间弹幕 - HTTP | Extract live room barrage - HTTP
184+
async def tiktok_live_room(self, live_room_url: str, danmaku_type: str):
185+
endpoint = "/api/v1/tiktok/web/tiktok_live_room"
186+
data = await self.client.fetch_get_json(f"{endpoint}?live_room_url={live_room_url}&danmaku_type={danmaku_type}")
187+
return data
188+
189+
# 提取直播间弹幕 - WebSocket | Extract live room barrage - WebSocket
190+
async def tiktok_live_room_ws(self, live_room_url: str, danmaku_type: str):
191+
"""
192+
提取直播间弹幕 - WebSocket | Extract webcast danmaku - WebSocket
193+
:param live_room_url: 直播间链接 | Room link
194+
:param danmaku_type: 弹幕类型 | Danmaku type
195+
:return: 弹幕数据 | Danmaku data
196+
"""
197+
endpoint = await self.tiktok_live_room(live_room_url, danmaku_type)
198+
# $.data.ws_url
199+
wss_url = endpoint["data"]["ws_url"]
200+
# 连接 WebSocket
201+
try:
202+
async with websockets.connect(wss_url, ping_interval=10, ping_timeout=5) as websocket:
203+
# 持续接收消息
204+
while True:
205+
response = await websocket.recv()
206+
print(f"Received from server: {response}")
207+
208+
# 你可以在这里处理接收到的消息 | You can process the received message here
209+
210+
except Exception as e:
211+
print(f"Failed to connect: {e}")
212+
175213

176214
if __name__ == "__main__":
177215
import asyncio

tikhub/api/v1/endpoints/xiaohongshu/web/xiaohongshu_web.py

+18
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,21 @@ async def get_user_notes(self, user_id: str, lastCursor: str = None):
3131
endpoint = "/api/v1/xiaohongshu/web/get_user_notes"
3232
data = await self.client.fetch_get_json(f"{endpoint}?user_id={user_id}&lastCursor={lastCursor}")
3333
return data
34+
35+
# 获取笔记评论
36+
async def get_note_comments(self, note_id: str, lastCursor: str = None):
37+
endpoint = "/api/v1/xiaohongshu/web/get_note_comments"
38+
data = await self.client.fetch_get_json(f"{endpoint}?note_id={note_id}&lastCursor={lastCursor}")
39+
return data
40+
41+
# 获取笔记评论回复
42+
async def get_note_comment_replies(self, note_id: str, comment_id: str, lastCursor: str = None):
43+
endpoint = "/api/v1/xiaohongshu/web/get_note_comment_replies"
44+
data = await self.client.fetch_get_json(f"{endpoint}?note_id={note_id}&comment_id={comment_id}&lastCursor={lastCursor}")
45+
return data
46+
47+
# 搜索用户
48+
async def search_users(self, keyword: str, page: int = 1):
49+
endpoint = "/api/v1/xiaohongshu/web/search_users"
50+
data = await self.client.fetch_get_json(f"{endpoint}?keyword={keyword}&page={page}")
51+
return data

tikhub/client/client.py

+11-1
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,15 @@ def __init__(self,
6767
if not self.api_key:
6868
raise RuntimeError("API Key is required to use the SDK. | 需要API Key才能使用SDK。")
6969

70+
# Version
71+
self.version = str(version)
72+
7073
# API Client
7174
self.client = APIClient(
7275
base_url=self.base_url,
7376
client_headers={
74-
"User-Agent": f"TikHub-API-SDK-Python/{version}",
77+
"User-Agent": f"TikHub-API-SDK-Python/{self.version}",
78+
"X-SDK-Version": f"{self.version}",
7579
"Authorization": f"Bearer {self.api_key}"
7680
},
7781
proxies=proxies,
@@ -124,3 +128,9 @@ def __init__(self,
124128

125129
# Hybrid Parsing
126130
self.HybridParsing = HybridParsing(self.client)
131+
132+
133+
if __name__ == '__main__':
134+
# Example
135+
api_key = "YOUR_API_KEY"
136+
client = Client(api_key=api_key)

tikhub/version.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
# tikhub/version.py
2-
version = "1.11.9"
2+
version = "1.12.0"

0 commit comments

Comments
 (0)