/ PyPortal_CMA_Art_Frame / code.py
code.py
1 # SPDX-FileCopyrightText: 2020 Dan Cogliano for Adafruit Industries 2 # 3 # SPDX-License-Identifier: MIT 4 5 import time 6 import random 7 import board 8 from adafruit_pyportal import PyPortal 9 from adafruit_display_shapes.circle import Circle 10 11 WIDTH = board.DISPLAY.width 12 HEIGHT = board.DISPLAY.height 13 14 #pylint: disable=line-too-long 15 16 # these lines show the entire collection 17 APIURL = "https://openaccess-api.clevelandart.org/api/artworks?cc0=1&has_image=1&indent=2&limit=1&skip=" 18 IMAGECOUNT = 31954 19 20 # uncomment these lines to show just paintings 21 # APIURL = "https://openaccess-api.clevelandart.org/api/artworks?cc0=1&has_image=1&indent=2&limit=1&type=Painting&skip=" 22 # IMAGECOUNT = 3223 23 24 BACKGROUND_FILE = "/background.bmp" 25 if WIDTH > 320: 26 BACKGROUND_FILE = "/background_480.bmp" 27 28 pyportal = PyPortal(default_bg=BACKGROUND_FILE, 29 image_json_path=["data", 0, "images", "web", "url"], 30 image_dim_json_path=(["data", 0, "images", "web", "width"], 31 ["data", 0, "images", "web", "height"]), 32 image_resize=(WIDTH, HEIGHT - 15), 33 image_position=(0, 0), 34 text_font="/fonts/OpenSans-9.bdf", 35 json_path=["data", 0, "title"], 36 text_position=(4, HEIGHT - 9), 37 text_color=0xFFFFFF) 38 39 circle = Circle(WIDTH - 8, HEIGHT - 7, 5, fill=0) 40 pyportal.splash.append(circle) 41 loopcount = 0 42 errorcount = 0 43 while True: 44 response = None 45 try: 46 circle.fill = 0xFF0000 47 itemid = random.randint(1, IMAGECOUNT) 48 # itemid = 20 # portrait mode example 49 # itemid = 21 # landscape mode example 50 print("retrieving url:", APIURL + str(itemid)) 51 response = pyportal.fetch(APIURL + str(itemid)) 52 circle.fill = 0 53 print("Response is", response) 54 loopcount = loopcount + 1 55 56 except (RuntimeError, KeyError, TypeError) as e: 57 print("An error occured, retrying! -", e) 58 print("loop counter:", loopcount) 59 assert errorcount < 20, "Too many errors, stopping" 60 errorcount = errorcount + 1 61 time.sleep(60) 62 continue 63 64 errorcount = 0 65 stamp = time.monotonic() 66 # wait 5 minutes before getting again 67 while (time.monotonic() - stamp) < (5*60): 68 # or, if they touch the screen, fetch immediately! 69 if pyportal.touchscreen.touch_point: 70 break