get_comments
Extract popular comments from Bilibili videos to analyze viewer engagement and community feedback. Returns comment content, user details, and like counts for video analysis.
Instructions
Get popular comments from a Bilibili video
Args:
url: Bilibili video URL, e.g., https://www.bilibili.com/video/BV1x341177NN
Returns:
List of popular comments including comment content, user information, and metadata such as like counts
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| url | Yes |
Implementation Reference
- bilibili_video_info_mcp/server.py:84-108 (handler)The primary handler function for the 'get_comments' MCP tool. It extracts the BV ID from the provided URL, retrieves video basic information to get the AID, fetches comments using the helper function, handles errors, and returns a list of popular comments.async def get_comments(url: str) -> list: """Get popular comments from a Bilibili video Args: url: Bilibili video URL, e.g., https://www.bilibili.com/video/BV1x341177NN Returns: List of popular comments including comment content, user information, and metadata such as like counts """ bvid = bilibili_api.extract_bvid(url) if not bvid: return [f"错误: 无法从 URL 提取 BV 号: {url}"] aid, cid, error = bilibili_api.get_video_basic_info(bvid) if error: return [f"获取视频信息失败: {error['error']}"] comments, error = bilibili_api.get_comments(aid) if error: return [f"获取评论失败: {error['error']}"] if not comments: return ["该视频没有热门评论"] return comments
- bilibili_video_info_mcp/server.py:77-83 (registration)Registration of the 'get_comments' tool using the FastMCP @mcp.tool decorator, providing metadata annotations including title in Chinese ('Get video comments'), read-only hint, and open-world hint.@mcp.tool( annotations={ "title": "获取视频评论", "readOnlyHint": True, "openWorldHint": False } )
- Helper utility function that performs the actual HTTP request to Bilibili's comments API endpoint for a given video AID, parses the JSON response to extract popular comments (sorted by hotness), formats them with user name, content, and like count, and returns the list along with error handling.def get_comments(aid): """Fetches comments for a given aid.""" headers = _get_headers() comments_list = [] try: params_comments = {'type': 1, 'oid': aid, 'sort': 2} # sort=2 fetches hot comments response_comments = requests.get(API_GET_COMMENTS, params=params_comments, headers=headers) response_comments.raise_for_status() comments_data = response_comments.json() if comments_data.get('code') == 0 and comments_data.get('data', {}).get('replies'): for comment in comments_data['data']['replies']: if comment.get('content', {}).get('message'): comments_list.append({ 'user': comment.get('member', {}).get('uname', 'Unknown User'), 'content': comment['content']['message'], 'likes': comment.get('like', 0) }) return comments_list, None except requests.RequestException as e: return [], {'error': f'Failed to get comments: {e}'}