/ PyPortal_LastFM / 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 lastFM 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 # 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 = "http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&limit=1&format=json" 23 CAPTION = "www.last.fm/user" 24 # If we have an access token, we can query more often 25 if 'lfm_username' in secrets: 26 DATA_SOURCE += "&user="+secrets['lfm_username'] 27 CAPTION += "/"+secrets['lfm_username'] 28 if 'lfm_key' in secrets: 29 DATA_SOURCE += "&api_key="+secrets['lfm_key'] 30 print(DATA_SOURCE) 31 32 # Total number of plays 33 DATA_LOCATION = ["recenttracks", "@attr", "totalPages"] 34 35 # the current working directory (where this file is) 36 cwd = ("/"+__file__).rsplit('/', 1)[0] 37 pyportal = PyPortal(url=DATA_SOURCE, json_path=DATA_LOCATION, 38 status_neopixel=board.NEOPIXEL, 39 default_bg=cwd+"/lastfm_background.bmp", 40 text_font=cwd+"/fonts/Collegiate-50.bdf", 41 text_position=(150, 160), 42 text_color=0xFFFFFF, 43 caption_text=CAPTION, 44 caption_font=cwd+"/fonts/Collegiate-24.bdf", 45 caption_position=(40, 220), 46 caption_color=0xFFFFFF, 47 debug=True) 48 49 # track the last value so we can play a sound when it updates 50 last_value = 0 51 52 while True: 53 try: 54 value = pyportal.fetch() 55 print("Response is", value) 56 except RuntimeError as e: 57 print("Some error occured, retrying! -", e) 58 59 time.sleep(60)