-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
45 lines (35 loc) · 1.36 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
const { GistBox } = require('gist-box')
const fetch = require('node-fetch')
const gistId = process.env.GIST_ID
const token = process.env.GITHUB_TOKEN
const productId = process.env.PRODUCT_ID || 'BTC-USD'
const products = productId.split('-');
const timeZone = process.env.TIME_ZONE || 'America/New_York'
if (!token || !gistId) return 1;
const updateGist = async (content) => {
const box = new GistBox({ id: gistId, token: token })
await box.update({
filename: `${productId}.txt`,
description: `${productId} Stats. 📈`,
content
})
}
const run = async () => {
let stats;
stats = await fetch(`https://api.pro.coinbase.com/products/${productId}/stats`).then(r => r.json())
console.log('Got coinbase API stats ✅', stats)
let percent = (stats.last - stats.open) / (stats.open * 100) * 10000
percent = Math.round(percent * 100) / 100
let change = Math.round((stats.last - stats.open) * 100) / 100
let content = `\
1 ${products[0]} = ${stats.last} ${products[1]}
${percent > 0 ? '+' : ''}${change}(${percent}%)${percent > 0 ? '👍' : '👎'}today
⌚ ${new Date().toLocaleString('en-US', {
timeZone,
timeZoneName: 'short'
})}`
console.log(`\n${content}\n`)
await updateGist(content)
console.log('Updated gist successfully ✅')
}
run().catch((err) => console.error('Failure ❌', err))