Skip to content

Commit ddfaf04

Browse files
committed
refactor image converter URL into function
* for use by demos like Thingiverse.
1 parent 6086ce3 commit ddfaf04

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

adafruit_pyportal.py

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,21 @@ def _connect_esp(self):
583583
print("Cound not connect to internet", error)
584584
print("Retrying in 3 seconds...")
585585
time.sleep(3)
586+
587+
def image_converter_url(self, image_url, width, height, color_depth=16):
588+
"""Generate a converted image url from the url passed in,
589+
with the given width and height. aio_username and aio_key must be
590+
set in secrets."""
591+
try:
592+
aio_username = secrets['aio_username']
593+
aio_key = secrets['aio_key']
594+
except KeyError:
595+
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
596+
597+
return IMAGE_CONVERTER_SERVICE % (aio_username, aio_key,
598+
width, height,
599+
color_depth, image_url)
600+
586601
def fetch(self):
587602
"""Fetch data from the url we initialized with, perfom any parsing,
588603
and display text or graphics. This function does pretty much everything"""
@@ -654,17 +669,11 @@ def fetch(self):
654669
gc.collect()
655670

656671
if image_url:
657-
try:
658-
aio_username = secrets['aio_username']
659-
aio_key = secrets['aio_key']
660-
except KeyError:
661-
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
662672
try:
663673
print("original URL:", image_url)
664-
image_url = IMAGE_CONVERTER_SERVICE % (aio_username, aio_key,
665-
self._image_resize[0],
666-
self._image_resize[1],
667-
16, image_url)
674+
image_url = self.image_converter_url(image_url,
675+
self._image_resize[0],
676+
self._image_resize[1])
668677
print("convert URL:", image_url)
669678
# convert image to bitmap and cache
670679
#print("**not actually wgetting**")
@@ -808,4 +817,4 @@ def wrap_nicely(string, max_chars):
808817
the_line = ''+w
809818
if the_line: # last line remaining
810819
the_lines.append(the_line)
811-
return the_lines
820+
return the_lines

0 commit comments

Comments
 (0)