analyze-channel-videos
Evaluate recent videos from a YouTube channel to uncover performance trends by analyzing metrics like views, ratings, and upload dates. Specify channel ID and sorting preferences for targeted insights.
Instructions
Analyze recent videos from a specific channel to identify performance trends
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| channelId | Yes | ||
| maxResults | No | ||
| sortBy | No |
Implementation Reference
- src/index.ts:569-660 (handler)The handler function that executes the tool's logic. It searches for recent videos in the specified channel, retrieves detailed statistics for each video, computes average performance metrics (views, likes, comments), and returns a JSON summary.async ({ channelId, maxResults = 10, sortBy = 'date' }) => { try { // First get all videos from the channel const searchResponse = await youtubeService.youtube.search.list({ part: ['snippet'], channelId, maxResults, order: sortBy, type: ['video'] }); // Extract videoIds and filter out any null or undefined values const videoIds: string[] = searchResponse.data.items ?.map(item => item.id?.videoId) .filter((id): id is string => id !== null && id !== undefined) || []; if (videoIds.length === 0) { return { content: [{ type: 'text', text: `No videos found for channel ${channelId}` }] }; } // Then get detailed stats for each video const videosResponse = await youtubeService.youtube.videos.list({ part: ['snippet', 'statistics', 'contentDetails'], id: videoIds }); interface VideoAnalysisItem { videoId: string; title: string | null | undefined; publishedAt: string | null | undefined; duration: string | null | undefined; viewCount: number; likeCount: number; commentCount: number; } const videoAnalysis: VideoAnalysisItem[] = videosResponse.data.items?.map(video => ({ videoId: video.id || '', title: video.snippet?.title, publishedAt: video.snippet?.publishedAt, duration: video.contentDetails?.duration, viewCount: Number(video.statistics?.viewCount || 0), likeCount: Number(video.statistics?.likeCount || 0), commentCount: Number(video.statistics?.commentCount || 0) })) || []; // Calculate averages if (videoAnalysis.length > 0) { const avgViews = videoAnalysis.reduce((sum: number, video: VideoAnalysisItem) => sum + video.viewCount, 0) / videoAnalysis.length; const avgLikes = videoAnalysis.reduce((sum: number, video: VideoAnalysisItem) => sum + video.likeCount, 0) / videoAnalysis.length; const avgComments = videoAnalysis.reduce((sum: number, video: VideoAnalysisItem) => sum + video.commentCount, 0) / videoAnalysis.length; const result = { channelId, videoCount: videoAnalysis.length, averages: { viewCount: avgViews, likeCount: avgLikes, commentCount: avgComments }, videos: videoAnalysis }; return { content: [{ type: 'text', text: JSON.stringify(result, null, 2) }] }; } return { content: [{ type: 'text', text: `No video data available for analysis` }] }; } catch (error) { return { content: [{ type: 'text', text: `Error analyzing channel videos: ${error}` }], isError: true }; } }
- src/index.ts:564-568 (schema)Zod schema defining the input parameters for the tool: channelId (required string), maxResults (optional number 1-50), sortBy (optional enum: 'date', 'viewCount', 'rating').{ channelId: z.string().min(1), maxResults: z.number().min(1).max(50).optional(), sortBy: z.enum(['date', 'viewCount', 'rating']).optional() },
- src/index.ts:561-563 (registration)The server.tool call that registers the 'analyze-channel-videos' tool with its name and description.server.tool( 'analyze-channel-videos', 'Analyze recent videos from a specific channel to identify performance trends',