147
index.js
147
index.js
@@ -391,12 +391,18 @@ async function searchVideos(query, limit = 5) {
|
||||
}
|
||||
}
|
||||
|
||||
// Google Trends function to get real-time trending topics
|
||||
// Function to get trending topics from Reddit
|
||||
// Function to get trending topics with country-specific sources
|
||||
async function getTrends(country = 'US') {
|
||||
try {
|
||||
console.log(`📊 Fetching Reddit trends...`);
|
||||
console.log(`📊 Fetching trends for ${country}...`);
|
||||
|
||||
// Try country-specific sources first
|
||||
const countryTrends = await getCountrySpecificTrends(country);
|
||||
if (countryTrends && Object.keys(countryTrends).length > 0) {
|
||||
return countryTrends;
|
||||
}
|
||||
|
||||
// Fallback to Reddit global trends
|
||||
const redditTrends = await getRedditTrending();
|
||||
return redditTrends;
|
||||
|
||||
@@ -406,7 +412,140 @@ async function getTrends(country = 'US') {
|
||||
}
|
||||
}
|
||||
|
||||
// Get trending from Reddit's popular posts
|
||||
// Get country-specific trending topics
|
||||
async function getCountrySpecificTrends(country) {
|
||||
try {
|
||||
const trends = {};
|
||||
|
||||
// Get Reddit trends with country-specific subreddits
|
||||
const redditTrends = await getCountryRedditTrends(country);
|
||||
if (redditTrends && Object.keys(redditTrends).length > 0) {
|
||||
Object.assign(trends, redditTrends);
|
||||
}
|
||||
|
||||
// Get news headlines for the country
|
||||
const newsTrends = await getNewsHeadlines(country);
|
||||
if (newsTrends && Object.keys(newsTrends).length > 0) {
|
||||
Object.assign(trends, newsTrends);
|
||||
}
|
||||
|
||||
return trends;
|
||||
|
||||
} catch (error) {
|
||||
console.error('Country trends error:', error.message);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
// Get Reddit trends with country-specific subreddits
|
||||
async function getCountryRedditTrends(country) {
|
||||
try {
|
||||
const countrySubreddits = {
|
||||
'US': ['news', 'UpliftingNews', 'todayilearned', 'worldnews'],
|
||||
'ES': ['es', 'spain', 'espanol', 'Madrid'],
|
||||
'GB': ['unitedkingdom', 'CasualUK', 'ukpolitics', 'AskUK'],
|
||||
'FR': ['france', 'AskFrance', 'rance', 'Lyon'],
|
||||
'DE': ['de', 'germany', 'Austria', 'Switzerland'],
|
||||
'JP': ['japan', 'newsokur', 'lowlevelaware', 'BakaNewsJP'],
|
||||
'BR': ['brasil', 'desabafos', 'circojeca', 'investimentos']
|
||||
};
|
||||
|
||||
const subreddits = countrySubreddits[country] || ['popular'];
|
||||
const groupedTrends = {};
|
||||
|
||||
// Fetch from multiple country-specific subreddits
|
||||
for (const subreddit of subreddits.slice(0, 2)) { // Limit to 2 to avoid rate limiting
|
||||
try {
|
||||
const response = await axios.get(`https://www.reddit.com/r/${subreddit}/hot.json?limit=5`, {
|
||||
headers: {
|
||||
'User-Agent': 'TrendBot/1.0'
|
||||
},
|
||||
timeout: 8000
|
||||
});
|
||||
|
||||
const posts = response.data.data.children;
|
||||
const categoryName = `r/${subreddit}`;
|
||||
|
||||
if (!groupedTrends[categoryName]) {
|
||||
groupedTrends[categoryName] = [];
|
||||
}
|
||||
|
||||
posts.forEach(post => {
|
||||
const data = post.data;
|
||||
groupedTrends[categoryName].push({
|
||||
title: data.title,
|
||||
traffic: `${data.score} upvotes`,
|
||||
url: `https://reddit.com${data.permalink}`,
|
||||
snippet: data.selftext ? data.selftext.substring(0, 100) + '...' : 'Click to view discussion'
|
||||
});
|
||||
});
|
||||
|
||||
// Small delay to avoid rate limiting
|
||||
await new Promise(resolve => setTimeout(resolve, 200));
|
||||
|
||||
} catch (subredditError) {
|
||||
console.log(`Failed to fetch from r/${subreddit}:`, subredditError.message);
|
||||
}
|
||||
}
|
||||
|
||||
return groupedTrends;
|
||||
} catch (error) {
|
||||
console.error('Country Reddit trends error:', error.message);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
// Get news headlines for specific countries
|
||||
async function getNewsHeadlines(country) {
|
||||
try {
|
||||
const countryMap = {
|
||||
'US': 'us',
|
||||
'GB': 'gb',
|
||||
'FR': 'fr',
|
||||
'DE': 'de',
|
||||
'ES': 'es',
|
||||
'JP': 'jp',
|
||||
'BR': 'br'
|
||||
};
|
||||
|
||||
const region = countryMap[country];
|
||||
if (!region) {
|
||||
return {}; // No news support for this country
|
||||
}
|
||||
|
||||
const response = await axios.get(`https://saurav.tech/NewsAPI/top-headlines/category/general/${region}.json`, {
|
||||
timeout: 10000,
|
||||
headers: {
|
||||
'User-Agent': 'TrendBot/1.0'
|
||||
}
|
||||
});
|
||||
|
||||
const articles = response.data.articles.slice(0, 8);
|
||||
const groupedTrends = {};
|
||||
|
||||
articles.forEach(article => {
|
||||
const source = article.source?.name || 'News Source';
|
||||
|
||||
if (!groupedTrends[source]) {
|
||||
groupedTrends[source] = [];
|
||||
}
|
||||
|
||||
groupedTrends[source].push({
|
||||
title: article.title,
|
||||
traffic: 'Breaking News',
|
||||
url: article.url,
|
||||
snippet: article.description || 'Latest news headline'
|
||||
});
|
||||
});
|
||||
|
||||
return groupedTrends;
|
||||
} catch (error) {
|
||||
console.error('News headlines error:', error.message);
|
||||
return {}; // Return empty object instead of throwing
|
||||
}
|
||||
}
|
||||
|
||||
// Get trending from Reddit's global popular posts (fallback)
|
||||
async function getRedditTrending() {
|
||||
try {
|
||||
const response = await axios.get('https://www.reddit.com/r/popular.json?limit=15', {
|
||||
|
||||
Referencia en una nueva incidencia
Block a user