Skip to content

Commit 3f560b0

Browse files
authored
Add Jokes JokeAPI app (Attempt #2) (#117)
* Add ESPN news app * Fix build error from .go file * Attempt number two at fixing build error * Attempt number 3 at fixing build error. * Changed `while` loop to `for` loop Infinite loops are bad... * Attempt number two of fixing while loop * Attempt number three for fixing lint error * espn_news: Fix title padding and run formatter * Fixed typo in whitespace calculation * Add Jokes JokeAPI app * Fixed `cache.set` bug and removed `fail()` line * Forgot to run `make lint` and had an indent error. my bad... * blacklist flags are removed and safe mode is always enabled.
1 parent 8d39200 commit 3f560b0

File tree

3 files changed

+137
-0
lines changed

3 files changed

+137
-0
lines changed

apps/apps.go

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

apps/jokesjokeapi/jokes_jokeapi.star

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
"""
2+
Applet: Jokes JokeAPI
3+
Summary: Displays jokes from JokeAPI
4+
Description: Displays different jokes from JokeAPI.
5+
Author: rs7q5 (RIS)
6+
"""
7+
8+
#jokeAPI.star
9+
#Created 20220130 RIS
10+
#Last Modified 20220201 RIS
11+
12+
load("render.star", "render")
13+
load("http.star", "http")
14+
15+
#load("encoding/base64.star", "base64")
16+
load("encoding/json.star", "json")
17+
load("cache.star", "cache")
18+
19+
#load("schema.star","schema")
20+
#load("math.star","math")
21+
#load("time.star","time")
22+
load("re.star", "re")
23+
24+
base_URL = "https://v2.jokeapi.dev/joke/Any"
25+
26+
def main():
27+
#get any flags
28+
full_URL = base_URL + "?safe-mode"
29+
30+
font = "tb-8" #set font
31+
32+
#check for cached data
33+
joke_cached = cache.get("joke_rate")
34+
if joke_cached != None: #if any are None then all(title_cached)==False
35+
print("Hit! Displaying cached data.")
36+
joke = json.decode(joke_cached)
37+
joke_txt = format_text(joke, font)
38+
else:
39+
print("Miss! Calling JokeAPI data.") #error code checked within each function!!!!
40+
41+
#get the data
42+
rep = http.get(full_URL)
43+
44+
if rep.status_code != 200:
45+
joke = ["Error, could not get jokes!!!!"]
46+
else:
47+
#get the joke strings
48+
if rep.json()["type"] == "twopart":
49+
joke = [re.sub('"\"|"\n"', "", rep.json()["setup"])]
50+
joke.append(re.sub('"\"|"\n"', "", rep.json()["delivery"]))
51+
else:
52+
joke = [re.sub('"\"|"\n"', "", rep.json()["joke"])]
53+
54+
#cache the data
55+
cache.set("joke_rate", json.encode(joke), ttl_seconds = 43200) #grabs it twice a day
56+
joke_txt = format_text(joke, font) #render the text
57+
return render.Root(
58+
delay = 200, #speed up scroll text
59+
child = render.Column(
60+
children = [
61+
render.Text("JokeAPI", color = "#6600cc", font = font),
62+
render.Marquee(
63+
height = 32,
64+
scroll_direction = "vertical",
65+
child = render.Column(
66+
main_align = "space_between",
67+
children = joke_txt,
68+
),
69+
),
70+
],
71+
),
72+
)
73+
74+
def format_text(x, font):
75+
#formats color and font of text
76+
text_vec = []
77+
for i, xtmp in enumerate(x):
78+
if i % 2 == 0:
79+
ctmp = "#fff"
80+
else:
81+
ctmp = "#ff8c00"
82+
text_vec.append(render.WrappedText(xtmp, font = font, color = ctmp, linespacing = -1))
83+
return (text_vec)
84+
85+
######################################################
86+
#functions
87+
def http_check(URL):
88+
rep = http.get(URL)
89+
if rep.status_code != 200:
90+
fail("ESPN request failed with status %d", rep.status_code)
91+
return rep
92+
93+
def pad_text(text):
94+
#format strings so they are all the same length (leads to better scrolling)
95+
if type(text) == "dict":
96+
max_len = max([len(x) for x in text.values()]) #length of each string
97+
98+
#add padding to shorter titles
99+
for key, val in text.items():
100+
text_new = val + " " * (max_len - len(val))
101+
text[key] = text_new
102+
else:
103+
max_len = max([len(x) for x in text]) #length of each string
104+
105+
#add padding to shorter titles
106+
for i, x in enumerate(text):
107+
text[i] = x + " " * (max_len - len(x))
108+
return text
109+
110+
######################################################

apps/jokesjokeapi/jokesjokeapi.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Package jokesjokeapi provides details for the Jokes JokeAPI applet.
2+
package jokesjokeapi
3+
4+
import (
5+
_ "embed"
6+
7+
"tidbyt.dev/community/apps/manifest"
8+
)
9+
10+
//go:embed jokes_jokeapi.star
11+
var source []byte
12+
13+
// New creates a new instance of the Jokes JokeAPI applet.
14+
func New() manifest.Manifest {
15+
return manifest.Manifest{
16+
ID: "jokes-jokeapi",
17+
Name: "Jokes JokeAPI",
18+
Author: "rs7q5 (RIS)",
19+
Summary: "Displays jokes from JokeAPI",
20+
Desc: "Displays different jokes from JokeAPI.",
21+
FileName: "jokes_jokeapi.star",
22+
PackageName: "jokesjokeapi",
23+
Source: source,
24+
}
25+
}

0 commit comments

Comments
 (0)