forked from Optixal/i3blocks-crypto
-
Notifications
You must be signed in to change notification settings - Fork 1
/
crypto
executable file
·51 lines (40 loc) · 1.61 KB
/
crypto
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
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env python3
# coding=utf-8
import json
from requests import Request, Session
from requests.exceptions import ConnectionError, Timeout, TooManyRedirects
import os
import sys
API_KEY = "YOUR-COINMARKETCAP-API-KEY-HERE"
PRICE_CHANGE_PERIOD = '1h' # Available: '1h', '24h', '7d'
PRICE_CHANGE_URGENT_PERCENT = 10
API_URL = 'https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest'
coin = os.environ.get('BLOCK_INSTANCE', 'bitcoin')
parameters = {
'slug': coin
}
headers = {
'Accepts': 'application/json',
'X-CMC_PRO_API_KEY': API_KEY,
}
session = Session()
session.headers.update(headers)
r = session.get(API_URL, params=parameters)
data = json.loads(r.text)['data']
data = data[list(data.keys())[0]]
base = 'USD'
quote = data['quote'][base]
price = float(quote['price'])
# Only USD as base currently supported by free version of Coinmarketcap API, AFAIK
if price > 100: precision = 0
elif price > 0.1: precision = 2
else: precision = 6
percentChange = float(quote['percent_change_' + PRICE_CHANGE_PERIOD])
percentChangeFormat = '<span color="{}">{}{:.2f}%</span>'
if percentChange > 0: percentChangeInfo = percentChangeFormat.format('#3BB92D', '🠕 ', percentChange)
elif percentChange == 0: percentChangeInfo = percentChangeFormat.format('#CCCCCC', '', percentChange)
else: percentChangeInfo = percentChangeFormat.format('#F7555E', '🠗 ', percentChange)
print(('{} {:.' + str(precision) + 'f} {}').format(data['symbol'], price, percentChangeInfo)) # Full Text
print(('{} {:.' + str(precision) + 'f}').format(data['symbol'], price)) # Short Text
if percentChange > PRICE_CHANGE_URGENT_PERCENT:
exit(33)