Skip to content

Commit 72b4100

Browse files
authored
Merge pull request #670 from isaacwellish/PyPortal-Word-of-the-Day
Add example feature
2 parents 9106f04 + d236963 commit 72b4100

1 file changed

Lines changed: 23 additions & 11 deletions

File tree

PyPortal_Word_of_the_Day/code.py

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
"""
22
This example uses the Wordnik API to display Wordnik's Word of the Day.
3-
Each day a new word, it's part of speech, and definition
4-
will appear automatically on the display.
3+
Each day a new word, its part of speech, and definition
4+
will appear automatically on the display. Tap the screen to start
5+
as well as to switch between the word's definition and an example sentence.
56
"""
67

7-
import time
88
import board
99
from adafruit_pyportal import PyPortal
1010

@@ -17,10 +17,13 @@
1717

1818
# Set up where we'll be fetching data from
1919
DATA_SOURCE = "https://api.wordnik.com/v4/words.json/wordOfTheDay?api_key="+secrets['wordnik_token']
20-
WORD_LOCATION = ['word']
2120
PART_OF_SPEECH = ['definitions', 0, 'partOfSpeech']
2221
DEF_LOCATION = ['definitions', 0, 'text']
22+
EXAMPLE_LOCATION = ['examples', 0, 'text']
2323
CAPTION = 'wordnik.com/word-of-the-day'
24+
DEFINITION_EXAMPLE_ARR = [DEF_LOCATION, EXAMPLE_LOCATION]
25+
#defintion and example array variable initialized at 0
26+
definition_example = 0
2427

2528
# determine the current working directory
2629
# needed so we know where to find files
@@ -29,7 +32,6 @@
2932
# Initialize the pyportal object and let us know what data to fetch and where
3033
# to display it
3134
pyportal = PyPortal(url=DATA_SOURCE,
32-
json_path=(WORD_LOCATION, PART_OF_SPEECH, DEF_LOCATION),
3335
status_neopixel=board.NEOPIXEL,
3436
default_bg=cwd+"/wordoftheday_background.bmp",
3537
text_font=cwd+"/fonts/Arial-ItalicMT-17.bdf",
@@ -53,9 +55,19 @@
5355
pyportal.set_text("\nWord of the Day") # show title
5456

5557
while True:
56-
try:
57-
value = pyportal.fetch()
58-
print("Response is", value)
59-
except RuntimeError as e:
60-
print("Some error occured, retrying! -", e)
61-
time.sleep(10*60) # Update every 10 minutes
58+
if pyportal.touchscreen.touch_point:
59+
try:
60+
#set the JSON path here to be able to change between definition and example
61+
# pylint: disable=protected-access
62+
pyportal._json_path=(['word'],
63+
PART_OF_SPEECH,
64+
DEFINITION_EXAMPLE_ARR[definition_example])
65+
value = pyportal.fetch()
66+
print("Response is", value)
67+
except RuntimeError as e:
68+
print("Some error occured, retrying! -", e)
69+
#Change between definition and example
70+
if definition_example == 0:
71+
definition_example = 1
72+
elif definition_example == 1:
73+
definition_example = 0

0 commit comments

Comments
 (0)