mirror of
https://github.com/NanmiCoder/MediaCrawler.git
synced 2026-02-23 00:20:48 +08:00
feat: ks comment api upgrade to v2
This commit is contained in:
@@ -54,6 +54,7 @@ class KuaiShouClient(AbstractApiClient, ProxyRefreshMixin):
|
||||
self.timeout = timeout
|
||||
self.headers = headers
|
||||
self._host = "https://www.kuaishou.com/graphql"
|
||||
self._rest_host = "https://www.kuaishou.com"
|
||||
self.playwright_page = playwright_page
|
||||
self.cookie_dict = cookie_dict
|
||||
self.graphql = KuaiShouGraphQL()
|
||||
@@ -86,6 +87,29 @@ class KuaiShouClient(AbstractApiClient, ProxyRefreshMixin):
|
||||
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:
|
||||
"""get a note to check if login state is ok"""
|
||||
utils.logger.info("[KuaiShouClient.pong] Begin pong kuaishou...")
|
||||
@@ -149,36 +173,32 @@ class KuaiShouClient(AbstractApiClient, ProxyRefreshMixin):
|
||||
return await self.post("", post_data)
|
||||
|
||||
async def get_video_comments(self, photo_id: str, pcursor: str = "") -> Dict:
|
||||
"""get video comments
|
||||
:param photo_id: photo id you want to fetch
|
||||
:param pcursor: last you get pcursor, defaults to ""
|
||||
:return:
|
||||
"""Get video first-level comments using REST API V2
|
||||
:param photo_id: video id you want to fetch
|
||||
:param pcursor: pagination cursor, defaults to ""
|
||||
:return: dict with rootCommentsV2, pcursorV2, commentCountV2
|
||||
"""
|
||||
post_data = {
|
||||
"operationName": "commentListQuery",
|
||||
"variables": {"photoId": photo_id, "pcursor": pcursor},
|
||||
"query": self.graphql.get("comment_list"),
|
||||
"photoId": photo_id,
|
||||
"pcursor": pcursor,
|
||||
}
|
||||
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(
|
||||
self, photo_id: str, rootCommentId: str, pcursor: str = ""
|
||||
self, photo_id: str, root_comment_id: int, pcursor: str = ""
|
||||
) -> Dict:
|
||||
"""get video sub comments
|
||||
:param photo_id: photo id you want to fetch
|
||||
:param pcursor: last you get pcursor, defaults to ""
|
||||
:return:
|
||||
"""Get video second-level comments using REST API V2
|
||||
:param photo_id: video id you want to fetch
|
||||
:param root_comment_id: parent comment id (must be int type)
|
||||
:param pcursor: pagination cursor, defaults to ""
|
||||
:return: dict with subCommentsV2, pcursorV2
|
||||
"""
|
||||
post_data = {
|
||||
"operationName": "visionSubCommentList",
|
||||
"variables": {
|
||||
"photoId": photo_id,
|
||||
"pcursor": pcursor,
|
||||
"rootCommentId": rootCommentId,
|
||||
},
|
||||
"query": self.graphql.get("vision_sub_comment_list"),
|
||||
"photoId": photo_id,
|
||||
"pcursor": pcursor,
|
||||
"rootCommentId": root_comment_id, # Must be int type for V2 API
|
||||
}
|
||||
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:
|
||||
post_data = {
|
||||
@@ -204,12 +224,12 @@ class KuaiShouClient(AbstractApiClient, ProxyRefreshMixin):
|
||||
max_count: int = 10,
|
||||
):
|
||||
"""
|
||||
get video all comments include sub comments
|
||||
:param photo_id:
|
||||
:param crawl_interval:
|
||||
:param callback:
|
||||
:param max_count:
|
||||
:return:
|
||||
Get video all comments including sub comments (V2 REST API)
|
||||
:param photo_id: video id
|
||||
:param crawl_interval: delay between requests (seconds)
|
||||
:param callback: callback function for processing comments
|
||||
:param max_count: max number of comments to fetch
|
||||
:return: list of all comments
|
||||
"""
|
||||
|
||||
result = []
|
||||
@@ -217,9 +237,9 @@ class KuaiShouClient(AbstractApiClient, ProxyRefreshMixin):
|
||||
|
||||
while pcursor != "no_more" and len(result) < max_count:
|
||||
comments_res = await self.get_video_comments(photo_id, pcursor)
|
||||
vision_commen_list = comments_res.get("visionCommentList", {})
|
||||
pcursor = vision_commen_list.get("pcursor", "")
|
||||
comments = vision_commen_list.get("rootComments", [])
|
||||
# V2 API returns data at top level, not nested in visionCommentList
|
||||
pcursor = comments_res.get("pcursorV2", "no_more")
|
||||
comments = comments_res.get("rootCommentsV2", [])
|
||||
if len(result) + len(comments) > max_count:
|
||||
comments = comments[: max_count - len(result)]
|
||||
if callback: # If there is a callback function, execute the callback function
|
||||
@@ -240,14 +260,14 @@ class KuaiShouClient(AbstractApiClient, ProxyRefreshMixin):
|
||||
callback: Optional[Callable] = None,
|
||||
) -> 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:
|
||||
comments: Comment list
|
||||
photo_id: Video ID
|
||||
crawl_interval: Delay unit for crawling comments once (seconds)
|
||||
callback: Callback after one comment crawl ends
|
||||
Returns:
|
||||
|
||||
List of sub comments
|
||||
"""
|
||||
if not config.ENABLE_GET_SUB_COMMENTS:
|
||||
utils.logger.info(
|
||||
@@ -257,29 +277,30 @@ class KuaiShouClient(AbstractApiClient, ProxyRefreshMixin):
|
||||
|
||||
result = []
|
||||
for comment in comments:
|
||||
sub_comments = comment.get("subComments")
|
||||
if sub_comments and callback:
|
||||
await callback(photo_id, sub_comments)
|
||||
|
||||
sub_comment_pcursor = comment.get("subCommentsPcursor")
|
||||
if sub_comment_pcursor == "no_more":
|
||||
# V2 API uses hasSubComments (boolean) instead of subCommentsPcursor (string)
|
||||
has_sub_comments = comment.get("hasSubComments", False)
|
||||
if not has_sub_comments:
|
||||
continue
|
||||
|
||||
# V2 API uses comment_id (int) instead of commentId (string)
|
||||
root_comment_id = comment.get("comment_id")
|
||||
if not root_comment_id:
|
||||
continue
|
||||
|
||||
root_comment_id = comment.get("commentId")
|
||||
sub_comment_pcursor = ""
|
||||
|
||||
while sub_comment_pcursor != "no_more":
|
||||
comments_res = await self.get_video_sub_comments(
|
||||
photo_id, root_comment_id, sub_comment_pcursor
|
||||
)
|
||||
vision_sub_comment_list = comments_res.get("visionSubCommentList", {})
|
||||
sub_comment_pcursor = vision_sub_comment_list.get("pcursor", "no_more")
|
||||
# V2 API returns data at top level
|
||||
sub_comment_pcursor = comments_res.get("pcursorV2", "no_more")
|
||||
sub_comments = comments_res.get("subCommentsV2", [])
|
||||
|
||||
comments = vision_sub_comment_list.get("subComments", {})
|
||||
if callback:
|
||||
await callback(photo_id, comments)
|
||||
if callback and sub_comments:
|
||||
await callback(photo_id, sub_comments)
|
||||
await asyncio.sleep(crawl_interval)
|
||||
result.extend(comments)
|
||||
result.extend(sub_comments)
|
||||
return result
|
||||
|
||||
async def get_creator_info(self, user_id: str) -> Dict:
|
||||
|
||||
@@ -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):
|
||||
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 = {
|
||||
"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"),
|
||||
"video_id": video_id,
|
||||
"content": comment_item.get("content"),
|
||||
"user_id": comment_item.get("authorId"),
|
||||
"nickname": comment_item.get("authorName"),
|
||||
# V2: author_id, Old: authorId
|
||||
"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"),
|
||||
"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(),
|
||||
}
|
||||
utils.logger.info(
|
||||
|
||||
Reference in New Issue
Block a user