forked from hnarayanan/shpotify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spotify
executable file
·283 lines (239 loc) · 11.5 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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#!/usr/bin/env bash
# Copyright (c) 2012--2015 Harish Narayanan <mail@harishnarayanan.org>
#
# Contains numerous helpful contributions from Jorge Colindres, Thomas
# Pritchard, iLan Epstein, Gabriele Bonetti and Sean Heller.
# 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;
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.";
}
cecho(){
bold=$(tput bold);
green=$(tput setaf 2);
reset=$(tput sgr0);
echo $bold$green"$1"$reset;
}
showStatus () {
state=`osascript -e 'tell application "Spotify" to player state as string'`;
cecho "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
}
if [ $# = 0 ]; then
showHelp;
else
if [ $(osascript -e 'application "Spotify" is running') = "false" ]; then
osascript -e 'tell application "Spotify" to activate'
sleep 2
fi
fi
while [ $# -gt 0 ]; do
arg=$1;
case $arg in
"play" )
if [ $# != 1 ]; then
# There are additional arguments, so find out how many
array=( $@ );
len=${#array[@]};
SPOTIFY_SEARCH_API="https://api.spotify.com/v1/search"
SPOTIFY_PLAY_URI="";
if [ "$2" = "album" ]; then
_args=${array[@]:2:$len};
Q=$_args;
cecho "Searching albums for: $Q";
SPOTIFY_PLAY_URI=$( \
curl -s -G $SPOTIFY_SEARCH_API --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;
cecho "Searching artists for: $Q";
SPOTIFY_PLAY_URI=$( \
curl -s -G $SPOTIFY_SEARCH_API --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;
cecho "Searching playlists for: $Q";
results=$( \
curl -s -G $SPOTIFY_SEARCH_API --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));
SPOTIFY_PLAY_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;
cecho "Searching tracks for: $Q";
SPOTIFY_PLAY_URI=$( \
curl -s -G $SPOTIFY_SEARCH_API --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 [ "$SPOTIFY_PLAY_URI" != "" ]; then
cecho "Playing ($Q Search) -> Spotify URL: $SPOTIFY_PLAY_URI";
osascript -e "tell application \"Spotify\" to play track \"$SPOTIFY_PLAY_URI\"";
else
cecho "No results when searching for $Q";
fi
else
# play is the only param
cecho "Playing Spotify.";
osascript -e 'tell application "Spotify" to play';
fi
break ;;
"pause" )
state=`osascript -e 'tell application "Spotify" to player state as string'`;
if [ $state = "playing" ]; then
cecho "Pausing Spotify.";
else
cecho "Playing Spotify.";
fi
osascript -e 'tell application "Spotify" to playpause';
break ;;
"quit" ) cecho "Quitting Spotify.";
osascript -e 'tell application "Spotify" to quit';
exit 1 ;;
"next" ) cecho "Going to next track." ;
osascript -e 'tell application "Spotify" to next track';
showStatus;
break ;;
"prev" ) cecho "Going to previous track.";
osascript -e 'tell application "Spotify" to previous track';
showStatus;
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
cecho "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'`;
cecho "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'`;
cecho "Spotify repeating set to $curr";
fi
break ;;
"status" )
showStatus;
break ;;
"info" )
info=`osascript -e 'tell application "Spotify"
set tM to round (duration of current track / 60) rounding down
set tS to duration of current track mod 60
set pos to player position as text
set myTime to tM as text & "min " & tS as text & "s"
set nM to round (player position / 60) rounding down
set nS to round (player position mod 60) rounding down
set nowAt to nM as text & "min " & nS as text & "s"
set info to "" & "\nArtist: " & artist of current track
set info to info & "\nTrack: " & name of current track
set info to info & "\nAlbum Artist: " & album artist of current track
set info to info & "\nAlbum: " & album of current track
set info to info & "\nSeconds: " & duration of current track
set info to info & "\nSeconds played: " & pos
set info to info & "\nDuration: " & mytime
set info to info & "\nNow at: " & nowAt
set info to info & "\nPlayed Count: " & played count of current track
set info to info & "\nTrack Number: " & track number of current track
set info to info & "\nPopularity: " & popularity of current track
set info to info & "\nId: " & id of current track
set info to info & "\nSpotify URL: " & spotify url of current track
set info to info & "\nArtwork: " & artwork of current track
set info to info & "\nPlayer: " & player state
set info to info & "\nVolume: " & sound volume
set info to info & "\nShuffle: " & shuffling
set info to info & "\nRepeating: " & repeating
end tell
return info'`
cecho "$info";
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"
cecho "Share URL: $url";
echo -n "$url" | pbcopy
break;;
"pos" )
cecho "Adjusting Spotify play position."
osascript -e "tell application \"Spotify\" to set player position to $2";
break;;
"help" | * )
showHelp;
break ;;
esac
done