Skip to content

Commit ce1bd60

Browse files
committed
add text transformation function and update image service to adafruit.io
1 parent 3e0524e commit ce1bd60

File tree

1 file changed

+30
-9
lines changed

1 file changed

+30
-9
lines changed

adafruit_pyportal.py

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,9 @@
7777
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_PyPortal.git"
7878

7979
# pylint: disable=line-too-long
80-
IMAGE_CONVERTER_SERVICE = "https://res.cloudinary.com/schmarty/image/fetch/w_320,h_240,c_fill,f_bmp/"
81-
#IMAGE_CONVERTER_SERVICE = "http://ec2-107-23-37-170.compute-1.amazonaws.com/rx/ofmt_bmp,rz_320x240/"
80+
# you'll need to pass in an io username, width, height, format (bit depth), io key, and then url!
81+
IMAGE_CONVERTER_SERVICE = "https://io.adafruit.com/api/v2/%s/integrations/image-formatter?x-aio-key=%s&width=%d&height=%d&output=BMP%d&url=%s"
82+
8283
TIME_SERVICE_IPADDR = "http://worldtimeapi.org/api/ip"
8384
TIME_SERVICE_LOCATION = "http://worldtimeapi.org/api/timezone/"
8485
LOCALFILE = "local.txt"
@@ -119,6 +120,7 @@ class PyPortal:
119120
:param text_wrap: Whether or not to wrap text (for long text data chunks). Defaults to
120121
``False``, no wrapping.
121122
:param text_maxlen: The max length of the text for text wrapping. Defaults to 0.
123+
:param text_transform: A function that will be called on the text before display
122124
:param image_json_path: The JSON traversal path for a background image to display. Defaults to
123125
``None``.
124126
:param image_resize: What size to resize the image we got from the json_path, make this a tuple
@@ -140,7 +142,7 @@ class PyPortal:
140142
def __init__(self, *, url=None, json_path=None, regexp_path=None,
141143
default_bg=0x000000, status_neopixel=None,
142144
text_font=None, text_position=None, text_color=0x808080,
143-
text_wrap=False, text_maxlen=0,
145+
text_wrap=False, text_maxlen=0, text_transform=None,
144146
image_json_path=None, image_resize=None, image_position=None,
145147
caption_text=None, caption_font=None, caption_position=None,
146148
caption_color=0x808080,
@@ -256,11 +258,13 @@ def __init__(self, *, url=None, json_path=None, regexp_path=None,
256258
text_color = (text_color,)
257259
text_wrap = (text_wrap,)
258260
text_maxlen = (text_maxlen,)
261+
text_transform = (text_transform,)
259262
self._text = [None] * num
260263
self._text_color = [None] * num
261264
self._text_position = [None] * num
262265
self._text_wrap = [None] * num
263266
self._text_maxlen = [None] * num
267+
self._text_transform = [None] * num
264268
self._text_font = bitmap_font.load_font(text_font)
265269
if self._debug:
266270
print("Loading font glyphs")
@@ -276,6 +280,7 @@ def __init__(self, *, url=None, json_path=None, regexp_path=None,
276280
self._text_position[i] = text_position[i]
277281
self._text_wrap[i] = text_wrap[i]
278282
self._text_maxlen[i] = text_maxlen[i]
283+
self._text_transform[i] = text_transform[i]
279284
else:
280285
self._text_font = None
281286
self._text = None
@@ -617,7 +622,11 @@ def fetch(self):
617622
# extract desired text/values from json
618623
if self._json_path:
619624
for path in self._json_path:
620-
values.append(PyPortal._json_traverse(json_out, path))
625+
try:
626+
values.append(PyPortal._json_traverse(json_out, path))
627+
except KeyError:
628+
print(json_out)
629+
raise
621630
elif self._regexp_path:
622631
for regexp in self._regexp_path:
623632
values.append(re.search(regexp, r.text).group(1))
@@ -637,9 +646,17 @@ def fetch(self):
637646
gc.collect()
638647

639648
if image_url:
649+
try:
650+
aio_username = secrets['aio_username']
651+
aio_key = secrets['aio_key']
652+
except KeyError:
653+
raise KeyError("\n\nOur image converter service require a login/password to rate-limit. Please register for a freeadafruit.io account and place the user/key in your secrets file under 'aio_username' and 'aio_key'")# pylint: disable=line-too-long
640654
try:
641655
print("original URL:", image_url)
642-
image_url = IMAGE_CONVERTER_SERVICE+image_url
656+
image_url = IMAGE_CONVERTER_SERVICE % (aio_username, aio_key,
657+
self._image_resize[0],
658+
self._image_resize[1],
659+
16, image_url)
643660
print("convert URL:", image_url)
644661
# convert image to bitmap and cache
645662
#print("**not actually wgetting**")
@@ -669,10 +686,14 @@ def fetch(self):
669686
if self._text:
670687
for i in range(len(self._text)):
671688
string = None
672-
try:
673-
string = "{:,d}".format(int(values[i]))
674-
except (TypeError, ValueError):
675-
string = values[i] # ok its a string
689+
if self._text_transform[i]:
690+
f = self._text_transform[i]
691+
string = f(values[i])
692+
else:
693+
try:
694+
string = "{:,d}".format(int(values[i]))
695+
except (TypeError, ValueError):
696+
string = values[i] # ok its a string
676697
if self._debug:
677698
print("Drawing text", string)
678699
if self._text_wrap[i]:

0 commit comments

Comments
 (0)