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}")
def play_discography(query: str): """Find an artist and play their discography""" sp = get_spotify_client() res = sp.search(query, limit=1, type="artist") items = res["artists"]["items"] if items: sp.start_playback(context_uri=items[0]["uri"]) else: click.echo("No matches found")
def play_list(query: str): """Find a playlist and play it""" sp = get_spotify_client() res = sp.search(query, limit=1, type="playlist") items = res["playlists"]["items"] if items: sp.start_playback(context_uri=items[0]["uri"]) else: click.echo("No matches found")
def increase_volume(): """Increase volume by 10""" sp = get_spotify_client() current = sp.current_playback()["device"]["volume_percent"] if current == 100: click.echo("Already at max volume") else: to = min(current + 10, 100) sp.volume(to) click.echo(f"Volume increased to {to}")
def decrease_volume(): """Decrease volume by 10""" sp = get_spotify_client() current = sp.current_playback()["device"]["volume_percent"] if current == 0: click.echo("Already muted") else: to = max(0, current - 10) sp.volume(to) click.echo(f"Volume decreased to {to}")
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'}")
def play_track(query: str): """Find a track and play it / resume playback""" sp = get_spotify_client() if query: res = sp.search(query, limit=1) items = res["tracks"]["items"] if items: sp.start_playback(uris=[items[0]["uri"]]) else: click.echo("No matches found") elif not sp.current_playback()["is_playing"]: sp.start_playback()
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." )
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'} """))
def pause(): """Pause the playback""" sp = get_spotify_client() if sp.current_playback()["is_playing"]: sp.pause_playback()
def seek(ms: int): """Play current song at TIMESTAMP""" get_spotify_client().seek_track(ms)
def show_volume(): """Show current volume""" sp = get_spotify_client() click.echo(sp.current_playback()["device"]["volume_percent"])
def set_volume(perc: int): """Set volume to PERC""" get_spotify_client().volume(perc)
def shift(seconds: int): sp = get_spotify_client() progress_ms = sp.current_playback()["progress_ms"] sp.seek_track(max(0, progress_ms + seconds * 1000))
def previous(): """Play the previous song""" get_spotify_client().previous_track()
def next(): """Skip to the next song""" get_spotify_client().next_track()
def replay(): """Replay the current song""" sp = get_spotify_client() sp.seek_track(0) if not sp.current_playback()["is_playing"]: sp.start_playback()
def stop(): """Stop the playback""" sp = get_spotify_client() if sp.current_playback()["is_playing"]: sp.pause_playback() sp.seek_track(0)
def uri(): """Show the current song's uri""" item = get_spotify_client().current_playback()["item"] click.echo(item["uri"])
def url(): """Show the current song's url""" item = get_spotify_client().current_playback()["item"] click.echo(item["external_urls"]["spotify"])