-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Better music control when multiple players
- Loading branch information
Showing
5 changed files
with
170 additions
and
77 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
#!/usr/bin/python3 | ||
# Usage: music-control [toggle] [previous] [next] | ||
|
||
# Note: Must be run from distro python, since venv doesn't work with PythonGObject | ||
|
||
import gi | ||
import os | ||
import subprocess | ||
import sys | ||
|
||
# Must call this before importing Playerctl | ||
gi.require_version("Playerctl", "2.0") | ||
from gi.repository import Playerctl | ||
|
||
def select_option(options, prompt="Player: "): | ||
if len(options) == 1: | ||
return options[0] | ||
|
||
if os.environ.get("WAYLAND_DISPLAY"): | ||
result = subprocess.run( | ||
[ | ||
"fuzzel", | ||
"--dmenu", | ||
"--lines=%s" % (len(options) + 1), | ||
"--prompt=%s" % prompt, | ||
], | ||
encoding="utf-8", | ||
input=("\n".join(options) + "\n"), | ||
text=True, | ||
stdout=subprocess.PIPE, | ||
) | ||
else: | ||
result = subprocess.run( | ||
["fzf", "--prompt=%s" % prompt], | ||
encoding="utf-8", | ||
input=("\n".join(options) + "\n"), | ||
stdout=subprocess.PIPE, | ||
text=True, | ||
) | ||
if result.returncode != 0: | ||
print("Non-zero FZF return code") | ||
sys.exit(1) | ||
selected_option = result.stdout.strip() | ||
return selected_option | ||
|
||
|
||
def get_track_info(player): | ||
keys = player.props.metadata.keys() | ||
if "xesam:albumArtist" in keys: | ||
return "%s - %s" % ( | ||
player.props.metadata["xesam:albumArtist"][0], | ||
player.get_title(), | ||
) | ||
elif "xesam:artist" in keys: | ||
return "%s - %s" % ( | ||
player.props.metadata["xesam:artist"][0], | ||
player.get_title(), | ||
) | ||
else: | ||
return player.get_title() | ||
|
||
|
||
# In priority order, use: `notify-send`, `tmux`, shell output | ||
def notify(msg, opts={}): | ||
title = opts.get("title", None) or 'Music' | ||
if os.environ.get("WAYLAND_DISPLAY"): | ||
app_name = opts.get("app_name", None) or "Music" | ||
icon = opts.get("icon", None) or "audio-headphones-symbolic" | ||
urgency = opts.get("urgency", None) or "low" | ||
subprocess.run( | ||
[ | ||
"notify-send", | ||
"--app-name=%s" % app_name, | ||
"--urgency=%s" % urgency, | ||
"--icon=%s" % icon, | ||
title, | ||
msg, | ||
] | ||
) | ||
elif os.environ.get("TMUX"): | ||
subprocess.run( | ||
[ | ||
"tmux", | ||
"display-message", | ||
"%s: %s" | ||
% ( | ||
title, | ||
msg, | ||
) | ||
if title | ||
else msg, | ||
] | ||
) | ||
else: | ||
print( | ||
"%s: %s" | ||
% ( | ||
title, | ||
msg, | ||
) | ||
if title | ||
else msg | ||
) | ||
|
||
|
||
def main(): | ||
player_list = Playerctl.list_players() | ||
|
||
all_players = [Playerctl.Player.new_from_name(player) for player in player_list] | ||
|
||
players = [player for player in all_players if player.props.status == "Playing"] | ||
player_state = "playing" | ||
if len(players) == 0: | ||
players = [player for player in all_players if player.props.status == "Paused"] | ||
player_state = "paused" | ||
|
||
if len(players) == 0: | ||
notify("No active players", {"app_name": "Music", "urgency": "normal"}) | ||
sys.exit() | ||
|
||
# Prompt for which player to pause | ||
selected_name = select_option([player.props.player_name for player in players]) | ||
selected_player = [ | ||
player for player in players if player.props.player_name == selected_name | ||
][0] | ||
selected_status = selected_player.props.status | ||
|
||
action = sys.argv[1] if len(sys.argv) > 1 else "toggle" | ||
|
||
# Perform the requested action | ||
if action == "toggle": | ||
selected_player.play_pause() | ||
notify( | ||
get_track_info(selected_player), | ||
{ | ||
"app_name": selected_name, | ||
"title": "Paused" if selected_status == "Playing" else "Playing", | ||
}, | ||
) | ||
elif action == "previous" or action == "next": | ||
if player_state == "paused": | ||
selected_player.play() | ||
if action == "previous": | ||
selected_player.previous() | ||
else: | ||
selected_player.next() | ||
|
||
notify( | ||
get_track_info(selected_player), | ||
{ | ||
"app_name": selected_name, | ||
"title": action.capitalize(), | ||
}, | ||
) | ||
else: | ||
notify("Invalid argument %s. Use 'play/pause', 'previous', or 'next'." % action) | ||
sys.exit(1) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file was deleted.
Oops, something went wrong.
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
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
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