-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmusic
executable file
·359 lines (327 loc) · 7.98 KB
/
music
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
#!/usr/bin/env bash
# _
# __ _ __ _____ (_)___
# / ' \/ // (_-</ / __/
# /_/_/_/\_,_/___/_/\__/
#
# author cherrynoize (https://github.com/cherrynoize)
# based upon some stuff from somewhere I can't remember
#
# location to store cover image in
cover_loc="/tmp/cover"
# fallback for when no cover is found
#fallback_cover_loc="${XDG_CONFIG_HOME:-"$HOME/.config"}/music/fallback.cover"
# Set the source audio player here.
# Players supporting the MPRIS spec are supported.
# Examples: spotify, vlc, chrome, mpv and others.
# Use `playerctld` to always detect the latest player.
# See more here: https://github.com/altdesktop/playerctl/#selecting-players-to-control
player="playerctld"
# placeholders
no_artist_msg="No artist"
no_album_msg="No album"
if playerctl_status="$(playerctl --player=$player status 2>/dev/null)"; then
status="$playerctl_status"
else
status="no player"
[ -e "$cover_loc" ] && rm "$cover_loc"
fi
# show usage
usage () {
cat<<EOF
usage:
music [options] [command]
options:
-p|--placeholder
commands:
song show formatted output
title show title
artist show artist
artist-or-placeholder|aop show artist or placeholder
artist-or-empty|aon show artist or empty
album show album
track show track number
status show status
status-icon show status icon
now|ctime show reproduction time
secs|atime show reproduction time in seconds
totsecs|attime show track duration in seconds
duration|ttime show track duration
cover show cover
coverloc show cover location
lyrics show lyrics
fill show fill
placeholder show placeholder
toggle|play-pause toggle reproduction
next next track
prev|previous previous track
seek|position move to time position
loop show loop status
loop-icon show loop icon
help show this help message
EOF
}
run_cmd () {
cmd="$*"
if [ "$status" = "no player" ] && [ -n "$placeholder" ]; then
cmd="echo $placeholder"
fi
$cmd
}
playerctl_get () {
playerctl --player="$player" metadata --format "{{ $* }}" 2>/dev/null
}
# if $1 is set then print $2 else print current status
# if $2 is not set then $1 is used
# if $3 is set use $3 instead of current status
if_set_then_echo () {
if [ -n "$1" ]; then
run_cmd echo "${2:-$1}"
else
output="${status^}"
run_cmd echo "${3:-$output}" # capitalize first letter
fi
}
# returns placeholder text
get_filler () {
echo "ılı.lıllılı.ıllı."
# echo ".ılılılllıılılıllllıılılllıllı."
# echo "||ıı|||ıı|||||ı|ıı|ı."
# echo "♫⋆。♪ ₊˚♬ ゚."
# echo "---˖⁺. ༶ ⋆˙⊹❀♡❀˖⁺. ༶ ⋆˙⊹---"
}
# returns either title or placeholder text
get_placeholder () {
title="$(playerctl_get title)"
echo "${title:-"$(get_filler)"}"
}
# get player status
get_status () {
if [ "$status" = "stopped" ] || [ "$status" = "Paused" ]; then
echo "paused"
else
echo "$status"
fi
}
# currently playing
get_song () {
title="$(playerctl_get title)"
artist="$(playerctl_get artist)"
if_set_then_echo "$title" "$title - $artist"
}
# track title
get_title () {
title="$(playerctl_get title)"
if_set_then_echo "$title"
}
# track artist
get_artist () {
artist="$(playerctl_get artist)"
if_set_then_echo "$artist" "$artist" "$no_artist_msg"
}
# track album
get_album () {
album="$(playerctl_get album)"
if_set_then_echo "$album" "$album" "$no_album_msg"
}
get_track_number () {
track="$(playerctl --player="$player" metadata xesam:trackNumber 2>/dev/null)"
[ -z "$track" ] && track="0/0"
echo "$track"
}
# get current formatted time
get_current_time () {
get-player-status --ctime 2>/dev/null
}
# get current time in seconds
get_current_time_in_seconds () {
get-player-status --atime 2>/dev/null
}
# get total time in seconds
get_total_time_in_seconds () {
get-player-status --attime 2>/dev/null
}
# get total duration
get_total_duration () {
get-player-status --ttime 2>/dev/null
}
# get next status icon
get_status_icon () {
[ "$status" = "stopped" ] ||
[ "$status" = "Paused" ] ||
[ "$status" = "no player" ] ||
is_playing=1
if [ -z "$is_playing" ]; then
# echo "奈"
echo ""
else
# echo ""
echo ""
fi
}
# get fallback cover location
get_fallback_cover () {
fc_default="${XDG_CONFIG_HOME:-"$HOME/.config"}/music/fallback.cover"
fallback_cover="${fallback_cover_lock:-"$fc_default"}"
[ -e "$fallback_cover" ] && echo "$fallback_cover"
}
# get cover location
get_cover_loc () {
fallback_cover="$(get_fallback_cover)"
if [ -f "$cover_loc" ]; then
echo "$cover_loc"
elif [ -f "$fallback_cover" ]; then
echo "$fallback_cover"
fi
}
# show cover image
show_cover () {
cover="$(get_cover_loc)"
if [ -n "$cover" ]; then
feh "$fallback_cover"
else
exit 4
fi
}
# fetch and update cover image
update_cover () {
if [ "$status" != "no player" ]; then
albumart="$(playerctl --player="$player" metadata mpris:artUrl | sed -e 's/open.spotify.com/i.scdn.co/g' 2>/dev/null)"
if [ "$(playerctl --player="$player" metadata mpris:artUrl)" ]; then
curl -s "$albumart" --output "$cover_loc" || {
[ -n "$(get_fallback_cover)" ] &&
cp "$(get_fallback_cover)" "$cover_loc"
}
fi
fi
}
# get loop status icon
get_loop_stat_ic () {
loop_status="$(playerctl --player="$player" loop 2>/dev/null)"
if [[ "$loop_status" = *"Track"* ]] || [[ "$loop_status" = "on"* ]]; then
icon=" "
elif [[ "$loop_status" = *"Playlist"* ]]; then
icon="凌"
else
icon="稜"
fi
echo "$icon"
}
# get lyrics
get_lyrics () {
title="$(playerctl_get title)"
artist="$(playerctl_get artist)"
if [ -n "$title" ] && [ -n "$artist" ]; then
url="https://api.lyrics.ovh/v1/$artist/$title"
lyrics="$(curl -s "$( echo "$url" | sed s/" "/%20/g | sed s/"&"/%26/g | sed s/,/%2C/g | sed s/-/%2D/g)" | jq '.lyrics' )"
[ "$lyrics" = "null" ] && lyrics="No lyrics found"
else
lyrics="No lyrics available"
fi
printf "$lyrics"
}
# parse args
while [ -n "$1" ]; do
case "$1" in
-p|--placeholder)
placeholder="$2"
shift 2
;;
song)
cmd=get_song
;;
title)
cmd=get_title
;;
artist)
cmd=get_artist
;;
artist-or-placeholder|aop)
# artist placeholder if idle
no_artist_msg="$(get_filler)"
cmd=get_artist
;;
artist-or-empty|aon)
# current artist or empty string
no_artist_msg=""
cmd=get_artist
;;
album)
cmd=get_album
;;
track)
cmd=get_track_number
;;
status)
cmd=get_status
;;
status-icon)
cmd=get_status_icon
;;
now|ctime)
cmd=get_current_time
;;
secs|atime)
cmd=get_current_time_in_seconds
;;
totsecs|attime)
cmd=get_total_time_in_seconds
;;
duration|ttime)
cmd=get_total_duration
;;
cover)
cmd=show_cover
;;
coverloc)
update_cover
get_cover_loc
exit
;;
fcloc)
update_cover
get_fallback_cover
exit
;;
lyrics)
cmd=get_lyrics
;;
fill)
cmd=get_filler
;;
placeholder)
cmd=get_placeholder
;;
toggle|play-pause)
playerctl play-pause
exit
;;
next)
playerctl next
update_cover
exit
;;
prev|previous)
playerctl previous
update_cover
exit
;;
seek|position)
playerctl position "$2"
exit
;;
loop)
playerctl loop "$2"
exit
;;
loop-icon)
cmd=get_loop_stat_ic
;;
help)
usage
exit
;;
esac
shift
done
"${cmd:-get_song}" "$@"