black PyPortal/PyPortal_Word_of_the_Day/code.py

This commit is contained in:
Dan Halbert 2024-04-09 15:43:30 -04:00
parent 88d94c09a7
commit b6d127c9db

View file

@ -14,57 +14,61 @@ import board
from adafruit_pyportal import PyPortal from adafruit_pyportal import PyPortal
# Set up where we'll be fetching data from # Set up where we'll be fetching data from
DATA_SOURCE = "https://api.wordnik.com/v4/words.json/wordOfTheDay?api_key=" + os.getenv("WORDNIK_TOKEN") DATA_SOURCE = "https://api.wordnik.com/v4/words.json/wordOfTheDay?api_key=" + os.getenv(
PART_OF_SPEECH = ['definitions', 0, 'partOfSpeech'] "WORDNIK_TOKEN"
DEF_LOCATION = ['definitions', 0, 'text'] )
EXAMPLE_LOCATION = ['examples', 0, 'text'] PART_OF_SPEECH = ["definitions", 0, "partOfSpeech"]
CAPTION = 'wordnik.com/word-of-the-day' DEF_LOCATION = ["definitions", 0, "text"]
EXAMPLE_LOCATION = ["examples", 0, "text"]
CAPTION = "wordnik.com/word-of-the-day"
DEFINITION_EXAMPLE_ARR = [DEF_LOCATION, EXAMPLE_LOCATION] DEFINITION_EXAMPLE_ARR = [DEF_LOCATION, EXAMPLE_LOCATION]
#defintion and example array variable initialized at 0 # defintion and example array variable initialized at 0
definition_example = 0 definition_example = 0
# determine the current working directory # determine the current working directory
# needed so we know where to find files # needed so we know where to find files
cwd = ("/"+__file__).rsplit('/', 1)[0] cwd = ("/" + __file__).rsplit("/", 1)[0]
# Initialize the pyportal object and let us know what data to fetch and where # Initialize the pyportal object and let us know what data to fetch and where
# to display it # to display it
pyportal = PyPortal(url=DATA_SOURCE, pyportal = PyPortal(
status_neopixel=board.NEOPIXEL, url=DATA_SOURCE,
default_bg=cwd+"/wordoftheday_background.bmp", status_neopixel=board.NEOPIXEL,
text_font=cwd+"/fonts/Arial-ItalicMT-17.bdf", default_bg=cwd + "/wordoftheday_background.bmp",
text_position=((50, 30), # word location text_font=cwd + "/fonts/Arial-ItalicMT-17.bdf",
(50, 50), # part of speech location text_position=(
(50, 135)), # definition location (50, 30), # word location
text_color=(0x8080FF, (50, 50), # part of speech location
0xFF00FF, (50, 135),
0xFFFFFF), ), # definition location
text_wrap=(0, # characters to wrap for text text_color=(0x8080FF, 0xFF00FF, 0xFFFFFF),
0, text_wrap=(0, 0, 28), # characters to wrap for text
28), text_maxlen=(180, 30, 115), # max text size for word, part of speech and def
text_maxlen=(180, 30, 115), # max text size for word, part of speech and def caption_text=CAPTION,
caption_text=CAPTION, caption_font=cwd + "/fonts/Arial-ItalicMT-17.bdf",
caption_font=cwd+"/fonts/Arial-ItalicMT-17.bdf", caption_position=(50, 220),
caption_position=(50, 220), caption_color=0x808080,
caption_color=0x808080) )
print("loading...") # print to repl while waiting for font to load print("loading...") # print to repl while waiting for font to load
pyportal.preload_font() # speed things up by preloading font pyportal.preload_font() # speed things up by preloading font
pyportal.set_text("\nWord of the Day") # show title pyportal.set_text("\nWord of the Day") # show title
while True: while True:
if pyportal.touchscreen.touch_point: if pyportal.touchscreen.touch_point:
try: try:
#set the JSON path here to be able to change between definition and example # set the JSON path here to be able to change between definition and example
# pylint: disable=protected-access # pylint: disable=protected-access
pyportal._json_path=(['word'], pyportal._json_path = (
PART_OF_SPEECH, ["word"],
DEFINITION_EXAMPLE_ARR[definition_example]) PART_OF_SPEECH,
DEFINITION_EXAMPLE_ARR[definition_example],
)
value = pyportal.fetch() value = pyportal.fetch()
print("Response is", value) print("Response is", value)
except RuntimeError as e: except RuntimeError as e:
print("Some error occured, retrying! -", e) print("Some error occured, retrying! -", e)
#Change between definition and example # Change between definition and example
if definition_example == 0: if definition_example == 0:
definition_example = 1 definition_example = 1
elif definition_example == 1: elif definition_example == 1: