mirror of
https://github.com/aljazceru/satshkd-vercel.git
synced 2025-12-18 13:44:22 +01:00
rewrite conversion from py to node
This commit is contained in:
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@@ -3,14 +3,16 @@ const fs = require('fs');
|
|||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
bfx: async function() {
|
bfx: async function() {
|
||||||
|
const hkdrate = 0.1287 // approximate rate
|
||||||
const btcDataURL = "https://api-pub.bitfinex.com/v2/ticker/tBTCUSD"
|
const btcDataURL = "https://api-pub.bitfinex.com/v2/ticker/tBTCUSD"
|
||||||
const response = await axios.get(btcDataURL)
|
const response = await axios.get(btcDataURL)
|
||||||
const data = response.data
|
const data = response.data
|
||||||
//console.log(data[6])
|
//console.log(data[6])
|
||||||
|
|
||||||
const satDenominator = 100000000
|
const satDenominator = 100000000
|
||||||
const hkdrate = 0.1287 // approximate rate
|
// see docs : https://docs.bitfinex.com/reference#rest-public-ticker
|
||||||
btcLastPrice = data[6]
|
btcLastPrice = data[6]
|
||||||
|
|
||||||
const sathkd = Math.round((1 / btcLastPrice) * satDenominator * hkdrate)
|
const sathkd = Math.round((1 / btcLastPrice) * satDenominator * hkdrate)
|
||||||
//console.log("bitfinex last price: ", btcLastPrice, "current satHKD: ", sathkd)
|
//console.log("bitfinex last price: ", btcLastPrice, "current satHKD: ", sathkd)
|
||||||
return sathkd
|
return sathkd
|
||||||
|
|||||||
@@ -4,18 +4,14 @@ const fs = require('fs');
|
|||||||
// this script is to convert the downloaded data
|
// this script is to convert the downloaded data
|
||||||
// 1. get data from site
|
// 1. get data from site
|
||||||
// bitfinex daily close data: https://www.investing.com/crypto/bitcoin/btc-usd-historical-data
|
// bitfinex daily close data: https://www.investing.com/crypto/bitcoin/btc-usd-historical-data
|
||||||
// 2. save as BTC_USD_Bitfinex_HistoricalData.csv
|
// 2. save as BTC_USD_Bitfinex_HistoricalData.csv,
|
||||||
// 3. convert to the format of historical. using convertformat()
|
// convert to the format of historical. using convertformat()
|
||||||
// check result.
|
// pipe result to new_history, concatenate to historical,
|
||||||
|
|
||||||
// 4. pipe result to new_history, concatenate to historical,
|
|
||||||
// use mergefiles(), check result
|
|
||||||
|
|
||||||
// new file is named historical_merged. copy over the public/static/historical
|
// new file is named historical_merged. copy over the public/static/historical
|
||||||
// convert historical to hkd_historical using other script
|
// convert historical to hkd_historical
|
||||||
|
|
||||||
|
|
||||||
function convertformat() {
|
function convertformat() {
|
||||||
|
|
||||||
const filepath = "./archive/BTC_USD_Bitfinex_HistoricalData.csv"
|
const filepath = "./archive/BTC_USD_Bitfinex_HistoricalData.csv"
|
||||||
let data = []
|
let data = []
|
||||||
|
|
||||||
@@ -37,31 +33,54 @@ function convertformat() {
|
|||||||
})
|
})
|
||||||
.on('end', () => {
|
.on('end', () => {
|
||||||
// console.log(data)
|
// console.log(data)
|
||||||
var new_json = JSON.stringify(data)
|
var new_history = JSON.stringify(data)
|
||||||
console.log(new_json)
|
console.log(new_history)
|
||||||
// append this to the historical data file.
|
fs.writeFileSync("./archive/new_history", new_history)
|
||||||
fs.writeFileSync("./archive/new_history", new_json)
|
let newh = JSON.parse(new_history).reverse()
|
||||||
// write to regular csv file:
|
|
||||||
// csvWriter.writeRecords(data).then(() => console.log('CSV file written'))
|
// get original historical data file. concat new dates
|
||||||
|
// no logic handled here for date overlap
|
||||||
|
const historical = "./public/static/historical"
|
||||||
|
const histcontent = fs.readFileSync(historical, { encoding: 'utf8' })
|
||||||
|
let hist = JSON.parse(histcontent)
|
||||||
|
|
||||||
|
const result = JSON.stringify(hist.concat(newh))
|
||||||
|
console.log(result)
|
||||||
|
fs.writeFileSync("./archive/historical_merged", result)
|
||||||
|
// delete old historical
|
||||||
|
fs.copyFile('./archive/historical_merged', './public/static/historical', (err) => {
|
||||||
|
if (err) throw err;
|
||||||
|
console.log('./archive/historical_merged was copied to ./public/static/historical');
|
||||||
|
});
|
||||||
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
function mergefiles() {
|
|
||||||
// merge two files together
|
function hkdrate() {
|
||||||
const newhistory = fs.readFileSync('./archive/new_history', { encoding: 'utf8' })
|
const hkdusd_rate = 7.75
|
||||||
let newh = JSON.parse(newhistory).reverse()
|
|
||||||
//console.log(newh)
|
|
||||||
const historical = "./public/static/historical"
|
const historical = "./public/static/historical"
|
||||||
const histcontent = fs.readFileSync(historical, { encoding: 'utf8' })
|
const histcontent = fs.readFileSync(historical, { encoding: 'utf8' })
|
||||||
let hist = JSON.parse(histcontent)
|
let hist = JSON.parse(histcontent)
|
||||||
|
|
||||||
const result = JSON.stringify(hist.concat(newh))
|
let hkdData = []
|
||||||
console.log(result)
|
hist.forEach(function(entry) {
|
||||||
fs.writeFileSync("./archive/historical_merged", result)
|
newEntry = {
|
||||||
|
"btcusd_rate": entry['btcusd_rate'],
|
||||||
|
"date": entry['date'],
|
||||||
|
"usdsat_rate": entry['usdsat_rate'],
|
||||||
|
"sathkd_rate": parseInt(entry['usdsat_rate'] / hkdusd_rate).toFixed(0),
|
||||||
|
"btchkd_rate": parseFloat(entry['btcusd_rate'] * hkdusd_rate).toFixed(2),
|
||||||
|
}
|
||||||
|
hkdData.push(newEntry)
|
||||||
|
})
|
||||||
|
console.log(hkdData)
|
||||||
|
const hkdHistorical = JSON.stringify(hkdData)
|
||||||
|
fs.writeFileSync("./public/static/hkd_historical", hkdHistorical)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// first run convertformat then run hkdrate
|
||||||
|
// convertformat()
|
||||||
|
|
||||||
//convertformat()
|
hkdrate()
|
||||||
// run 1st before 2nd command
|
|
||||||
|
|
||||||
mergefiles()
|
|
||||||
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user