python-DiskPlayer/sync_screen.py

85 lines
2.7 KiB
Python
Raw Normal View History

2021-11-08 12:12:12 +00:00
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()
2021-11-09 11:26:01 +00:00
if mopidy.playback.get_state(timeout=5) == "stopped":
2021-11-08 12:12:12 +00:00
oled.text("Nothing", 32, 26, 1)
oled.text("Playing", 32, 36, 1)
return
2021-11-09 11:26:01 +00:00
if mopidy.playback.get_state(timeout=5) == "paused":
2021-11-08 12:12:12 +00:00
oled.text("Paused", 32, 36, 1)
return
2021-11-09 11:26:01 +00:00
current_track_data = mopidy.playback.get_current_track(timeout=5)
2021-11-08 12:12:12 +00:00
time_position = datetime.fromtimestamp(
2021-11-09 11:26:01 +00:00
mopidy.playback.get_time_position(timeout=5) / 1000.0
2021-11-08 12:12:12 +00:00
).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:
2021-11-09 11:26:01 +00:00
stream_title = mopidy.playback.get_stream_title(timeout=5)
2021-11-08 12:12:12 +00:00
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()
2021-11-09 11:26:01 +00:00
time.sleep(3)
2021-11-08 12:12:12 +00:00