Skip to content

Commit 95f3cb0

Browse files
authored
Add --songuri and --listuri to enable playing a song or playlist Uri (#86)
* Add --openuri to allow specifying a song Uri to play * Add --listuri
1 parent 9be0195 commit 95f3cb0

File tree

2 files changed

+42
-25
lines changed

2 files changed

+42
-25
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ use one of the following parameters:
6767
--playpause plays or pauses the song (toggles a state)
6868
--next plays the next song
6969
--prev plays the previous song
70+
--songuri OPENURI plays the track at the provided Uri
71+
--listuri OPENURI plays the playlist at the provided Uri
7072
--client CLIENT sets client's dbus name
7173
```
7274

spotifycli/spotifycli.py

+40-25
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,10 @@ def main():
5858
perform_spotify_action("Next")
5959
elif args.prev:
6060
perform_spotify_action("Previous")
61-
61+
elif args.songuri:
62+
perform_spotify_action("OpenUri", f"string:spotify:track:{args.songuri}")
63+
elif args.listuri:
64+
perform_spotify_action("OpenUri", f"string:spotify:playlist:{args.listuri}")
6265

6366
def start_shell():
6467
while True:
@@ -82,32 +85,37 @@ def start_shell():
8285
def add_arguments():
8386
parser = argparse.ArgumentParser(description=__doc__)
8487
for argument in get_arguments():
85-
parser.add_argument(argument[0], help=argument[1], action="store_true")
88+
if not argument[2]:
89+
parser.add_argument(argument[0], help=argument[1], action="store_true")
90+
else:
91+
parser.add_argument(argument[0], help=argument[1], action="store")
8692
parser.add_argument("--client", action="store", dest="client",
8793
help="sets client's dbus name", default="spotify")
8894
return parser.parse_args()
8995

9096

9197
def get_arguments():
9298
return [
93-
("--version", "shows version number"),
94-
("--status", "shows song name and artist"),
95-
("--statusposition", "shows song name and artist, with current playback position"),
96-
("--statusshort", "shows status in a short way"),
97-
("--song", "shows the song name"),
98-
("--songshort", "shows the song name in a short way"),
99-
("--artist", "shows artist name"),
100-
("--artistshort", "shows artist name in a short way"),
101-
("--album", "shows album name"),
102-
("--position", "shows song position"),
103-
("--arturl", "shows album image url"),
104-
("--playbackstatus", "shows playback status"),
105-
("--play", "plays the song"),
106-
("--pause", "pauses the song"),
107-
("--playpause", "plays or pauses the song (toggles a state)"),
108-
("--lyrics", "shows the lyrics for the song"),
109-
("--next", "plays the next song"),
110-
("--prev", "plays the previous song")
99+
("--version", "shows version number", False),
100+
("--status", "shows song name and artist", False),
101+
("--statusposition", "shows song name and artist, with current playback position", False),
102+
("--statusshort", "shows status in a short way", False),
103+
("--song", "shows the song name", False),
104+
("--songshort", "shows the song name in a short way", False),
105+
("--artist", "shows artist name", False),
106+
("--artistshort", "shows artist name in a short way", False),
107+
("--album", "shows album name", False),
108+
("--position", "shows song position", False),
109+
("--arturl", "shows album image url", False),
110+
("--playbackstatus", "shows playback status", False),
111+
("--play", "plays the song", False),
112+
("--pause", "pauses the song", False),
113+
("--playpause", "plays or pauses the song (toggles a state)", False),
114+
("--lyrics", "shows the lyrics for the song", False),
115+
("--next", "plays the next song", False),
116+
("--prev", "plays the previous song", False),
117+
("--songuri", "plays the track at the provided Uri", True),
118+
("--listuri", "plays the playlist at the provided Uri", True),
111119
]
112120

113121

@@ -253,11 +261,18 @@ def get_spotify_property(spotify_property):
253261
sys.exit(1)
254262

255263

256-
def perform_spotify_action(spotify_command):
257-
Popen('dbus-send --print-reply --dest=org.mpris.MediaPlayer2."%s" ' %
258-
client +
259-
'/org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player."%s"' %
260-
spotify_command, shell=True, stdout=PIPE)
264+
def perform_spotify_action(spotify_command, extra_arg = None):
265+
command_list = [
266+
"dbus-send",
267+
"--print-reply",
268+
f"--dest=org.mpris.MediaPlayer2.{client}",
269+
"/org/mpris/MediaPlayer2",
270+
f"org.mpris.MediaPlayer2.Player.{spotify_command}",
271+
]
272+
if extra_arg is not None:
273+
command_list.append(extra_arg)
274+
command_string = " ".join(command_list) # could avoid this by taking out shell=False below
275+
Popen(command_string, shell=True, stdout=PIPE)
261276

262277
def show_position():
263278
metadata = get_spotify_property("Metadata")

0 commit comments

Comments
 (0)