diff --git a/index.js b/index.js index 265fc4a..a530d55 100644 --- a/index.js +++ b/index.js @@ -416,24 +416,42 @@ async function getTrends(country = 'US') { async function getCountrySpecificTrends(country) { try { const trends = {}; + let sourcesSucceeded = 0; // Get Reddit trends with country-specific subreddits - const redditTrends = await getCountryRedditTrends(country); - if (redditTrends && Object.keys(redditTrends).length > 0) { - Object.assign(trends, redditTrends); + try { + const redditTrends = await getCountryRedditTrends(country); + if (redditTrends && Object.keys(redditTrends).length > 0) { + Object.assign(trends, redditTrends); + sourcesSucceeded++; + } + } catch (redditError) { + console.log(`Reddit trends failed for ${country}: ${redditError.message}`); } - // Get news headlines for the country - const newsTrends = await getNewsHeadlines(country); - if (newsTrends && Object.keys(newsTrends).length > 0) { - Object.assign(trends, newsTrends); + // Get news headlines for the country (non-blocking) + try { + const newsTrends = await getNewsHeadlines(country); + if (newsTrends && Object.keys(newsTrends).length > 0) { + Object.assign(trends, newsTrends); + sourcesSucceeded++; + } + } catch (newsError) { + // News failure is not critical - Reddit trends are sufficient + console.log(`News trends failed for ${country}, continuing with Reddit only`); } - return trends; + // Return trends if we got data from at least one source + if (sourcesSucceeded > 0) { + return trends; + } + + // If no country-specific sources worked, return empty to trigger global fallback + return {}; } catch (error) { console.error('Country trends error:', error.message); - throw error; + return {}; // Return empty to trigger global fallback } } @@ -513,35 +531,75 @@ async function getNewsHeadlines(country) { 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' + // Try primary news API endpoint + try { + const response = await axios.get(`https://saurav.tech/NewsAPI/top-headlines/category/general/${region}.json`, { + timeout: 8000, + headers: { + 'User-Agent': 'TrendBot/1.0' + } }); - }); - return groupedTrends; + if (response.data && response.data.articles && response.data.articles.length > 0) { + const articles = response.data.articles.slice(0, 6); + const groupedTrends = {}; + + articles.forEach(article => { + // Skip articles with missing essential data + if (!article.title || !article.url) return; + + const source = article.source?.name || 'News Headlines'; + + if (!groupedTrends[source]) { + groupedTrends[source] = []; + } + + groupedTrends[source].push({ + title: article.title, + traffic: 'Breaking News', + url: article.url, + snippet: article.description || 'Latest news update' + }); + }); + + return groupedTrends; + } + } catch (apiError) { + console.log(`Primary news API failed for ${country}: ${apiError.message}`); + } + + // Fallback: Try alternative news endpoint + try { + const altResponse = await axios.get(`https://saurav.tech/NewsAPI/everything/cnn.json`, { + timeout: 8000, + headers: { + 'User-Agent': 'TrendBot/1.0' + } + }); + + if (altResponse.data && altResponse.data.articles && altResponse.data.articles.length > 0) { + const articles = altResponse.data.articles.slice(0, 4); + const groupedTrends = { + 'Global News': articles.map(article => ({ + title: article.title || 'News Update', + traffic: 'Breaking News', + url: article.url || '#', + snippet: article.description || 'Latest global news' + })) + }; + + return groupedTrends; + } + } catch (fallbackError) { + console.log(`Fallback news API also failed: ${fallbackError.message}`); + } + + // If both APIs fail, return empty object (don't log error - it's handled gracefully) + return {}; + } catch (error) { - console.error('News headlines error:', error.message); - return {}; // Return empty object instead of throwing + // Silent handling - news is supplementary, not critical + return {}; } }