import axios from 'axios'; // Test the new country-specific trends implementation async function testCountryTrends() { console.log('๐Ÿงช Testing country-specific trends implementation...\n'); const countries = ['US', 'ES', 'GB', 'FR', 'DE']; for (const country of countries) { console.log(`๐ŸŒ Testing trends for ${country}...`); try { const trends = await getTrends(country); if (trends && Object.keys(trends).length > 0) { console.log(`โœ… ${country}: Found ${Object.keys(trends).length} trend categories`); // Show first trend from first category const firstCategory = Object.keys(trends)[0]; const firstTrend = trends[firstCategory][0]; console.log(` ๐Ÿ“ˆ Example: ${firstCategory} - "${firstTrend.title}"`); console.log(` ๐Ÿ“Š Traffic: ${firstTrend.traffic}`); } else { console.log(`โŒ ${country}: No trends found`); } } catch (error) { console.log(`โŒ ${country}: Error - ${error.message}`); } console.log(''); // Add spacing // Add delay to avoid rate limiting await new Promise(resolve => setTimeout(resolve, 1000)); } } // Function to get trending topics with country-specific sources async function getTrends(country = 'US') { try { 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; } catch (error) { console.error('โŒ Error fetching trends:', error.message); throw error; } } // 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', { 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; } } // Run the test testCountryTrends().catch(console.error);