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);