add github daily workflow

This commit is contained in:
bitkarrot
2022-03-20 22:17:43 -07:00
parent 2f57afb9de
commit d3f5524a18
7 changed files with 135 additions and 1 deletions

26
.github/workflows/main.yml vendored Normal file
View File

@@ -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

2
.gitignore vendored
View File

@@ -109,3 +109,5 @@ credentials.json
env env
hkd_one hkd_one
hkd_one~ hkd_one~
.DS_Store
package-lock.json

12
action.yml Normal file
View File

@@ -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'

78
btcpoll.js Normal file
View File

@@ -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)

1
generated.txt Normal file
View File

@@ -0,0 +1 @@
Mon Mar 21 05:00:42 UTC 2022

View File

@@ -25,6 +25,8 @@
"csv-parser": "^3.0.0", "csv-parser": "^3.0.0",
"express": "^4.17.1", "express": "^4.17.1",
"express-handlebars": "^5.3.4", "express-handlebars": "^5.3.4",
"morgan": "1.9.1" "morgan": "1.9.1",
"@actions/core": "^1.2.6",
"moment": "^2.29.1"
} }
} }

13
updaterate.js Normal file
View File

@@ -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}`);
}