Instantly share code, notes, and snippets.

@wandernauta /sp
Last active Dec 11, 2018

Embed
What would you like to do?
sp is a command-line client for Spotify's dbus interface. Play, pause, skip and search tracks from the comfort of your command line.
#!/usr/bin/env bash
#
# This is sp, the command-line Spotify controller. It talks to a running
# instance of the Spotify Linux client over dbus, providing an interface not
# unlike mpc.
#
# Put differently, it allows you to control Spotify without leaving the comfort
# of your command line, and without a custom client or Premium subscription.
#
# As an added bonus, it also works with ssh, at and cron.
#
# Example:
# $ sp weather girls raining men
# $ sp current
# Album 100 Hits Of The '80s
# Artist The Weather Girls
# Title It's Raining Men
# $ sp pause
#
# Alarm clock example:
# $ at 7:45 <<< 'sp bangarang'
#
# Remote example:
# $ ssh vader@prod02.nomoon.ta 'sp imperial march'
#
#
# Copyright (C) 2013 Wander Nauta
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software, to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to permit
# persons to whom the Software is furnished to do so, subject to the following
# conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# The software is provided "as is", without warranty of any kind, express or
# implied, including but not limited to the warranties of merchantability,
# fitness for a particular purpose and noninfringement. In no event shall the
# authors or copyright holders be liable for any claim, damages or other
# liability, whether in an action of contract, tort or otherwise, arising from,
# out of or in connection with the software or the use or other dealings in the
# software.
#
# CONSTANTS
SP_VERSION="0.1"
SP_DEST="org.mpris.MediaPlayer2.spotify"
SP_PATH="/org/mpris/MediaPlayer2"
SP_MEMB="org.mpris.MediaPlayer2.Player"
# SHELL OPTIONS
shopt -s expand_aliases
# UTILITY FUNCTIONS
function require {
hash $1 2>/dev/null || {
echo >&2 "Error: '$1' is required, but was not found."; exit 1;
}
}
# COMMON REQUIRED BINARIES
# We need dbus-send to talk to Spotify.
require dbus-send
# Assert standard Unix utilities are available.
require grep
require sed
require cut
require tr
# 'SPECIAL' (NON-DBUS-ALIAS) COMMANDS
function sp-dbus {
# Sends the given method to Spotify over dbus.
dbus-send --print-reply --dest=$SP_DEST $SP_PATH $SP_MEMB.$1 ${*:2} > /dev/null
}
function sp-open {
# Opens the given spotify: URI in Spotify.
sp-dbus OpenUri string:$1
}
function sp-metadata {
# Prints the currently playing track in a parseable format.
dbus-send \
--print-reply `# We need the reply.` \
--dest=$SP_DEST \
$SP_PATH \
org.freedesktop.DBus.Properties.Get \
string:"$SP_MEMB" string:'Metadata' \
| grep -Ev "^method" `# Ignore the first line.` \
| grep -Eo '("(.*)")|(\b[0-9][a-zA-Z0-9.]*\b)' `# Filter interesting fiels.`\
| sed -E '2~2 a|' `# Mark odd fields.` \
| tr -d '\n' `# Remove all newlines.` \
| sed -E 's/\|/\n/g' `# Restore newlines.` \
| sed -E 's/(xesam:)|(mpris:)//' `# Remove ns prefixes.` \
| sed -E 's/^"//' `# Strip leading...` \
| sed -E 's/"$//' `# ...and trailing quotes.` \
| sed -E 's/"+/|/' `# Regard "" as seperator.` \
| sed -E 's/ +/ /g' `# Merge consecutive spaces.`
}
function sp-current {
# Prints the currently playing track in a friendly format.
require column
sp-metadata \
| grep --color=never -E "(title)|(album)|(artist)" \
| sed 's/^\(.\)/\U\1/' \
| column -t -s'|'
}
function sp-eval {
# Prints the currently playing track as shell variables, ready to be eval'ed
require sort
sp-metadata \
| grep --color=never -E "(title)|(album)|(artist)|(trackid)|(trackNumber)" \
| sort -r \
| sed 's/^\([^|]*\)\|/\U\1/' \
| sed -E 's/\|/="/' \
| sed -E 's/$/"/' \
| sed -E 's/^/SPOTIFY_/'
}
function sp-art {
# Prints the artUrl.
sp-metadata | grep "artUrl" | cut -d'|' -f2
}
function sp-display {
# Calls display on the artUrl.
require display
display $(sp-art)
}
function sp-feh {
# Calls feh on the artURl.
require feh
feh $(sp-art)
}
function sp-url {
# Prints the HTTP url.
TRACK=$(sp-metadata | grep "url" | cut -d'|' -f2 | cut -d':' -f3)
echo "http://open.spotify.com/track/$TRACK"
}
function sp-clip {
# Copies the HTTP url.
require xclip
sp-url | xclip
}
function sp-http {
# xdg-opens the HTTP url.
require xdg-open
xdg-open $(sp-url)
}
function sp-help {
# Prints usage information.
echo "Usage: sp [command]"
echo "Control a running Spotify instance from the command line."
echo ""
echo " sp play - Play/pause Spotify"
echo " sp pause - Pause Spotify"
echo " sp next - Go to next track"
echo " sp prev - Go to previous track"
echo ""
echo " sp current - Format the currently playing track"
echo " sp metadata - Dump the current track's metadata"
echo " sp eval - Return the metadata as a shell script"
echo ""
echo " sp art - Print the URL to the current track's album artwork"
echo " sp display - Display the current album artwork with \`display\`"
echo " sp feh - Display the current album artwork with \`feh\`"
echo ""
echo " sp url - Print the HTTP URL for the currently playing track"
echo " sp clip - Copy the HTTP URL to the X clipboard"
echo " sp http - Open the HTTP URL in a web browser"
echo ""
echo " sp open <uri> - Open a spotify: uri"
echo " sp search <q> - Start playing the best search result for the given query"
echo ""
echo " sp version - Show version information"
echo " sp help - Show this information"
echo ""
echo "Any other argument will start a search (i.e. 'sp foo' will search for foo)."
}
function sp-search {
# Searches for tracks, plays the first result.
require curl
Q="$@"
SPTFY_URI=$( \
curl -s -G --data-urlencode "q=$Q" https://api.spotify.com/v1/search\?type=track \
| grep -E -o "spotify:track:[a-zA-Z0-9]+" -m 1 \
)
sp-open $SPTFY_URI
}
function sp-version {
# Prints version information.
echo "sp $SP_VERSION"
echo "Copyright (C) 2013 Wander Nauta"
echo "License MIT"
}
# 'SIMPLE' (DBUS-ALIAS) COMMANDS
alias sp-play=" sp-dbus PlayPause"
alias sp-pause=" sp-dbus Pause"
alias sp-next=" sp-dbus Next"
alias sp-prev=" sp-dbus Previous"
# DISPATCHER
# First, we connect to the dbus session spotify is on. This isn't really needed
# when running locally, but is crucial when we don't have an X display handy
# (for instance, when running sp over ssh.)
SPOTIFY_PID="$(pidof -s spotify || pidof -s .spotify-wrapped)"
if [[ -z "$SPOTIFY_PID" ]]; then
echo "Error: Spotify is not running."
exit 1
fi
QUERY_ENVIRON="$(cat /proc/${SPOTIFY_PID}/environ | tr '\0' '\n' | grep "DBUS_SESSION_BUS_ADDRESS" | cut -d "=" -f 2-)"
if [[ "${QUERY_ENVIRON}" != "" ]]; then
export DBUS_SESSION_BUS_ADDRESS="${QUERY_ENVIRON}"
fi
# Then we dispatch the command.
subcommand="$1"
if [[ -z "$subcommand" ]]; then
# No arguments given, print help.
sp-help
else
# Arguments given, check if it's a command.
if $(type sp-$subcommand > /dev/null 2> /dev/null); then
# It is. Run it.
shift
eval "sp-$subcommand $@"
else
# It's not. Try a search.
eval "sp-search $@"
fi
fi
@borkabrak

