diff --git a/.gitignore b/.gitignore index f945812..b9810b1 100644 --- a/.gitignore +++ b/.gitignore @@ -107,3 +107,5 @@ credentials.json .vercel env +hkd_one +hkd_one~ diff --git a/calculate.py b/archive/calculate.py similarity index 100% rename from calculate.py rename to archive/calculate.py diff --git a/convert2hkd.py b/archive/convert2hkd.py similarity index 100% rename from convert2hkd.py rename to archive/convert2hkd.py diff --git a/rates.yml b/archive/rates.yml similarity index 100% rename from rates.yml rename to archive/rates.yml diff --git a/requirements.txt b/archive/requirements.txt similarity index 100% rename from requirements.txt rename to archive/requirements.txt diff --git a/calculate.js b/calculate.js index 3fc3d5b..14239e6 100644 --- a/calculate.js +++ b/calculate.js @@ -1,23 +1,60 @@ const axios = require('axios') +const fs = require('fs').promises; module.exports = { bfx: async function() { const btcDataURL = "https://api-pub.bitfinex.com/v2/ticker/tBTCUSD" const response = await axios.get(btcDataURL) const data = response.data - console.log(data[6]) + //console.log(data[6]) const satDenominator = 100000000 const hkdrate = 0.1287 btcLastPrice = data[6] const sathkd = Math.round((1 / btcLastPrice) * satDenominator * hkdrate) - console.log("bitfinex last price: ", btcLastPrice, "current satHKD: ", sathkd) - return data + //console.log("bitfinex last price: ", btcLastPrice, "current satHKD: ", sathkd) + return sathkd }, - get10yr: function(lang) { + get10yr: async function(lang) { console.log("get10yr language: ", lang) - return true + let data = "testing" + try { + const content = await fs.readFile('./public/static/hkd_historical') + const historical = JSON.parse(content) + //console.log(historical[0]) + hist_entries = [] + // get all the years you need from 1 - 10 + let datelist = [] + 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)) { + //console.log('hit', hdate, historical[j]) + hist_entries.push(historical[j]) + } + } + // console.log(hist_entries) + + let final_list = [] + let today_sats = await this.bfx() + // console.log("today sats", today_sats) + for (var v = 0; v < hist_entries.length; v++) { + year = new Date(hist_entries[v]['date']).getFullYear(); + rawsat = hist_entries[v]['sathkd_rate'] + percentage = (-100 * ((rawsat - today_sats) / rawsat)).toFixed(3) + final_list.push({ "year": year, "sats": rawsat, "percentage": percentage }); + } + // console.log(final_list.reverse()) + + return true + //return final_list.reverse() + } catch (error) { + console.error("Error trying to read file ", error) + } } } \ No newline at end of file diff --git a/index.js b/index.js index f33b186..3d8cca7 100644 --- a/index.js +++ b/index.js @@ -16,13 +16,8 @@ const zhcnjson = require('./locales/zh-cn.json'); const zhhkjson = require('./locales/zh-hk.json'); const enjson = require('./locales/en.json'); - -//calculate.bfx() -//calculate.get10yr('en') - -//const product = require("./api/product"); -//app.use("/api/product", product); - +const data = calculate.get10yr('en').then(value => { console.log(value) }) + //console.log(data) const pydata = [{ 'year': '1 year ago', 'sats': '1,199 sats', 'percent': '-79.149%' }, { 'year': '2 years ago', 'sats': '1,582 sats', 'percent': '-84.197%' }, @@ -62,8 +57,6 @@ app.use(logger('dev')); // will look for "./public/js/app.js". app.use(express.static(path.join(__dirname, 'public'))); -//app.use(express.static(path.join(__dirname, 'views'))); -//app.use(express.static(path.join(__dirname, 'views/layouts'))); // if you wanted to "prefix" you may use