From 17a70e888267317583b4b4725bdccfc9b256e553 Mon Sep 17 00:00:00 2001 From: ale Date: Sun, 8 Jun 2025 06:33:54 +0200 Subject: [PATCH] google trends Signed-off-by: ale --- README.md | 17 ++++++ index.js | 147 +++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 1 + test-bot.sh | 8 ++- test-trends.js | 31 +++++++++++ 5 files changed, 203 insertions(+), 1 deletion(-) create mode 100644 test-trends.js diff --git a/README.md b/README.md index 13e1736..372802a 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,9 @@ A powerful Discord bot that can send messages and search for multimedia files on - 🏓 **Ping Command** - Simple ping command to check if the bot is working - 📨 **Message Sending** - Send messages to any channel in the server - 🔍 **Multimedia Search** - Search for images and videos with embedded preview +- 📈 **Google Trends** - Show current trending topics by country with grouped articles - ⬇️ **File Download** - Download and share multimedia files directly in Discord +- 🟠 **Bitcoin Monitor** - Real-time Bitcoin transaction monitoring ## Installation @@ -62,6 +64,21 @@ Searches for multimedia content on Google with enhanced display. - Includes video descriptions when available - Supports YouTube, Vimeo, and direct video files +### 📈 Google Trends +``` +/trends country:ES +``` +Shows current Google Trends by country with articles grouped by context. +- `country`: Country code (US, ES, GB, FR, DE, JP, BR, or Global) (optional, default: US) + +**Trends Features:** +- Real-time trending topics from Google Trends +- Articles grouped by news source/category +- Top 3 articles per trending story +- Support for multiple countries +- Rich embeds with clickable article links +- Timestamp and source attribution + ### ⬇️ Download Files ``` /download url:https://example.com/image.jpg title:My Image diff --git a/index.js b/index.js index 20e5731..6f3df6b 100644 --- a/index.js +++ b/index.js @@ -11,6 +11,7 @@ import { createRequire } from 'module'; const require = createRequire(import.meta.url); const Socket = require('blockchain.info/Socket'); const blockexplorer = require('blockchain.info/blockexplorer'); +const googleTrends = require('google-trends-api'); // Load environment variables config(); @@ -141,6 +142,28 @@ client.on(Events.ClientReady, async readyClient => { min_value: 0 } ] + }, + { + name: 'trends', + description: 'Show current Google Trends by country', + options: [ + { + name: 'country', + type: 3, // STRING type + description: 'Country code (e.g., US, ES, GB, FR, DE)', + required: false, + choices: [ + { name: 'United States', value: 'US' }, + { name: 'Spain', value: 'ES' }, + { name: 'United Kingdom', value: 'GB' }, + { name: 'France', value: 'FR' }, + { name: 'Germany', value: 'DE' }, + { name: 'Japan', value: 'JP' }, + { name: 'Brazil', value: 'BR' }, + { name: 'Global', value: '' } + ] + } + ] } ]; @@ -368,6 +391,55 @@ async function searchVideos(query, limit = 5) { } } +// Google Trends function to get real-time trending topics +async function getTrends(countryCode = 'US') { + try { + const query = countryCode ? { geo: countryCode } : {}; + + const results = await googleTrends.realTimeTrends(query); + const trendsData = JSON.parse(results); + + if (!trendsData.storySummaries || !trendsData.storySummaries.trendingStories) { + throw new Error('No trending stories found'); + } + + // Group trends by context and format for Discord + const trendingStories = trendsData.storySummaries.trendingStories; + const groupedTrends = {}; + + trendingStories.forEach((story, index) => { + // Use the first article's category or create a generic category + const category = story.articles && story.articles[0] && story.articles[0].source + ? story.articles[0].source + : 'General'; + + if (!groupedTrends[category]) { + groupedTrends[category] = []; + } + + // Get top 3 articles for each story + const topArticles = story.articles ? story.articles.slice(0, 3) : []; + + groupedTrends[category].push({ + title: story.title, + rank: index + 1, + articles: topArticles.map(article => ({ + title: article.articleTitle ? article.articleTitle.replace(/\n/g, ' ').trim() : 'No title', + source: article.source || 'Unknown', + url: article.url || '', + time: article.time || 'Unknown' + })) + }); + }); + + return groupedTrends; + + } catch (error) { + console.error('Trends fetch failed:', error); + throw new Error(`Failed to fetch trends: ${error.message}`); + } +} + // Bitcoin transaction monitoring functions async function initBitcoinMonitoring() { try { @@ -707,6 +779,81 @@ client.on(Events.InteractionCreate, async interaction => { } break; + case 'trends': + await interaction.deferReply(); + const countryCode = interaction.options.getString('country') || 'US'; + const countryNames = { + 'US': 'United States', + 'ES': 'Spain', + 'GB': 'United Kingdom', + 'FR': 'France', + 'DE': 'Germany', + 'JP': 'Japan', + 'BR': 'Brazil', + '': 'Global' + }; + + try { + const trendGroups = await getTrends(countryCode); + const countryName = countryNames[countryCode] || countryCode; + + if (Object.keys(trendGroups).length === 0) { + await interaction.editReply(`❌ No trending topics found for ${countryName}.`); + return; + } + + // Create embeds for each category + const embeds = []; + let trendCount = 0; + + for (const [category, trends] of Object.entries(trendGroups)) { + if (embeds.length >= 10) break; // Discord limit + + const embed = new EmbedBuilder() + .setTitle(`📈 ${category} Trends`) + .setColor(0x4285F4) + .setFooter({ text: `Google Trends • ${countryName}` }) + .setTimestamp(); + + // Add top 3 trends from this category + const topTrends = trends.slice(0, 3); + + topTrends.forEach((trend, index) => { + trendCount++; + let fieldValue = `**#${trend.rank} Trending**\n`; + + if (trend.articles && trend.articles.length > 0) { + // Show top 2 articles for each trend + const topArticles = trend.articles.slice(0, 2); + topArticles.forEach(article => { + const shortTitle = article.title.length > 60 + ? article.title.substring(0, 60) + '...' + : article.title; + fieldValue += `• [${shortTitle}](${article.url}) - *${article.source}*\n`; + }); + } + + embed.addFields({ + name: `${trend.title}`, + value: fieldValue, + inline: false + }); + }); + + embeds.push(embed); + } + + await interaction.editReply({ + content: `🌍 **Current Trending Topics in ${countryName}**\nShowing ${trendCount} trending stories:`, + embeds: embeds + }); + + } catch (error) { + console.error('Trends error:', error); + await interaction.editReply(`❌ Error fetching trends for ${countryName}: ${error.message}\n\nThis could be due to rate limiting or the service being unavailable.`); + } + break; + case 'btc-monitor': const action = interaction.options.getString('action'); const channel = interaction.options.getChannel('channel'); diff --git a/package.json b/package.json index cc7b95a..2713f6b 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,7 @@ "cheerio": "^1.0.0", "discord.js": "^14.18.0", "dotenv": "^16.5.0", + "google-trends-api": "^4.9.2", "node-fetch": "^2.7.0" } } diff --git a/test-bot.sh b/test-bot.sh index d81119e..65630d1 100755 --- a/test-bot.sh +++ b/test-bot.sh @@ -29,7 +29,13 @@ echo " - Searches for funny videos" echo " - Will show video links with thumbnails" echo "" -echo "5. /download url:https://example.com/image.jpg title:Test Image" +echo "5. /trends country:ES" +echo " - Shows current Google Trends for Spain" +echo " - Displays trending topics grouped by news source" +echo " - Includes clickable article links" +echo "" + +echo "6. /download url:https://example.com/image.jpg title:Test Image" echo " - Downloads and shares a multimedia file" echo " - File must be under 8MB for Discord" echo "" diff --git a/test-trends.js b/test-trends.js new file mode 100644 index 0000000..368188e --- /dev/null +++ b/test-trends.js @@ -0,0 +1,31 @@ +// Test Google Trends API +import { createRequire } from 'module'; +const require = createRequire(import.meta.url); +const googleTrends = require('google-trends-api'); + +async function testTrends() { + try { + console.log('Testing Google Trends API...'); + + const query = { geo: 'ES' }; + const results = await googleTrends.realTimeTrends(query); + const trendsData = JSON.parse(results); + + console.log('✅ API working!'); + console.log('📈 Found', trendsData.storySummaries.trendingStories.length, 'trending stories'); + + // Show first trend + if (trendsData.storySummaries.trendingStories.length > 0) { + const firstTrend = trendsData.storySummaries.trendingStories[0]; + console.log('🔥 Top trend:', firstTrend.title); + if (firstTrend.articles && firstTrend.articles.length > 0) { + console.log('📰 First article:', firstTrend.articles[0].articleTitle); + } + } + + } catch (error) { + console.error('❌ Error:', error.message); + } +} + +testTrends();