get_comments
Retrieve popular comments from any Bilibili video by providing its URL. Get comment content, user info, and like counts for analysis or display.
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 countsInput Schema
| Name | Required | Description | Default |
|---|---|---|---|
| url | Yes |
Implementation Reference
- bilibili_video_info_mcp/server.py:84-108 (handler)The MCP tool handler for 'get_comments'. Extracts BV ID from URL, gets basic video info (aid), then calls bilibili_api.get_comments(aid) to fetch 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 decorator @mcp.tool with annotations (title='获取视频评论', readOnlyHint=True) that registers get_comments as an MCP tool.
@mcp.tool( annotations={ "title": "获取视频评论", "readOnlyHint": True, "openWorldHint": False } ) - Helper function get_comments(aid) that calls the Bilibili API (x/v2/reply) with type=1, oid=aid, sort=2 to fetch hot comments, returning a list of comments with user, content, and likes.
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}'} - Response parsing schema: extracts 'user' (from member.uname), 'content' (from content.message), and 'likes' (from like) from each reply in the API response.
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}'}