-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathfiatprice.py
120 lines (102 loc) Β· 4.97 KB
/
fiatprice.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
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#! /usr/bin/env python3
from PIL import Image, ImageColor, ImageDraw
from vicariouspanel import NodeyezPanel
import sys
import vicariousnetwork
import vicarioustext
class FiatPricePanel(NodeyezPanel):
def __init__(self):
"""Instantiates a new Fiat Price panel"""
# Define which additional attributes we have
self.configAttributes = {
# legacy key name mappings
"colorBackground": "backgroundColor",
"colorBisq": "attributionColor",
"colorHeader": "headerColor",
"colorPriceDown": "priceDownColor",
"colorPriceShadow": "priceShadowColor",
"colorPriceUp": "priceUpColor",
"colorTextFG": "textColor",
"priceurl": "priceUrl",
"sleepInterval": "interval",
"showBigText": "bigTextEnabled",
"showBigTextOnTop": "bigTextOnTopEnabled",
# panel specific key names
"attributionColor": "attributionColor",
"bigTextEnabled": "bigTextEnabled",
"bigTextOnTopEnabled": "bigTextOnTopEnabled",
"fiatUnit": "fiatUnit",
"headerColor": "headerColor",
"priceDownColor": "priceDownColor",
"priceShadowColor": "priceShadowColor",
"priceUpColor": "priceUpColor",
"priceUrl": "priceUrl",
"useTor": "useTor",
}
# Define our defaults (all panel specific key names should be listed)
self._defaultattr("attributionColor", "#1bd8f4")
self._defaultattr("bigTextEnabled", True)
self._defaultattr("bigTextOnTopEnabled", True)
self._defaultattr("fiatUnit", "USD")
self._defaultattr("headerColor", "#ffffff")
self._defaultattr("interval", 300)
self._defaultattr("priceColor", "#40ff40")
self._defaultattr("priceDownColor", "#ff4040")
self._defaultattr("priceHigh", 0)
self._defaultattr("priceLast", 0)
self._defaultattr("priceLow", 0)
self._defaultattr("priceShadowColor", "#ffffff7f")
self._defaultattr("priceUpColor", "#40ff40")
self._defaultattr("priceUrl", "https://mempool.space/api/v1/prices")
self._defaultattr("useTor", True)
self._defaultattr("watermarkAnchor", "bottom")
# Initialize
super().__init__(name="fiatprice")
def fetchData(self):
"""Fetches all the data needed for this panel"""
priceBefore = self.priceLast
self.priceLast, self.priceHigh, self.priceLow, fiatkeyname = vicariousnetwork.getpriceinfo(self.useTor, self.priceUrl, self.priceLast, self.priceHigh, self.priceLow, self.fiatUnit)
self.priceChange = self.priceLast - priceBefore
if self.priceChange < 0:
self.priceColor = self.priceDownColor
if self.priceChange > 0:
self.priceColor = self.priceUpColor
def run(self):
self.headerText = f"{self.fiatUnit} Price of Bitcoin"
super().startImage()
alphaLayer = Image.new(mode="RGBA", size=(self.canvas.width, self.canvas.height), color=(0,0,0,0))
alphaDraw = ImageDraw.Draw(alphaLayer)
priceText = "$" + str(self.priceLast)
x = self.width//2
y = self.height//2
smallPriceSize = int(self.height * 20/320)
attributionSize = int(self.height * 14/320)
if self.bigTextEnabled:
fs, _, _ = vicarioustext.getmaxfontsize(self.draw, priceText, self.width, self.height)
vicarioustext.drawcenteredtext((self.draw if self.bigTextOnTopEnabled else alphaDraw), priceText, fs, x, y, ImageColor.getrgb(self.priceShadowColor))
vicarioustext.drawcenteredtext((self.draw if self.bigTextOnTopEnabled else alphaDraw), priceText, fs, x-2, y-2, ImageColor.getrgb(self.priceColor))
else:
vicarioustext.drawcenteredtext(self.draw, f"Price: {priceText}", smallPriceSize, x, y, ImageColor.getrgb(self.priceColor))
# attribution
vicarioustext.drawbottomlefttext(self.draw, "Data from mempool", attributionSize, 0, self.height, ImageColor.getrgb(self.attributionColor))
self.canvas.alpha_composite(alphaLayer)
alphaLayer.close()
super().finishImage()
# --------------------------------------------------------------------------------------
# Entry point if running this script directly
# --------------------------------------------------------------------------------------
if __name__ == '__main__':
p = FiatPricePanel()
# If arguments were passed in, treat as a single run
if len(sys.argv) > 1:
if sys.argv[1] in ['-h','--help']:
print(f"Retrieves the market rate of BTC from Bisq and renders the price in dollars")
print(f"Usage:")
print(f"1) Call without arguments to run continuously using the configuration or defaults")
print(f"2) Pass an argument other than -h or --help to run once and exit")
else:
p.fetchData()
p.run()
exit(0)
# Continuous run
p.runContinuous()