77
77
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_PyPortal.git"
78
78
79
79
# 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
+
82
83
TIME_SERVICE_IPADDR = "http://worldtimeapi.org/api/ip"
83
84
TIME_SERVICE_LOCATION = "http://worldtimeapi.org/api/timezone/"
84
85
LOCALFILE = "local.txt"
@@ -119,6 +120,7 @@ class PyPortal:
119
120
:param text_wrap: Whether or not to wrap text (for long text data chunks). Defaults to
120
121
``False``, no wrapping.
121
122
: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
122
124
:param image_json_path: The JSON traversal path for a background image to display. Defaults to
123
125
``None``.
124
126
: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:
140
142
def __init__ (self , * , url = None , json_path = None , regexp_path = None ,
141
143
default_bg = 0x000000 , status_neopixel = None ,
142
144
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 ,
144
146
image_json_path = None , image_resize = None , image_position = None ,
145
147
caption_text = None , caption_font = None , caption_position = None ,
146
148
caption_color = 0x808080 ,
@@ -256,11 +258,13 @@ def __init__(self, *, url=None, json_path=None, regexp_path=None,
256
258
text_color = (text_color ,)
257
259
text_wrap = (text_wrap ,)
258
260
text_maxlen = (text_maxlen ,)
261
+ text_transform = (text_transform ,)
259
262
self ._text = [None ] * num
260
263
self ._text_color = [None ] * num
261
264
self ._text_position = [None ] * num
262
265
self ._text_wrap = [None ] * num
263
266
self ._text_maxlen = [None ] * num
267
+ self ._text_transform = [None ] * num
264
268
self ._text_font = bitmap_font .load_font (text_font )
265
269
if self ._debug :
266
270
print ("Loading font glyphs" )
@@ -276,6 +280,7 @@ def __init__(self, *, url=None, json_path=None, regexp_path=None,
276
280
self ._text_position [i ] = text_position [i ]
277
281
self ._text_wrap [i ] = text_wrap [i ]
278
282
self ._text_maxlen [i ] = text_maxlen [i ]
283
+ self ._text_transform [i ] = text_transform [i ]
279
284
else :
280
285
self ._text_font = None
281
286
self ._text = None
@@ -617,7 +622,11 @@ def fetch(self):
617
622
# extract desired text/values from json
618
623
if self ._json_path :
619
624
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
621
630
elif self ._regexp_path :
622
631
for regexp in self ._regexp_path :
623
632
values .append (re .search (regexp , r .text ).group (1 ))
@@ -637,9 +646,17 @@ def fetch(self):
637
646
gc .collect ()
638
647
639
648
if image_url :
649
+ try :
650
+ aio_username = secrets ['aio_username' ]
651
+ aio_key = secrets ['aio_key' ]
652
+ except KeyError :
653
+ raise KeyError ("\n \n Our 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
640
654
try :
641
655
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 )
643
660
print ("convert URL:" , image_url )
644
661
# convert image to bitmap and cache
645
662
#print("**not actually wgetting**")
@@ -669,10 +686,14 @@ def fetch(self):
669
686
if self ._text :
670
687
for i in range (len (self ._text )):
671
688
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
676
697
if self ._debug :
677
698
print ("Drawing text" , string )
678
699
if self ._text_wrap [i ]:
0 commit comments