This repository has been archived by the owner on May 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
76 lines (68 loc) · 2.49 KB
/
index.js
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
69
70
71
72
73
74
75
76
const CoinGecko = require('coingecko-api');
const Twitter = require('twitter');
const { default: millify } = require('millify');
require('dotenv').config()
const CoinGeckoClient = new CoinGecko();
const ids = [
"bitcoin",
"bitcoin-cash",
"bitcoin-cash-sv",
]
function getTicker(id) {
switch (id) {
case 'bitcoin': return 'BTC'
case 'bitcoin-cash': return 'BCH'
case 'bitcoin-cash-sv': return 'BSV'
}
}
async function createTweet () {
let globalDataProm = CoinGeckoClient.global();
let coinDataProm = CoinGeckoClient.simple.price({
ids,
include_market_cap: true,
})
const [globalDataResp, coinDataResp] = await Promise.all([globalDataProm, coinDataProm])
const globalData = globalDataResp.data.data
const coinData = coinDataResp.data
const totalMktCap = globalData.total_market_cap.usd
const totalMktCapFormatted = '$' + millify(totalMktCap, {
precision: 1,
})
const coinDataArr = Object.entries(coinData).sort((a, b) => {
return b[1].usd_market_cap - a[1].usd_market_cap
})
const bitcoinDominancePct = coinDataArr.map(([, data]) => data.usd_market_cap).reduce((total, current) => total += current) / totalMktCap
const bitcoinDominancePctFormatted = `${millify(bitcoinDominancePct * 100, { precision: 0 })}%`
let tweet = ''
tweet += `#Bitcoin dominates ${bitcoinDominancePctFormatted} of the ${totalMktCapFormatted} crypto market.\n\n`
coinDataArr.forEach(([id, data]) => {
tweet +=
`#${getTicker(id)}: ` + new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD', minimumFractionDigits: 0, maximumFractionDigits: 0 }).format(data.usd) + ' ' +
'\n'
})
return tweet
};
async function sendTweet(status) {
const client = new Twitter({
consumer_key: process.env.TWITTER_API_KEY,
consumer_secret: process.env.TWITTER_API_SECRET_KEY,
access_token_key: process.env.TWITTER_API_ACCESS_TOKEN,
access_token_secret: process.env.TWITTER_API_ACCESS_TOKEN_SECRET
});
client.post('statuses/update', {status}, function(error, tweet, response) {
if (!error) {
console.log('---- Sent this tweet ----')
console.log(tweet.text);
console.log('---- End ----')
} else {
console.error(error)
process.exit(1)
}
});
}
async function exec() {
const tweet = await createTweet()
await sendTweet(tweet)
// console.log(tweet)
}
exec()