-
Notifications
You must be signed in to change notification settings - Fork 2
/
wordnik.star
295 lines (277 loc) · 9.67 KB
/
wordnik.star
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# Word Of the Day powered by Wordnik
#
# Copyright (c) 2022 Henry So, Jr.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# Note: this app uses the Wordnik API and requires a Wordnik API key to be
# passed as api_key
#
# The Wordnik logo in the background is used with permission.
# Wordnik is at https://wordnik.com
# To get an API key and/or learn about Wordnik's API, go to
# https://developer.wordnik.com
load("http.star", "http")
load("render.star", "render")
load("cache.star", "cache")
load("encoding/base64.star", "base64")
load("encoding/json.star", "json")
URL = "https://api.wordnik.com/v4/words.json/wordOfTheDay?api_key="
WIDTH = 64
HEIGHT = 32
LOGO_PAD = (0, HEIGHT - 14, 0, 0)
LONG_WORD = 13
LONG_DEFINITION = 25
FAST_DEFINITION = 50
WORD_COLOR = "#fff"
LINE_COLOR = "#008"
SEPARATOR_COLOR = "#035"
PART_COLOR = "#468"
TEXT_COLOR = "#9ac"
KEY = "wod"
TTL = 60 * 60
FONTS = {
True: ( "tb-8", 8 ),
False: ( "tom-thumb", 6 )
}
# takes 'static' for a static definition, 'api_key' to retrieve a word, and
# 'hide_logo' (truthy value) if the logo should be hidden
def main(config):
static = config.get("static")
if static != None:
content = STATIC_CONTENT[int(static) % len(STATIC_CONTENT)]
else:
content = cache.get(KEY)
if content == None:
api_key = config.get("api_key")
if api_key:
print("retrieving word")
content = http.get(URL + api_key)
if content.status_code == 200:
content = content.json()
content = {
"word": content["word"],
"defs": [
{
"text": definition["text"],
"part": definition["partOfSpeech"],
}
for definition in content["definitions"]
],
}
cache.set(KEY, json.encode(content), TTL)
else:
print("Server returned %s" % content.status_code)
content = {
"word": "error %s" % content.status_code,
"defs": [
{
"text": "the issue where the server returns %s" % content.status_code,
"part": "n/a",
},
],
}
else:
print("api_key was not passed")
content = {
"word": "no api key",
"defs": [
{
"text": "the issue where api_key is not passed",
"part": "n/a",
},
],
}
else:
print("using cache for word")
content = json.decode(content)
print(content)
word = content["word"]
definitions = content["defs"]
word_font = FONTS[len(word) < LONG_WORD]
longest_definition_word_length = 0
definition_word_count = 0
for d in definitions:
definition_word_count += 1 # for the part of speech
for w in d["text"].split():
definition_word_count += 1
length = len(list(w.codepoints()))
if longest_definition_word_length < length:
longest_definition_word_length = length
definition_font_is_large = (
longest_definition_word_length < LONG_WORD and
definition_word_count < LONG_DEFINITION
)
definition_font = FONTS[definition_font_is_large][0]
delay = 100 if definition_word_count < FAST_DEFINITION else 50
display = render.Column([
render.Stack([
render.Column([
render.Box(
width = WIDTH,
height = word_font[1] - 1,
),
render.Box(
width = WIDTH,
height = 1,
color = LINE_COLOR,
),
]),
render.Text(
color = WORD_COLOR,
font = word_font[0],
content = word,
),
]),
render.Marquee(
height = HEIGHT - word_font[1],
offset_start = 16,
offset_end = 16,
scroll_direction = "vertical",
child = render.Column(flatten([
(
render.Box(
width = WIDTH,
height = 1,
color = SEPARATOR_COLOR,
) if d[0] != 0 else tuple(),
render_definition(d[1], definition_font),
) for d in enumerate(definitions)
])),
),
])
return render.Root(
delay = delay,
child = display if config.get("hide_logo") else render.Stack([
render.Padding(
pad = LOGO_PAD,
child = render.Image(LOGO),
),
display,
]),
)
# Returns the widget for a given definition
# definition - the definition
def render_definition(definition, font):
part = definition["part"] # #58a
part = "%s," % PARTS.get(part, part)
text = definition["text"]
return render.Stack([
render.WrappedText(
width = WIDTH,
color = TEXT_COLOR,
content = "%s %s" % (part, text),
font = font,
),
render.Text(
color = PART_COLOR,
content = part,
font = font,
),
])
# Flattens a list or tuple of lists or tuples into a single list
# item - the item to flatten
def flatten(item):
if type(item) == "list" or type(item) == "tuple":
result = []
for elem in item:
result += flatten(elem)
return result
else:
return [ item ]
PARTS = {
"noun": "n.",
"adjective": "adj.",
"verb": "v.",
"adverb": "adv.",
"interjection": "int.",
"pronoun": "pron.",
"preposition": "prep.",
"abbreviation": "abbr.",
"affix": "aff.",
"article": "art.",
"auxiliary-verb": "v.",
"conjunction": "conj.",
"definite-article": "art.",
"idiom": "id.",
"imperative": "imp.",
"noun-plural": "n.",
"noun-posessive": "n.",
"past-participle": "part.",
"phrasal-prefix": "phr.",
"proper-noun": "n.",
"proper-noun-plural": "n.",
"proper-noun-posessive": "n.",
"suffix": "suf.",
"verb-intransitive": "v.",
"verb-transitive": "v.",
"transitive verb": "v.",
}
STATIC_CONTENT = [
{
"word": "incipience",
"defs": [
{
"text": "The condition of being incipient; beginning; commencement.",
"part": "noun",
},
{
"text": "A beginning, or first stage",
"part": "noun",
},
],
},
{
"word": "heterochromatic",
"defs": [
{
"text": "Of or characterized by different colors; varicolored.",
"part": "adjective",
},
{
"text": "Consisting of different wavelengths or frequencies.",
"part": "adjective",
},
{
"text": "Of or relating to heterochromatin.",
"part": "adjective",
},
],
},
{
"word": "pallasite",
"defs": [
{
"text": "A stony-iron meteorite embedded with glassy crystals of olivine.",
"part": "noun",
},
],
},
]
LOGO = base64.decode("""
iVBORw0KGgoAAAANSUhEUgAAAEAAAAAOCAMAAACo5erwAAAAYFBMVEUAAAAHAQARAQAEBwMNBQIV
BQAcBQIJCwcjBQAqBgAbDQA4BQAOEQ0TFRI/CwE2DwQWGBU/EQA3FQAaHBkzGAAdHx1AFwI/HAA8
HwAlJyUrLCovMS4zNTM2ODY6PDk9Pzw+R+8dAAABaklEQVQoz5WT0XbDIAiGzWjqsupma7tFEXj/
txxo0vWc3qxcgeLHDyHO/csgx4cgJvAZ3CvmmR4CkogSXgS0BwWlQvsD+GBiwO8+HNSZe+CzCY05
ewPopR4Eyw0DAMHeVs6aXawCIPuPdb3O67rOUJmEk0cW4aqAIoXULZpG3gC+MSogSdWyTRQTBA/r
ZtcDimDRTKpVHzZXxVyWBI2DAmIT7LqJQSmiMJQ8/2xmACWXnuSxA0hl30ShBrix3ZvpkT5lDoHJ
z5fvYRcF3Jxr3L9f6IBik5E6ANLD8VUoSVOMNummr/OwZUKJDojGdMkAydw7oN03QbWpLhLrxR03
wBsYQPsbCvgJcO/A9AkDlE3T8ql2ProBKFJHjSdA3meohvY08Cg2nRSwTBsASDAllGdAsMluhEQm
BnGTtJzebSlqB0Ybl7S+B70A7wDL2AjQl9Hvyzm9jcPYgT7lHCHq3qQep2A3wQL7q34BfEop1DYa
VFcAAAAASUVORK5CYII=
""")