File size: 2,050 Bytes
f29c287
bd18039
f29c287
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bd18039
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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.";
    }
};