import time import board import adafruit_ssd1306 from tenacity import retry, stop_after_attempt from mopidy_json_client import MopidyClient from datetime import datetime @retry(stop=stop_after_attempt(5)) def current_track(): mopidy = MopidyClient() if mopidy.playback.get_state() == "stopped": oled.text("Nothing", 32, 26, 1) oled.text("Playing", 32, 36, 1) return if mopidy.playback.get_state() == "paused": oled.text("Paused", 32, 36, 1) return current_track_data = mopidy.playback.get_current_track() time_position = datetime.fromtimestamp( mopidy.playback.get_time_position() / 1000.0 ).strftime("%M:%S") if "ytmusic" in current_track_data["uri"]: if "album" in current_track_data: totalTime = current_track_data["length"] track_total_length = datetime.fromtimestamp(totalTime / 1000.0).strftime( "%M:%S" ) current_data = { "Artist": current_track_data["artists"][0]["name"], "Album": current_track_data["album"]["name"], "Song": current_track_data["name"], "TrackTime": time_position + "/" + track_total_length, } elif "ytmusic:track" in current_track_data["uri"]: totalTime = current_track_data["length"] track_total_length = datetime.fromtimestamp(totalTime / 1000.0).strftime( "%M:%S" ) current_data = { "Artist": "-", "Album": current_track_data["artists"][0]["name"], "Song": current_track_data["name"], "TrackTime": time_position + "/" + track_total_length, } else: try: stream_title = mopidy.playback.get_stream_title() artist_and_song = stream_title.split(" - ") except: stream_title = "None" artist_and_song = ["None", "None"] current_data = { "Artist": current_track_data["album"]["name"], "Album": artist_and_song[0], "Song": artist_and_song[1], "TrackTime": time_position, } mopidy.disconnect() oled.text(current_data["Artist"], 0, 16, 1) oled.text(current_data["Album"], 0, 26, 1) oled.text(current_data["Song"], 0, 36, 1) oled.text(current_data["TrackTime"], 0, 46, 1) return WIDTH = 128 HEIGHT = 64 # Use for I2C. i2c = board.I2C() oled = adafruit_ssd1306.SSD1306_I2C(WIDTH, HEIGHT, i2c, addr=0x3C) while True: oled.fill(0) oled.text("Floppy Player", 26, 0, 1) current_track() oled.show() time.sleep(2)