Created
January 22, 2022 18:07
-
-
Save rsbohn/83a632c81d44841aa6a952fcbb8efa4b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# SPDX-FileCopyrightText: Copyright (c) 2022 Randall Bohn (dexter) | |
# | |
# SPDX-License-Identifier: MIT | |
# | |
# View random products from Adafruit on the PyPortal | |
from random import randrange | |
from adafruit_pyportal import PyPortal | |
base_url = "https://adafruit.com/api/product/" | |
portal = PyPortal( | |
url=base_url+"4864", | |
json_path=(['product_name'], | |
['date_added'], | |
['product_stock'], | |
['product_id'], ['product_price']), | |
text_font="/fonts/Arial-18.pcf", | |
text_position=((20,60), (20,120), (20,144), (20,240), (120,240)), | |
text_color=(0xAAAAAA, 0xCCCCCC, 0xCCCCCC, 0xFFFF33, 0x33FF33), | |
text_wrap=(32, 0, 0, 0, 0), | |
default_bg=0x302010 | |
) | |
import time | |
from supervisor import runtime as RT | |
print("Initializing...") | |
value = portal.fetch() | |
last_update = time.monotonic() | |
print("Ready!") | |
while True: | |
age = time.monotonic() - last_update | |
if age > 60: | |
try: | |
portal.url = base_url + str(randrange(8000)) | |
print(portal.url) | |
value = portal.fetch() | |
print(value) | |
except (ValueError, RuntimeError) as error: | |
print("Alas,", error) | |
last_update = time.monotonic() | |
if RT.serial_bytes_available: break | |
time.sleep(0.1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment