-
Notifications
You must be signed in to change notification settings - Fork 4
/
market_prices.py
41 lines (36 loc) · 1.48 KB
/
market_prices.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import asyncio
import json
import urllib.request
import ssl
import iterm2
async def main(connection):
component = iterm2.StatusBarComponent(
short_description='Market Prices',
detailed_description='Displays current prices of BTC, ETH, BCH and LTC',
exemplar='BTC $10,321.50, ETH $321.42, BCH $471.03, LTC $104.53',
update_cadence=30,
identifier='schnogz.iterm-crypto-components.bch-price',
knobs=[],
)
@iterm2.StatusBarRPC
async def market_prices_coroutine(knobs):
price_url = 'https://api.blockchair.com/stats'
try:
context = ssl._create_unverified_context()
request = urllib.request.Request(
price_url,
headers={},
)
resp = json.loads(
urllib.request.urlopen(request, context=context).read().decode()
)['data']
btc_price = format(round(resp['bitcoin']['data']['market_price_usd'], 2), ',')
eth_price = format(round(resp['ethereum']['data']['market_price_usd'], 2), ',')
bch_price = format(round(resp['bitcoin-cash']['data']['market_price_usd'], 2), ',')
ltc_price = format(round(resp['litecoin']['data']['market_price_usd'], 2), ',')
except:
raise
else:
return f'BTC ${btc_price} ETH ${eth_price} BCH ${bch_price} LTC ${ltc_price}'
await component.async_register(connection, market_prices_coroutine)
iterm2.run_forever(main)