Skip to content

Commit d9098e0

Browse files
committed
Change CMC api calls to use 1 conversion by default
1 parent 6f1509d commit d9098e0

File tree

2 files changed

+58
-25
lines changed

2 files changed

+58
-25
lines changed

plugins/cryptocurrency.py

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,7 @@ def __init__(
470470
api_url: str = "https://pro-api.coinmarketcap.com/v1/",
471471
) -> None:
472472
self.api_key = api_key
473+
self.show_btc = False
473474
self.api_url = URL(api_url)
474475
self.cache = Cache()
475476

@@ -491,10 +492,13 @@ def get_quote(self, symbol: str, currency: str = "USD") -> CryptoCurrency:
491492
if currency not in self.get_fiat_currency_map().symbols:
492493
raise UnknownFiatCurrencyError(currency)
493494

495+
if self.show_btc:
496+
convert = "{},BTC".format(currency)
497+
else:
498+
convert = currency
499+
494500
data = self.request(
495-
"cryptocurrency/quotes/latest",
496-
symbol=symbol.upper(),
497-
convert="{},BTC".format(currency),
501+
"cryptocurrency/quotes/latest", symbol=symbol.upper(), convert=convert,
498502
).data.cast_to(QuoteRequestResponse)
499503
_, out = data.data.popitem()
500504
return out
@@ -534,10 +538,20 @@ def check(self, response: APIResponse) -> None:
534538
api = CoinMarketCapAPI()
535539

536540

541+
def get_plugin_config(conf, name, default):
542+
try:
543+
return conf["plugins"]["cryptocurrency"][name]
544+
except LookupError:
545+
return default
546+
547+
537548
@hook.onload
538549
def init_api(bot):
539550
api.api_key = bot.config.get_api_key("coinmarketcap")
540551

552+
# Enabling this requires a paid CoinMarketCap API plan
553+
api.show_btc = get_plugin_config(bot.config, "show_btc", False)
554+
541555

542556
class Alias:
543557
__slots__ = ("name", "cmds")
@@ -597,7 +611,6 @@ def crypto_command(text, event):
597611
raise
598612

599613
quote = data.quote[currency]
600-
btc_quote = data.quote["BTC"]
601614
change = quote.percent_change_24h
602615
if change > 0:
603616
change_str = "$(dark_green)+{}%$(clear)".format(change)
@@ -608,15 +621,15 @@ def crypto_command(text, event):
608621

609622
currency_sign = api.get_currency_sign(currency)
610623

624+
if api.show_btc:
625+
btc_quote = data.quote["BTC"]
626+
btc = "- {:,.7f} BTC ".format(btc_quote.price)
627+
else:
628+
btc = ""
629+
611630
return colors.parse(
612-
"{} ({}) // $(orange){}{:,.2f}$(clear) {} - {:,.7f} BTC // {} change".format(
613-
data.symbol,
614-
data.slug,
615-
currency_sign,
616-
quote.price,
617-
currency,
618-
btc_quote.price,
619-
change_str,
631+
("{} ({}) // $(orange){}{:,.2f}$(clear) {} " + btc + "// {} change").format(
632+
data.symbol, data.slug, currency_sign, quote.price, currency, change_str,
620633
)
621634
)
622635

tests/plugin_tests/test_cryptocurrency.py

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,13 @@ def init_response(
5454
error_msg=None,
5555
check_api_key=False,
5656
pct_change=18.9,
57+
show_btc=True,
5758
):
5859
if check_api_key:
5960
cryptocurrency.init_api(bot.get())
6061

6162
cryptocurrency.api.cache.clear()
63+
cryptocurrency.api.show_btc = show_btc
6264
now = datetime.now()
6365

6466
iso_fmt = "%Y-%m-%dT%H:%M:%S.%f%z"
@@ -122,15 +124,6 @@ def init_response(
122124
"last_updated": (now - timedelta(hours=1)).strftime(iso_fmt),
123125
"tags": [],
124126
"quote": {
125-
"BTC": {
126-
"price": 2,
127-
"volume_24h": 5,
128-
"market_cap": 97,
129-
"percent_change_1h": 12.5,
130-
"percent_change_24h": 17.4,
131-
"percent_change_7d": 54.1,
132-
"last_updated": (now - timedelta(minutes=6)).strftime(iso_fmt),
133-
},
134127
"USD": {
135128
"price": 50000000000,
136129
"volume_24h": 20,
@@ -143,6 +136,17 @@ def init_response(
143136
},
144137
},
145138
}
139+
if show_btc:
140+
response_data["1"]["quote"]["BTC"] = {
141+
"price": 2,
142+
"volume_24h": 5,
143+
"market_cap": 97,
144+
"percent_change_1h": 12.5,
145+
"percent_change_24h": 17.4,
146+
"percent_change_7d": 54.1,
147+
"last_updated": (now - timedelta(minutes=6)).strftime(iso_fmt),
148+
}
149+
146150
data = {
147151
"status": {
148152
"timestamp": now.strftime(iso_fmt),
@@ -158,14 +162,16 @@ def init_response(
158162
mock_requests.add(
159163
MatchAPIKey(
160164
"GET",
161-
"https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest?symbol=BTC&convert=USD%2CBTC",
165+
"https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest?symbol=BTC&convert=USD"
166+
+ ("%2CBTC" if show_btc else ""),
162167
api_key="APIKEY" if check_api_key else None,
163168
json=data,
164169
)
165170
)
166171

167172

168173
def test_api(mock_requests, mock_api_keys):
174+
bot.config["plugins"] = {}
169175
init_response(mock_requests, check_api_key=True)
170176

171177
result = cryptocurrency.api.get_quote("BTC", "USD")
@@ -334,9 +340,23 @@ def test_btc_alias_no_change(mock_requests):
334340
res = _run_alias()
335341

336342
assert res == [
337-
HookResult(
338-
return_type="return",
339-
value="BTC (bitcoin) // \x0307$50,000,000,000.00\x0f USD - 2.0000000 BTC // 0% change",
343+
(
344+
"return",
345+
"BTC (bitcoin) // \x0307$50,000,000,000.00\x0f USD - 2.0000000 BTC // 0% change",
346+
)
347+
]
348+
349+
350+
def test_no_show_btc(mock_requests):
351+
show_btc = cryptocurrency.get_plugin_config({}, "show_btc", False)
352+
init_response(mock_requests, show_btc=show_btc)
353+
354+
res = _run_alias()
355+
356+
assert res == [
357+
(
358+
"return",
359+
"BTC (bitcoin) // \x0307$50,000,000,000.00\x0f USD // \x0303+18.9%\x0f change",
340360
)
341361
]
342362

0 commit comments

Comments
 (0)