summary: fix broken price API and other bugs

1. bitcoinaverage does not supply public prices since a while now:
>  Unauthenticated requests are not allowed. Take out a new plan or start
>   a free trial at https://pro.bitcoinaverage.com"
This commit fixes this by porting to bitstamp public ticker API.

2. This commit fixes the missing thread loop, so the price is
   actually updated after a while.

3. This commit fixes an unchecked attribute access that crashed during
   my tests.
This commit is contained in:
Michael Schmoock
2020-07-20 16:41:46 +02:00
committed by Christian Decker
parent ded4f6b380
commit e484db385a

View File

@@ -3,7 +3,6 @@ from pyln.client import Plugin, Millisatoshi
from packaging import version from packaging import version
from collections import namedtuple from collections import namedtuple
import pyln.client import pyln.client
import json
from math import floor, log10 from math import floor, log10
import requests import requests
import threading import threading
@@ -34,15 +33,14 @@ class PriceThread(threading.Thread):
def __init__(self): def __init__(self):
super().__init__() super().__init__()
self.daemon = True self.daemon = True
self.start()
def run(self): def run(self):
while True:
try: try:
r = requests.get('https://apiv2.bitcoinaverage.com/convert/global' r = requests.get('https://www.bitstamp.net/api/v2/ticker/BTC{}'.format(plugin.currency))
'?from=BTC&to={}&amount=1'.format(plugin.currency)) plugin.fiat_per_btc = float(r.json()['last'])
plugin.fiat_per_btc = json.loads(r.content.decode('UTF-8'))['price'] except Exception as ex:
except Exception: plugin.log("[PriceThread] " + str(ex), 'error')
pass
# Six hours is more than often enough for polling # Six hours is more than often enough for polling
time.sleep(6*3600) time.sleep(6*3600)
@@ -85,6 +83,7 @@ def msat_to_approx_str(msat, digits: int = 3):
else: else:
return result return result
# appends an output table header that explains fields and capacity # appends an output table header that explains fields and capacity
def append_header(table, max_msat): def append_header(table, max_msat):
short_str = msat_to_approx_str(Millisatoshi(max_msat)) short_str = msat_to_approx_str(Millisatoshi(max_msat))
@@ -105,10 +104,10 @@ def summary(plugin, exclude=''):
if info['network'] != 'bitcoin': if info['network'] != 'bitcoin':
reply['network'] = info['network'].upper() reply['network'] = info['network'].upper()
if not plugin.my_address: if hasattr(plugin, 'my_address') and plugin.my_address:
reply['warning_no_address'] = "NO PUBLIC ADDRESSES"
else:
reply['my_address'] = plugin.my_address reply['my_address'] = plugin.my_address
else:
reply['warning_no_address'] = "NO PUBLIC ADDRESSES"
utxos = [int(f['amount_msat']) for f in funds['outputs'] utxos = [int(f['amount_msat']) for f in funds['outputs']
if f['status'] == 'confirmed'] if f['status'] == 'confirmed']
@@ -224,7 +223,7 @@ def init(options, configuration, plugin):
info = plugin.rpc.getinfo() info = plugin.rpc.getinfo()
# Try to grab conversion price # Try to grab conversion price
PriceThread() PriceThread().start()
# Prefer IPv4, otherwise take any to give out address. # Prefer IPv4, otherwise take any to give out address.
best_address = None best_address = None