2017-06-14 18:26:04 +02:00
|
|
|
# pylint: disable=C0111,R0903
|
|
|
|
|
|
|
|
"""Displays the price of a cryptocurrency.
|
|
|
|
|
|
|
|
Requires the following python packages:
|
|
|
|
* requests
|
|
|
|
|
|
|
|
Parameters:
|
2017-06-14 18:30:48 +02:00
|
|
|
* getcrypto.interval: Interval in seconds for updating the price, default is 120, less than that will probably get your IP banned.
|
2017-06-14 18:26:04 +02:00
|
|
|
* 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.getltc: 0 for not getting price of LTC, 1 for getting it (default).
|
2017-06-26 22:35:33 +02:00
|
|
|
* getcrypto.getcur: Set the currency to display the price in, usd is the default.
|
2017-06-14 18:26:04 +02:00
|
|
|
"""
|
|
|
|
|
|
|
|
import requests
|
|
|
|
import time
|
2017-07-15 08:36:33 +02:00
|
|
|
import bumblebee.util
|
2017-06-14 18:26:04 +02:00
|
|
|
import bumblebee.input
|
|
|
|
import bumblebee.output
|
|
|
|
import bumblebee.engine
|
2017-06-24 17:41:40 +02:00
|
|
|
from requests.exceptions import RequestException
|
2017-10-13 17:06:18 +02:00
|
|
|
def getfromkrak(coin, currency):
|
2017-08-19 16:14:42 +02:00
|
|
|
abbrev = {
|
|
|
|
"Btc": ["xbt", "XXBTZ"],
|
|
|
|
"Eth": ["eth", "XETHZ"],
|
|
|
|
"Ltc": ["ltc", "XLTCZ"],
|
|
|
|
}
|
|
|
|
data = abbrev.get(coin, None)
|
|
|
|
if not data: return
|
|
|
|
epair = "{}{}".format(data[0], currency)
|
|
|
|
tickname = "{}{}".format(data[1], currency.upper())
|
2017-06-24 17:41:40 +02:00
|
|
|
try:
|
|
|
|
krakenget = requests.get('https://api.kraken.com/0/public/Ticker?pair='+epair).json()
|
2017-08-19 16:14:42 +02:00
|
|
|
except (RequestException, Exception):
|
2017-06-24 17:41:40 +02:00
|
|
|
return "No connection"
|
2018-01-11 20:32:15 +01:00
|
|
|
if not 'result' in krakenget:
|
|
|
|
return "No data"
|
2017-06-14 18:26:04 +02:00
|
|
|
kethusdask = float(krakenget['result'][tickname]['a'][0])
|
|
|
|
kethusdbid = float(krakenget['result'][tickname]['b'][0])
|
|
|
|
return coin+": "+str((kethusdask+kethusdbid)/2)[0:6]
|
|
|
|
|
|
|
|
|
|
|
|
class Module(bumblebee.engine.Module):
|
|
|
|
def __init__(self, engine, config):
|
|
|
|
super(Module, self).__init__(engine, config,
|
|
|
|
bumblebee.output.Widget(full_text=self.curprice)
|
|
|
|
)
|
2017-06-14 21:31:31 +02:00
|
|
|
self._curprice = ""
|
2017-06-14 18:26:04 +02:00
|
|
|
self._nextcheck = 0
|
|
|
|
self._interval = int(self.parameter("interval", "120"))
|
2017-07-15 08:36:33 +02:00
|
|
|
self._getbtc = bumblebee.util.asbool(self.parameter("getbtc", True))
|
|
|
|
self._geteth = bumblebee.util.asbool(self.parameter("geteth", True))
|
|
|
|
self._getltc = bumblebee.util.asbool(self.parameter("getltc", True))
|
2017-06-26 22:35:33 +02:00
|
|
|
self._getcur = self.parameter("getcur", "usd")
|
2017-06-14 19:00:51 +02:00
|
|
|
engine.input.register_callback(self, button=bumblebee.input.LEFT_MOUSE,
|
|
|
|
cmd="xdg-open https://cryptowat.ch/")
|
2017-06-14 18:26:04 +02:00
|
|
|
|
|
|
|
def curprice(self, widget):
|
|
|
|
return self._curprice
|
|
|
|
|
|
|
|
def update(self, widgets):
|
|
|
|
if self._nextcheck < int(time.time()):
|
|
|
|
self._nextcheck = int(time.time()) + self._interval
|
2017-06-26 22:35:33 +02:00
|
|
|
currency = self._getcur
|
2017-06-14 18:26:04 +02:00
|
|
|
btcprice, ethprice, ltcprice = "", "", ""
|
2017-07-15 08:36:33 +02:00
|
|
|
if self._getbtc:
|
2017-10-13 17:06:18 +02:00
|
|
|
btcprice = getfromkrak('Btc', currency)
|
2017-07-15 08:36:33 +02:00
|
|
|
if self._geteth:
|
2017-10-13 17:06:18 +02:00
|
|
|
ethprice = getfromkrak('Eth', currency)
|
2017-07-15 08:36:33 +02:00
|
|
|
if self._getltc:
|
2017-10-13 17:06:18 +02:00
|
|
|
ltcprice = getfromkrak('Ltc', currency)
|
2017-06-14 18:26:04 +02:00
|
|
|
self._curprice = btcprice+" "*(self._getbtc*self._geteth)+ethprice+" "*(self._getltc*max(self._getbtc, self._geteth))+ltcprice
|
|
|
|
|
|
|
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|