forked from hnarayanan/shpotify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spotify
executable file
·229 lines (188 loc) · 9.22 KB
/
spotify
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#!/usr/bin/env bash
# Copyright (c) 2012--2015 Harish Narayanan <[email protected]>
#
# Contains numerous helpful contributions from Jorge Colindres, Thomas
# Pritchard and iLan Epstein.
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation files
# (the "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.
showHelp () {
echo "Usage:";
echo " `basename $0` <command>";
echo;
echo "Commands:";
echo;
echo " play # Resume playback where Spotify last left off.";
echo " play [song name] # Finds a song by name and plays it.";
echo " play album [album name] # Finds an album by name and plays it.";
echo " play artist [artist name] # Finds an artist by name and plays it.";
echo " play list [playlist name] # Finds a playlist by name and plays it.";
echo;
echo " next # Skips to the next song in a playlist.";
echo " prev # Returns to the previous song in a playlist.";
echo " pos [time] # Jump to a specific time (in seconds) in the current song.";
echo " pause # Pauses Spotify playback.";
echo " quit # Stops playback and quits Spotify.";
echo;
echo " vol up # Increases the volume by 10%.";
echo " vol down # Decreases the volume by 10%.";
echo " vol [amount] # Sets the volume to an amount between 0 and 100.";
echo;
echo " status # Shows the play status, including the current song details.";
echo " share # Shows and copies the current song URL to the clipboard for sharing."
echo;
echo " toggle shuffle # Toggle shuffle playback mode.";
echo " toggle repeat # Toggle repeat playback mode.";
}
if [ $# = 0 ]; then
showHelp;
fi
while [ $# -gt 0 ]; do
arg=$1;
bold=$(tput bold);
green=$(tput setaf 2);
reset=$(tput sgr0);
case $arg in
"play" )
if [ $# != 1 ]; then
# There are additional parameters
#get the length of arguments
array=( $@ );
len=${#array[@]};
SPTFY_URI="";
if [ $2 = "album" ]; then
_args=${array[@]:2:$len};
Q=$_args;
echo $bold$green"Searching albums for: $Q";
SPTFY_URI=$( \
curl -s -G "https://api.spotify.com/v1/search" --data-urlencode "q=$Q" -d "type=album&limit=1&offset=0" -H "Accept: application/json" \
| grep -E -o "spotify:album:[a-zA-Z0-9]+" -m 1 \
)
elif [ $2 = "artist" ]; then
_args=${array[@]:2:$len};
Q=$_args;
echo $bold$green"Searching artists for: $Q";
SPTFY_URI=$( \
curl -s -G "https://api.spotify.com/v1/search" --data-urlencode "q=$Q" -d "type=artist&limit=1&offset=0" -H "Accept: application/json" \
| grep -E -o "spotify:artist:[a-zA-Z0-9]+" -m 1 \
)
elif [ $2 = "list" ]; then
_args=${array[@]:2:$len};
Q=$_args;
echo $bold$green"Searching playlists for: $Q";
results=$( \
curl -s -G "https://api.spotify.com/v1/search" --data-urlencode "q=$Q" -d "type=playlist&limit=10&offset=0" -H "Accept: application/json" \
| grep -E -o "spotify:user:[a-zA-Z0-9]+:playlist:[a-zA-Z0-9]+" -m 10 \
)
count=$( \
echo "$results" | grep -c "spotify:user" \
)
if [ "$count" -gt 0 ]; then
random=$(( $RANDOM % $count));
SPTFY_URI=$( \
echo "$results" | awk -v random="$random" '/spotify:user:[a-zA-Z0-9]+:playlist:[a-zA-Z0-9]+/{i++}i==random{print; exit}' \
)
fi
else
if [ $2 = "track" ]; then
_args=${array[@]:2:$len};
else
_args=${array[@]:1:$len};
fi
Q=$_args;
echo $bold$green"Searching tracks for: $Q";
SPTFY_URI=$( \
curl -s -G "https://api.spotify.com/v1/search" --data-urlencode "q=$Q" -d "type=track&limit=1&offset=0" -H "Accept: application/json" \
| grep -E -o "spotify:track:[a-zA-Z0-9]+" -m 1 \
)
fi
if [ "$SPTFY_URI" != "" ]; then
echo $bold$green"Playing ($Q Search) -> Spotify URL: $SPTFY_URI";
osascript -e "tell application \"Spotify\" to play track \"$SPTFY_URI\"";
else
echo $bold$green"No results when searching for $Q";
fi
else
# play is the only param
echo $bold$green"Playing Spotify.";
osascript -e 'tell application "Spotify" to play';
fi
break ;;
"pause" ) echo $bold$green"Pausing Spotify.";
osascript -e 'tell application "Spotify" to pause';
break ;;
"quit" ) echo $bold$green"Quitting Spotify.";
osascript -e 'tell application "Spotify" to quit';
exit 1 ;;
"next" ) echo $bold$green"Going to next track." ;
osascript -e 'tell application "Spotify" to next track';
break ;;
"prev" ) echo $bold$green"Going to previous track.";
osascript -e 'tell application "Spotify" to previous track';
break ;;
"vol" )
vol=`osascript -e 'tell application "Spotify" to sound volume as integer'`;
text=$2;
if [ $2 = "up" ]; then
newvol=$(( vol+10 ));
elif [ $2 = "down" ]; then
newvol=$(( vol-10 ));
elif [ $2 -gt 0 ]; then
newvol=$2;
text="to $2";
fi
echo $bold$green"Changing Spotify volume level $text.";
osascript -e "tell application \"Spotify\" to set sound volume to $newvol";
break ;;
"toggle" )
if [ $2 = "shuffle" ]; then
osascript -e "tell application \"Spotify\" to set shuffling to not shuffling";
curr=`osascript -e 'tell application "Spotify" to shuffling'`;
echo $bold$green"Spotify shuffling set to $curr";
elif [ $2 = "repeat" ]; then
osascript -e "tell application \"Spotify\" to set repeating to not repeating";
curr=`osascript -e 'tell application "Spotify" to repeating'`;
echo $bold$green"Spotify repeating set to $curr";
fi
break ;;
"status" )
state=`osascript -e 'tell application "Spotify" to player state as string'`;
echo $bold$green"Spotify is currently $state.";
if [ $state = "playing" ]; then
artist=`osascript -e 'tell application "Spotify" to artist of current track as string'`;
album=`osascript -e 'tell application "Spotify" to album of current track as string'`;
track=`osascript -e 'tell application "Spotify" to name of current track as string'`;
echo -e $reset"Artist: $artist\nAlbum: $album\nTrack: $track";
fi
break ;;
"share" )
url=`osascript -e 'tell application "Spotify" to spotify url of current track'`;
remove='spotify:track:'
url=${url#$remove}
url="http://open.spotify.com/track/$url"
echo $bold$green"Share URL: $url";
echo "$url" | pbcopy
break;;
"pos" )
echo $bold$green"Adjusting Spotify play position."
osascript -e "tell application \"Spotify\" to set player position to $2";
break;;
"help" | * )
showHelp;
break ;;
esac
done