Skip to content

Commit 6aef8c8

Browse files
authored
Merge pull request #6 from martymcguire/mm-img-conv-func
refactor image converter URL into function
2 parents 9bfe2e9 + e3a2d69 commit 6aef8c8

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

adafruit_pyportal.py

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

656672
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
662673
try:
663674
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)
675+
image_url = self.image_converter_url(image_url,
676+
self._image_resize[0],
677+
self._image_resize[1])
668678
print("convert URL:", image_url)
669679
# convert image to bitmap and cache
670680
#print("**not actually wgetting**")

0 commit comments

Comments
 (0)