This comment has been minimized.

borkabrak commented Jun 19, 2014

This works perfectly. Super handy. Thanks!

@evasconcelos

This comment has been minimized.

evasconcelos commented Sep 1, 2014

finally got my xfce4 pause/next/play key bindings to work with spotify.

@funktapus

This comment has been minimized.

funktapus commented Nov 14, 2014

Works great! Would there by an easy way to integrate login and logout into this script?

@Ps0ke

This comment has been minimized.

Ps0ke commented Nov 23, 2014

I've created an AUR package for Arch Linux users: https://aur.archlinux.org/packages/sp/

@AnwariasEu

This comment has been minimized.

AnwariasEu commented Feb 9, 2015

Nice script thank you for that !

@perilstar

This comment has been minimized.

perilstar commented May 10, 2015

How do I install this?

@ilario92

This comment has been minimized.

ilario92 commented May 17, 2015

Thank!! 😄

@jamespo

This comment has been minimized.

jamespo commented Jun 14, 2015

Fantastic script

@nikreiman

This comment has been minimized.

nikreiman commented Aug 5, 2015

@wandernauta Thanks so much for this script! I've been using it to debug DBus support in the 1.x Spotify Linux client, it's been a real lifesaver. ;)

@jianinz

This comment has been minimized.

jianinz commented Sep 6, 2015

This is great, thank you so much!

@johannfr

This comment has been minimized.

johannfr commented Sep 8, 2015

Great script! Created another script that pauses spotify and then locks my (enlightenment) screen (which is also done over DBus). :D

@juesato

This comment has been minimized.

juesato commented Dec 21, 2015

thanks a bunch - this works great!

@vervaekejonathan

This comment has been minimized.

vervaekejonathan commented Jan 3, 2016

still works! thanks!

@kloga

This comment has been minimized.

kloga commented Apr 12, 2016

Excellent script to control Spotify from command line, to make shorcuts con media keys, and even automatize or integrate with Conky. Thank you so much :D
@duncan-bayne

This comment has been minimized.

duncan-bayne commented May 12, 2016

Thanks for the script! I found the search functionality broken due to Spotify deprecating their API, so I forked and fixed it:

https://gist.github.com/duncan-bayne/3f7ef98a15b02b693bf47a03fda79b3a

@kanishkaganguly

This comment has been minimized.

kanishkaganguly commented Jul 8, 2016

Excellent stuff! Works like a boss with xbindkeys to replace the media keys on my work keyboard. Thanks so much.

@riccardomc

This comment has been minimized.

riccardomc commented Jul 26, 2016

Thanks! :)

@dedeibel

This comment has been minimized.

dedeibel commented Sep 14, 2016

Hi, any idea how to find out if spotify is currently playing or in pause state? Metatada does always return the currently active song.
Edit: I found this "solution" ... but it works https://muffinresearch.co.uk/ubuntu-lock-screen-and-pause-spotify/

@stumash

This comment has been minimized.

stumash commented Sep 14, 2016

search does not work for me, it takes a moment and does/says nothing. open does work, though.
It looks like the Metadata API has been discontinued by Spotify so your search function does not work with the most recent version of Spotify for linux. They have a page on migrating from the old 'metadata api' to the new 'web api'

@leothelocust

This comment has been minimized.

leothelocust commented Oct 19, 2016

In order for the search to work, you'll have to update line 215 to the new API url @wandernauta.

curl -s -G --data-urlencode "q=$Q" https://api.spotify.com/v1/search\?type=track \

Thanks for this awesome utility!

@wandernauta

This comment has been minimized.

Owner

wandernauta commented Dec 4, 2016

Thank you, @leothelocust! That works like a charm. I've updated the gist.

@Mladia

This comment has been minimized.

Mladia commented Feb 8, 2017

Brilliant! 👍

@chobot2014

This comment has been minimized.

chobot2014 commented Feb 10, 2017

I'm usually on linux, but at my current job I have to use windows (.NET) and I was wondering if anyone knew of a way that I could do this same thing on windows?

@dakujem

This comment has been minimized.

dakujem commented Mar 1, 2017

Still works!
This allows to have 2 or more keyboard shortcuts to play/pause spotify - by default the standard media keys will work and you can create a new shortcut to do run sp play in keyboard settings > shortcuts > custom shortcuts section and assing it to whatever other keyboard shortcut (useful with Ubuntu notebooks when switching between external keyboard and an integrated one that does not have media keys).

@dbatten5

This comment has been minimized.

dbatten5 commented Mar 13, 2017

is there a way to save the current song to the library?

@o1354921

This comment has been minimized.

o1354921 commented Apr 2, 2017

Great tool, but I ran into problems with "sp eval" when the title(or any other information) contained double quotes.
In order to get it working I would suggest the following change in the function called sp-eval:

sp-metadata
| grep --color=never -E "(title)|(album)|(artist)|(trackid)|(trackNumber)"
| sort -r
| sed 's/^([^|]*)|/\U\1/'
| sed 's/"/\"/g' \ # ESCAPE THE DOUBLE QUOTES
| sed -E 's/|/="/'
| sed -E 's/$/"/'
| sed -E 's/^/SPOTIFY_/'

@sbmkvp

This comment has been minimized.

sbmkvp commented May 25, 2017

You can extend the search via these functions.

function sp-list {
	# Searches for playlists, plays the first result.
	require curl
	Q="$@"
	SPTFY_URI=$( \
		curl -s -G --data-urlencode "q=$Q" https://api.spotify.com/v1/search\?type=playlist \
		| grep -E -o "spotify:user:.*:playlist:[a-zA-Z0-9]+" -m 1 \
	)
	sp-open $SPTFY_URI
}

function sp-album {
	# Searches for albums, plays the first result.
	require curl
	Q="$@"
	SPTFY_URI=$( \
		curl -s -G --data-urlencode "q=$Q" https://api.spotify.com/v1/search\?type=album \
		| grep -E -o "spotify:album:[a-zA-Z0-9]+" -m 1 \
	)
	sp-open $SPTFY_URI
}

function sp-artist {
	# Searches for artists, plays the first result.
	require curl
	Q="$@"
	SPTFY_URI=$( \
		curl -s -G --data-urlencode "q=$Q" https://api.spotify.com/v1/search\?type=artist \
		| grep -E -o "spotify:artist:[a-zA-Z0-9]+" -m 1 \
	)
	sp-open $SPTFY_URI
}
@ivanmilov

This comment has been minimized.

ivanmilov commented May 30, 2017

Hello! What about volume control? Does spotify ignore it?

@sbmkvp

This comment has been minimized.

sbmkvp commented May 31, 2017

@ivanmilov I am sure the d-bus mpris support is not complete in the spotify client. Most of the methods and variables do not work. I think volume control must be one of them

They also made the authentication mandatory for the api now so all the search functions do not work. It needs a token with authorisation code or client credentials :( Will definitely try and fix this as I figure them out.

@vorbeiei

This comment has been minimized.

vorbeiei commented Jun 2, 2017

@sbmkvp I had a go at the authentication and came up with a working fix. Might not be the greatest one but I just wanted it to get working again. I used the information from here which also links to the site to register your application for your own Client ID and Client Secret.

Edit: Actually if you have a look at my fork of sp, I added your search functions and you just have to fill in the variables at the top with your own ID and Secret and it should work fine.

function sp-search {
  # Searches for tracks, plays the first result.

  require curl
    #send request for token with ID and SecretID encoded to base64->grep take only  token from reply->trim reply down to token-> modified request to include token in header
  Q="$@"
    ST=$(curl -H "Authorization: Basic YOURBASE64ID:SECRETHERE" -d grant_type=client_credentials https://accounts.spotify.com/api/token \
    | grep -E -o "access_token\":\"[a-zA-Z0-9_-]+\"" -m 1 )

    ST2=${ST:15:86}}
  SPTFY_URI=$( \
    curl -H "Authorization: Bearer $ST2" -s -G --data-urlencode "q=$Q" https://api.spotify.com/v1/search\?type=track \
    | grep -E -o "spotify:track:[a-zA-Z0-9]+" -m 1 \
  )

  sp-open $SPTFY_URI
}
@rarrais

This comment has been minimized.

rarrais commented Jul 27, 2017

Thank you @wandernauta for the cli and @vorbeiei for the search alterations!

I've further revisioned @vorbeiei's verison on my fork of sp to make curl silent when performing searches.

function sp-search {
  # Searches for tracks, plays the first result.

  require curl
    #send request for token with ID and SecretID encoded to base64->grep take only  token from reply->trim reply down to token-> modified request to include token in header
  Q="$@"
    ST=$(curl -H "Authorization: Basic $SP_B64ID" -d grant_type=client_credentials https://accounts.spotify.com/api/token --silent\
    | grep -E -o "access_token\":\"[a-zA-Z0-9_-]+\"" -m 1 )

    ST2=${ST:15:86}}
  SPTFY_URI=$( \
    curl -H "Authorization: Bearer $ST2" -s -G --data-urlencode "q=$Q" https://api.spotify.com/v1/search\?type=track \
    | grep -E -o "spotify:track:[a-zA-Z0-9]+" -m 1 \
  )

  sp-open $SPTFY_URI
}
@pyrho

This comment has been minimized.

pyrho commented Aug 2, 2017

🎉

@boardfish

This comment has been minimized.

boardfish commented Nov 6, 2017

@rarrais' version isn't working for me still - it's not returning any sort of error, and after a brief pause, it seems to do nothing.

@vorbeiei

This comment has been minimized.

vorbeiei commented Jan 2, 2018

@boardfish You have to create an app here: https://beta.developer.spotify.com/dashboard/applications and insert your ID and Secret in the file where indicated. (I updated my fork to include the link because it's really not obvious)

@Joself

This comment has been minimized.

Joself commented Jan 3, 2018

The image 'sp display' displays is quite low-res and comes with a Spotify logo down in the corner. This can quite easily be fixed by changing the sp-art function to

function sp-art {
	# Prints the artUrl.

	sp-metadata | grep "artUrl" | cut -d'|' -f2 | sed 's|open.spotify.com|i.scdn.co|g'
}

The images at "i.scdn.co" have a resolution of 300x300 (as opposed to the 180x180 that "open.spotify.com" gives) and doesn't have the logo. :)

@Valeyard1

This comment has been minimized.

Valeyard1 commented Mar 1, 2018

This script doesn't work for me, it gives me an error:
Error org.freedesktop.DBus.Error.ServiceUnknown: The name org.mpris.MediaPlayer2.spotify was not provided by any .service files
Does anyone know why?

@parlanmurray

This comment has been minimized.

parlanmurray commented May 13, 2018

Hey @wandernauta
I was running into an error using sp eval with artist names or titles containing the '$' character.
example using a song by 'Ty Dolla $ign':
eval $(sp eval) echo $SPOTIFY_ARTIST
the result is 'Ty Dolla ' as the '$ign' is interpreted by bash as a variable.

I have fixed this in my fork by adding an additional line to the eval function that changes the output of eval to use single quotes instead of double quotes. You can merge to fix this for other people. Thank you for creating the original!

@emilio1625

This comment has been minimized.

emilio1625 commented Jul 4, 2018

@wandernauta thanks for this script
I have revisioned @rarrais version to include @Joself and @parlanmurray's changes, and added some terminal only image viewers
Maybe this should be in a repo, it is exactly what i was looking for, but i found it only by luck.

@John-AZ1

This comment has been minimized.

John-AZ1 commented Jul 10, 2018

Sorry for the bother, but how would I go about getting the Album, Artist, and Title separately?

@parlanmurray

This comment has been minimized.

parlanmurray commented Jul 11, 2018

@John-AZ1 If you are only after certain fields, use sp eval. it creates shell variables of the metadata, so you can easily access it.
for example, echo $SPOTIFY_ARTIST would give the artist name after calling sp eval.

@emilio1625

This comment has been minimized.

emilio1625 commented Jul 14, 2018

I found that using single quotes instead of double quotes yields to problems with songs using a ' in the title, and I think that's more common than songs with a $

@shanness

This comment has been minimized.

shanness commented Jul 31, 2018

Hi, open has stopped working for me. Not sure how long ago, as haven't used this for quite a while, but looking to get it working again.

Basically, when I run
sp open https://open.spotify.com/track/7jEKOH8vD6v6nVYYbYPMVL
The client does indeed open a song list and highlight the right song, but doesn't start playing it.
And sp play doesn't either.

The metadata is showing the right info (see below), but play, next, pause etc do nothing until I manually click the play button in the UI (next to the track, the main play button isn't enabled). Then the sp play/pause etc start working again.

I've just updated the spotify client, so maybe that has something to do with it? Running Version: 1:1.0.80.480.g51b03ac3-13 on linux

sp metadata
trackid|spotify:track:7jEKOH8vD6v6nVYYbYPMVL
length|225693000
artUrl|https://open.spotify.com/image/d0937c0f498e8ccb6cb9d0c5d331d7a78b1eb593
album|Hiphop World - Eastside ? Westside ? Global Hiphop!
albumArtist|Various Artists
artist|One Self feat. DJ Vadim, Yarah Bravo and BluRum 18, One Self
autoRating|0
discNumber|1
title|Bluebird
trackNumber|4
url|https://open.spotify.com/track/7jEKOH8vD6v6nVYYbYPMVL
@nicklasring

This comment has been minimized.

nicklasring commented Aug 31, 2018

@rarrais Did the same 👍 thanks alot for this script!

@bool3max

This comment has been minimized.

bool3max commented Oct 14, 2018

sp search seems to be broken with Spotify 1.0.89.313. It just stops the current song and goes into some sort of blank mode (no song playing):
...

@PiotrDabrowskey

This comment has been minimized.

PiotrDabrowskey commented Nov 16, 2018

Thanks for this great script!

Any chance we could get the current song playing progress (either in percent, seconds from beginning or so)?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment