/ PyPortal_GithubStars / code.py
code.py
1 # SPDX-FileCopyrightText: 2019 Limor Fried for Adafruit Industries 2 # 3 # SPDX-License-Identifier: MIT 4 5 """ 6 This example will access the github API, grab a number like 7 the number of stars for a repository... and display it on a screen! 8 if you can find something that spits out JSON data, we can display it 9 """ 10 import time 11 import board 12 from adafruit_pyportal import PyPortal 13 14 # Get wifi details and more from a secrets.py file 15 try: 16 from secrets import secrets 17 except ImportError: 18 print("WiFi secrets are kept in secrets.py, please add them there!") 19 raise 20 21 # Set up where we'll be fetching data from 22 DATA_SOURCE = "https://api.github.com/repos/adafruit/circuitpython" 23 CAPTION = "www.github.com/adafruit/circuitpython" 24 # If we have an access token, we can query more often 25 if 'github_token' in secrets: 26 DATA_SOURCE += "?access_token="+secrets['github_token'] 27 28 # The data we want to display 29 DATA_LOCATION = ["stargazers_count"] 30 31 cwd = ("/"+__file__).rsplit('/', 1)[0] 32 pyportal = PyPortal(url=DATA_SOURCE, json_path=DATA_LOCATION, 33 status_neopixel=board.NEOPIXEL, 34 default_bg=cwd+"/stars_background.bmp", 35 text_font=cwd+"/fonts/Collegiate-50.bdf", 36 text_position=(200, 100), 37 text_color=0xFFFFFF, 38 caption_text=CAPTION, 39 caption_font=cwd+"/fonts/Arial.bdf", 40 caption_position=(40, 220), 41 caption_color=0xFFFFFF) 42 43 # track the last value so we can play a sound when it updates 44 last_value = 0 45 46 while True: 47 try: 48 value = pyportal.fetch() 49 print("Response is", value) 50 if last_value < value: # ooh it went up! 51 print("New star!") 52 pyportal.play_file(cwd+"/coin.wav") 53 last_value = value 54 except (ValueError, RuntimeError, ConnectionError, OSError) as e: 55 print("Some error occured, retrying! -", e) 56 57 time.sleep(60) # wait a minute before getting again