You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

59 lines
2.2 KiB

import os
import time
import structures
from modules import renderer
focus = 0
while True:
# Get the terminal size
rows, columns = os.popen('stty size', 'r').read().split()
rows, columns = int(rows), int(columns)
sources = os.popen("playerctl -l -s", "r").read().splitlines()
processed_sources = []
for source in sources:
metadata = os.popen(f"playerctl metadata -p {source} -s", "r").read().splitlines()
status = os.popen(f'playerctl status -p {source} -s', 'r').read().strip()
watched = float(os.popen(f'playerctl position -p {source} -s', 'r').read().strip() or "0")
metadata_dict = {}
for line in metadata:
split = line.split()
key = split[1]
value = " ".join(split[2:]) if len(split) > 2 else None
metadata_dict[key] = value
if "mpris:length" in metadata_dict:
metadata_dict["mpris:length"] = round(float(metadata_dict.get("mpris:length", 1)) / 10**6)
watched = round(watched)
if "xesam:url" in metadata_dict:
# Get everything after the last slash
metadata_dict["xesam:url"] = metadata_dict["xesam:url"].split("/")[-1]
title = ["xesam:title", "xesam:url"]
source_title = "Unknown Title"
for t in title:
if t in metadata_dict:
source_title = metadata_dict[t]
break
artist = ["xesam:artist", "xesam:albumArtist", "vlc:publisher"]
source_artist = None
for a in artist:
if a in metadata_dict:
source_artist = metadata_dict[a]
break
processed_sources.append(structures.Source(
source=source.split(".")[0],
name=source_title,
artist=source_artist,
length=metadata_dict.get('mpris:length', 1),
watched=watched,
status=status
))
focus = min(max(focus, 0), len(processed_sources) - 1) if processed_sources else -1
processed_sources = sorted(processed_sources, key=lambda source: source.source)
out = renderer.render(rows, columns, processed_sources, focus) or {}
time.sleep(0.25 * out.get("delay_multiplier", 1))