google trends
Signed-off-by: ale <ale@manalejandro.com>
This commit is contained in:
parent
d839b464ec
commit
6bc4955f3d
74
index.js
74
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
|
||||
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
|
||||
// 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 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,18 +531,24 @@ async function getNewsHeadlines(country) {
|
||||
return {}; // No news support for this country
|
||||
}
|
||||
|
||||
// Try primary news API endpoint
|
||||
try {
|
||||
const response = await axios.get(`https://saurav.tech/NewsAPI/top-headlines/category/general/${region}.json`, {
|
||||
timeout: 10000,
|
||||
timeout: 8000,
|
||||
headers: {
|
||||
'User-Agent': 'TrendBot/1.0'
|
||||
}
|
||||
});
|
||||
|
||||
const articles = response.data.articles.slice(0, 8);
|
||||
if (response.data && response.data.articles && response.data.articles.length > 0) {
|
||||
const articles = response.data.articles.slice(0, 6);
|
||||
const groupedTrends = {};
|
||||
|
||||
articles.forEach(article => {
|
||||
const source = article.source?.name || 'News Source';
|
||||
// Skip articles with missing essential data
|
||||
if (!article.title || !article.url) return;
|
||||
|
||||
const source = article.source?.name || 'News Headlines';
|
||||
|
||||
if (!groupedTrends[source]) {
|
||||
groupedTrends[source] = [];
|
||||
@ -534,14 +558,48 @@ async function getNewsHeadlines(country) {
|
||||
title: article.title,
|
||||
traffic: 'Breaking News',
|
||||
url: article.url,
|
||||
snippet: article.description || 'Latest news headline'
|
||||
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 {};
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user