fdc59e5499c5aabfd287c73df66a4fb7472732e8

Author
Robby Russell <robby@planetargon.com>
Committer
Robby Russell <robby@planetargon.com>
Date

Message

Merge branch 'mahi97-master'

Diff

This diff is truncated to protect this page.

  1diff --git a/plugins/osx/README.md b/plugins/osx/README.md
  2index 0fcd23dd52fe4623d6c91e7a62f92dfb23192242..b77daecc5dc224a3ab6f18ba34667d83fdf1af47 100644
  3--- a/plugins/osx/README.md
  4+++ b/plugins/osx/README.md
  5@@ -30,3 +30,4 @@ Original author: [Sorin Ionescu](https://github.com/sorin-ionescu)
  6 | `showfiles`     | Show hidden files                                |
  7 | `hidefiles`     | Hide the hidden files                            |
  8 | `itunes`        | Control iTunes. User `itunes -h` for usage details |
  9+| `spotify`       | Control Spotify and search by artist, album, track and etc.|
 10diff --git a/plugins/osx/osx.plugin.zsh b/plugins/osx/osx.plugin.zsh
 11index a3e55097203beb18a0319c91eaab73afba1aa2da..aa6a256c11b53a641311e06676680e03490d0e1a 100644
 12--- a/plugins/osx/osx.plugin.zsh
 13+++ b/plugins/osx/osx.plugin.zsh
 14@@ -261,6 +261,270 @@ EOF
 15 	osascript -e "tell application \"iTunes\" to $opt"
 16 }
 17 
 18+# Spotify control function
 19+function spotify() {
 20+
 21+  showHelp () {
 22+    echo "Usage:";
 23+    echo;
 24+    echo "  $(basename "$0") <command>";
 25+    echo;
 26+    echo "Commands:";
 27+    echo;
 28+    echo "  play                         # Resumes playback where Spotify last left off.";
 29+    echo "  play [song name]             # Finds a song by name and plays it.";
 30+    echo "  play album [album name]      # Finds an album by name and plays it.";
 31+    echo "  play artist [artist name]    # Finds an artist by name and plays it.";
 32+    echo "  play list [playlist name]    # Finds a playlist by name and plays it.";
 33+    echo "  pause                        # Pauses Spotify playback.";
 34+    echo "  next                         # Skips to the next song in a playlist.";
 35+    echo "  prev                         # Returns to the previous song in a playlist.";
 36+    echo "  pos [time]                   # Jumps to a time (in secs) in the current song.";
 37+    echo "  quit                         # Stops playback and quits Spotify.";
 38+    echo;
 39+    echo "  vol up                       # Increases the volume by 10%.";
 40+    echo "  vol down                     # Decreases the volume by 10%.";
 41+    echo "  vol [amount]                 # Sets the volume to an amount between 0 and 100.";
 42+    echo "  vol show                     # Shows the current Spotify volume.";
 43+    echo;
 44+    echo "  status                       # Shows the current player status.";
 45+    echo "  share                        # Copies the current song URL to the clipboard."
 46+    echo "  info                         # Shows Full Information about song that is playing.";
 47+    echo;
 48+    echo "  toggle shuffle               # Toggles shuffle playback mode.";
 49+    echo "  toggle repeat                # Toggles repeat playback mode.";
 50+  }
 51+
 52+  cecho(){
 53+    bold=$(tput bold);
 54+    green=$(tput setaf 2);
 55+    reset=$(tput sgr0);
 56+    echo "$bold$green$1$reset";
 57+  }
 58+
 59+  showStatus () {
 60+      state=$(osascript -e 'tell application "Spotify" to player state as string');
 61+      cecho "Spotify is currently $state.";
 62+      if [ "$state" = "playing" ]; then
 63+        artist=$(osascript -e 'tell application "Spotify" to artist of current track as string');
 64+        album=$(osascript -e 'tell application "Spotify" to album of current track as string');
 65+        track=$(osascript -e 'tell application "Spotify" to name of current track as string');
 66+        duration=$(osascript -e 'tell application "Spotify" to duration of current track as string');
 67+        duration=$(echo "scale=2; $duration / 60 / 1000" | bc);
 68+        position=$(osascript -e 'tell application "Spotify" to player position as string' | tr ',' '.');
 69+        position=$(echo "scale=2; $position / 60" | bc | awk '{printf "%0.2f", $0}');
 70+
 71+        printf "$reset""Artist: %s\nAlbum: %s\nTrack: %s \nPosition: %s / %s" "$artist" "$album" "$track" "$position" "$duration";
 72+      fi
 73+  }
 74+
 75+
 76+
 77+  if [ $# = 0 ]; then
 78+    showHelp;
 79+  else
 80+    if [ "$(osascript -e 'application "Spotify" is running')" = "false" ]; then
 81+      osascript -e 'tell application "Spotify" to activate'
 82+      sleep 2
 83+    fi
 84+  fi
 85+
 86+  while [ $# -gt 0 ]; do
 87+    arg=$1;
 88+
 89+    case $arg in
 90+      "play"    )
 91+        if [ $# != 1 ]; then
 92+          # There are additional arguments, so find out how many
 93+          array=( $@ );
 94+          len=${#array[@]};
 95+          SPOTIFY_SEARCH_API="https://api.spotify.com/v1/search"
 96+          SPOTIFY_PLAY_URI="";
 97+
 98+          searchAndPlay() {
 99+            type="$1"
100+            Q="$2"
101+
102+            cecho "Searching ${type}s for: $Q";
103+
104+            SPOTIFY_PLAY_URI=$( \
105+              curl -s -G $SPOTIFY_SEARCH_API --data-urlencode "q=$Q" -d "type=$type&limit=1&offset=0" -H "Accept: application/json" \
106+              | grep -E -o "spotify:$type:[a-zA-Z0-9]+" -m 1
107+              )
108+          }
109+
110+          case $2 in
111+            "list"  )
112+              _args=${array[*]:2:$len};
113+              Q=$_args;