feat: ks comment api upgrade to v2

This commit is contained in:
程序员阿江(Relakkes)
2026-01-09 21:09:39 +08:00
parent 2517e51ed4
commit 4de2a325a9
2 changed files with 77 additions and 50 deletions

View File

@@ -54,6 +54,7 @@ class KuaiShouClient(AbstractApiClient, ProxyRefreshMixin):
self.timeout = timeout self.timeout = timeout
self.headers = headers self.headers = headers
self._host = "https://www.kuaishou.com/graphql" self._host = "https://www.kuaishou.com/graphql"
self._rest_host = "https://www.kuaishou.com"
self.playwright_page = playwright_page self.playwright_page = playwright_page
self.cookie_dict = cookie_dict self.cookie_dict = cookie_dict
self.graphql = KuaiShouGraphQL() self.graphql = KuaiShouGraphQL()
@@ -86,6 +87,29 @@ class KuaiShouClient(AbstractApiClient, ProxyRefreshMixin):
method="POST", url=f"{self._host}{uri}", data=json_str, headers=self.headers method="POST", url=f"{self._host}{uri}", data=json_str, headers=self.headers
) )
async def request_rest_v2(self, uri: str, data: dict) -> Dict:
"""
Make REST API V2 request (for comment endpoints)
:param uri: API endpoint path
:param data: request body
:return: response data
"""
await self._refresh_proxy_if_expired()
json_str = json.dumps(data, separators=(",", ":"), ensure_ascii=False)
async with httpx.AsyncClient(proxy=self.proxy) as client:
response = await client.request(
method="POST",
url=f"{self._rest_host}{uri}",
data=json_str,
timeout=self.timeout,
headers=self.headers,
)
result: Dict = response.json()
if result.get("result") != 1:
raise DataFetchError(f"REST API V2 error: {result}")
return result
async def pong(self) -> bool: async def pong(self) -> bool:
"""get a note to check if login state is ok""" """get a note to check if login state is ok"""
utils.logger.info("[KuaiShouClient.pong] Begin pong kuaishou...") utils.logger.info("[KuaiShouClient.pong] Begin pong kuaishou...")
@@ -149,36 +173,32 @@ class KuaiShouClient(AbstractApiClient, ProxyRefreshMixin):
return await self.post("", post_data) return await self.post("", post_data)
async def get_video_comments(self, photo_id: str, pcursor: str = "") -> Dict: async def get_video_comments(self, photo_id: str, pcursor: str = "") -> Dict:
"""get video comments """Get video first-level comments using REST API V2
:param photo_id: photo id you want to fetch :param photo_id: video id you want to fetch
:param pcursor: last you get pcursor, defaults to "" :param pcursor: pagination cursor, defaults to ""
:return: :return: dict with rootCommentsV2, pcursorV2, commentCountV2
""" """
post_data = { post_data = {
"operationName": "commentListQuery", "photoId": photo_id,
"variables": {"photoId": photo_id, "pcursor": pcursor}, "pcursor": pcursor,
"query": self.graphql.get("comment_list"),
} }
return await self.post("", post_data) return await self.request_rest_v2("/rest/v/photo/comment/list", post_data)
async def get_video_sub_comments( async def get_video_sub_comments(
self, photo_id: str, rootCommentId: str, pcursor: str = "" self, photo_id: str, root_comment_id: int, pcursor: str = ""
) -> Dict: ) -> Dict:
"""get video sub comments """Get video second-level comments using REST API V2
:param photo_id: photo id you want to fetch :param photo_id: video id you want to fetch
:param pcursor: last you get pcursor, defaults to "" :param root_comment_id: parent comment id (must be int type)
:return: :param pcursor: pagination cursor, defaults to ""
:return: dict with subCommentsV2, pcursorV2
""" """
post_data = { post_data = {
"operationName": "visionSubCommentList", "photoId": photo_id,
"variables": { "pcursor": pcursor,
"photoId": photo_id, "rootCommentId": root_comment_id, # Must be int type for V2 API
"pcursor": pcursor,
"rootCommentId": rootCommentId,
},
"query": self.graphql.get("vision_sub_comment_list"),
} }
return await self.post("", post_data) return await self.request_rest_v2("/rest/v/photo/comment/sublist", post_data)
async def get_creator_profile(self, userId: str) -> Dict: async def get_creator_profile(self, userId: str) -> Dict:
post_data = { post_data = {
@@ -204,12 +224,12 @@ class KuaiShouClient(AbstractApiClient, ProxyRefreshMixin):
max_count: int = 10, max_count: int = 10,
): ):
""" """
get video all comments include sub comments Get video all comments including sub comments (V2 REST API)
:param photo_id: :param photo_id: video id
:param crawl_interval: :param crawl_interval: delay between requests (seconds)
:param callback: :param callback: callback function for processing comments
:param max_count: :param max_count: max number of comments to fetch
:return: :return: list of all comments
""" """
result = [] result = []
@@ -217,9 +237,9 @@ class KuaiShouClient(AbstractApiClient, ProxyRefreshMixin):
while pcursor != "no_more" and len(result) < max_count: while pcursor != "no_more" and len(result) < max_count:
comments_res = await self.get_video_comments(photo_id, pcursor) comments_res = await self.get_video_comments(photo_id, pcursor)
vision_commen_list = comments_res.get("visionCommentList", {}) # V2 API returns data at top level, not nested in visionCommentList
pcursor = vision_commen_list.get("pcursor", "") pcursor = comments_res.get("pcursorV2", "no_more")
comments = vision_commen_list.get("rootComments", []) comments = comments_res.get("rootCommentsV2", [])
if len(result) + len(comments) > max_count: if len(result) + len(comments) > max_count:
comments = comments[: max_count - len(result)] comments = comments[: max_count - len(result)]
if callback: # If there is a callback function, execute the callback function if callback: # If there is a callback function, execute the callback function
@@ -240,14 +260,14 @@ class KuaiShouClient(AbstractApiClient, ProxyRefreshMixin):
callback: Optional[Callable] = None, callback: Optional[Callable] = None,
) -> List[Dict]: ) -> List[Dict]:
""" """
Get all second-level comments under specified first-level comments, this method will continue to find all second-level comment information under first-level comments Get all second-level comments under specified first-level comments (V2 REST API)
Args: Args:
comments: Comment list comments: Comment list
photo_id: Video ID photo_id: Video ID
crawl_interval: Delay unit for crawling comments once (seconds) crawl_interval: Delay unit for crawling comments once (seconds)
callback: Callback after one comment crawl ends callback: Callback after one comment crawl ends
Returns: Returns:
List of sub comments
""" """
if not config.ENABLE_GET_SUB_COMMENTS: if not config.ENABLE_GET_SUB_COMMENTS:
utils.logger.info( utils.logger.info(
@@ -257,29 +277,30 @@ class KuaiShouClient(AbstractApiClient, ProxyRefreshMixin):
result = [] result = []
for comment in comments: for comment in comments:
sub_comments = comment.get("subComments") # V2 API uses hasSubComments (boolean) instead of subCommentsPcursor (string)
if sub_comments and callback: has_sub_comments = comment.get("hasSubComments", False)
await callback(photo_id, sub_comments) if not has_sub_comments:
continue
sub_comment_pcursor = comment.get("subCommentsPcursor")
if sub_comment_pcursor == "no_more": # V2 API uses comment_id (int) instead of commentId (string)
root_comment_id = comment.get("comment_id")
if not root_comment_id:
continue continue
root_comment_id = comment.get("commentId")
sub_comment_pcursor = "" sub_comment_pcursor = ""
while sub_comment_pcursor != "no_more": while sub_comment_pcursor != "no_more":
comments_res = await self.get_video_sub_comments( comments_res = await self.get_video_sub_comments(
photo_id, root_comment_id, sub_comment_pcursor photo_id, root_comment_id, sub_comment_pcursor
) )
vision_sub_comment_list = comments_res.get("visionSubCommentList", {}) # V2 API returns data at top level
sub_comment_pcursor = vision_sub_comment_list.get("pcursor", "no_more") sub_comment_pcursor = comments_res.get("pcursorV2", "no_more")
sub_comments = comments_res.get("subCommentsV2", [])
comments = vision_sub_comment_list.get("subComments", {}) if callback and sub_comments:
if callback: await callback(photo_id, sub_comments)
await callback(photo_id, comments)
await asyncio.sleep(crawl_interval) await asyncio.sleep(crawl_interval)
result.extend(comments) result.extend(sub_comments)
return result return result
async def get_creator_info(self, user_id: str) -> Dict: async def get_creator_info(self, user_id: str) -> Dict:

View File

@@ -87,16 +87,22 @@ async def batch_update_ks_video_comments(video_id: str, comments: List[Dict]):
async def update_ks_video_comment(video_id: str, comment_item: Dict): async def update_ks_video_comment(video_id: str, comment_item: Dict):
comment_id = comment_item.get("commentId") # V2 API uses snake_case field names and comment_id is int type
# Old GraphQL API used camelCase field names
# Support both formats for backward compatibility
comment_id = comment_item.get("comment_id") or comment_item.get("commentId")
save_comment_item = { save_comment_item = {
"comment_id": comment_id, "comment_id": str(comment_id) if comment_id else None, # Convert to string for storage
"create_time": comment_item.get("timestamp"), "create_time": comment_item.get("timestamp"),
"video_id": video_id, "video_id": video_id,
"content": comment_item.get("content"), "content": comment_item.get("content"),
"user_id": comment_item.get("authorId"), # V2: author_id, Old: authorId
"nickname": comment_item.get("authorName"), "user_id": comment_item.get("author_id") or comment_item.get("authorId"),
# V2: author_name, Old: authorName
"nickname": comment_item.get("author_name") or comment_item.get("authorName"),
"avatar": comment_item.get("headurl"), "avatar": comment_item.get("headurl"),
"sub_comment_count": str(comment_item.get("subCommentCount", 0)), # V2: commentCount, Old: subCommentCount
"sub_comment_count": str(comment_item.get("commentCount") or comment_item.get("subCommentCount", 0)),
"last_modify_ts": utils.get_current_timestamp(), "last_modify_ts": utils.get_current_timestamp(),
} }
utils.logger.info( utils.logger.info(