bumblebee-status/bumblebee_status/modules/contrib/getcrypto.py

90 lines
2.9 KiB
Python
Raw Permalink Normal View History

2020-04-19 10:35:09 +02:00
# pylint: disable=C0111,R0903
"""Displays the price of a cryptocurrency.
Requires the following python packages:
* requests
Parameters:
* getcrypto.interval: Interval in seconds for updating the price, default is 120, less than that will probably get your IP banned.
* 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).
* getcrypto.getcur: Set the currency to display the price in, usd is the default.
contributed by `Ryunaq <https://github.com/Ryunaq>`_ - many thanks!
2020-04-19 10:35:09 +02:00
"""
import requests
from requests.exceptions import RequestException
import time
import core.module
import core.widget
import core.input
import core.decorators
import util.format
2020-04-19 10:35:09 +02:00
def getfromkrak(coin, currency):
abbrev = {
"Btc": ["xbt", "XXBTZ"],
"Eth": ["eth", "XETHZ"],
"Ltc": ["ltc", "XLTCZ"],
2020-04-19 10:35:09 +02:00
}
data = abbrev.get(coin, None)
if not data:
return
epair = "{}{}".format(data[0], currency)
tickname = "{}{}".format(data[1], currency.upper())
2020-04-19 10:35:09 +02:00
try:
krakenget = requests.get(
"https://api.kraken.com/0/public/Ticker?pair=" + epair
).json()
2020-04-19 10:35:09 +02:00
except (RequestException, Exception):
return "No connection"
if not "result" in krakenget:
return "No data"
kethusdask = float(krakenget["result"][tickname]["a"][0])
kethusdbid = float(krakenget["result"][tickname]["b"][0])
return coin + ": " + str((kethusdask + kethusdbid) / 2)[0:6]
2020-04-19 10:35:09 +02:00
class Module(core.module.Module):
@core.decorators.every(minutes=30)
def __init__(self, config, theme):
super().__init__(config, theme, core.widget.Widget(self.curprice))
2020-04-19 10:35:09 +02:00
self.__curprice = ""
self.__getbtc = util.format.asbool(self.parameter("getbtc", True))
self.__geteth = util.format.asbool(self.parameter("geteth", True))
self.__getltc = util.format.asbool(self.parameter("getltc", True))
self.__getcur = self.parameter("getcur", "usd")
core.input.register(
self, button=core.input.LEFT_MOUSE, cmd="xdg-open https://cryptowat.ch/"
)
2020-04-19 10:35:09 +02:00
def curprice(self, widget):
return self.__curprice
2020-04-19 10:35:09 +02:00
def update(self):
currency = self.__getcur
btcprice, ethprice, ltcprice = "", "", ""
if self.__getbtc:
btcprice = getfromkrak("Btc", currency)
if self.__geteth:
ethprice = getfromkrak("Eth", currency)
if self.__getltc:
ltcprice = getfromkrak("Ltc", currency)
self.__curprice = (
btcprice
+ " " * (self.__getbtc * self.__geteth)
+ ethprice
+ " " * (self.__getltc * max(self.__getbtc, self.__geteth))
+ ltcprice
)
2020-04-19 10:35:09 +02:00
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4