-
Notifications
You must be signed in to change notification settings - Fork 2
/
ccurrency
executable file
·101 lines (88 loc) · 3.16 KB
/
ccurrency
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/usr/bin/env python3
import datetime
import argparse
import requests
import json
import sys
# Get coin/fiat from arguments, set to btc/usd as default
parser = argparse.ArgumentParser(description="Set Coin/Fiat pair")
parser.add_argument('-c', '--coin',
action='store',
default='BTC')
parser.add_argument('-f', '--fiat',
action='store',
default='USD')
parser.add_argument('-m', '--movement',
dest='movement',
action='store_true')
parser.add_argument('-p', '--polybar',
dest='polybar',
action='store_true')
parser.set_defaults(movement=False)
parser.set_defaults(polybar=False)
args = parser.parse_args()
coin = args.coin.upper()
fiat = args.fiat.upper()
hist_time = datetime.datetime.now() - datetime.timedelta(days=30)
hist_time = int(hist_time.timestamp())
lookup = {
"GBP": "£",
"EUR": "€",
"USD": "$",
"BTC": "",
"LTC": "Ł",
"ETH": "Ξ",
}
curr_url = 'https://min-api.cryptocompare.com/data/pricemultifull?fsyms={}&tsyms={}'.format(coin, fiat)
hist_url = 'https://min-api.cryptocompare.com/data/pricehistorical?fsym={}&tsyms={}&ts={}'.format(coin, fiat, hist_time)
curr_r = requests.get(curr_url)
curr_j = curr_r.json()
hist_r = requests.get(hist_url)
hist_j = hist_r.json()
price = curr_j['RAW'][coin][fiat]['PRICE']
change = round(curr_j['RAW'][coin][fiat]['CHANGE24HOUR'], 2)
change_pct = round(curr_j['RAW'][coin][fiat]['CHANGEPCT24HOUR'], 2)
hist_price = round(hist_j[coin][fiat], 2)
diff_price = round(price - hist_price, 2)
diff_pct = round((diff_price/hist_price)*100, 2)
# Shift the minus symbol to be before the fiat symbol if negative so it looks
# better, add coloured ↑↓s for market movement.
if change > 0:
change = '{}{}'.format(lookup[fiat], change)
if args.polybar:
change_direction = '%{F#00ff00}↑%{F-}'
else:
change_direction = '↑'
else:
change = str(change)
change = '-{}{}'.format(lookup[fiat], change[1:])
if args.polybar:
change_direction = '%{F#f00}↓%{F-}'
else:
change_direction = '↓'
if diff_price > 0:
diff_price = '{}{}'.format(lookup[fiat], diff_price)
if args.polybar:
diff_direction = '%{F#00ff00}↑%{F-}'
else:
diff_direction = '↑'
else:
str(diff_price)
diff_price = '-{}{}'.format(lookup[fiat], diff_price)
if args.polybar:
diff_direction = '%{F#f00}↓%{F-}'
else:
diff_direction = '↓'
if args.movement:
output = '{} {}{} {} {}%{} {} {}%{}'.format(lookup[coin],
lookup[fiat],
round(price, 2),
change,
change_pct,
change_direction,
diff_price,
diff_pct,
diff_direction)
else:
output = '{} {}{}'.format(lookup[coin], lookup[fiat], round(price, 2))
print(output)