-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathgeyserfund.py
284 lines (255 loc) Β· 11.4 KB
/
geyserfund.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#! /usr/bin/env python3
from PIL import Image, ImageColor, ImageDraw
from os.path import exists
from vicariouspanel import NodeyezPanel
import hashlib
import os
import qrcode
import random
import shutil
import sys
import vicariousnetwork
import vicarioustext
class GeyserFundPanel(NodeyezPanel):
def __init__(self):
"""Instantiates a new Geyser Fund panel"""
# Define which additional attributes we have
self.configAttributes = {
# legacy key name mappings
"colorBackground": "backgroundColor",
"colorTextFG": "textColor",
"sleepInterval": "interval",
# panel specific key names
"qrCodeEnabled": "qrCodeEnabled",
"qrCodePixelSize": "qrCodePixelSize",
"saveUniqueFile": "saveUniqueFile",
"tagLabelsEnabled": "tagLabelsEnabled",
"tagRestriction": "tagRestriction",
"useTor": "useTor",
}
# Define our defaults (all panel specific key names should be listed)
self._defaultattr("interval", 300)
self._defaultattr("qrCodeEnabled", True)
self._defaultattr("qrCodePixelSize", 2)
self._defaultattr("saveUniqueFile", False)
self._defaultattr("tagLabelsEnabled", True)
self._defaultattr("tagRestriction", "bitcoin* nostr open-source")
self._defaultattr("useTor", False)
self._defaultattr("watermarkAnchor", "bottom")
# Initialize
super().__init__(name="geyserfund")
self.geyserprojects = {}
self.attributionColor = "#20ECC7"
def getTagId(self, tags):
if len(tags) == 0: return 1
anyTag = False | (self.tagRestriction == None)
anyTag = anyTag | (self.tagRestriction == "")
anyTag = anyTag | (self.tagRestriction == "*")
anyTag = anyTag | (self.tagRestriction == "any")
if not anyTag:
tagRestrictions = self.tagRestriction.split()
tagRestrictionIds = []
for tagRestriction in tagRestrictions:
tagRestrictionIds.append([])
self.log(f"looking for tags matching restriction rule: {tagRestriction}")
for tag in tags:
if "id" not in tag: continue
if "label" not in tag: continue
tagId = tag["id"]
tagLabel = tag["label"]
if tagRestriction == tagLabel:
self.log(f"-found tag matching restriction list: {tagLabel} ({tagId})")
tagRestrictionIds[-1].append(tagId)
if tagRestriction.endswith("*"):
tagRestrictionPrefix = tagRestriction[:-1]
if tagLabel.startswith(tagRestrictionPrefix):
self.log(f"-found tag matching restriction list: {tagLabel} ({tagId})")
tagRestrictionIds[-1].append(tagId)
tagSet = random.choice(tagRestrictionIds)
tagId = random.choice(tagSet)
self.log(f"chose tag with id {tagId} from the restriction list")
return tagId
# any tag
tag = random.choice(tags)
if "id" not in tag: return 1
tagId = tag["id"]
tagLabel = tag["label"]
self.log(f"chose tag {tagLabel} with id {tagId} from the tag list")
return tagId
def getProject(self):
ok = False
attemptsRemaining = 10
project = None
projectName = "unknown"
projectTitle = "unknown"
while not ok and attemptsRemaining > 0:
project = random.choice(self.geyserprojects)
if "project" in project: project = project["project"]
if "name" in project: projectName = project["name"]
if "title" in project: projectTitle = project["title"]
if "status" in project:
projectStatus = project["status"]
if projectStatus != "inactive":
ok = True
self.log(f"using project: {projectName} ({projectTitle}) - {projectStatus}")
else:
self.log(f"skipping inactive project: {projectName} ({projectTitle})")
else:
self.log(f"skipping project {projectName} ({projectTitle}) as it has no status")
attemptsRemaining -= 1
return project
def fetchData(self):
"""Fetches all the data needed for this panel"""
tags = vicariousnetwork.getgeysertags(useTor=self.useTor)
tagId = self.getTagId(tags)
projects = vicariousnetwork.getgeyserprojects(useTor=self.useTor, tagId=tagId)
self.geyserprojects = projects
def loadAndPasteImage(self, sourceFile):
if not exists(sourceFile): return
sourceImage=Image.open(sourceFile)
sourceImage=sourceImage.convert("RGBA")
sourceImage = self.resizeImageToInset(sourceImage)
sourceWidth=int(sourceImage.getbbox()[2])
sourceHeight=int(sourceImage.getbbox()[3])
sourceLeft = (self.getInsetWidth() - sourceWidth) // 2
sourceTop = self.getInsetTop() + ((self.getInsetHeight() - sourceHeight) // 2)
self.canvas.paste(sourceImage, (sourceLeft,sourceTop))
sourceImage.close()
def renderImage(self, project):
imagefile = None
imageurl = None
if "image" in project: imageurl = project["image"]
if imageurl is None and "thumbnailImage" in project: imageurl = project["thumbnailImage"]
if imageurl is not None:
self.log(f"image url: {imageurl}")
projectName = project["name"]
if imageurl.startswith("https://storage.googleapis.com/geyser-images-distribution"):
imagefile = f"{self.dataDirectory}{self.name}/images/{projectName}"
if not exists(imagefile.rpartition("/")[0]):
os.makedirs(imagefile.rpartition("/")[0])
if not exists(imagefile):
vicariousnetwork.getandsavefile(useTor=self.useTor, url=imageurl, savetofile=imagefile, headers=None)
self.loadAndPasteImage(imagefile)
else:
self.log(f"project has no image")
def renderDescription(self, project):
overlay = Image.new(mode="RGBA", size=(self.width, self.height), color=(255,255,255,0))
overlaydraw = ImageDraw.Draw(overlay)
overlayBG = (64,64,64,128)
texty = self.getInsetTop()
textystop = self.height - self.getFooterHeight()
description = project["shortDescription"]
fontsize = int(self.height * (18/320))
rwords = description.split()
rpart, rwords = vicarioustext.getmaxtextforwidth(overlaydraw, rwords, self.width, fontsize)
while len(rpart) > 0:
_,sh,_ = vicarioustext.gettextdimensions(overlaydraw, rpart, fontsize)
textyold = texty
texty += sh
if texty > textystop: break
overlaydraw.rectangle(xy=((0,textyold),(self.width,texty)),fill=overlayBG)
vicarioustext.drawbottomlefttext(overlaydraw, rpart, fontsize, 0, texty, ImageColor.getrgb(self.textColor), False)
rpart, rwords = vicarioustext.getmaxtextforwidth(overlaydraw, rwords, self.width, fontsize)
self.canvas.alpha_composite(overlay)
overlay.close()
def renderQRCode(self,project):
if not self.qrCodeEnabled: return
projectName = project["name"]
projecturl = f"https://geyser.fund/project/{projectName}"
qr = qrcode.QRCode(box_size=self.qrCodePixelSize)
qr.add_data(projecturl)
qr.make()
img = qr.make_image()
s = img.size[1]
# right side
pos = (self.getInsetWidth()-s,self.getInsetTop()+self.getInsetHeight()-s)
# left side
pos = (0,self.getInsetTop()+self.getInsetHeight()-s)
self.canvas.paste(img, pos)
img.close()
def renderAttribution(self):
fontsize = int(self.height * 12/320)
t = "Data from Geyser.Fund"
vicarioustext.drawbottomlefttext(self.draw, t, fontsize, 0, self.height+4, ImageColor.getrgb(self.attributionColor))
def getColorForText(self, t):
m = hashlib.sha256(t.encode('UTF-8')).hexdigest()
c = ""
while len(m) > 0 and len(c) < 6:
p = m[0]
if p in ['0','1','2','3','4','5','6','7']:
c = f"{c}{p}{p}"
m = m[1:]
while len(c) < 6:
c = f"{c}21"
c = f"#{c}"
return ImageColor.getrgb(c)
def renderTags(self, project):
if not self.tagLabelsEnabled: return
fontsize = int(self.height * (12/320))
ib = self.getInsetBottom()
y2 = ib
padleftright = 10
padtopbottom = 5
radius=5.0
tagCount = 0
if "tags" in project:
for tag in project["tags"]:
tagCount += 1
tagValue = tag["label"]
self.log(f"Adding tag annotation for: {tagValue}")
sw,sh,_ = vicarioustext.gettextdimensions(self.draw, tagValue, fontsize)
y1 = y2 - (sh + (padtopbottom))
tagBG = self.getColorForText(tagValue)
labelWidth = sw + (padleftright * 2)
self.draw.rounded_rectangle(xy=((self.width - labelWidth, y1),(self.width,y2 - (padtopbottom//2))),radius=radius,fill=tagBG)
centerx = self.width - (labelWidth//2)
centery = y1 + ((y2 - y1) // 2)
vicarioustext.drawcenteredtext(self.draw, tagValue, fontsize, centerx, centery)
y2 = y1
def copyAsUniqueFile(self, projectName):
rFrom = f"/{self.name}."
rTo = f"/{self.name}-{projectName}."
panelFileName = super().getOutputFile()
projectFileName = panelFileName.replace(rFrom, rTo)
self.log(f"Saving copy to {projectFileName}")
shutil.copyfile(panelFileName, projectFileName)
def run(self):
# Bail if we have no projects
if len(self.geyserprojects) == 0:
super()._markAsRan()
return
# Pick random project and set the header
project = self.getProject()
if project is None:
super()._markAsRan()
return
self.log(project)
self.headerText = project["title"]
super().startImage()
# Render background image for project if exists
self.renderImage(project)
self.renderDescription(project)
self.renderQRCode(project)
self.renderTags(project)
self.renderAttribution()
super().finishImage()
if self.saveUniqueFile:
self.copyAsUniqueFile(project["name"])
# --------------------------------------------------------------------------------------
# Entry point if running this script directly
# --------------------------------------------------------------------------------------
if __name__ == '__main__':
p = GeyserFundPanel()
# If arguments were passed in, treat as a single run
if len(sys.argv) > 1:
if sys.argv[1] in ['-h','--help']:
print(f"Show info about a randomly chosen project from Geyser.Fund")
print(f"Usage:")
print(f"1) Call without arguments to run continuously using the configuration or defaults")
print(f"2) Call with an argument other then -h or --help to run once and exit")
else:
p.fetchData()
p.run()
exit(0)
# Continuous run
p.runContinuous()