discordbot/test-new-trends.js
ale c21e4d891a
google trends
Signed-off-by: ale <ale@manalejandro.com>
2025-06-08 06:45:29 +02:00

243 lines
7.8 KiB
JavaScript

import axios from 'axios';
// Test the new trends implementation
async function testNewTrends() {
console.log('🧪 Testing new trends implementation...\n');
// Test Reddit trending
try {
console.log('📊 Testing Reddit trending...');
const redditTrends = await getRedditTrending();
if (redditTrends && Object.keys(redditTrends).length > 0) {
console.log('✅ Reddit trending works!');
console.log(`Found ${Object.keys(redditTrends).length} subreddits with trends`);
// Show first trend
const firstSubreddit = Object.keys(redditTrends)[0];
const firstTrend = redditTrends[firstSubreddit][0];
console.log(`Example: ${firstSubreddit} - "${firstTrend.title}"`);
} else {
console.log('❌ Reddit trending returned no data');
}
} catch (error) {
console.log('❌ Reddit trending failed:', error.message);
}
console.log('\n' + '='.repeat(50) + '\n');
// Test news headlines
try {
console.log('📰 Testing news headlines...');
const newsTrends = await getNewsHeadlines('US');
if (newsTrends && Object.keys(newsTrends).length > 0) {
console.log('✅ News headlines work!');
console.log(`Found ${Object.keys(newsTrends).length} news sources`);
// Show first headline
const firstSource = Object.keys(newsTrends)[0];
const firstHeadline = newsTrends[firstSource][0];
console.log(`Example: ${firstSource} - "${firstHeadline.title}"`);
} else {
console.log('❌ News headlines returned no data');
}
} catch (error) {
console.log('❌ News headlines failed:', error.message);
}
console.log('\n' + '='.repeat(50) + '\n');
// Test mock data
try {
console.log('🎭 Testing mock trends...');
const mockTrends = getMockTrends('US');
if (mockTrends && Object.keys(mockTrends).length > 0) {
console.log('✅ Mock trends work!');
console.log(`Found ${Object.keys(mockTrends).length} mock categories`);
// Show first mock trend
const firstCategory = Object.keys(mockTrends)[0];
const firstMockTrend = mockTrends[firstCategory][0];
console.log(`Example: ${firstCategory} - "${firstMockTrend.title}"`);
} else {
console.log('❌ Mock trends returned no data');
}
} catch (error) {
console.log('❌ Mock trends failed:', error.message);
}
console.log('\n' + '='.repeat(50) + '\n');
// Test main getTrends function
try {
console.log('🔄 Testing main getTrends function...');
const allTrends = await getTrends('US');
if (allTrends && Object.keys(allTrends).length > 0) {
console.log('✅ Main getTrends function works!');
console.log(`Found ${Object.keys(allTrends).length} trend categories`);
// Show summary
for (const [category, trends] of Object.entries(allTrends)) {
console.log(` 📂 ${category}: ${trends.length} trends`);
}
} else {
console.log('❌ Main getTrends function returned no data');
}
} catch (error) {
console.log('❌ Main getTrends function failed:', error.message);
}
}
// Reddit trending function
async function getRedditTrending() {
try {
const response = await axios.get('https://www.reddit.com/r/popular.json?limit=15', {
headers: {
'User-Agent': 'TrendBot/1.0'
},
timeout: 10000
});
const posts = response.data.data.children;
const groupedTrends = {};
posts.forEach(post => {
const data = post.data;
const subreddit = `r/${data.subreddit}`;
if (!groupedTrends[subreddit]) {
groupedTrends[subreddit] = [];
}
groupedTrends[subreddit].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'
});
});
return groupedTrends;
} catch (error) {
console.error('Reddit trending error:', error.message);
throw error;
}
}
// News headlines function
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] || 'us';
const response = await axios.get(`https://saurav.tech/NewsAPI/top-headlines/category/general/${region}.json`, {
timeout: 10000
});
const articles = response.data.articles.slice(0, 12);
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);
throw error;
}
}
// Mock trends function
function getMockTrends(country) {
const mockData = {
'TechCrunch': [
{
title: 'AI Development Trends 2024',
traffic: '50K+ searches',
url: 'https://techcrunch.com',
snippet: 'Latest developments in artificial intelligence and machine learning'
}
],
'BBC News': [
{
title: 'Global Market Updates',
traffic: '25K+ searches',
url: 'https://bbc.com/news',
snippet: 'Latest financial and economic news from around the world'
}
],
'Reddit Discussions': [
{
title: 'Technology Innovations',
traffic: '15K+ upvotes',
url: 'https://reddit.com/r/technology',
snippet: 'Community discussions about emerging technologies'
}
]
};
console.log(`📋 Using mock trends data for ${country}`);
return mockData;
}
// Main getTrends function
async function getTrends(country = 'US') {
try {
console.log(`📊 Fetching trends for ${country}...`);
// Try Reddit trending first (most reliable)
try {
const redditTrends = await getRedditTrending();
if (redditTrends && Object.keys(redditTrends).length > 0) {
return redditTrends;
}
} catch (error) {
console.log('Reddit trending failed, trying news...');
}
// Fallback to news headlines
try {
const newsTrends = await getNewsHeadlines(country);
if (newsTrends && Object.keys(newsTrends).length > 0) {
return newsTrends;
}
} catch (error) {
console.log('News headlines failed, using mock data...');
}
// Final fallback - mock trending data
return getMockTrends(country);
} catch (error) {
console.error('❌ Error fetching trends:', error.message);
throw error;
}
}
// Run the test
testNewTrends().catch(console.error);