Skip to content

Instantly share code, notes, and snippets.

@lmansur
Created August 10, 2019 00:33
Show Gist options
  • Save lmansur/c45f6d8565125a0525a2e518ba6a128d to your computer and use it in GitHub Desktop.
Save lmansur/c45f6d8565125a0525a2e518ba6a128d to your computer and use it in GitHub Desktop.
#!/usr/bin/bash
function _open() {
spotify_uri=$1
is_spotify_running=$(pgrep spotify)
if [[ $is_spotify_running ]]; then
dbus-send --type=method_call --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.OpenUri string:$spotify_uri
else
spotify --uri=$spotify_uri
fi
}
function xdg-open() {
uri=$1
resource_type="playlist|track|album|artist|episode|show"
if [[ $uri =~ ^spotify:($resource_type):([a-zA-Z0-9]+)(.*)$ ]]; then
#echo $uri | grep -Po "^spotify:($resource_type):\K([a-zA-Z0-9]+)(?=.*)$"
spotify_uri=$uri
_open $spotify_uri
elif [[ $uri =~ ^https:\/\/open.spotify.com\/($resource_type)\/([a-zA-Z0-9]+)(.*)$ ]]; then
resource_type_and_id=$(echo $uri | grep -Po "^https:\/\/open.spotify.com\/\K($resource_type)\/([a-zA-Z0-9]+)(?=.*)$")
spotify_uri=$(echo spotify:$(echo $resource_type_and_id | sed -e 's|/|:|'))
_open $spotify_uri
else
command xdg-open $uri
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment