mirror of
https://github.com/aljazceru/satshkd-vercel.git
synced 2025-12-17 05:04:24 +01:00
add python calculate
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -106,3 +106,4 @@ dist
|
||||
credentials.json
|
||||
|
||||
.vercel
|
||||
env
|
||||
|
||||
111
calculate.py
Normal file
111
calculate.py
Normal file
@@ -0,0 +1,111 @@
|
||||
from datetime import date
|
||||
import json
|
||||
import yaml
|
||||
import logging
|
||||
import requests
|
||||
|
||||
logging.basicConfig(filename='satshkd.log', level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
|
||||
logging.getLogger('satslogger').setLevel(level=logging.WARNING)
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def del_years(d, years):
|
||||
"""
|
||||
Return a date that's `years` years after the date (or datetime)
|
||||
object `d`. Return the same calendar date (month and day) in the
|
||||
destination year, if it exists, otherwise use the following day
|
||||
(thus changing February 29 to March 1).
|
||||
|
||||
"""
|
||||
try:
|
||||
return d.replace(year = d.year - years)
|
||||
except ValueError:
|
||||
return d + (date(d.year + years, 1, 1) - date(d.year, 1, 1))
|
||||
|
||||
|
||||
# generate 10 year historical prices based on today's date
|
||||
def get_10year(lang):
|
||||
# path = '/home/bitkarrot/satshkd/static/hkd_historical'
|
||||
path = './public/static/hkd_historical'
|
||||
filep = open(path)
|
||||
historical = json.load(filep)
|
||||
hist_entries = []
|
||||
|
||||
datelist = []
|
||||
years = list(range(1,11))
|
||||
for i in years:
|
||||
adate = str(del_years(date.today(), i))
|
||||
datelist.append(adate)
|
||||
|
||||
for entry in historical:
|
||||
if entry['date'] in datelist:
|
||||
hist_entries.append(entry)
|
||||
hist_entries.reverse()
|
||||
|
||||
final_list = []
|
||||
today_sats = get_bitfinex_rate()
|
||||
|
||||
i = 1
|
||||
text_array = []
|
||||
if lang == 'zh-cn' or lang == 'zh-hk':
|
||||
text_array.append('年前')
|
||||
text_array.append('年前')
|
||||
elif lang =='en':
|
||||
text_array.append("year ago")
|
||||
text_array.append("years ago")
|
||||
|
||||
|
||||
for entry in hist_entries:
|
||||
year = entry['date']
|
||||
rawsat = entry['sathkd_rate']
|
||||
sats = "{:,}".format(rawsat)
|
||||
percentage = -100 * (rawsat - today_sats)/rawsat
|
||||
strp = "{:.3f}".format(percentage) + "%"
|
||||
# print(f'year: {year} sats: {sats} percent: {strp}')
|
||||
if i == 1:
|
||||
aset = {'year' : f"{i} {text_array[0]}", 'sats': sats + " sats", 'percent': strp}
|
||||
else:
|
||||
aset = {'year' : f"{i} {text_array[1]}", 'sats': sats + " sats", 'percent': strp}
|
||||
final_list.append(aset)
|
||||
i = i + 1
|
||||
return final_list
|
||||
|
||||
|
||||
def get_bitfinex_rate():
|
||||
try:
|
||||
path = "./"
|
||||
rates_file = path + 'rates.yml'
|
||||
|
||||
with open(rates_file, 'rb') as f:
|
||||
doc = yaml.load(f, Loader=yaml.FullLoader)
|
||||
sort_file = yaml.dump(doc, sort_keys=True)
|
||||
f.close()
|
||||
|
||||
hkdrate = doc['hkdrate']
|
||||
|
||||
satDenominator = 100000000
|
||||
btcDataURL = "https://api-pub.bitfinex.com/v2/ticker/tBTCUSD"
|
||||
|
||||
btcRates = requests.get(btcDataURL).json()
|
||||
btcLastPrice = btcRates[6]
|
||||
|
||||
sathkd = round((1/btcLastPrice)*satDenominator*hkdrate)
|
||||
# print(f'bitfinex last price: {btcLastPrice}, current sat rate: {sathkd}')
|
||||
return sathkd
|
||||
|
||||
except Exception as e:
|
||||
logger.info(e)
|
||||
|
||||
|
||||
|
||||
# initial test
|
||||
# rate = get_bitfinex_rate()
|
||||
# print(f'getting bitfinex rate - initial test: {rate}')
|
||||
|
||||
|
||||
lang = 'zh-cn'
|
||||
lang = 'zh-hk'
|
||||
lang = 'en'
|
||||
final = get_10year(lang)
|
||||
print(final)
|
||||
|
||||
55
convert2hkd.py
Normal file
55
convert2hkd.py
Normal file
@@ -0,0 +1,55 @@
|
||||
import json
|
||||
import requests
|
||||
import logging
|
||||
|
||||
logging.basicConfig(filename='rates.log', level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
|
||||
logging.getLogger('rateslogger').setLevel(level=logging.WARNING)
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
path = "./"
|
||||
|
||||
def convert():
|
||||
try:
|
||||
# grab file from original site
|
||||
r = requests.get("http://usdsat.com/historical")
|
||||
with open(path + "static/historical", "wb") as f:
|
||||
f.write(r.content)
|
||||
f.close()
|
||||
|
||||
# convert to hkd
|
||||
my_file = open(path + 'static/historical', 'rt')
|
||||
lines = my_file.read()
|
||||
my_file.close()
|
||||
jlist = json.loads(lines)
|
||||
|
||||
print(len(jlist))
|
||||
|
||||
|
||||
# we don't need exact rate for historical
|
||||
# just a ball park rate is sufficient for the amts
|
||||
for i in jlist:
|
||||
print(i)
|
||||
price = i['usdsat_rate']
|
||||
i['sathkd_rate'] = int(price/7.75)
|
||||
whole_price = i['btcusd_rate']
|
||||
i['btchkd_rate'] = whole_price*7.75
|
||||
|
||||
|
||||
|
||||
print(jlist[len(jlist)-1])
|
||||
logger.info(jlist[len(jlist)-1])
|
||||
|
||||
with open(path + 'static/hkd_historical', 'w') as output:
|
||||
output.write(json.dumps(jlist))
|
||||
output.close()
|
||||
|
||||
except Exception as e:
|
||||
print("Exception" + e)
|
||||
logger.info(e)
|
||||
logger.info("Something unexpected occurred!")
|
||||
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
convert()
|
||||
27
index.js
27
index.js
@@ -17,6 +17,24 @@ const zhcnjson = require('./locales/zh-cn.json');
|
||||
const zhhkjson = require('./locales/zh-hk.json');
|
||||
const enjson = require('./locales/en.json');
|
||||
|
||||
const { spawn } = require('child_process');
|
||||
const temperatures = []; // Store readings
|
||||
|
||||
const pydata = [{ 'year': '1 year ago', 'sats': '1,199 sats', 'percent': '-79.149%' },
|
||||
{ 'year': '2 years ago', 'sats': '1,582 sats', 'percent': '-84.197%' },
|
||||
{ 'year': '3 years ago', 'sats': '1,958 sats', 'percent': '-87.232%' },
|
||||
{ 'year': '4 years ago', 'sats': '2,984 sats', 'percent': '-91.622%' },
|
||||
{ 'year': '5 years ago', 'sats': '21,122 sats', 'percent': '-98.816%' },
|
||||
{ 'year': '6 years ago', 'sats': '53,632 sats', 'percent': '-99.534%' },
|
||||
{ 'year': '7 years ago', 'sats': '40,367 sats', 'percent': '-99.381%' },
|
||||
{ 'year': '8 years ago', 'sats': '106,515 sats', 'percent': '-99.765%' },
|
||||
{ 'year': '9 years ago', 'sats': '1,016,962 sats', 'percent': '-99.975%' },
|
||||
{ 'year': '10 years ago', 'sats': '2,649,533 sats', 'percent': '-99.991%' }
|
||||
]
|
||||
|
||||
const yeardata = { 'yeardata': pydata }
|
||||
//console.log(yeardata['yeardata'][0])
|
||||
|
||||
app.set('view engine', 'hbs');
|
||||
app.set('views', __dirname + '/views')
|
||||
|
||||
@@ -65,15 +83,18 @@ app.get('/', function(req, res) {
|
||||
});
|
||||
|
||||
app.get('/en', function(req, res) {
|
||||
res.render('sats', enjson)
|
||||
let endata = Object.assign(enjson, yeardata)
|
||||
res.render('sats', endata)
|
||||
});
|
||||
|
||||
app.get('/zh-cn', function(req, res) {
|
||||
res.render('sats', zhcnjson)
|
||||
let zhcndata = Object.assign(zhcnjson, yeardata)
|
||||
res.render('sats', zhcndata)
|
||||
});
|
||||
|
||||
app.get('/zh-hk', function(req, res) {
|
||||
res.render('sats', zhhkjson)
|
||||
let zhhkdata = Object.assign(zhhkjson, yeardata)
|
||||
res.render('sats', zhhkdata)
|
||||
});
|
||||
|
||||
app.get('/test', (req, res) => {
|
||||
|
||||
2
requirements.txt
Normal file
2
requirements.txt
Normal file
@@ -0,0 +1,2 @@
|
||||
PyYAML==5.4
|
||||
requests==2.25.1
|
||||
@@ -143,18 +143,15 @@
|
||||
<th>{{ percentchange }} </th>
|
||||
</tr>
|
||||
</thead>
|
||||
<!--
|
||||
<tbody>
|
||||
{% for dict_item in my_list %}
|
||||
{{#each yeardata}}
|
||||
<tr>
|
||||
{% for key, value in dict_item.items() %}
|
||||
<td>{{value}}</td>
|
||||
{% endfor %}
|
||||
<td>{{ this.year }}</td>
|
||||
<td>{{ this.sats }}</td>
|
||||
<td>{{ this.percent }}</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
{{/each}}
|
||||
</tbody>
|
||||
-->
|
||||
|
||||
</table>
|
||||
|
||||
|
||||
|
||||
@@ -5,6 +5,29 @@
|
||||
<h1>{{ Title }} </h1>
|
||||
<h4>{{ subtitle }} </h4>
|
||||
|
||||
<table class="u-full-width">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>{{ date }} </th>
|
||||
<th>{{ price }} </th>
|
||||
<th>{{ percentchange }} </th>
|
||||
</tr>
|
||||
</thead>
|
||||
|
||||
<tbody>
|
||||
{{#each yeardata}}
|
||||
<tr>
|
||||
<td>{{ this.year }}</td>
|
||||
<td>{{ this.sats }}</td>
|
||||
<td>{{ this.percent }}</td>
|
||||
{{/each}}
|
||||
</tr>
|
||||
</tbody>
|
||||
|
||||
|
||||
</table>
|
||||
|
||||
|
||||
<div class="posts">
|
||||
<div class="row justify-content-center">
|
||||
<div class="col-lg-7" style="margin-top: 50px;">
|
||||
|
||||
Reference in New Issue
Block a user