google trends

Signed-off-by: ale <ale@manalejandro.com>
This commit is contained in:
ale 2025-06-08 07:07:49 +02:00
parent 6bc4955f3d
commit 752d5d3f6d
Signed by: ale
GPG Key ID: 244A9C4DAB1C0C81
2 changed files with 4 additions and 237 deletions

View File

@ -394,8 +394,6 @@ async function searchVideos(query, limit = 5) {
// 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) {
@ -407,7 +405,6 @@ async function getTrends(country = 'US') {
return redditTrends;
} catch (error) {
console.error('❌ Error fetching trends:', error.message);
throw error;
}
}
@ -426,7 +423,7 @@ async function getCountrySpecificTrends(country) {
sourcesSucceeded++;
}
} catch (redditError) {
console.log(`Reddit trends failed for ${country}: ${redditError.message}`);
// Reddit failure handled silently
}
// Get news headlines for the country (non-blocking)
@ -438,7 +435,6 @@ async function getCountrySpecificTrends(country) {
}
} 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
@ -450,7 +446,6 @@ async function getCountrySpecificTrends(country) {
return {};
} catch (error) {
console.error('Country trends error:', error.message);
return {}; // Return empty to trigger global fallback
}
}
@ -502,7 +497,7 @@ async function getCountryRedditTrends(country) {
await new Promise(resolve => setTimeout(resolve, 200));
} catch (subredditError) {
console.log(`Failed to fetch from r/${subreddit}:`, subredditError.message);
// Subreddit failed silently - continue to next one
}
}
@ -565,7 +560,7 @@ async function getNewsHeadlines(country) {
return groupedTrends;
}
} catch (apiError) {
console.log(`Primary news API failed for ${country}: ${apiError.message}`);
// Primary news API failed silently
}
// Fallback: Try alternative news endpoint
@ -591,7 +586,7 @@ async function getNewsHeadlines(country) {
return groupedTrends;
}
} catch (fallbackError) {
console.log(`Fallback news API also failed: ${fallbackError.message}`);
// Fallback news API also failed silently
}
// If both APIs fail, return empty object (don't log error - it's handled gracefully)

View File

@ -1,228 +0,0 @@
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);