Created
February 22, 2023 22:07
-
-
Save axwax/49377aaa4792a42af7c037b8f1c81681 to your computer and use it in GitHub Desktop.
MicroPython script to grab @[email protected]'s now playing metadata and display it on a Pimoroni Pico GFX Pack
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
# MicroPython script to grab @[email protected]'s now playing metadata and display it on a Pimoroni Pico GFX Pack | |
# requires WIFI_CONFIG.py and network_manager.py as outlined at https://github.com/pimoroni/pimoroni-pico/tree/main/micropython/examples/gfx_pack | |
# see https://fosstodon.org/@axwax/109910396644259122 for details | |
import WIFI_CONFIG | |
import time | |
from gfx_pack import GfxPack | |
from network_manager import NetworkManager | |
import urequests | |
import uasyncio | |
import re | |
# set up variables | |
instance = 'fosstodon.org' # change to your own Mastodon instance | |
token = '' # your Mastodon API token | |
account_id = 109822578252506735 # this is the id for @[email protected] | |
header = {'Authorization': 'Bearer ' + token} | |
request_url = 'https://' + instance + '/api/v1/accounts/' + str(account_id) + '/statuses?limit=1' # only | |
sys_status = "STATUS" | |
old_status = sys_status | |
# initialise the display | |
gp = GfxPack() | |
display = gp.display | |
display.set_font("bitmap8") | |
display.set_backlight(0) | |
WIDTH, HEIGHT = display.get_bounds() | |
# update the display | |
def display_status(): | |
global sys_status | |
display.set_pen(0) # Set pen to white | |
display.clear() | |
display.set_pen(15) | |
display.text(sys_status, 0, 0, WIDTH-1, 1) | |
display.update() | |
# grab the latest status from @[email protected] | |
def grab_status(): | |
global header, old_status, request_url, sys_status | |
gp.set_backlight(0, 40, 0) # set backlight to a light green | |
res = urequests.get(request_url, headers = header).json() | |
# filter out any HTML tags and strings we don't want to display | |
# (this is very hacky and will break if the bot's toot format changes) | |
sys_status = re.sub('<[^>]+>', '', res[0]['content']) | |
sys_status = re.sub('Now playing on radio free fedi: ', 'radio free fedi now playing: \n\r', sys_status) | |
sys_status = re.sub('Tune in now:', '\n\r', sys_status) | |
sys_status = re.sub('your 24/7 community radio from the fediverse to the universe', '\n\r', sys_status) | |
sys_status = re.sub(r'(https|http|ftp):\/\/\S+', '\n\r', sys_status) | |
gp.set_backlight(20, 20, 0) # let's have a mellow yellow backlight | |
# update the display only if there is new song metadata | |
if(sys_status != old_status): | |
old_status = sys_status | |
gp.set_backlight(0, 150, 0) # a new song deserves a bright green backlight for a few seconds! | |
display_status() | |
time.sleep(5) | |
gp.set_backlight(20, 20, 0) # and we're back to mellow yellow | |
#display.update() | |
def status_handler(mode, status, ip): | |
# reports wifi connection status | |
global sys_status | |
if(status): | |
sys_status = 'connected as ' + str(ip) | |
else: | |
sys_status = 'connecting' | |
display_status() | |
# connect to Wifi | |
try: | |
network_manager = NetworkManager(WIFI_CONFIG.COUNTRY, status_handler=status_handler) | |
uasyncio.get_event_loop().run_until_complete(network_manager.client(WIFI_CONFIG.SSID, WIFI_CONFIG.PSK)) | |
except Exception as e: | |
sys_status = 'Wifi connection failed!' | |
display_status() | |
# grab Metadata from @[email protected] every 20 seconds | |
while True: | |
grab_status() | |
time.sleep(20) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment