/
code.py
executable file
·75 lines (68 loc) · 2.64 KB
/
code.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# SPDX-FileCopyrightText: 2019 Isaac Wellish for Adafruit Industries
#
# SPDX-License-Identifier: MIT
"""
This example uses the Wordnik API to display Wordnik's Word of the Day.
Each day a new word, its part of speech, and definition
will appear automatically on the display. Tap the screen to start
as well as to switch between the word's definition and an example sentence.
"""
import os
import board
from adafruit_pyportal import PyPortal
# 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"
)
PART_OF_SPEECH = ["definitions", 0, "partOfSpeech"]
DEF_LOCATION = ["definitions", 0, "text"]
EXAMPLE_LOCATION = ["examples", 0, "text"]
CAPTION = "wordnik.com/word-of-the-day"
DEFINITION_EXAMPLE_ARR = [DEF_LOCATION, EXAMPLE_LOCATION]
# defintion and example array variable initialized at 0
definition_example = 0
# determine the current working directory
# needed so we know where to find files
cwd = ("/" + __file__).rsplit("/", 1)[0]
# Initialize the pyportal object and let us know what data to fetch and where
# to display it
pyportal = PyPortal(
url=DATA_SOURCE,
status_neopixel=board.NEOPIXEL,
default_bg=cwd + "/wordoftheday_background.bmp",
text_font=cwd + "/fonts/Arial-ItalicMT-17.bdf",
text_position=(
(50, 30), # word location
(50, 50), # part of speech location
(50, 135),
), # definition location
text_color=(0x8080FF, 0xFF00FF, 0xFFFFFF),
text_wrap=(0, 0, 28), # characters to wrap for text
text_maxlen=(180, 30, 115), # max text size for word, part of speech and def
caption_text=CAPTION,
caption_font=cwd + "/fonts/Arial-ItalicMT-17.bdf",
caption_position=(50, 220),
caption_color=0x808080,
)
print("loading...") # print to repl while waiting for font to load
pyportal.preload_font() # speed things up by preloading font
pyportal.set_text("\nWord of the Day") # show title
while True:
if pyportal.touchscreen.touch_point:
try:
# set the JSON path here to be able to change between definition and example
# pylint: disable=protected-access
pyportal._json_path = (
["word"],
PART_OF_SPEECH,
DEFINITION_EXAMPLE_ARR[definition_example],
)
value = pyportal.fetch()
print("Response is", value)
except RuntimeError as e:
print("Some error occured, retrying! -", e)
# Change between definition and example
if definition_example == 0:
definition_example = 1
elif definition_example == 1:
definition_example = 0