diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml new file mode 100644 index 0000000..a2d5c0d --- /dev/null +++ b/.github/workflows/main.yml @@ -0,0 +1,26 @@ +name: Simple update btc rates +on: + push: + schedule: + - cron: '4 5 * * *' + +jobs: + run: + name: update hkdhistorical + runs-on: ubuntu-latest + steps: + - name: setup actions + uses: actions/checkout@v3 + with: + node-version: 12.x + - name: add and push + run: | + node updaterate.js + date > generated.txt + git config user.name github-actions + git config user.email github-actions@github.com + git add . + git commit -m "generated" + git push + + diff --git a/.gitignore b/.gitignore index b9810b1..9d8ad5f 100644 --- a/.gitignore +++ b/.gitignore @@ -109,3 +109,5 @@ credentials.json env hkd_one hkd_one~ +.DS_Store +package-lock.json diff --git a/action.yml b/action.yml new file mode 100644 index 0000000..6ec3540 --- /dev/null +++ b/action.yml @@ -0,0 +1,12 @@ +name: 'fetch my custom url and update' +description: 'fetch url and cron job.' +branding: + icon: 'check-circle' + color: 'green' +inputs: + url: + description: 'URL to get rate' + required: true +runs: + using: 'node12' + main: 'index.js' diff --git a/btcpoll.js b/btcpoll.js new file mode 100644 index 0000000..311c463 --- /dev/null +++ b/btcpoll.js @@ -0,0 +1,78 @@ +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/hkd_historical" +const fileToRead = dirPath + "/public/hkd_historical" + + + // get btc/usd and btc/hkd 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 btchkd = data['market_data']['current_price']['hkd'] + const satsrate = 100000000 + const sathkd = parseInt(satsrate / btchkd) + const usdsat = parseInt(satsrate / btcusd) + + row = { + btcusd_rate: parseInt(btcusd), + date: dbdate, + usdsat_rate: usdsat, + sathkd_rate: sathkd, + btchkd_rate: parseFloat(btchkd).toFixed(2), + } + console.log("row data: ", row) + }) + return row + } + + // update file in the target github repo + async function updateFile() { + const row = await BTCDaily() + + 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 satshkd....") + let result = updateFile(); + console.log(result) + return true + } +} + + +//const res = main() +//console.log('Result from main() : ', res) + + diff --git a/generated.txt b/generated.txt new file mode 100644 index 0000000..b2a361c --- /dev/null +++ b/generated.txt @@ -0,0 +1 @@ +Mon Mar 21 05:00:42 UTC 2022 diff --git a/package.json b/package.json index 01c70c8..97ddd7a 100644 --- a/package.json +++ b/package.json @@ -25,6 +25,8 @@ "csv-parser": "^3.0.0", "express": "^4.17.1", "express-handlebars": "^5.3.4", - "morgan": "1.9.1" + "morgan": "1.9.1", + "@actions/core": "^1.2.6", + "moment": "^2.29.1" } } diff --git a/updaterate.js b/updaterate.js new file mode 100644 index 0000000..ecdb782 --- /dev/null +++ b/updaterate.js @@ -0,0 +1,13 @@ +const core = require('@actions/core'); +const poll = require('./btcpoll'); + +// 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}`); +} +