youtube-auto-bot / agents /content-strategy-agent.js
Nathan
Security: Fresh clean migration to GitHub
36d488d
Raw History Blame Contribute Delete
13.9 kB
const axios = require('axios');
const { Logger } = require('../utils/logger');
class ContentStrategyAgent {
constructor(db, credentials) {
this.db = db;
this.credentials = credentials;
this.logger = new Logger('ContentStrategy');
this.trendingTopics = [];
this.competitorData = [];
this.contentCalendar = [];
}
async initialize() {
this.logger.info('Initializing Content Strategy Agent...');
await this.loadHistoricalData();
await this.analyzeTrends();
return true;
}
async loadHistoricalData() {
try {
const history = await this.db.getContentHistory();
this.historicalPerformance = history;
} catch (error) {
this.logger.warn('No historical data found, starting fresh');
this.historicalPerformance = [];
}
}
async analyzeTrends() {
try {
// Analyze YouTube trends
const trends = await this.fetchYouTubeTrends();
// Analyze competitor channels
const competitors = await this.analyzeCompetitors();
// Combine insights
this.trendingTopics = this.mergeTrendData(trends, competitors);
this.logger.info(`Identified ${this.trendingTopics.length} trending topics`);
} catch (error) {
this.logger.error('Error analyzing trends:', error);
}
}
async fetchYouTubeTrends() {
// Use YouTube API to fetch trending videos
const youtube = this.credentials.getYouTubeClient();
try {
const response = await youtube.videos.list({
part: 'snippet,statistics',
chart: 'mostPopular',
maxResults: 50,
regionCode: process.env.YOUTUBE_REGION || 'US'
});
return response.data.items.map(video => ({
title: video.snippet.title,
tags: video.snippet.tags || [],
viewCount: parseInt(video.statistics.viewCount),
category: video.snippet.categoryId,
publishedAt: video.snippet.publishedAt
}));
} catch (error) {
this.logger.error('Failed to fetch YouTube trends:', error);
return [];
}
}
async analyzeCompetitors() {
const competitorChannels = (process.env.COMPETITOR_CHANNELS || '').split(',');
const competitorData = [];
for (const channelId of competitorChannels) {
if (!channelId) continue;
try {
const videos = await this.getChannelVideos(channelId);
const analysis = this.analyzeVideoPerformance(videos);
competitorData.push({
channelId,
topPerformingTopics: analysis.topTopics,
averageViews: analysis.avgViews,
uploadFrequency: analysis.frequency
});
} catch (error) {
this.logger.error(`Failed to analyze competitor ${channelId}:`, error);
}
}
return competitorData;
}
async getChannelVideos(channelId) {
const youtube = this.credentials.getYouTubeClient();
try {
const response = await youtube.search.list({
part: 'snippet',
channelId: channelId,
maxResults: 20,
order: 'date',
type: 'video'
});
const videoIds = response.data.items.map(item => item.id.videoId).join(',');
const videoDetails = await youtube.videos.list({
part: 'statistics,snippet',
id: videoIds
});
return videoDetails.data.items;
} catch (error) {
this.logger.error(`Failed to get videos for channel ${channelId}:`, error);
return [];
}
}
analyzeVideoPerformance(videos) {
if (!videos || videos.length === 0) {
return { topTopics: [], avgViews: 0, frequency: 0 };
}
const topics = {};
let totalViews = 0;
videos.forEach(video => {
const title = video.snippet.title.toLowerCase();
const views = parseInt(video.statistics.viewCount);
totalViews += views;
// Extract topics from title
const keywords = this.extractKeywords(title);
keywords.forEach(keyword => {
if (!topics[keyword]) topics[keyword] = { count: 0, views: 0 };
topics[keyword].count++;
topics[keyword].views += views;
});
});
const topTopics = Object.entries(topics)
.sort((a, b) => b[1].views - a[1].views)
.slice(0, 10)
.map(([topic, data]) => ({ topic, avgViews: data.views / data.count }));
return {
topTopics,
avgViews: totalViews / videos.length,
frequency: videos.length
};
}
extractKeywords(text) {
// Simple keyword extraction
const stopWords = ['the', 'is', 'at', 'which', 'on', 'and', 'a', 'an', 'as', 'are', 'was', 'were', 'been', 'be', 'have', 'has', 'had', 'do', 'does', 'did', 'will', 'would', 'could', 'should', 'may', 'might', 'must', 'can', 'could', 'i', 'you', 'he', 'she', 'it', 'we', 'they', 'what', 'which', 'who', 'when', 'where', 'why', 'how', 'all', 'each', 'every', 'both', 'few', 'more', 'most', 'other', 'some', 'such', 'no', 'nor', 'not', 'only', 'own', 'same', 'so', 'than', 'too', 'very', 'can', 'will', 'just', 'should', 'now'];
return text
.toLowerCase()
.replace(/[^\w\s]/g, '')
.split(/\s+/)
.filter(word => word.length > 3 && !stopWords.includes(word));
}
mergeTrendData(trends, competitors) {
const mergedTopics = new Map();
// Add trending topics
trends.forEach(trend => {
const keywords = this.extractKeywords(trend.title);
keywords.forEach(keyword => {
if (!mergedTopics.has(keyword)) {
mergedTopics.set(keyword, { score: 0, sources: [] });
}
const topic = mergedTopics.get(keyword);
topic.score += trend.viewCount / 1000000; // Normalize by millions
topic.sources.push('trending');
});
});
// Add competitor topics
competitors.forEach(competitor => {
if (competitor.topPerformingTopics) {
competitor.topPerformingTopics.forEach(({ topic, avgViews }) => {
if (!mergedTopics.has(topic)) {
mergedTopics.set(topic, { score: 0, sources: [] });
}
const topicData = mergedTopics.get(topic);
topicData.score += avgViews / 100000; // Normalize
topicData.sources.push('competitor');
});
}
});
// Convert to array and sort by score
return Array.from(mergedTopics.entries())
.map(([topic, data]) => ({ topic, ...data }))
.sort((a, b) => b.score - a.score)
.slice(0, 50);
}
async generateContentStrategy(requestedTopic = null) {
try {
let topic, angle, targetAudience, contentType;
if (requestedTopic) {
topic = requestedTopic;
angle = await this.generateAngle(topic);
} else {
// Select from trending topics
const selectedTopic = this.selectOptimalTopic();
topic = selectedTopic.topic;
angle = await this.generateAngle(topic);
}
// Determine target audience
targetAudience = await this.identifyTargetAudience(topic);
// Select content type
contentType = this.selectContentType(topic);
// Generate content calendar entry
const strategy = {
topic,
angle,
targetAudience,
contentType,
keywords: this.extractKeywords(topic),
estimatedViews: this.predictViews(topic),
bestPublishTime: this.calculateBestPublishTime(),
competitorAnalysis: this.getCompetitorInsights(topic),
createdAt: new Date().toISOString()
};
// Save to database
await this.db.saveContentStrategy(strategy);
this.logger.info(`Generated strategy for: ${topic}`);
return strategy;
} catch (error) {
this.logger.error('Failed to generate content strategy:', error);
throw error;
}
}
selectOptimalTopic() {
// Use scoring algorithm to select best topic
const recentTopics = this.getRecentTopics();
const scoredTopics = this.trendingTopics
.filter(topic => !recentTopics.includes(topic.topic))
.map(topic => ({
...topic,
finalScore: topic.score * this.getSeasonalMultiplier(topic.topic) * this.getAudienceMultiplier(topic.topic)
}));
return scoredTopics[0] || { topic: 'Technology Trends', score: 1 };
}
async generateAngle(topic) {
// Generate unique angle for the topic
const angles = [
`The Ultimate Guide to ${topic}`,
`${topic}: What Nobody Is Telling You`,
`How ${topic} Will Change Everything in 2025`,
`The Hidden Truth About ${topic}`,
`${topic} Explained in 5 Minutes`,
`Why ${topic} Is More Important Than You Think`,
`${topic}: Expert Secrets Revealed`,
`The Complete ${topic} Tutorial for Beginners`
];
return angles[Math.floor(Math.random() * angles.length)];
}
async identifyTargetAudience(topic) {
// Simplified audience identification
const audiences = {
tech: 'Tech enthusiasts, developers, early adopters',
business: 'Entrepreneurs, business owners, professionals',
education: 'Students, educators, lifelong learners',
entertainment: 'General audience, entertainment seekers',
lifestyle: 'Lifestyle enthusiasts, self-improvement seekers'
};
const category = this.categorize(topic);
return audiences[category] || audiences.entertainment;
}
categorize(topic) {
const categories = {
tech: ['technology', 'software', 'app', 'ai', 'code', 'programming', 'crypto', 'blockchain'],
business: ['business', 'money', 'finance', 'startup', 'entrepreneur', 'marketing'],
education: ['learn', 'tutorial', 'how to', 'guide', 'course', 'study'],
lifestyle: ['life', 'health', 'fitness', 'food', 'travel', 'fashion']
};
const topicLower = topic.toLowerCase();
for (const [category, keywords] of Object.entries(categories)) {
if (keywords.some(keyword => topicLower.includes(keyword))) {
return category;
}
}
return 'entertainment';
}
selectContentType(topic) {
const types = [
{ type: 'Tutorial', suitableFor: ['how to', 'guide', 'learn'] },
{ type: 'List', suitableFor: ['best', 'top', 'worst'] },
{ type: 'Review', suitableFor: ['review', 'vs', 'comparison'] },
{ type: 'Explainer', suitableFor: ['what is', 'why', 'explained'] },
{ type: 'News', suitableFor: ['breaking', 'latest', 'new'] },
{ type: 'Story', suitableFor: ['story', 'journey', 'experience'] }
];
const topicLower = topic.toLowerCase();
for (const contentType of types) {
if (contentType.suitableFor.some(keyword => topicLower.includes(keyword))) {
return contentType.type;
}
}
return 'Explainer';
}
predictViews(topic) {
// Simplified view prediction based on topic score
const topicData = this.trendingTopics.find(t => t.topic === topic);
const baseViews = topicData ? topicData.score * 10000 : 5000;
const variance = baseViews * 0.3;
return Math.floor(baseViews + (Math.random() * variance * 2) - variance);
}
calculateBestPublishTime() {
// Analyze best publishing times
const bestTimes = [
{ day: 'Tuesday', hour: 14 },
{ day: 'Wednesday', hour: 14 },
{ day: 'Thursday', hour: 14 },
{ day: 'Friday', hour: 15 },
{ day: 'Saturday', hour: 10 },
{ day: 'Sunday', hour: 10 }
];
const selected = bestTimes[Math.floor(Math.random() * bestTimes.length)];
const nextDate = this.getNextWeekday(selected.day);
nextDate.setHours(selected.hour, 0, 0, 0);
return nextDate.toISOString();
}
getNextWeekday(dayName) {
const days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
const targetDay = days.indexOf(dayName);
const today = new Date();
const currentDay = today.getDay();
const daysUntilTarget = (targetDay - currentDay + 7) % 7 || 7;
const nextDate = new Date(today);
nextDate.setDate(today.getDate() + daysUntilTarget);
return nextDate;
}
getCompetitorInsights(topic) {
// Get insights from competitor analysis
return this.competitorData
.filter(competitor =>
competitor.topPerformingTopics.some(t =>
t.topic.toLowerCase().includes(topic.toLowerCase())
)
)
.map(competitor => ({
channelId: competitor.channelId,
averageViews: competitor.averageViews,
relevantVideos: competitor.topPerformingTopics.filter(t =>
t.topic.toLowerCase().includes(topic.toLowerCase())
)
}));
}
getRecentTopics() {
// Get topics used in last 7 days to avoid repetition
return this.historicalPerformance
.filter(content => {
const contentDate = new Date(content.createdAt);
const weekAgo = new Date();
weekAgo.setDate(weekAgo.getDate() - 7);
return contentDate > weekAgo;
})
.map(content => content.topic);
}
getSeasonalMultiplier(topic) {
// Adjust score based on seasonal relevance
const month = new Date().getMonth();
const seasonalTopics = {
winter: ['christmas', 'holiday', 'new year', 'winter'],
spring: ['spring', 'easter', 'garden'],
summer: ['summer', 'vacation', 'beach', 'travel'],
fall: ['halloween', 'thanksgiving', 'autumn', 'back to school']
};
const season = month < 3 ? 'winter' : month < 6 ? 'spring' : month < 9 ? 'summer' : 'fall';
const topicLower = topic.toLowerCase();
if (seasonalTopics[season].some(keyword => topicLower.includes(keyword))) {
return 1.5;
}
return 1.0;
}
getAudienceMultiplier(topic) {
// Adjust score based on target audience size
const category = this.categorize(topic);
const multipliers = {
tech: 1.2,
business: 1.1,
education: 1.0,
entertainment: 1.3,
lifestyle: 1.15
};
return multipliers[category] || 1.0;
}
}
module.exports = { ContentStrategyAgent };