|
1 | 1 | """ |
2 | 2 | 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. |
5 | 6 | """ |
6 | 7 |
|
7 | | -import time |
8 | 8 | import board |
9 | 9 | from adafruit_pyportal import PyPortal |
10 | 10 |
|
|
17 | 17 |
|
18 | 18 | # Set up where we'll be fetching data from |
19 | 19 | DATA_SOURCE = "https://api.wordnik.com/v4/words.json/wordOfTheDay?api_key="+secrets['wordnik_token'] |
20 | | -WORD_LOCATION = ['word'] |
21 | 20 | PART_OF_SPEECH = ['definitions', 0, 'partOfSpeech'] |
22 | 21 | DEF_LOCATION = ['definitions', 0, 'text'] |
| 22 | +EXAMPLE_LOCATION = ['examples', 0, 'text'] |
23 | 23 | 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 |
24 | 27 |
|
25 | 28 | # determine the current working directory |
26 | 29 | # needed so we know where to find files |
|
29 | 32 | # Initialize the pyportal object and let us know what data to fetch and where |
30 | 33 | # to display it |
31 | 34 | pyportal = PyPortal(url=DATA_SOURCE, |
32 | | - json_path=(WORD_LOCATION, PART_OF_SPEECH, DEF_LOCATION), |
33 | 35 | status_neopixel=board.NEOPIXEL, |
34 | 36 | default_bg=cwd+"/wordoftheday_background.bmp", |
35 | 37 | text_font=cwd+"/fonts/Arial-ItalicMT-17.bdf", |
|
53 | 55 | pyportal.set_text("\nWord of the Day") # show title |
54 | 56 |
|
55 | 57 | 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