Repository navigation
Expand file tree
/
Copy pathraretoshi.py
More file actions
376 lines (343 loc) Β· 16.5 KB
/
Copy pathraretoshi.py
File metadata and controls
376 lines (343 loc) Β· 16.5 KB
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
#! /usr/bin/env python3
from PIL import Image, ImageColor, ImageDraw, ImageFile
from os.path import exists
from vicariouspanel import NodeyezPanel
import json
import os
import qrcode
import random
import re
import sys
import time
import vicariousbitcoin
import vicariousnetwork
import vicarioustext
class RaretoshiPanel(NodeyezPanel):
def __init__(self):
"""Instantiates a new Raretoshi panel"""
# Define which additional attributes we have
self.configAttributes = {
# legacy key name mappings
"colorBackground": "backgroundColor",
"colorTextFG": "textColor",
"qrCodeSize": "qrCodePizelSize",
"overlayTextEnabled": "overlayEnabled",
"overlayTextColorBG": "overlayBackgroundColor",
"overlayTextColorFG": "overlayTextColor",
"sleepInterval": "interval",
# panel specific key names
"overlayBackgroundColor": "overlayBackgroundColor",
"overlayEnabled": "overlayEnabled",
"overlayTextColor": "overlayTextColor",
"qrCodeEnabled": "qrCodeEnabled",
"qrCodePixelSize": "qrCodePixelSize",
"randomUserEnabled": "randomUserEnabled",
"randomUserInterval": "randomUserInterval",
"stretchEdgeEnabled": "stretchEdgeEnabled",
"stretchEdgeSpacing": "stretchEdgeSpacing",
"raretoshiUser": "raretoshiUser",
"useTor": "useTor",
"userInfoInterval": "userInfoInterval",
}
# Define our defaults (all panel specific key names should be listed)
self._defaultattr("footerEnabled", False)
self._defaultattr("headerText", "Raretoshi Image")
self._defaultattr("interval", 120)
self._defaultattr("overlayBackgroundColor", "#00000080")
self._defaultattr("overlayEnabled", True)
self._defaultattr("overlayTextColor", "#ffffff")
self._defaultattr("qrCodeEnabled", True)
self._defaultattr("qrCodePixelSize", 2)
self._defaultattr("randomUserEnabled", True)
self._defaultattr("randomUserInterval", 300)
self._defaultattr("raretoshiUser", "valeriyageorg")
self._defaultattr("stretchEdgeEnabled", True)
self._defaultattr("stretchEdgeSpacing", 30)
self._defaultattr("useTor", True)
self._defaultattr("userInfoInterval", 3600)
self._defaultattr("watermarkEnabled", False)
# Initialize
super().__init__(name="raretoshi")
# Make directories as needed
d = f"{self.dataDirectory}ipfs/"
if not exists(d): os.makedirs(d)
self.ipfsDataDirectory = d
d = f"{self.dataDirectory}raretoshi/"
if not exists(d): os.makedirs(d)
self.raretoshiDataDirectory = d
# Set starting state
self.collectionIndex=-1
self.userInfoLast=0
self.userInfo={"subject":{"holdings":[]}}
self.randomUserLast=0
self.loadUsernames()
# Support truncated images
ImageFile.LOAD_TRUNCATED_IMAGES=True
def getValuesForKeyname(self, o, keyName="username"):
values = []
for key, value in o.items():
if key == keyName:
if type(value) is str and value not in values: values.append(value)
else:
if type(value) is list:
for listItem in value:
if type(listItem) is str and listItem not in values: values.append(listItem)
if type(listItem) is dict:
dictValues = self.getValuesForKeyname(listItem, keyName)
for dictValue in dictValues:
if dictValue not in values: values.append(dictValue)
if type(value) is dict:
dictValues = self.getValuesForKeyname(value, keyName)
for dictValue in dictValues:
if dictValue not in values: values.append(dictValue)
return values
def loadUsernames(self):
self.usernames=vicariousbitcoin.loadJSONData(f"{self.raretoshiDataDirectory}/raretoshi-usernames.json", [self.raretoshiUser])
def saveUsernames(self):
with open(f"{self.raretoshiDataDirectory}/raretoshi-usernames.json", "w") as f:
json.dump(self.usernames, f)
def setupRaretoshiUser(self):
now = int(time.time())
if self.randomUserInterval + self.randomUserLast > now: return
self.randomUserLast = now
usernames = self.getValuesForKeyname(self.userInfo, "username")
self.usernames = list(set(self.usernames + usernames))
if len(self.usernames) > 0: self.raretoshiUser = random.choice(self.usernames)
self.saveUsernames()
def setupHoldingInfo(self):
self.holding = None
allowedFileTypes = ['image/png','image/jpeg']
holdingsCount = len(self.userInfo["subject"]["holdings"])
holdingIndex = self.collectionIndex
fileType = ""
# quick bail if no holdings
if holdingsCount == 0:
self.log(f"User {self.raretoshiUser} has no holdings on raretoshi")
return
# check if requested index exists and suitable
if holdingIndex > -1 and holdingIndex < holdingsCount:
holding = self.userInfo["subject"]["holdings"][holdingIndex]
if self.isValidHoldingFormat(holding):
fileType = holding["filetype"]
if fileType not in allowedFileTypes:
self.log(f"Holding #{holdingIndex} is '{fileType}' which is unsupported.")
else:
self.log(f"Holding #{holdingIndex} is not valid format.")
# Pick randomly if not yet suitable
holdingChecked = [holdingIndex]
holdingChecks = 8
while (fileType not in allowedFileTypes) and (holdingChecks > 0):
holdingChecks -= 1
while holdingIndex in holdingChecked and len(holdingChecked) < holdingsCount:
holdingIndex = int(random.random() * holdingsCount)
holdingChecked.append(holdingIndex)
holding = self.userInfo["subject"]["holdings"][holdingIndex]
if self.isValidHoldingFormat(holding):
fileType = holding["filetype"] if "filetype" in holding else ""
if fileType not in allowedFileTypes:
self.log(f"Holding #{holdingIndex} is '{fileType}' which is unsupported.")
else:
self.log(f"Holding #{holdingIndex} is not valid format.")
if fileType not in allowedFileTypes:
self.log(f"User {self.raretoshiUser} has no suitable holdings on raretoshi matching allowed types {allowedFileTypes}")
return
if self.isValidHoldingFormat(holding): self.holding = holding
def downloadIPFSfile(self, ipfshash):
downloadFromRaretoshi = False
if ipfshash.endswith(".png") or ipfshash.endswith(".jpeg"):
# seems that avatar_url for artist and owner may not be in ipfs.io.
# these urls also contain the file extension, which needs to be removed
downloadFromRaretoshi = True
ipfshash = ipfshash.replace(".png", "")
ipfshash = ipfshash.replace(".jpeg", "")
saveto = self.ipfsDataDirectory + ipfshash
if exists(saveto):
self.log(f"Skipping IPFS download. File already exists at {saveto}")
return 0
if downloadFromRaretoshi:
url = "https://raretoshi.com/api/ipfs/" + ipfshash
else:
url = "https://ipfs.io/ipfs/" + ipfshash
self.log(f"Downloading from {url}")
rc = vicariousnetwork.getandsavefile(self.useTor, url, saveto, "")
if rc == 0:
self.log(f"IPFS Downloaded to {saveto}")
else:
# If we were originally hitting IPFS, we can try fallback to raretoshi
if url.startswith("https://ipfs.io/"):
url = url.replace("https://ipfs.io/", "https://raretoshi.com/api/")
self.log(f"Retrying with {url}")
rc = vicariousnetwork.getandsavefile(self.useTor, url, saveto, "")
if rc == 0:
self.log(f"IPFS Downloaded to {saveto}")
else:
self.log(f"Unable to download file from {url}")
return rc
def isValidUserFormat(self, user):
return user.keys() & {'username', 'avatar_url'}
def isValidHoldingFormat(self, holding):
if not holding.keys() & {'artist','edition','editions','filename','filetype','owner','slug','title'}: return False
if not self.isValidUserFormat(holding["artist"]): return False
if not self.isValidUserFormat(holding["owner"]): return False
return True
def downloadFiles(self):
ipfshash = self.holding["filename"]
rc1 = self.downloadIPFSfile(ipfshash)
artist = self.holding["artist"]["username"]
print(f"Downloading avatar for artist: {artist}")
rc2 = self.downloadIPFSfile(self.holding["artist"]["avatar_url"])
owner = self.holding["owner"]["username"]
print(f"Downloading avatar for owner: {owner}")
rc3 = self.downloadIPFSfile(self.holding["owner"]["avatar_url"])
return rc1 # + rc2 + rc3
def loadAndPasteImage(self):
ipfshash = self.holding["filename"]
sourceFile = self.ipfsDataDirectory + ipfshash
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))
if self.stretchEdgeEnabled:
# left
if sourceLeft - self.stretchEdgeSpacing > 0:
imLine = sourceImage.crop((0,0,1,sourceHeight))
for x in range(0,sourceLeft-self.stretchEdgeSpacing):
self.canvas.paste(imLine, (x, sourceTop))
# right
if sourceLeft + sourceWidth + self.stretchEdgeSpacing < self.width:
imLine = sourceImage.crop((sourceWidth-1,0,sourceWidth,sourceHeight))
for x in range(sourceLeft+sourceWidth+self.stretchEdgeSpacing,self.getInsetWidth()):
self.canvas.paste(imLine, (x, sourceTop))
# top
if sourceTop - self.stretchEdgeSpacing > self.getInsetTop():
imLine = sourceImage.crop((0,0,sourceWidth,1))
for y in range(self.getInsetTop(),sourceTop-self.stretchEdgeSpacing):
self.canvas.paste(imLine, (sourceLeft, y))
# bottom
if sourceTop + sourceHeight + self.stretchEdgeSpacing < self.getInsetTop() + self.getInsetHeight():
imLine = sourceImage.crop((0,sourceHeight-1,sourceWidth,sourceHeight))
for y in range(sourceTop+sourceHeight+self.stretchEdgeSpacing, self.getInsetTop() + self.getInsetHeight()):
self.canvas.paste(imLine, (sourceLeft, y))
sourceImage.close()
def renderAnnotation(self):
if not self.overlayEnabled: return
overlay = Image.new(mode="RGBA", size=(self.width, self.height), color=(255,255,255,0))
overlaydraw = ImageDraw.Draw(overlay)
fontsize = int(self.height * 12 / 320)
overlayBG = ImageColor.getrgb(self.overlayBackgroundColor)
overlayFG = ImageColor.getrgb(self.overlayTextColor)
artist = self.holding["artist"]["username"]
owner = self.holding["owner"]["username"]
edition = self.holding["edition"]
editions = self.holding["editions"]
editionText = f"Edition: {edition}/{editions}"
if int(editions) == 1: editionText = f"Edition: One of a Kind"
listprice = self.holding["list_price"] if "list_price" in self.holding else None
x = 0
y = self.getInsetTop() + self.getInsetHeight()
t = "Asset from Raretoshi.com"
if self.qrCodeEnabled: t = f"{t} - scan QR code ->"
fontsize2 = int(self.height * 16 / 320)
w,h,_ = vicarioustext.gettextdimensions(overlaydraw, t, fontsize2, False)
h = (fontsize2 * 1.4) // 1
overlaydraw.rectangle(xy=((x,y),(w,y-h)),fill=overlayBG)
vicarioustext.drawbottomlefttext(overlaydraw, t, fontsize2, x, y, overlayFG, False)
y -= h
if listprice is not None and int(listprice) > 0:
t = f"List Price: {listprice}"
w,h,_ = vicarioustext.gettextdimensions(overlaydraw, t, fontsize, False)
h = (fontsize * 1.4) // 1
overlaydraw.rectangle(xy=((x,y),(w,y-h)),fill=overlayBG)
vicarioustext.drawbottomlefttext(overlaydraw, t, fontsize, x, y, overlayFG, False)
y -= h
t = editionText
w,h,_ = vicarioustext.gettextdimensions(overlaydraw, t, fontsize, False)
h = (fontsize * 1.4) // 1
overlaydraw.rectangle(xy=((x,y),(w,y-h)),fill=overlayBG)
vicarioustext.drawbottomlefttext(overlaydraw, t, fontsize, x, y, overlayFG, False)
y -= h
t = f"Owner: {owner}"
w,h,_ = vicarioustext.gettextdimensions(overlaydraw, t, fontsize, False)
h = (fontsize * 1.4) // 1
overlaydraw.rectangle(xy=((x,y),(w,y-h)),fill=overlayBG)
vicarioustext.drawbottomlefttext(overlaydraw, t, fontsize, x, y, overlayFG, False)
y -= h
t = f"Artist: {artist}"
w,h,_ = vicarioustext.gettextdimensions(overlaydraw, t, fontsize, False)
h = (fontsize * 1.4) // 1
overlaydraw.rectangle(xy=((x,y),(w,y-h)),fill=overlayBG)
vicarioustext.drawbottomlefttext(overlaydraw, t, fontsize, x, y, overlayFG, False)
y -= h
self.canvas.alpha_composite(overlay)
overlay.close()
def renderQRCode(self):
if not self.qrCodeEnabled: return
slug=self.holding["slug"]
raretoshiurl=f"https://raretoshi.com/a/{slug}"
qr = qrcode.QRCode(box_size=self.qrCodePixelSize)
qr.add_data(raretoshiurl)
qr.make()
img = qr.make_image()
s = img.size[1]
pos = (self.getInsetWidth()-s,self.getInsetTop()+self.getInsetHeight()-s)
self.canvas.paste(img, pos)
img.close()
def fetchData(self):
"""Fetches all the data needed for this panel"""
userInfo, userInfoLast = vicariousnetwork.getraretoshiuserinfo(
self.useTor,
self.raretoshiDataDirectory,
self.raretoshiUser,
self.userInfo,
self.userInfoLast,
self.userInfoInterval)
self.userInfo = userInfo
self.userInfoLast = userInfoLast
self.setupHoldingInfo()
if self.holding is not None:
rc = self.downloadFiles()
if rc > 0: self.holding = None
if self.randomUserEnabled: self.setupRaretoshiUser()
def run(self):
if self.holding is None:
self.log("Holding not available. Skipping image creation")
self._markAsRan()
return
super().startImage()
self.headerText = self.holding["title"]
self.loadAndPasteImage()
self.renderAnnotation()
self.renderQRCode()
super().finishImage()
# --------------------------------------------------------------------------------------
# Entry point if running this script directly
# --------------------------------------------------------------------------------------
if __name__ == '__main__':
p = RaretoshiPanel()
# If arguments were passed in, treat as a single run
if len(sys.argv) > 1:
arg1 = sys.argv[1]
if arg1 in ['-h','--help']:
arg0 = sys.argv[0]
print(f"Prepares an image from the raretoshi colletion for a user, scales and annotates")
print(f"Usage:")
print(f"1) Call without arguments to run continuously using the configuration or defaults")
print(f"2) Pass the desired raretoshi user (case sensitive) as an argument as follows")
print(f" {arg0} valeriyageorg")
print(f"3) Pass the desired raretoshi user and an index number")
print(f" {arg0} valeriyageorg 3")
else:
if len(sys.argv) > 2:
arg2 = sys.argv[2]
if re.match(r'^-?\d+$', arg2) is not None:
if int(arg2) >= 0: p.collectionIndex = arg2
p.raretoshiUser = arg1
p.fetchData()
p.run()
exit(0)
# Continuous run
p.runContinuous()