get_channel_info
Extract YouTube channel details and 10 recent videos from any video URL. Retrieve channel metadata and upload history for content analysis and research.
Instructions
Get channel information and 10 recent videos from a YouTube video URL
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| video_url | Yes |
Implementation Reference
- The main handler function for the 'get_channel_info' tool. It extracts a video ID from a YouTube URL, fetches channel information via YouTube Data API, and retrieves the 10 most recent videos from the channel's RSS feed. Returns channel metadata including title, subscriber count, view count, and recent videos.
@mcp.tool() def get_channel_info(video_url: str) -> dict: """Get channel information and 10 recent videos from a YouTube video URL""" def extract_video_id(url): match = re.search(r"(?:v=|\/)([0-9A-Za-z_-]{11})", url) return match.group(1) if match else None def fetch_recent_videos(channel_id): rss_url = f"https://www.youtube.com/feeds/videos.xml?channel_id={channel_id}" try: response = httpx.get(rss_url) if response.status_code != 200: return [] root = ET.fromstring(response.text) ns = {'atom': 'http://www.w3.org/2005/Atom'} videos = [] for entry in root.findall('.//atom:entry', ns)[:10]: title = entry.find('./atom:title', ns).text link = entry.find('./atom:link', ns).attrib['href'] published = entry.find('./atom:published', ns).text videos.append({ 'title': title, 'link': link, 'published': published, 'updatedDate': datetime.now().strftime("%Y-%m-%d %H:%M:%S") }) return videos except Exception: return [] video_id = extract_video_id(video_url) if not video_id: raise ValueError("Invalid YouTube URL") video_api = f"{YOUTUBE_API_URL}/videos?part=snippet,statistics&id={video_id}&key={YOUTUBE_API_KEY}" video_response = httpx.get(video_api) video_response.raise_for_status() video_data = video_response.json() if not video_data.get('items'): raise ValueError("No video found") video_info = video_data['items'][0] channel_id = video_info['snippet']['channelId'] channel_api = f"{YOUTUBE_API_URL}/channels?part=snippet,statistics&id={channel_id}&key={YOUTUBE_API_KEY}" channel_response = httpx.get(channel_api) channel_response.raise_for_status() channel_data = channel_response.json()['items'][0] return { 'channelTitle': channel_data['snippet']['title'], 'channelUrl': f"https://www.youtube.com/channel/{channel_id}", 'subscriberCount': channel_data['statistics'].get('subscriberCount', '0'), 'viewCount': channel_data['statistics'].get('viewCount', '0'), 'videoCount': channel_data['statistics'].get('videoCount', '0'), 'videos': fetch_recent_videos(channel_id) }