Skip to content

Commit

Permalink
Ensure content is centered (#2763)
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelYagi authored Oct 7, 2024
1 parent b33622d commit 6ca37f4
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 26 deletions.
Binary file added apps/apiimage/api_image.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
70 changes: 44 additions & 26 deletions apps/apiimage/api_image.star
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
"""
Applet: API image
Summary: API image display
Description: Display an image from an API endpoint.
Author: Michael Yagi
"""
Applet: API image
Summary: API image display
Description: Display an image from an API endpoint.
Author: Michael Yagi
"""

load("encoding/json.star", "json")
load("http.star", "http")
load("random.star", "random")
load("render.star", "render")
load("schema.star", "schema")
load("time.star", "time")

def main(config):
base_url = config.str("base_url", "")
random.seed(time.now().unix // 10)

api_url = config.str("api_url", "")
response_path = config.get("response_path", "")
request_headers = config.get("request_headers", "")
Expand All @@ -23,16 +26,16 @@ def main(config):
if debug_output:
print("------------------------------")
print("CONFIG - api_url: " + api_url)
print("CONFIG - base_url: " + base_url)
print("CONFIG - response_path: " + response_path)
print("CONFIG - request_headers: " + request_headers)
print("CONFIG - debug_output: " + str(debug_output))
print("CONFIG - fit_screen: " + str(fit_screen))
print("CONFIG - ttl_seconds: " + str(ttl_seconds))

return get_image(base_url, api_url, response_path, request_headers, debug_output, fit_screen, ttl_seconds)
return get_image(api_url, response_path, request_headers, debug_output, fit_screen, ttl_seconds)

def get_image(base_url, api_url, response_path, request_headers, debug_output, fit_screen, ttl_seconds):
def get_image(api_url, response_path, request_headers, debug_output, fit_screen, ttl_seconds):
base_url = ""
failure = False
message = ""

Expand Down Expand Up @@ -86,7 +89,20 @@ def get_image(base_url, api_url, response_path, request_headers, debug_output, f

for item in responsePathArray:
item = item.strip()
if item.isdigit():

if item == "[rand]":
if type(output) == "list":
item = random.number(0, len(output) - 1)
if debug_output:
print("Random index chosen " + str(item))
else:
failure = True
message = "Response path invalid. Use of [rand] only allowable in lists."
if debug_output:
print("responsePathArray invalid. Use of [rand] only allowable in lists.")
break

if type(item) != "int" and item.isdigit():
item = int(item)

if debug_output:
Expand All @@ -98,22 +114,29 @@ def get_image(base_url, api_url, response_path, request_headers, debug_output, f
output = output[item]
else:
failure = True
message = "Response path invalid. " + str(item) + " does not exist"
message = "Response path invalid. " + str(item) + " does not exist or value is null"
if debug_output:
print("responsePathArray invalid. " + str(item) + " does not exist")
print("responsePathArray invalid. " + str(item) + " does not exist or value is null")
break

if debug_output:
print("Response content type JSON")

api_url_array = api_url.split("/")
if len(api_url_array) > 2:
base_url = api_url_array[0] + "//" + api_url_array[2]

if type(output) == "string" and output.startswith("http") == False and (base_url == "" or base_url.startswith("http") == False):
failure = True
message = "Base URL required"
if debug_output:
print("Invalid URL. Requires a base_url")
elif type(output) == "string":
if output.startswith("http") == False and base_url != "":
url = base_url + output
if output.startswith("/"):
url = base_url + output
else:
url = base_url + "/" + output
else:
url = output

Expand Down Expand Up @@ -151,11 +174,13 @@ def get_image(base_url, api_url, response_path, request_headers, debug_output, f
)

return render.Root(
render.Row(
expanded = True,
main_align = "space_evenly",
cross_align = "center",
children = [imgRender],
child = render.Box(
render.Row(
expanded = True,
main_align = "space_evenly",
cross_align = "center",
children = [imgRender],
),
),
)

Expand Down Expand Up @@ -264,17 +289,10 @@ def get_schema():
default = "",
# default = "https://dog.ceo/api/breeds/image/random",
),
schema.Text(
id = "base_url",
name = "Base URL",
desc = "The base URL if response JSON contains relative paths.",
icon = "",
default = "",
),
schema.Text(
id = "response_path",
name = "JSON response path",
desc = "A comma separated path to the image URL in the response JSON. eg. `json_key_1, 2, json_key_to_image_url`",
desc = "A comma separated path to the image URL in the response JSON. Use `[rand]` to choose a random index. eg. `json_key_1, 0, [rand], json_key_to_image_url`",
icon = "",
default = "",
# default = "message",
Expand Down

0 comments on commit 6ca37f4

Please sign in to comment.