|
1 | 1 | const axios = require('axios');
|
2 | 2 | const cheerio = require('cheerio');
|
3 | 3 |
|
4 |
| -async function loadCheerio(url) { |
5 |
| - try { |
6 |
| - const response = await axios.get(url); |
7 |
| - const html = response.data; |
| 4 | +const defaultMessage = ` |
| 5 | +uso: *!coin* [--flag] name |
| 6 | +_--all -> mostra todas as informações disponiveis_ |
8 | 7 |
|
9 |
| - return cheerio.load(html); |
10 |
| - } catch (err) { |
11 |
| - console.log('error', err.response.status); |
| 8 | +a flag _all_ pode retornar dados em excesso, sua utilização repetida será considera spam |
| 9 | +`; |
12 | 10 |
|
| 11 | +async function loadCheerio(url) { |
| 12 | + try { |
| 13 | + const { data } = await axios.get(url); |
| 14 | + return cheerio.load(data); |
| 15 | + } catch { |
13 | 16 | return null;
|
14 | 17 | }
|
15 | 18 | }
|
16 | 19 |
|
17 | 20 | async function getData(url) {
|
18 | 21 | const $ = await loadCheerio(url);
|
19 |
| - if (typeof $ === 'function') { |
20 |
| - const priceStatistics = $('.sc-AxhCb.gsRRvB.container___E9axz'); |
21 |
| - const priceStatisticsTable = priceStatistics.find('table'); |
22 |
| - const priceStatisticsTableBody = priceStatisticsTable.find('tbody'); |
23 |
| - const priceStatisticsTableRow = priceStatisticsTableBody.find('tr'); |
24 |
| - |
25 |
| - const data = []; |
26 |
| - priceStatisticsTableRow.each(function () { |
27 |
| - const elem = $(this); |
28 |
| - |
29 |
| - const key = elem.find('th').text(); |
30 |
| - |
31 |
| - let value = elem.find('td'); |
32 |
| - if (value.find('span.sc-1v2ivon-0.gClTFY').text()) { |
33 |
| - value = `${value.find('span').first().text()} || ${value |
34 |
| - .find('span.sc-1v2ivon-0.gClTFY') |
35 |
| - .text()}`; |
36 |
| - } else { |
37 |
| - value = value.text(); |
38 |
| - } |
39 |
| - |
40 |
| - console.log(value); |
41 |
| - |
42 |
| - if (value !== 'No Data' || value !== 'Sem Dados') { |
43 |
| - const object = Object.fromEntries([[key, value]]); |
44 |
| - data.push(object); |
45 |
| - } |
46 |
| - }); |
47 |
| - |
48 |
| - return data; |
| 22 | + |
| 23 | + if (!(typeof $ === 'function')) { |
| 24 | + return null; |
49 | 25 | }
|
50 | 26 |
|
51 |
| - return null; |
52 |
| -} |
| 27 | + const priceStatistics = $('.sc-16r8icm-0.nds9rn-0.dAxhCK') |
| 28 | + .find('table') |
| 29 | + .find('tbody') |
| 30 | + .find('tr'); |
| 31 | + const statsArray = []; |
| 32 | + |
| 33 | + priceStatistics.each(function () { |
| 34 | + const tr = $(this); |
| 35 | + const key = tr.find('th').text(); |
| 36 | + let value = tr.find('td'); |
| 37 | + |
| 38 | + if (value.find('.sc-15yy2pl-0.hzgCfk').text()) { |
| 39 | + const valueInCash = value.find('span').first().text(); |
| 40 | + const valueInPerc = value.find('.sc-15yy2pl-0.hzgCfk').text(); |
| 41 | + value = `${valueInCash} || ${valueInPerc}`; |
| 42 | + } else { |
| 43 | + value = value.text(); |
| 44 | + } |
53 | 45 |
|
54 |
| -const defaultMessage = ` |
55 |
| -uso: *!coin* [--flag] name |
56 |
| -_--all -> mostra todas as informações disponiveis_ |
| 46 | + if (value !== 'No Data' || value !== 'Sem Dados') { |
| 47 | + const object = Object.fromEntries([[key, value]]); |
| 48 | + statsArray.push(object); |
| 49 | + } |
| 50 | + }); |
57 | 51 |
|
58 |
| -a flag _all_ pode retornar dados em excesso, sua utilização repetida será considera spam |
59 |
| -`; |
| 52 | + return statsArray; |
| 53 | +} |
60 | 54 |
|
61 |
| -module.exports = async (data) => { |
62 |
| - let BASE_URL = 'https://coinmarketcap.com/currencies/'; |
| 55 | +function getUrl(args, text) { |
| 56 | + let baseURL = 'https://coinmarketcap.com/currencies/'; |
| 57 | + const path = text.replace(/\s/g, '-').toLowerCase(); |
63 | 58 |
|
64 |
| - if (data.args.includes('brl')) { |
65 |
| - BASE_URL = 'https://coinmarketcap.com/pt-br/currencies/'; |
| 59 | + if (args.includes('brl')) { |
| 60 | + baseURL = 'https://coinmarketcap.com/pt-br/currencies/'; |
66 | 61 | }
|
67 | 62 |
|
68 |
| - if (data.text) { |
69 |
| - const text = data.text.replace(/\s/g, '-').toLowerCase(); |
70 |
| - const url = BASE_URL + text; |
71 |
| - let coinData = await getData(url); |
| 63 | + return baseURL + path; |
| 64 | +} |
72 | 65 |
|
73 |
| - if (coinData) { |
74 |
| - if (!data.args.includes('all')) coinData = coinData.slice(0, 3); |
| 66 | +module.exports = async (data) => { |
| 67 | + const { args, text } = data; |
75 | 68 |
|
76 |
| - let coinDataString = ''; |
77 |
| - coinData.forEach((elem) => { |
78 |
| - const [key, value] = Object.entries(elem)[0]; |
| 69 | + if (!text) { |
| 70 | + return defaultMessage.trim(); |
| 71 | + } |
79 | 72 |
|
80 |
| - const string = `*_${key}_*:\n - ${value}\n\n`; |
81 |
| - coinDataString += string; |
82 |
| - }); |
| 73 | + const url = getUrl(args, text); |
| 74 | + let coinStats = await getData(url); |
83 | 75 |
|
84 |
| - return coinDataString.trim(); |
85 |
| - } |
| 76 | + if (!coinStats) { |
86 | 77 | return 'moeda não encontrada';
|
87 | 78 | }
|
88 |
| - return defaultMessage.trim(); |
| 79 | + if (!args.includes('all')) { |
| 80 | + coinStats = coinStats.slice(0, 3); |
| 81 | + } |
| 82 | + |
| 83 | + let output = ''; |
| 84 | + |
| 85 | + coinStats.forEach((s) => { |
| 86 | + const [key, value] = Object.entries(s)[0]; |
| 87 | + const string = `*_${key}_*:\n - ${value}\n\n`; |
| 88 | + output += string; |
| 89 | + }); |
| 90 | + |
| 91 | + return output.trim(); |
89 | 92 | };
|
0 commit comments