-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.py
executable file
·235 lines (191 loc) · 6.21 KB
/
cli.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import click
import inspect
import re
from config import configure, configure_command, get_spotify_client
from click_aliases import ClickAliasedGroup
from volume import volume
from play import play
@click.group(cls=ClickAliasedGroup, invoke_without_command=True)
@click.pass_context
def cli(ctx):
configure()
if not ctx.invoked_subcommand:
# behave as if --help
click.echo(ctx.command.get_help(ctx))
elif ctx.invoked_subcommand != "config":
sp = get_spotify_client()
if not sp.devices().get("devices"):
ctx.fail(
"No device detected! Try opening spotify on your phone or computer."
)
elif not sp.current_playback():
ctx.fail(
"Your spotify app is currently inactive. Try issuing a command with it first."
)
cli.add_command(volume, name="vol")
cli.add_command(configure_command, name="config")
cli.add_command(play)
@cli.command()
def pause():
"""Pause the playback"""
sp = get_spotify_client()
if sp.current_playback()["is_playing"]:
sp.pause_playback()
@cli.command()
def stop():
"""Stop the playback"""
sp = get_spotify_client()
if sp.current_playback()["is_playing"]:
sp.pause_playback()
sp.seek_track(0)
@cli.command()
def replay():
"""Replay the current song"""
sp = get_spotify_client()
sp.seek_track(0)
if not sp.current_playback()["is_playing"]:
sp.start_playback()
@cli.command()
def next():
"""Skip to the next song"""
get_spotify_client().next_track()
@cli.command(aliases=["prev"])
def previous():
"""Play the previous song"""
get_spotify_client().previous_track()
def shift(seconds: int):
sp = get_spotify_client()
progress_ms = sp.current_playback()["progress_ms"]
sp.seek_track(max(0, progress_ms + seconds * 1000))
@cli.command(aliases=["ffwd"])
@click.argument("seconds", required=False, type=int, default=10)
def fast_forward(seconds: int):
"""Fast-forward by SECONDS (10 by default)"""
shift(seconds)
@cli.command(aliases=["rew"])
@click.argument("seconds", required=False, type=int, default=10)
def rewind(seconds: int):
"""Rewind by SECONDS (10 by default)"""
shift(-seconds)
class TrackTimestampType(click.ParamType):
"""Converts a valid timestamp in `minutes:seconds` format into milliseconds"""
name = "track_timestamp"
def convert(self, value, param, ctx):
prog = re.compile("^([0-5]?[0-9]):([0-6]{1}[0-9]{1})$")
match = prog.search(value)
if not match:
self.fail("Should be in (m)m:ss format, e.g. 1:30", param, ctx)
minutes = int(match.group(1))
seconds = int(match.group(2))
return ((60 * minutes) + seconds) * 1000
@cli.command()
@click.argument("ms", type=TrackTimestampType(), metavar="TIMESTAMP")
def seek(ms: int):
"""Play current song at TIMESTAMP"""
get_spotify_client().seek_track(ms)
@cli.command()
@click.argument(
"state", required=False, type=click.Choice(["on", "off"]),
)
def shuffle(state):
"""Toggle shuffle or explicitly turn it on/off"""
sp = get_spotify_client()
if state == "on":
new_state = True
elif state == "off":
new_state = False
else:
new_state = not sp.current_playback()["shuffle_state"]
sp.shuffle(new_state)
click.echo(f"Shuffle turned {'on' if new_state else 'off'}")
@cli.command(short_help="Set repeat mode")
@click.argument(
"state", type=click.Choice(["track", "context", "off"]),
)
def repeat(state):
"""
Set repeat mode
\b
track Set repeat mode to track
context Set repeat mode to context
off Turn off repeat
"""
get_spotify_client().repeat(state)
if state == "off":
click.echo("Repeat turned off")
else:
click.echo(f"Repeating {state}")
@cli.group(invoke_without_command=True)
@click.pass_context
def share(ctx):
"""Show the current song's url/uri"""
if ctx.invoked_subcommand is None:
ctx.invoke(url)
ctx.invoke(uri)
@share.command()
def url():
"""Show the current song's url"""
item = get_spotify_client().current_playback()["item"]
click.echo(item["external_urls"]["spotify"])
@share.command()
def uri():
"""Show the current song's uri"""
item = get_spotify_client().current_playback()["item"]
click.echo(item["uri"])
def ms_to_duration(milliseconds: int) -> str:
"""
Converts milliseconds into a human-readable duration format
"""
seconds = milliseconds // 1000
minutes, seconds = divmod(seconds, 60)
return "{:02d}:{:02d}".format(minutes, seconds)
@cli.command(short_help="Show playback status, including the elapsed time")
@click.argument(
"info", required=False, type=click.Choice(["track", "album", "artist"]),
)
@click.option("-v", "--verbose", is_flag=True)
def status(info, verbose):
"""Show playback status, including the elapsed time
\b
track Show track title
album Show album title
artist Show artist/s
"""
playback = get_spotify_client().current_playback()
track = playback["item"]
title = track["name"]
album = track["album"]["name"]
artists = ", ".join([a["name"] for a in track["artists"]])
if info == "track":
click.echo(title)
elif info == "album":
click.echo(album)
elif info == "artist":
click.echo(artists)
else:
if playback["is_playing"]:
play_status = "\u23F5"
else:
play_status = "\u23F8" if playback["progress_ms"] else "\u23F9"
progress = ms_to_duration(playback["progress_ms"])
duration = ms_to_duration(track["duration_ms"])
label = "Artists" if len(track["artists"]) > 1 else "Artist"
click.echo(
inspect.cleandoc(
f"""
Title: {title}
Album: {album}
{label}: {artists}
{play_status} {progress} / {duration}
"""
)
)
if verbose:
click.echo(
inspect.cleandoc(
f"""
Repeat {playback["repeat_state"]}
Shuffle {'on' if playback['shuffle_state'] else 'off'}
"""
)
)