load("encoding/base64.star", "base64") load("encoding/json.star", "json") load("http.star", "http") load("render.star", "render") load("schema.star", "schema") load("secret.star", "secret") DEMO_MODE = True ENCRYPTED_CLIENT_TOKEN = "..." DEMO_ART = """iVBORw0KGgoAAAANSUhEUgAAABkAAAAYCAMAAAA4a6b0AAAAJ1BMVEUAAABBJBb+0wvT min//vr71QQEBAT///cMCQQBCwD6zwT/0BL31g9Dk4qKAAAAAXRSTlMAQObYZgAAAItJ REFUKM99ktkSgCAIRQW1bPn/7w1kycg6T8TpojOYkgJGegKASn466hdCVIYxYG2mupIA DujELhDn5kU/a/miGzrGG6WsagDpS2oa4zWN+zNt00FYd61a64b/lMixe6lmdm8ymY vg622iYuPKcGHmHblVZkZBJqZ8eRAGjgvX2Bm3LTFj8nrCq7oAcPEFwi8JX50AAAAAS UVORK5CYII=""" DEMO_TRACK = { "title": "Neon Skyline", "artist": "The Pixels", "artwork": DEMO_ART, } def fetch_track(config): token = secret.decrypt(ENCRYPTED_CLIENT_TOKEN) or config.get("dev_token") if token == None: fail("Missing provider token") response = http.get( "https://music.example/me/now-playing", headers = {"Authorization": "Bearer %s" % token}, ttl_seconds = 30, ) if response.status_code != 200: fail("Music request failed: %d" % response.status_code) return response.json() def main(config): track = DEMO_TRACK if DEMO_MODE else fetch_track(config) return render.Root( delay = 50, child = render.Padding( pad = (2, 4, 2, 4), child = render.Row( cross_align = "center", children = [ render.Image( src = base64.decode(track["artwork"]), width = 24, height = 24, ), render.Box(width = 3, height = 1), render.Column( children = [ render.Marquee( width = 33, child = render.Text(track["title"], color = "#f8fafc"), ), render.Text( track["artist"], font = "CG-pixel-3x5-mono", color = "#a78bfa", ), ], ), ], ), ), ) def oauth_handler(raw_params): params = json.decode(raw_params) token = params.get("access_token") if token == None: fail("Exchange the provider authorization code for a token") return token def get_schema(): return schema.Schema( version = "1", fields = [ schema.OAuth2( id = "auth", name = "Music account", desc = "Connect a music provider.", icon = "key", handler = oauth_handler, client_id = "your-client-id", authorization_endpoint = "https://music.example/oauth/authorize", scopes = ["now-playing:read"], ), ], )