/ PyPortal_Reddit / 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 reddit API, grab a number like subreddit followers 7 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 SUBREDDIT = "circuitpython" 15 16 # Set up where we'll be fetching data from 17 DATA_SOURCE = "https://www.reddit.com/r/"+SUBREDDIT+"/about.json" 18 DATA_LOCATION = ["data", "subscribers"] 19 CAPTION="reddit.com/r/"+SUBREDDIT 20 21 # the current working directory (where this file is) 22 cwd = ("/"+__file__).rsplit('/', 1)[0] 23 pyportal = PyPortal(url=DATA_SOURCE, json_path=DATA_LOCATION, 24 status_neopixel=board.NEOPIXEL, 25 default_bg=cwd+"/reddit_background.bmp", 26 text_font=cwd+"/fonts/Collegiate-50.bdf", 27 text_position=(210, 80), 28 text_color=0xFFFFFF, 29 caption_text=CAPTION, 30 caption_font=cwd+"/fonts/Collegiate-24.bdf", 31 caption_position=(40, 200), 32 caption_color=0xFFFFFF) 33 34 # track the last value so we can play a sound when it updates 35 last_value = 0 36 37 while True: 38 try: 39 value = pyportal.fetch() 40 print("Response is", value) 41 if last_value < value: # ooh it went up! 42 print("New subscriber!") 43 pyportal.play_file(cwd+"/coin.wav") 44 last_value = value 45 except (ValueError, RuntimeError, ConnectionError, OSError) as e: 46 print("Some error occured, retrying! -", e) 47 48 time.sleep(60)