Merge pull request #121 from quartje/master

Add error handling and the ability to change currencies for the getcrypto module
This commit is contained in:
tobi-wan-kenobi 2017-06-27 18:20:34 +02:00 committed by GitHub
commit 494b11be88

View file

@ -10,6 +10,7 @@ Parameters:
* getcrypto.getbtc: 0 for not getting price of BTC, 1 for getting it (default). * getcrypto.getbtc: 0 for not getting price of BTC, 1 for getting it (default).
* getcrypto.geteth: 0 for not getting price of ETH, 1 for getting it (default). * getcrypto.geteth: 0 for not getting price of ETH, 1 for getting it (default).
* getcrypto.getltc: 0 for not getting price of LTC, 1 for getting it (default). * getcrypto.getltc: 0 for not getting price of LTC, 1 for getting it (default).
* getcrypto.getcur: Set the currency to display the price in, usd is the default.
""" """
import requests import requests
@ -17,18 +18,23 @@ import time
import bumblebee.input import bumblebee.input
import bumblebee.output import bumblebee.output
import bumblebee.engine import bumblebee.engine
from requests.exceptions import RequestException
def getfromkrak(coin): def getfromkrak(coin,currency):
if coin=='Btc': if coin=='Btc':
epair = "xbtusd" epair = "xbt"+currency
tickname = "XXBTZUSD" tickname = "XXBTZ"+currency.upper()
if coin=='Eth': if coin=='Eth':
epair = "ethusd" epair = "eth"+currency
tickname = "XETHZUSD" tickname = "XETHZ"+currency.upper()
if coin=='Ltc': if coin=='Ltc':
epair = "ltcusd" epair = "ltc"+currency
tickname = "XLTCZUSD" tickname = "XLTCZ"+currency.upper()
krakenget = requests.get('https://api.kraken.com/0/public/Ticker?pair='+epair).json() try:
krakenget = requests.get('https://api.kraken.com/0/public/Ticker?pair='+epair).json()
except RequestException:
return "No connection"
except Exception:
return "No connection"
kethusdask = float(krakenget['result'][tickname]['a'][0]) kethusdask = float(krakenget['result'][tickname]['a'][0])
kethusdbid = float(krakenget['result'][tickname]['b'][0]) kethusdbid = float(krakenget['result'][tickname]['b'][0])
return coin+": "+str((kethusdask+kethusdbid)/2)[0:6] return coin+": "+str((kethusdask+kethusdbid)/2)[0:6]
@ -45,6 +51,7 @@ class Module(bumblebee.engine.Module):
self._getbtc = int(self.parameter("getbtc", "1")) self._getbtc = int(self.parameter("getbtc", "1"))
self._geteth = int(self.parameter("geteth", "1")) self._geteth = int(self.parameter("geteth", "1"))
self._getltc = int(self.parameter("getltc", "1")) self._getltc = int(self.parameter("getltc", "1"))
self._getcur = self.parameter("getcur", "usd")
engine.input.register_callback(self, button=bumblebee.input.LEFT_MOUSE, engine.input.register_callback(self, button=bumblebee.input.LEFT_MOUSE,
cmd="xdg-open https://cryptowat.ch/") cmd="xdg-open https://cryptowat.ch/")
@ -54,13 +61,14 @@ class Module(bumblebee.engine.Module):
def update(self, widgets): def update(self, widgets):
if self._nextcheck < int(time.time()): if self._nextcheck < int(time.time()):
self._nextcheck = int(time.time()) + self._interval self._nextcheck = int(time.time()) + self._interval
currency = self._getcur
btcprice, ethprice, ltcprice = "", "", "" btcprice, ethprice, ltcprice = "", "", ""
if self._getbtc==1: if self._getbtc==1:
btcprice= getfromkrak('Btc') btcprice= getfromkrak('Btc',currency)
if self._geteth==1: if self._geteth==1:
ethprice=getfromkrak('Eth') ethprice=getfromkrak('Eth',currency)
if self._getltc==1: if self._getltc==1:
ltcprice=getfromkrak('Ltc') ltcprice=getfromkrak('Ltc',currency)
self._curprice = btcprice+" "*(self._getbtc*self._geteth)+ethprice+" "*(self._getltc*max(self._getbtc, self._geteth))+ltcprice self._curprice = btcprice+" "*(self._getbtc*self._geteth)+ethprice+" "*(self._getltc*max(self._getbtc, self._geteth))+ltcprice
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4