Codeai / backend /utils /specialTools.js
zhlajiex
Uplink: Deploy Universal Architect Toolset v2.0 (Multi-Sector Search, News, Crypto, Wiki, Math)
bd18039
const axios = require('axios');
const cheerio = require('cheerio');
/**
* WIKIPEDIA_UPLINK
*/
exports.searchWiki = async (query) => {
try {
const res = await axios.get(`https://en.wikipedia.org/api/rest_v1/page/summary/${encodeURIComponent(query)}`);
return `WIKI_DATA: ${res.data.extract}\nSOURCE: ${res.data.content_urls.desktop.page}`;
} catch (e) {
return null;
}
};
/**
* MATH_ENGINE
*/
exports.calculate = (expression) => {
try {
const result = eval(expression.replace(/[^0-9+\-*/().]/g, ''));
return `CALCULATION_RESULT: ${result}`;
} catch (e) {
return "MATH_ERROR: Malformed expression.";
}
};
/**
* CHRONOS_SYNC
*/
exports.getTime = () => {
return `CURRENT_TEMPORAL_MARK: ${new Date().toLocaleString()}`;
};
/**
* CRYPTO_TRACKER (100% Free)
*/
exports.getCryptoPrice = async (coin) => {
try {
const res = await axios.get(`https://api.coingecko.com/api/v3/simple/price?ids=${coin.toLowerCase()}&vs_currencies=usd`);
const price = res.data[coin.toLowerCase()].usd;
return `CRYPTO_DATA: ${coin.toUpperCase()} is currently trading at $${price} USD.`;
} catch (e) {
return `CRYPTO_OFFLINE: Could not fetch price for ${coin}.`;
}
};
/**
* GLOBAL_NEWS_UPLINK
*/
exports.getNews = async (topic = 'top stories') => {
try {
const res = await axios.get(`https://news.google.com/rss/search?q=${encodeURIComponent(topic)}&hl=en-US&gl=US&ceid=US:en`);
const $ = cheerio.load(res.data, { xmlMode: true });
const news = [];
$('item').each((i, el) => {
if (i >= 5) return false;
news.push({
title: $(el).find('title').text(),
link: $(el).find('link').text(),
date: $(el).find('pubDate').text()
});
});
return news.map(n => `NEWS: ${n.title}\nLINK: ${n.link}\nDATE: ${n.date}`).join('\n\n');
} catch (e) {
return "NEWS_OFFLINE: Unable to retrieve global headlines.";
}
};