|
| 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 | +###################################################### |
0 commit comments