69 lines
2.1 KiB
Python
Executable File
69 lines
2.1 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
import json
|
|
import subprocess
|
|
import sys
|
|
import signal
|
|
|
|
def signal_handler(sig, frame):
|
|
sys.exit(0)
|
|
|
|
signal.signal(signal.SIGTERM, signal_handler)
|
|
signal.signal(signal.SIGINT, signal_handler)
|
|
|
|
def get_output(status, artist, title, player):
|
|
if not status or status == "Stopped" or not title:
|
|
return {"text": "", "tooltip": "No media playing", "class": "stopped"}
|
|
|
|
if artist and title:
|
|
text = f"{artist} - {title}"
|
|
elif title:
|
|
text = title
|
|
else:
|
|
return {"text": "", "tooltip": "No media playing", "class": "stopped"}
|
|
|
|
if status == "Playing":
|
|
tooltip = f"▶ {text}"
|
|
display = text
|
|
elif status == "Paused":
|
|
tooltip = f"⏸ {text}"
|
|
display = f"⏸ {text}"
|
|
else:
|
|
tooltip = text
|
|
display = text
|
|
|
|
return {
|
|
"text": display[:40] + "..." if len(display) > 40 else display,
|
|
"tooltip": tooltip,
|
|
"class": status.lower() if status else "stopped"
|
|
}
|
|
|
|
def main():
|
|
# Use playerctl follow to get real-time updates
|
|
try:
|
|
proc = subprocess.Popen(
|
|
['playerctl', '--follow', 'metadata', '--format', '{{status}}|||{{artist}}|||{{title}}|||{{playerName}}'],
|
|
stdout=subprocess.PIPE,
|
|
stderr=subprocess.DEVNULL,
|
|
text=True
|
|
)
|
|
|
|
for line in proc.stdout:
|
|
line = line.strip()
|
|
if not line:
|
|
output = {"text": "", "tooltip": "No media playing", "class": "stopped"}
|
|
else:
|
|
parts = line.split('|||')
|
|
if len(parts) >= 4:
|
|
status, artist, title, player = parts[0], parts[1], parts[2], parts[3]
|
|
output = get_output(status, artist, title, player)
|
|
else:
|
|
output = {"text": "", "tooltip": "No media playing", "class": "stopped"}
|
|
|
|
print(json.dumps(output), flush=True)
|
|
|
|
except Exception as e:
|
|
print(json.dumps({"text": "", "tooltip": "No media playing", "class": "stopped"}), flush=True)
|
|
|
|
if __name__ == "__main__":
|
|
main()
|