Last active
July 25, 2017 01:11
-
-
Save tpruvot/05e4661231516189c3378b2cacf9947a to your computer and use it in GitHub Desktop.
Hexchat CoinMarketCap crypto price json query in python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import urllib.request | |
import hexchat | |
import json | |
__module_name__ = "Coinmarketcap API query" | |
__module_version__ = "2.2" | |
__module_description__ = "Say BTC price when you type /BTC or trigger !BTC" | |
__author__ = "tpruvot" | |
__url_cmc__ = "http://api.coinmarketcap.com/v1/ticker" | |
__fiat_currencies__ = ["AUD","BRL","CAD","CHF","CNY","EUR","GBP","HKD","IDR","INR","JPY","KRW","MXN","RUB","USD"] | |
# Dict filled by /BTC list | |
__alts_symbols__ = { | |
"ETH": "ethereum", | |
"ZEC": "zcash", | |
} | |
full_name = "%s v%s by %s" % (__module_name__, __module_version__, __author__) | |
help_hook = "\"/BTC [FIAT]\" will send btc price in channel. \"/BTC list\" for alt currencies." | |
def price_query(words, word_eol, userdata): | |
url = __url_cmc__ + "/bitcoin/" | |
currency = "BTC" | |
base = "USD" | |
# common mistakes.. | |
if words[1] == "BTX": | |
words[1] = "bitcore" | |
if len(words) >= 2 and words[1] in ["list","search"]: | |
currency = "list" | |
url = __url_cmc__ + "/" | |
elif len(words) >= 2 and words[1] == words[1].lower(): | |
currency = words[1] | |
base = "BTC" | |
url = __url_cmc__ + "/%s/" % currency | |
if len(words) == 3: # /BTC decred USD | |
base = words[2] | |
url += "?convert=" + base | |
elif len(words) >= 2 and words[1] in __alts_symbols__.keys(): | |
currency = __alts_symbols__[words[1]] | |
base = "BTC" | |
url = __url_cmc__ + "/%s/" % currency | |
if len(words) == 3: # /BTC ETH USD | |
base = words[2] | |
url += "?convert=" + base | |
elif len(words) == 2: | |
base = words[1] | |
url += "?convert=" + base | |
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) | |
return hexchat.EAT_ALL | |
try: | |
data = json.loads(response.read().decode()) | |
except JSONDecodeError as e: | |
hexchat.prnt("JSON error %s at %d " % (e.msg, e.lineno)) | |
return hexchat.EAT_ALL | |
# list (or search) other available currencies | |
if currency == "list": | |
ids = "" | |
for obj in data: | |
if len(words) < 3 or obj["id"].find(words[2]) >= 0 or obj["symbol"].find(words[2]) >= 0: | |
ids += obj["id"] + " " | |
# fill symbol=>id cache dict | |
symbol = obj["symbol"] | |
__alts_symbols__[symbol] = obj["id"] | |
if ids != "": | |
hexchat.prnt("currencies: " + ids) | |
return hexchat.EAT_ALL | |
obj = data[0] | |
price_field = "price_" + base.lower() | |
pct24 = pct7d = 0 | |
if price_field in obj: | |
price = float(obj[price_field]) | |
if obj["percent_change_24h"]: | |
pct24 = float(obj["percent_change_24h"]) | |
if obj["percent_change_7d"]: | |
pct7d = float(obj["percent_change_7d"]) | |
if base in __fiat_currencies__: | |
hexchat.command("say %s price: %.2f %s, 24h %+.1f%%, week %+.1f%%" % (currency, price, base, pct24, pct7d)) | |
else: # percent changes are for the market cap in FIAT, not the BTC price | |
hexchat.command("say %s price: %.8f %s" % (currency, price, base)) | |
return hexchat.EAT_ALL | |
def chan_trigger(params, word_eol, userdata): | |
if params[1].find('!BTC') != -1: | |
price_query(params[1].split(' '), word_eol, userdata) | |
hexchat.hook_command("BTC", 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 symbol cache, can take some time.. | |
price_query(["BTC","list"], "\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