google trends

Signed-off-by: ale <ale@manalejandro.com>
This commit is contained in:
ale 2025-06-08 07:04:44 +02:00
parent d839b464ec
commit 6bc4955f3d
Signed by: ale
GPG Key ID: 244A9C4DAB1C0C81

128
index.js
View File

@ -416,24 +416,42 @@ async function getTrends(country = 'US') {
async function getCountrySpecificTrends(country) { async function getCountrySpecificTrends(country) {
try { try {
const trends = {}; const trends = {};
let sourcesSucceeded = 0;
// Get Reddit trends with country-specific subreddits // Get Reddit trends with country-specific subreddits
const redditTrends = await getCountryRedditTrends(country); try {
if (redditTrends && Object.keys(redditTrends).length > 0) { const redditTrends = await getCountryRedditTrends(country);
Object.assign(trends, redditTrends); 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)
const newsTrends = await getNewsHeadlines(country); try {
if (newsTrends && Object.keys(newsTrends).length > 0) { const newsTrends = await getNewsHeadlines(country);
Object.assign(trends, newsTrends); 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) { } catch (error) {
console.error('Country trends error:', error.message); 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 return {}; // No news support for this country
} }
const response = await axios.get(`https://saurav.tech/NewsAPI/top-headlines/category/general/${region}.json`, { // Try primary news API endpoint
timeout: 10000, try {
headers: { const response = await axios.get(`https://saurav.tech/NewsAPI/top-headlines/category/general/${region}.json`, {
'User-Agent': 'TrendBot/1.0' timeout: 8000,
} 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; 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) { } catch (error) {
console.error('News headlines error:', error.message); // Silent handling - news is supplementary, not critical
return {}; // Return empty object instead of throwing return {};
} }
} }