Skip to content

Instantly share code, notes, and snippets.

@tpruvot
Last active July 23, 2021 17:57
Show Gist options
  • Save tpruvot/6e7f574f4db781497902c002ed4efab4 to your computer and use it in GitHub Desktop.
Save tpruvot/6e7f574f4db781497902c002ed4efab4 to your computer and use it in GitHub Desktop.
Hexchat cryptowatch plugin
import urllib.request
import hexchat
import json
import time
__module_name__ = "Cryptowat.ch API query"
__module_version__ = "1.65"
__module_description__ = "Answer market price when you type /kraken or trigger !kraken !bitstamp !coinbase ..."
__author__ = "tpruvot"
__url_api__ = "https://api.cryptowat.ch"
__fiat_currencies__ = ["AUD","BRL","CAD","CHF","CNY","EUR","GBP","HKD","IDR","ILS","INR","JPY","KRW","MXN","RUB","BUSD","DAI","USD","USDC","USDT"]
__cache__ = {}
_cache_ts = 0
full_name = "%s v%s by %s" % (__module_name__, __module_version__, __author__)
help_hook = "\"/kraken [FIAT]\" will send btc price in channel. \"/kraken list\" to list markets."
def price_query(words, word_eol, userdata):
exchange = words[0].lower()
if exchange[:1] in ['/','!']:
exchange = exchange[1:]
if exchange == "coinbase":
exchange = "coinbase-pro"
currency = "BTC"
base = "USD"
if exchange == "btc":
exchange = "coinbase-pro"
elif len(words) == 2:
base = words[1]
elif len(words) == 3:
currency = words[1]
base = words[2]
url = __url_api__ + "/markets/" + exchange + "/" + currency.lower() + base.lower() + "/price"
global __cache__ # no more useful
global _cache_ts
if (time.time() - _cache_ts > 3):
request = urllib.request.Request(url)
try:
response = urllib.request.urlopen(request);
except IOError as e:
if hasattr(e, 'code'):
hexchat.prnt("An error occured, HTTP code %d was returned." % e.code)
hexchat.prnt("%s" % url)
return hexchat.EAT_ALL
try:
data = json.loads(response.read().decode())
__cache__ = data['result']
_cache_ts = time.time()
except JSONDecodeError as e:
hexchat.prnt("JSON error %s at %d " % (e.msg, e.lineno))
if _cache_ts == 0:
return hexchat.EAT_ALL
result = __cache__
# reduce /say output
if exchange == "coinbase-pro":
exchange = "coinbase"
# markets/.../.../price
price_field = 'price'
if price_field in result:
price = float(result['price'])
if price > 10 and base.upper() in __fiat_currencies__:
hexchat.command("say %s %s price: %.2f %s" % (exchange, currency.upper(), price, base.upper()))
else:
hexchat.command("say %s %s price: %.8f %s" % (exchange, currency.upper(), price, base.upper()))
return hexchat.EAT_ALL
return hexchat.EAT_ALL
def chan_trigger(params, word_eol, userdata):
if params[1].find('!BTC') != -1:
price_query(params[1].rstrip().split(' '), word_eol, userdata)
if params[1].find('!kraken') != -1:
price_query(params[1].rstrip().split(' '), word_eol, userdata)
if params[1].find('!bitfinex') != -1:
price_query(params[1].rstrip().split(' '), word_eol, userdata)
if params[1].find('!binance') != -1:
price_query(params[1].rstrip().split(' '), word_eol, userdata)
if params[1].find('!bitstamp') != -1:
price_query(params[1].rstrip().split(' '), word_eol, userdata)
if params[1].find('!bittrex') != -1:
price_query(params[1].rstrip().split(' '), word_eol, userdata)
if params[1].find('!coinbase') != -1:
price_query(params[1].rstrip().split(' '), word_eol, userdata)
if params[1].find('!poloniex') != -1:
price_query(params[1].rstrip().split(' '), word_eol, userdata)
hexchat.hook_command("BTC", price_query, help=help_hook)
hexchat.hook_command("kraken", price_query, help=help_hook)
hexchat.hook_command("bitfinex", price_query, help=help_hook)
hexchat.hook_command("binance", price_query, help=help_hook)
hexchat.hook_command("bitstamp", price_query, help=help_hook)
hexchat.hook_command("bittrex", price_query, help=help_hook)
hexchat.hook_command("coinbase", price_query, help=help_hook)
hexchat.hook_command("poloniex", price_query, help=help_hook)
hexchat.hook_print("Channel Message", chan_trigger)
# hexchat.hook_print("Private Message", chan_trigger)
hexchat.hook_print("Private Message to Dialog", chan_trigger) # test to myself
# load cache
# price_query(["kraken","EUR"], "\n", 0)
hexchat.prnt(full_name + " loaded.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment