diff --git a/README.md b/README.md index 28844fe..2a34bc5 100644 --- a/README.md +++ b/README.md @@ -37,6 +37,7 @@ acronyms are valid for most cases:
* To hide the Spotify GUI you can try out these commands. (will work better on a WM)
- `xdotool search --name spotify windowunmap` - `wmctrl -r "spotify" -b toggle,hidden` +* This script also supports [Spotifyd](https://github.com/Spotifyd/spotifyd) as Spotify client, you can defind which to use when both are running. (default being the official client) * Some songs may not start due to country restrictions. ## Installation : diff --git a/play b/play old mode 100644 new mode 100755 index d201d64..824d953 --- a/play +++ b/play @@ -1,72 +1,86 @@ #!/bin/bash -# https://github.com/BelkaDev/mustream -# Requires a running Spotify desktop client +# https://github.com/BelkaDev/mustream +# Requires a running Spotify desktop client or spotifyd -check::network() { - [[ $? != 0 ]] && echo "Network error: couldn't reach server." && exit 1 +#client= +# spotify or spotifyd (leave it blank for auto detection) + +check::network() { + [[ $? != 0 ]] && echo "Network error: couldn't reach server." && exit 1 } check::running() { -[[ -z $(pidof -s spotify) ]] && echo "Spotify is not running." && exit 1 + if [[ -z "$client" ]]; then + if [[ $(pidof -s spotify) ]]; then + client=spotify + elif [[ $(pidof -s spotifyd) ]]; then + : # client=spotify (redundancy, as currently it has only two possible options) + else + echo "No supported Spotify client running." && exit 1 + fi + elif [[ $(pidof -s spotifyd) ]] || [[ $(pidof -s spotify) ]]; then + echo "Spotify is not running." && exit 1 + fi } -_author_name(){ - echo $(echo $( echo "$1") | grep -oh -P '(?<=a playlist by ).*?"'| awk -F"on Spotify" '{print $1}') +_author_name() { + echo $(echo $(echo "$1") | grep -oh -P '(?<=a playlist by ).*?"' | awk -F"on Spotify" '{print $1}') } -_artist_name() -{ - echo $(echo $( echo "$1") | grep -oh -P '(?<=a song by ).*' | awk -F"on Spotify" '{print $1}') +_artist_name() { + echo $(echo $(echo "$1") | grep -oh -P '(?<=a song by ).*' | awk -F"on Spotify" '{print $1}') } -_album_artist_name(){ - echo $(echo $( echo "$1") | grep -oh -P '(?<=an album by ).*?"'| awk -F"on Spotify" '{print $1}' | head -1) +_album_artist_name() { + echo $(echo $(echo "$1") | grep -oh -P '(?<=an album by ).*?"' | awk -F"on Spotify" '{print $1}' | head -1) } -_current_list() -{ -echo $(echo $( echo "$1") | grep -oh -P '(?<="name":").*(?=","description")' ) +_current_list() { + echo $(echo $(echo "$1") | grep -oh -P '(?<="name":").*(?=","description")') } -_tracks_number() -{ - echo $(echo $( echo "$1") | grep -oh -P '(?<="track_number").*?}'|sed s'/.$//' | sed 's/^/"track_id": /' | cut -d, -f1,3 | wc -l) +_tracks_number() { + echo $(echo $(echo "$1") | grep -oh -P '(?<="track_number").*?}' | sed s'/.$//' | sed 's/^/"track_id": /' | cut -d, -f1,3 | wc -l) } - + check::running arg=track engine=https://www.google.com ###### Parsing arguments ###### if [[ ${1^^} =~ ^(-A|--ALBUM|ALBUM)$ ]]; then -arg=album && shift + arg=album && shift elif [[ ${1^^} =~ ^(-P|--PLAYLIST|PLAYLIST|SOME)$ ]]; then -arg=playlist && shift + arg=playlist && shift fi if [[ $* == *"open.spotify.com/playlist/"* ]]; then -arg=playlist -key=$(echo $* | sed 's:.*/::' | cut -c 1-22) + arg=playlist + key=$(echo $* | sed 's:.*/::' | cut -c 1-22) else -###### Preparing request ###### -req=$engine"/search?hl=en&q=$(echo $* | sed 's/ /+/g')"+"site:open.spotify.com/"$arg -###### Fetching key ###### -key=$(curl -sA "FF" $req | \ - grep -o '/dev/null +if [[ "$client" == "spotify" ]]; then + dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.OpenUri string:spotify:$arg:$key &>/dev/null +else # elif [[ "$client" == "spotifyd" ]]; then (redundancy, as currently it has only two possible options) + dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotifyd /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.OpenUri string:spotify:$arg:$key &>/dev/null +fi ###### Output ###### echo "###### Queued ${arg^} ######" if [[ $arg = "track" ]]; then @@ -78,7 +92,7 @@ elif [[ $arg = "album" ]]; then echo "Album : $(_current_list "$curr")" echo "added $(_tracks_number "$curr") tracks to queue." elif [[ $arg = "playlist" ]]; then -###echo "Playlist author : $(author_name "$curr")" #useless info + ###echo "Playlist author : $(author_name "$curr")" #useless info echo "Playlist name : $(_current_list "$curr")" echo "Playlist URL : $link" echo "added $(_tracks_number "$curr") tracks to queue."