|
1 | 1 | # 导入API SDK Client类
|
2 | 2 | import json
|
3 | 3 |
|
| 4 | +import websockets |
| 5 | + |
4 | 6 | from tikhub.http_client.api_client import APIClient
|
5 | 7 |
|
6 | 8 |
|
@@ -417,6 +419,80 @@ async def get_all_webcast_id(self, url: list):
|
417 | 419 | data = await self.client.fetch_post_json(f"{endpoint}", data=json.dumps(url))
|
418 | 420 | return data
|
419 | 421 |
|
| 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 | + |
420 | 496 |
|
421 | 497 | if __name__ == "__main__":
|
422 | 498 | import asyncio
|
|
0 commit comments