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.
|
2020-05-08 20:58:35 +02:00
|
|
|
|
|
|
|
contributed by `Ryunaq <https://github.com/Ryunaq>`_ - many thanks!
|
2020-04-19 10:35:09 +02:00
|
|
|
"""
|
|
|
|
|
|
|
|
import requests
|
|
|
|
from requests.exceptions import RequestException
|
2020-04-19 10:38:09 +02:00
|
|
|
import time
|
|
|
|
|
|
|
|
import core.module
|
|
|
|
import core.widget
|
|
|
|
import core.input
|
|
|
|
import core.decorators
|
|
|
|
|
|
|
|
import util.format
|
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
|
2020-04-19 10:35:09 +02:00
|
|
|
def getfromkrak(coin, currency):
|
|
|
|
abbrev = {
|
2020-05-03 11:15:52 +02:00
|
|
|
"Btc": ["xbt", "XXBTZ"],
|
|
|
|
"Eth": ["eth", "XETHZ"],
|
|
|
|
"Ltc": ["ltc", "XLTCZ"],
|
2020-04-19 10:35:09 +02:00
|
|
|
}
|
|
|
|
data = abbrev.get(coin, None)
|
2020-05-03 11:15:52 +02:00
|
|
|
if not data:
|
|
|
|
return
|
|
|
|
epair = "{}{}".format(data[0], currency)
|
|
|
|
tickname = "{}{}".format(data[1], currency.upper())
|
2020-04-19 10:35:09 +02:00
|
|
|
try:
|
2020-05-03 11:15:52 +02:00
|
|
|
krakenget = requests.get(
|
|
|
|
"https://api.kraken.com/0/public/Ticker?pair=" + epair
|
|
|
|
).json()
|
2020-04-19 10:35:09 +02:00
|
|
|
except (RequestException, Exception):
|
2020-05-03 11:15:52 +02:00
|
|
|
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
|
|
|
|
2020-04-19 10:38:09 +02:00
|
|
|
class Module(core.module.Module):
|
|
|
|
@core.decorators.every(minutes=30)
|
2020-04-26 16:39:24 +02:00
|
|
|
def __init__(self, config, theme):
|
|
|
|
super().__init__(config, theme, core.widget.Widget(self.curprice))
|
2020-04-19 10:35:09 +02:00
|
|
|
|
2020-05-03 11:15:52 +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):
|
2020-04-19 10:38:09 +02:00
|
|
|
return self.__curprice
|
2020-04-19 10:35:09 +02:00
|
|
|
|
2020-04-19 10:38:09 +02:00
|
|
|
def update(self):
|
|
|
|
currency = self.__getcur
|
2020-05-03 11:15:52 +02:00
|
|
|
btcprice, ethprice, ltcprice = "", "", ""
|
2020-04-19 10:38:09 +02:00
|
|
|
if self.__getbtc:
|
2020-05-03 11:15:52 +02:00
|
|
|
btcprice = getfromkrak("Btc", currency)
|
2020-04-19 10:38:09 +02:00
|
|
|
if self.__geteth:
|
2020-05-03 11:15:52 +02:00
|
|
|
ethprice = getfromkrak("Eth", currency)
|
2020-04-19 10:38:09 +02:00
|
|
|
if self.__getltc:
|
2020-05-03 11:15:52 +02:00
|
|
|
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
|