Add Spotify authorization support in osx plugin

This commit is contained in:
Nakul Dhotre 2017-08-07 17:56:11 -07:00
commit bc9c6b5099
2 changed files with 12 additions and 6 deletions

View file

@ -9,12 +9,8 @@ To start using it, add the `osx` plugin to your plugins array in `~/.zshrc`:
```zsh ```zsh
plugins=(... osx) plugins=(... osx)
``` ```
Original author: [Sorin Ionescu](https://github.com/sorin-ionescu) Original author: [Sorin Ionescu](https://github.com/sorin-ionescu)
## Commands ## Commands
| Command | Description | | Command | Description |
| :-------------- | :----------------------------------------------- | | :-------------- | :----------------------------------------------- |
| `tab` | Open the current directory in a new tab | | `tab` | Open the current directory in a new tab |
@ -31,3 +27,12 @@ Original author: [Sorin Ionescu](https://github.com/sorin-ionescu)
| `hidefiles` | Hide the hidden files | | `hidefiles` | Hide the hidden files |
| `itunes` | Control iTunes. User `itunes -h` for usage details | | `itunes` | Control iTunes. User `itunes -h` for usage details |
| `spotify` | Control Spotify and search by artist, album, track and etc.| | `spotify` | Control Spotify and search by artist, album, track and etc.|
Note: Spotify command requires OAUTH token for searching music. It can be generated
from [Spotify Web API Console](https://developer.spotify.com/web-api/console/get-search-item/).
Do not make your Spotify OAUTH token public.
```zsh
export SPOTIFY_OAUTH_TOKEN="..."
```

View file

@ -355,6 +355,7 @@ function spotify() {
len=${#array[@]}; len=${#array[@]};
SPOTIFY_SEARCH_API="https://api.spotify.com/v1/search" SPOTIFY_SEARCH_API="https://api.spotify.com/v1/search"
SPOTIFY_PLAY_URI=""; SPOTIFY_PLAY_URI="";
SPOTIFY_AUTH="Authorization: Bearer $SPOTIFY_OAUTH_TOKEN"
searchAndPlay() { searchAndPlay() {
type="$1" type="$1"
@ -363,7 +364,7 @@ function spotify() {
cecho "Searching ${type}s for: $Q"; cecho "Searching ${type}s for: $Q";
SPOTIFY_PLAY_URI=$( \ SPOTIFY_PLAY_URI=$( \
curl -s -G $SPOTIFY_SEARCH_API --data-urlencode "q=$Q" -d "type=$type&limit=1&offset=0" -H "Accept: application/json" \ curl -s -G $SPOTIFY_SEARCH_API --data-urlencode "q=$Q" -d "type=$type&limit=1&offset=0" -H "Accept: application/json" -H $SPOTIFY_AUTH \
| grep -E -o "spotify:$type:[a-zA-Z0-9]+" -m 1 | grep -E -o "spotify:$type:[a-zA-Z0-9]+" -m 1
) )
} }
@ -376,7 +377,7 @@ function spotify() {
cecho "Searching playlists for: $Q"; cecho "Searching playlists for: $Q";
results=$( \ results=$( \
curl -s -G $SPOTIFY_SEARCH_API --data-urlencode "q=$Q" -d "type=playlist&limit=10&offset=0" -H "Accept: application/json" \ curl -s -G $SPOTIFY_SEARCH_API --data-urlencode "q=$Q" -d "type=playlist&limit=10&offset=0" -H "Accept: application/json" -H $SPOTIFY_AUTH \
| grep -E -o "spotify:user:[a-zA-Z0-9_]+:playlist:[a-zA-Z0-9]+" -m 10 \ | grep -E -o "spotify:user:[a-zA-Z0-9_]+:playlist:[a-zA-Z0-9]+" -m 10 \
) )