diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 630872b..f8bacd2 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -21,9 +21,10 @@ jobs: - name: add and push run: | - npm install + npm install npm install axios@0.26.1 --save node updaterate.js + node updaterate-eur.js date > generated.txt git config user.name github-actions git config user.email github-actions@github.com diff --git a/btcpoll-eur.js b/btcpoll-eur.js new file mode 100644 index 0000000..f7a29cd --- /dev/null +++ b/btcpoll-eur.js @@ -0,0 +1,80 @@ +const fs = require('fs') +const axios = require('axios'); +const moment = require('moment'); + +const path = require('path'); +const dirPath = path.join(__dirname, "."); +const fileToWrite = dirPath + "/public/eur_historical" +const fileToRead = dirPath + "/public/eur_historical" + + +// get btc/usd and btc/eur daily rate +async function BTCDaily() { + let url = "https://api.coingecko.com/api/v3/coins/bitcoin/history?localization=false&date=" + + const yesterday = moment().subtract(1, 'days') // YYYY-MM-DD + const reverse = yesterday.format('DD-MM-YYYY') + + // format is YYYY-MM-DD + const dbdate = yesterday.format('YYYY-MM-DD') + let full_url = url + reverse + let row = {} + //console.log("db date: ", dbdate) + //console.log("new date format: ", reverse, "\n") + + await axios.get(full_url).then( + async function(response) { + // console.log("full url: ", full_url) + const data = await response.data; + const btcusd = data['market_data']['current_price']['usd'] + const btceur = data['market_data']['current_price']['eur'] + const satsrate = 100000000 + const sateur = parseInt(satsrate / btceur) + const usdsat = parseInt(satsrate / btcusd) + + row = { + btcusd_rate: parseInt(btcusd), + date: dbdate, + usdsat_rate: usdsat, + sateur_rate: sateur, + btceur_rate: parseFloat(btceur).toFixed(2), + } + console.log("row data: ", row) + }) + return row +} + +// update file in the target github repo +async function updateFile() { + const row = await BTCDaily() + //const row = [] + + if (Object.keys(row).length > 0) { + //console.log("dirpath", dirPath) + //console.log("dirname", __dirname) + const original = await fs.readFileSync(fileToRead) + let orig = JSON.parse(original) + //console.log(orig[0]) + orig.push(row) + //console.log(orig[orig.length - 1]) + const new_content = JSON.stringify(orig) + await fs.writeFileSync(fileToWrite, new_content); + } +} + +module.exports = { + + // start here + main: async function () { + console.log("starting btcpoll script for sateur....") + let result = updateFile(); + console.log(result) + return true + } +} + + +//const res = main() +//console.log('Result from main() : ', res) + + diff --git a/calculate-eur.js b/calculate-eur.js new file mode 100644 index 0000000..fbe8189 --- /dev/null +++ b/calculate-eur.js @@ -0,0 +1,58 @@ +const axios = require('axios') +const fs = require('fs'); + +module.exports = { + bfx: async function() { + const eurrate = 0.92 // approximate EUR/USD rate + const btcDataURL = "https://api-pub.bitfinex.com/v2/ticker/tBTCUSD" + const response = await axios.get(btcDataURL) + const data = response.data + //console.log(data[6]) + + const satDenominator = 100000000 + // see docs : https://docs.bitfinex.com/reference#rest-public-ticker + btcLastPrice = data[6] + + const sateur = Math.round((1 / btcLastPrice) * satDenominator * eurrate) + //console.log("bitfinex last price: ", btcLastPrice, "current satEUR: ", sateur) + return sateur + }, + + get10yr: async function() { + // console.log("get10yr") + try { + // const content = await fs.readFile('./public/eur_historical') + const content = fs.readFileSync('./public/eur_historical', { encoding: 'utf8' }) + + const historical = JSON.parse(content) + hist_entries = [] + let datelist = [] + + // get all the years you need from 1 - 10 + for (let i = 1; i < 11; i++) { + const y = new Date(new Date().setFullYear(new Date().getFullYear() - i)).toISOString().slice(0, 10) + datelist.push(y) + } + for (let j = 0; j < historical.length; j++) { + const hdate = historical[j]['date'] + if (datelist.includes(hdate)) { + hist_entries.push(historical[j]) + } + } + // console.log(hist_entries) + let final_list = [] + let today_sats = await this.bfx() + for (var v = 0; v < hist_entries.length; v++) { + const date = new Date(hist_entries[v]['date']) + year = date.getFullYear(); + rawsat = hist_entries[v]['sateur_rate'] + percentage = (-100 * ((rawsat - today_sats) / rawsat)).toFixed(3) + final_list.push({ "year": date.toLocaleDateString(), "sats": rawsat.toLocaleString("en-US"), "percent": percentage }); + } + return final_list.reverse() + } catch (error) { + console.error("Error trying to read file ", error) + } + } + +} diff --git a/index.js b/index.js index fc5df79..392242a 100644 --- a/index.js +++ b/index.js @@ -11,9 +11,18 @@ const handlebars = require('express-handlebars'); const port = 3000; const calculate = require('./calculate') +const calculateEur = require('./calculate-eur') const zhcnjson = require('./locales/zh-cn.json'); const zhhkjson = require('./locales/zh-hk.json'); const enjson = require('./locales/en.json'); +const eneurjson = require('./locales/en-eur.json'); +const dejson = require('./locales/de.json'); +const frjson = require('./locales/fr.json'); +const esjson = require('./locales/es.json'); +const itjson = require('./locales/it.json'); +const nljson = require('./locales/nl.json'); +const ptjson = require('./locales/pt.json'); +const pljson = require('./locales/pl.json'); app.set('view engine', 'hbs'); app.set('views', __dirname + '/views') @@ -62,6 +71,71 @@ app.get('/zh-hk', function(req, res) { }) }); +// EUR routes +app.get('/en-eur', function(req, res) { + calculateEur.get10yr().then(pydata => { + const yeardata = { 'yeardata': pydata } + let eneurdata = Object.assign(eneurjson, yeardata) + res.render('sats', eneurdata) + }) +}); + +app.get('/de', function(req, res) { + calculateEur.get10yr().then(pydata => { + const yeardata = { 'yeardata': pydata } + let dedata = Object.assign(dejson, yeardata) + res.render('sats', dedata) + }) +}); + +app.get('/fr', function(req, res) { + calculateEur.get10yr().then(pydata => { + const yeardata = { 'yeardata': pydata } + let frdata = Object.assign(frjson, yeardata) + res.render('sats', frdata) + }) +}); + +app.get('/es', function(req, res) { + calculateEur.get10yr().then(pydata => { + const yeardata = { 'yeardata': pydata } + let esdata = Object.assign(esjson, yeardata) + res.render('sats', esdata) + }) +}); + +app.get('/it', function(req, res) { + calculateEur.get10yr().then(pydata => { + const yeardata = { 'yeardata': pydata } + let itdata = Object.assign(itjson, yeardata) + res.render('sats', itdata) + }) +}); + +app.get('/nl', function(req, res) { + calculateEur.get10yr().then(pydata => { + const yeardata = { 'yeardata': pydata } + let nldata = Object.assign(nljson, yeardata) + res.render('sats', nldata) + }) +}); + +app.get('/pt', function(req, res) { + calculateEur.get10yr().then(pydata => { + const yeardata = { 'yeardata': pydata } + let ptdata = Object.assign(ptjson, yeardata) + res.render('sats', ptdata) + }) +}); + +app.get('/pl', function(req, res) { + calculateEur.get10yr().then(pydata => { + const yeardata = { 'yeardata': pydata } + let pldata = Object.assign(pljson, yeardata) + res.render('sats', pldata) + }) +}); + //Makes the app listen to port 3000 app.listen(port, () => console.log(`App listening to port ${port}`)); \ No newline at end of file diff --git a/locales/de.json b/locales/de.json new file mode 100644 index 0000000..ebbbcca --- /dev/null +++ b/locales/de.json @@ -0,0 +1,35 @@ +{ + "post": { + "author": "Janith Kasun", + "image": "https://picsum.photos/500/500", + "comments": [ + "This is the first comment", + "This is the second comment", + "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum nec fermentum ligula. Sed vitae erat lectus." + ] + }, + "layout": "main", + "Title": "1 Euro ist derzeit wert ", + "subtitle": "EURSAT Historische Performance", + "date": "Datum", + "price": "Preis", + "percentchange": "Prozentuale Änderung", + "footnote": "Datenquelle von usdsat.com, angepasst für EUR", + "data_file": "eur_historical", + "rate_field": "sateur_rate", + "exchange_rate": "0.92", + "lang1_link": "/en-eur/", + "lang1": "English", + "lang2_link": "/fr/", + "lang2": "Français", + "lang3_link": "/es/", + "lang3": "Español", + "lang4_link": "/it/", + "lang4": "Italiano", + "lang5_link": "/nl/", + "lang5": "Nederlands", + "lang6_link": "/pt/", + "lang6": "Português", + "lang7_link": "/pl/", + "lang7": "Polski" +} diff --git a/locales/en-eur.json b/locales/en-eur.json new file mode 100644 index 0000000..fe9a2c4 --- /dev/null +++ b/locales/en-eur.json @@ -0,0 +1,35 @@ +{ + "post": { + "author": "Janith Kasun", + "image": "https://picsum.photos/500/500", + "comments": [ + "This is the first comment", + "This is the second comment", + "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum nec fermentum ligula. Sed vitae erat lectus." + ] + }, + "layout": "main", + "Title": "1 Euro is currently worth ", + "subtitle": "EURSAT Historical Performance", + "date": "Date", + "price": "Price", + "percentchange": "Percent Change", + "footnote": "data source from usdsat.com, adapted for EUR", + "data_file": "eur_historical", + "rate_field": "sateur_rate", + "exchange_rate": "0.92", + "lang1_link": "/de/", + "lang1": "Deutsch", + "lang2_link": "/fr/", + "lang2": "Français", + "lang3_link": "/es/", + "lang3": "Español", + "lang4_link": "/it/", + "lang4": "Italiano", + "lang5_link": "/nl/", + "lang5": "Nederlands", + "lang6_link": "/pt/", + "lang6": "Português", + "lang7_link": "/pl/", + "lang7": "Polski" +} diff --git a/locales/en.json b/locales/en.json index 6d800db..b9f907c 100644 --- a/locales/en.json +++ b/locales/en.json @@ -15,6 +15,9 @@ "price": "Price", "percentchange": "Percent Change", "footnote": "data source from usdsat.com, adapted for HKD", + "data_file": "hkd_historical", + "rate_field": "sathkd_rate", + "exchange_rate": "7.75", "lang1_link": "/zh-cn/", "lang1": "中文(中国)", "lang2_link": "/zh-hk/", diff --git a/locales/es.json b/locales/es.json new file mode 100644 index 0000000..59e63fd --- /dev/null +++ b/locales/es.json @@ -0,0 +1,35 @@ +{ + "post": { + "author": "Janith Kasun", + "image": "https://picsum.photos/500/500", + "comments": [ + "This is the first comment", + "This is the second comment", + "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum nec fermentum ligula. Sed vitae erat lectus." + ] + }, + "layout": "main", + "Title": "1 Euro vale actualmente ", + "subtitle": "Rendimiento Histórico EURSAT", + "date": "Fecha", + "price": "Precio", + "percentchange": "Cambio Porcentual", + "footnote": "fuente de datos de usdsat.com, adaptado para EUR", + "data_file": "eur_historical", + "rate_field": "sateur_rate", + "exchange_rate": "0.92", + "lang1_link": "/en-eur/", + "lang1": "English", + "lang2_link": "/de/", + "lang2": "Deutsch", + "lang3_link": "/fr/", + "lang3": "Français", + "lang4_link": "/it/", + "lang4": "Italiano", + "lang5_link": "/nl/", + "lang5": "Nederlands", + "lang6_link": "/pt/", + "lang6": "Português", + "lang7_link": "/pl/", + "lang7": "Polski" +} diff --git a/locales/fr.json b/locales/fr.json new file mode 100644 index 0000000..7dca8a2 --- /dev/null +++ b/locales/fr.json @@ -0,0 +1,35 @@ +{ + "post": { + "author": "Janith Kasun", + "image": "https://picsum.photos/500/500", + "comments": [ + "This is the first comment", + "This is the second comment", + "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum nec fermentum ligula. Sed vitae erat lectus." + ] + }, + "layout": "main", + "Title": "1 Euro vaut actuellement ", + "subtitle": "Performance Historique EURSAT", + "date": "Date", + "price": "Prix", + "percentchange": "Variation en Pourcentage", + "footnote": "source de données de usdsat.com, adaptée pour EUR", + "data_file": "eur_historical", + "rate_field": "sateur_rate", + "exchange_rate": "0.92", + "lang1_link": "/en-eur/", + "lang1": "English", + "lang2_link": "/de/", + "lang2": "Deutsch", + "lang3_link": "/es/", + "lang3": "Español", + "lang4_link": "/it/", + "lang4": "Italiano", + "lang5_link": "/nl/", + "lang5": "Nederlands", + "lang6_link": "/pt/", + "lang6": "Português", + "lang7_link": "/pl/", + "lang7": "Polski" +} diff --git a/locales/it.json b/locales/it.json new file mode 100644 index 0000000..6b9a373 --- /dev/null +++ b/locales/it.json @@ -0,0 +1,35 @@ +{ + "post": { + "author": "Janith Kasun", + "image": "https://picsum.photos/500/500", + "comments": [ + "This is the first comment", + "This is the second comment", + "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum nec fermentum ligula. Sed vitae erat lectus." + ] + }, + "layout": "main", + "Title": "1 Euro vale attualmente ", + "subtitle": "Performance Storica EURSAT", + "date": "Data", + "price": "Prezzo", + "percentchange": "Variazione Percentuale", + "footnote": "fonte dati da usdsat.com, adattato per EUR", + "data_file": "eur_historical", + "rate_field": "sateur_rate", + "exchange_rate": "0.92", + "lang1_link": "/en-eur/", + "lang1": "English", + "lang2_link": "/de/", + "lang2": "Deutsch", + "lang3_link": "/fr/", + "lang3": "Français", + "lang4_link": "/es/", + "lang4": "Español", + "lang5_link": "/nl/", + "lang5": "Nederlands", + "lang6_link": "/pt/", + "lang6": "Português", + "lang7_link": "/pl/", + "lang7": "Polski" +} diff --git a/locales/nl.json b/locales/nl.json new file mode 100644 index 0000000..7205526 --- /dev/null +++ b/locales/nl.json @@ -0,0 +1,35 @@ +{ + "post": { + "author": "Janith Kasun", + "image": "https://picsum.photos/500/500", + "comments": [ + "This is the first comment", + "This is the second comment", + "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum nec fermentum ligula. Sed vitae erat lectus." + ] + }, + "layout": "main", + "Title": "1 Euro is momenteel waard ", + "subtitle": "EURSAT Historische Prestaties", + "date": "Datum", + "price": "Prijs", + "percentchange": "Procentuele Verandering", + "footnote": "gegevensbron van usdsat.com, aangepast voor EUR", + "data_file": "eur_historical", + "rate_field": "sateur_rate", + "exchange_rate": "0.92", + "lang1_link": "/en-eur/", + "lang1": "English", + "lang2_link": "/de/", + "lang2": "Deutsch", + "lang3_link": "/fr/", + "lang3": "Français", + "lang4_link": "/es/", + "lang4": "Español", + "lang5_link": "/it/", + "lang5": "Italiano", + "lang6_link": "/pt/", + "lang6": "Português", + "lang7_link": "/pl/", + "lang7": "Polski" +} diff --git a/locales/pl.json b/locales/pl.json new file mode 100644 index 0000000..556098e --- /dev/null +++ b/locales/pl.json @@ -0,0 +1,35 @@ +{ + "post": { + "author": "Janith Kasun", + "image": "https://picsum.photos/500/500", + "comments": [ + "This is the first comment", + "This is the second comment", + "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum nec fermentum ligula. Sed vitae erat lectus." + ] + }, + "layout": "main", + "Title": "1 Euro jest obecnie warte ", + "subtitle": "Historyczna Wydajność EURSAT", + "date": "Data", + "price": "Cena", + "percentchange": "Zmiana Procentowa", + "footnote": "źródło danych z usdsat.com, dostosowane dla EUR", + "data_file": "eur_historical", + "rate_field": "sateur_rate", + "exchange_rate": "0.92", + "lang1_link": "/en-eur/", + "lang1": "English", + "lang2_link": "/de/", + "lang2": "Deutsch", + "lang3_link": "/fr/", + "lang3": "Français", + "lang4_link": "/es/", + "lang4": "Español", + "lang5_link": "/it/", + "lang5": "Italiano", + "lang6_link": "/nl/", + "lang6": "Nederlands", + "lang7_link": "/pt/", + "lang7": "Português" +} diff --git a/locales/pt.json b/locales/pt.json new file mode 100644 index 0000000..e072b26 --- /dev/null +++ b/locales/pt.json @@ -0,0 +1,35 @@ +{ + "post": { + "author": "Janith Kasun", + "image": "https://picsum.photos/500/500", + "comments": [ + "This is the first comment", + "This is the second comment", + "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum nec fermentum ligula. Sed vitae erat lectus." + ] + }, + "layout": "main", + "Title": "1 Euro vale atualmente ", + "subtitle": "Desempenho Histórico EURSAT", + "date": "Data", + "price": "Preço", + "percentchange": "Variação Percentual", + "footnote": "fonte de dados de usdsat.com, adaptado para EUR", + "data_file": "eur_historical", + "rate_field": "sateur_rate", + "exchange_rate": "0.92", + "lang1_link": "/en-eur/", + "lang1": "English", + "lang2_link": "/de/", + "lang2": "Deutsch", + "lang3_link": "/fr/", + "lang3": "Français", + "lang4_link": "/es/", + "lang4": "Español", + "lang5_link": "/it/", + "lang5": "Italiano", + "lang6_link": "/nl/", + "lang6": "Nederlands", + "lang7_link": "/pl/", + "lang7": "Polski" +} diff --git a/locales/zh-cn.json b/locales/zh-cn.json index a3a3fc6..aa516da 100644 --- a/locales/zh-cn.json +++ b/locales/zh-cn.json @@ -5,6 +5,9 @@ "price": "价格", "percentchange": "百分比变化", "footnote": "来自usdsat.com的数据源", + "data_file": "hkd_historical", + "rate_field": "sathkd_rate", + "exchange_rate": "7.75", "lang1_link": "/", "lang1": "EN", "lang2_link": "/zh-hk/", diff --git a/locales/zh-hk.json b/locales/zh-hk.json index 6a65507..68c69c2 100644 --- a/locales/zh-hk.json +++ b/locales/zh-hk.json @@ -5,6 +5,9 @@ "price" : "價格", "percentchange" : "百分比變化", "footnote" : "來自usdsat.com的數據源", + "data_file": "hkd_historical", + "rate_field": "sathkd_rate", + "exchange_rate": "7.75", "lang1_link": "/zh-cn/", "lang1": "中文(中国)", "lang2_link": "/", diff --git a/public/eur_historical b/public/eur_historical new file mode 100644 index 0000000..0637a08 --- /dev/null +++ b/public/eur_historical @@ -0,0 +1 @@ +[] \ No newline at end of file diff --git a/updaterate-eur.js b/updaterate-eur.js new file mode 100644 index 0000000..c96b5a4 --- /dev/null +++ b/updaterate-eur.js @@ -0,0 +1,13 @@ +const core = require('@actions/core'); +const poll = require('./btcpoll-eur'); + +// modify this to ping url, get data and update, commit and push file to github repo +try { + const res = poll.main() + console.log("main response: ", res) + core.setOutput('✅ Success'); + +} catch (error) { + core.setFailed(`🛑 ${error.message}`); +} + diff --git a/views/sats.hbs b/views/sats.hbs index af39fca..cbb5498 100644 --- a/views/sats.hbs +++ b/views/sats.hbs @@ -58,8 +58,7 @@ - {{ lang1 }} | - {{ lang2 }} + {{ lang1 }} {{#if lang2}} | {{ lang2 }}{{/if}}{{#if lang3}} | {{ lang3 }}{{/if}}{{#if lang4}} | {{ lang4 }}{{/if}}{{#if lang5}} | {{ lang5 }}{{/if}}{{#if lang6}} | {{ lang6 }}{{/if}}{{#if lang7}} | {{ lang7 }}{{/if}} @@ -125,7 +124,7 @@ i.forEach(function(item) { if (Array.isArray(item)) { var btc_price = item[0]; - currentPrice = Math.round((1 / btc_price) * 100000000 / 7.75); // satoshis per HKD dollar + currentPrice = Math.round((1 / btc_price) * 100000000 / {{ exchange_rate }}); // satoshis per currency unit document.title = currentPrice.toLocaleString() + " sats"; document.querySelector('#current').textContent = currentPrice.toLocaleString(); @@ -165,7 +164,7 @@