-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy paththey_said_so.star
206 lines (188 loc) · 6.98 KB
/
they_said_so.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
"""
Applet: They Said So
Summary: Quote of the Day
Description: Quote of the day powered by theysaidso.com.
Author: Henry So, Jr.
"""
# Quote Of the Day powered by theysaidso.com
#
# 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 theysaidso.com's public RSS feed (via feedburner)
load("http.star", "http")
load("render.star", "render")
load("schema.star", "schema")
load("cache.star", "cache")
load("xpath.star", "xpath")
load("encoding/base64.star", "base64")
load("encoding/json.star", "json")
URL = "http://feeds.feedburner.com/theysaidso/qod"
WIDTH = 64
HEIGHT = 32
QUOTE_W = 22
QUOTE_H = 19
QUOTE_COLOR = "#555"
TEXT_OFFSET = 3
TEXT_COLOR = "#fff"
CITATION_COLOR = "#37f"
ATTRIBUTION_COLOR = "#484"
# subtract one extra for aesthetics
CLOSE_OFFSET = -(QUOTE_H - TEXT_OFFSET - TEXT_OFFSET - 1)
KEY = "qod"
TTL = 60 * 60
ERROR_TTL = 5 * 60
CATEGORIES = [
"inspire", # Inspiring Quote of the day
"management", # Management Quote of the day
"sports", # Sports Quote of the day
"life", # Quote of the day about life
"funny", # Funny Quote of the day
"love", # Quote of the day about Love
"art", # Art quote of the day
"students", # Quote of the day for students
]
# Takes category (value from CATEGORIES)
def main(config):
category = config.get("category") or "fake"
if category in CATEGORIES:
data = cache.get(KEY)
if data == None:
#print("retrieving feed")
content = http.get(URL)
if content.status_code == 200 and content.body():
xml = xpath.loads(content.body())
data = {
c: {
"quote": xml.query("//item[ends-with(link,'#" + c + "')]/quote"),
"author": xml.query("//item[ends-with(link,'#" + c + "')]/author"),
}
for c in CATEGORIES
}
else:
#print('Server returned %s' % content.status_code)
data = {
c: {
"quote": 'Forsooth, the server quoteth "%s".' % content.status_code,
"author": "Anonymous",
}
for c in CATEGORIES
}
cache.set(KEY, json.encode(data), TTL if content.status_code < 300 else ERROR_TTL)
else:
#print("using cache")
data = json.decode(data)
quote = data[category]["quote"] or "Strange, the API didn't return a quote."
author = data[category]["author"] or "Author Unknown"
else:
quote = "Some say passing an invalid category will yield good results. I don't."
author = "Anonymous"
# try to adjust when the quote is too long
delay = 100
if len(quote) > 160:
delay = 50
if len(quote) > 320:
quote = quote[0:(quote.rindex(" ", 0, 320))] + "..."
# generate the widget for the app
return render.Root(
delay = delay,
child = render.Marquee(
height = HEIGHT,
# offset it to give some time for the user to read the first line
offset_start = 5,
offset_end = 5,
scroll_direction = "vertical",
child = render.Stack([
render.Column(
children = [
# use this Padding to hackily measure the text
render.Padding(
pad = (0, CLOSE_OFFSET, 0, 0),
child = render.WrappedText(
content = quote,
width = WIDTH,
color = "#000",
),
),
render.Box(
width = QUOTE_W,
height = QUOTE_H,
color = QUOTE_COLOR,
child = render.Image(RQUOTE),
),
],
cross_align = "end",
),
render.Box(
width = QUOTE_W,
height = QUOTE_H,
color = QUOTE_COLOR,
child = render.Image(LQUOTE),
),
render.Column([
render.Box(
width = WIDTH,
height = TEXT_OFFSET,
),
render.WrappedText(
content = quote,
width = WIDTH,
color = TEXT_COLOR,
),
render.WrappedText(
content = "- " + author,
width = WIDTH,
color = CITATION_COLOR,
),
render.Text(
content = "theysaidso.com",
color = ATTRIBUTION_COLOR,
),
]),
]),
),
)
def get_schema():
categories = [
schema.Option(display = category, value = category)
for category in CATEGORIES
]
return schema.Schema(
version = "1",
fields = [
schema.Dropdown(
id = "category",
name = "Category",
icon = "quoteRight",
desc = "The quote category to select from.",
options = categories,
default = "inspire",
),
],
)
LQUOTE = base64.decode("""
iVBORw0KGgoAAAANSUhEUgAAABYAAAATAgMAAADpFxUbAAAACVBMVEUAAAAAAAD///+D3c/SAAAA
AXRSTlMAQObYZgAAADlJREFUCNeVybENADAIA8GnSE/DPh4hjfdfJZA+RbDkkwzGZnc8JayhrNrM
q+ew4wUUZLO494kokQelgwyOl8+GjgAAAABJRU5ErkJggg==
""")
RQUOTE = base64.decode("""
iVBORw0KGgoAAAANSUhEUgAAABYAAAATAgMAAADpFxUbAAAACVBMVEUAAAAAAAD///+D3c/SAAAA
AXRSTlMAQObYZgAAADpJREFUCNeVyaERADEQQtFv8DHbDyUgsv23kk38iQPxZgBTZgHi5SdGHnqr
9xdRZ3C1F6HCZbbpXMoB+pgMpp7gufIAAAAASUVORK5CYII=
""")