-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
99 lines (87 loc) · 2.88 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
const axios = require('axios')
const cron = require('node-cron')
const dotenv = require('dotenv').config()
const { Client } = require('@notionhq/client')
const axiosClient = axios.create({
timeout: 160000,
maxContentLength: 500 * 1000 * 1000,
httpsAgent: new https.Agent({ keepAlive: true }),
})
const notion = new Client({
auth: process.env.NOTION_TOKEN,
})
const databaseId = process.env.NOTION_DATABASE_ID
const defaultCurrency = process.env.DEFAULT_CURRENCY
const refreshDatabase = async () => {
const payload = {
path: `databases/${databaseId}/query`,
method: 'POST'
}
const { results } = await notion.request(payload)
updateCryptoConversions(results)
updateCurrencyConversions(results)
}
async function updateCryptoConversions(notionPages) {
notionPages.map(async (page) => {
const coinType = page.properties.Crypto_ID.rich_text[0]?.text.content || "EMPTY"
if (coinType != "EMPTY") {
const cryptoValue = await fetchPriceOnCoinGecko(coinType, defaultCurrency)
_updateNotionTable(page.id, cryptoValue)
}
})
}
async function updateCurrencyConversions(notionPages) {
notionPages.map(async (page) => {
const coinType = page.properties.Currency_ID.rich_text[0]?.text.content || "EMPTY"
if (coinType != "EMPTY") {
const currencyValue = await fetchCurrencyPrice(coinType, defaultCurrency)
_updateNotionTable(page.id, parseFloat(currencyValue))
}
})
}
async function _updateNotionTable(pageId, monetaryValue) {
notion.pages.update({
page_id: pageId,
properties: {
USD: {
number: monetaryValue
}
}
})
}
/**
* Get most recent price of any crypto listed at CoinGecko.
* Params: (coin) - Crypto ID from CoinGecko, all crypto IDs can be found at:
* https://api.coingecko.com/api/v3/coins/list?include_platform=false
* (defaultCurrency) - Base Currency Code to calculate price. Ex: USD, BRL, NZD
**/
async function fetchPriceOnCoinGecko(coin, defaultCurrency) {
try {
const response = await axiosClient.get(`https://api.coingecko.com/api/v3/simple/price?ids=${coin}&vs_currencies=${defaultCurrency}`);
return response.data[`${coin}`][defaultCurrency.toLowerCase()]
} catch (error) {
console.error(error);
}
}
/**
* Get most recent price of any currency listed at 'API de Moedas'.
* https://docs.awesomeapi.com.br/api-de-moedas
*
* Params: (from) - Currency Code. Ex: USD, BRL, NZD
* (to) - Currency Code. Ex: USD, BRL, NZD
**/
async function fetchCurrencyPrice(from, to) {
try {
if (from.toUpperCase() == to.toUpperCase()) {
return 1
}
const response = await axiosClient.get(`https://economia.awesomeapi.com.br/json/last/${from.toUpperCase()}-${to}`);
return response.data[`${from}${to}`]['high']
} catch (error) {
console.error(error);
}
}
// Run the refresh every minute
cron.schedule('*/30 * * * *', () => {
refreshDatabase()
})