-
Notifications
You must be signed in to change notification settings - Fork 0
/
spotify_downloader_WSL.sh
319 lines (262 loc) · 7.25 KB
/
spotify_downloader_WSL.sh
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
#!/bin/bash
# https://github.com/GitEin11/
cd $PWD
clientID="YourClientIDhere"
clientST="YourClientSecretHere"
# Set the terminal title to show that mp3 is being downloaded from Spotify
echo -ne '\033]0;downloading mp3 from Spotify\007'
rm ./*.mp3
clear
# Run the script "spotifycli" with the output saved to a file "stat"
script -c ./data/spotifycli --force ./data/stat
result=""
# Loop until the result is found
# this is to ensure that a song is currently playing/paused (*if paused for too long spotify will kill the process)
while [ -z "$result" ]; do
result=$(grep -o "URL" ./data/stat)
if [ -z "$result" ]; then
sleep 1
script -c ./data/spotifycli --force ./data/stat
fi
done
wait
# Extract the url of the currently playing song from the "stat" file and save it to a file "url"
url=`awk -F"URL" '{print (NF>1)? $NF : ""}' ./data/stat`
echo $url > ./data/url
sed -i 's/\r//g' ./data/url
url=`cat ./data/url`
# Define the forbidden characters for filename/filepath (add list if neccesary)
forbidden_chars="<>:\"/\\|?*"
# Extract the title of the currently playing song from the "stat" file and save it to a file "title"
title=`awk -F"Playing:" '{print (NF>1)? $NF : ""}' ./data/stat`
if [ -z "$title" ]; then
title=`awk -F"Paused:" '{print (NF>1)? $NF : ""}' ./data/stat`
else
title=$title
fi
# Replace the forbidden characters with a space, to prevent breaking some script
title=$(echo "$title" | sed "s/[$forbidden_chars]/ /g")
# Replace single straight quote ' with single closing quote ’ to prevent breaking gdbus call
# title="${title//\'/’}"
echo $title > ./data/title
# removes new line and carriage return, *sometimes hidden in text viewer, breaks some script if not remove
tr -d '\n\r' < ./data/title > temp_file && mv temp_file ./data/title
# Remove trailing space to prevent error when destination is NTFS
sed -i 's/ [ \\t]*$//' ./data/title
# Remove trailing dot to prevent error when destination is NTFS
sed -i 's/\.$//' ./data/title
title=`cat ./data/title`
# Extract the artist of the currently playing song from the "stat" file and save it to a file "artist"
artist=`awk -F"Artist" '{print (NF>1)? $NF : ""}' ./data/stat`
artist=$(echo "$artist" | sed "s/[$forbidden_chars]/ /g")
echo $artist > ./data/artist
tr -d '\n\r' < ./data/artist > temp_file && mv temp_file ./data/artist
sed -i 's/ [ \\t]*$//' ./data/artist
sed -i 's/\.$//' ./data/artist
artist=`cat ./data/artist`
# Extract the album name of the currently playing song from the "stat" file and save it to a file "album"
album=`awk -F"Album" '{print (NF>1)? $NF : ""}' ./data/stat`
album=$(echo "$album" | sed "s/[$forbidden_chars]/ /g")
echo $album > ./data/album
tr -d '\n\r' < ./data/album > temp_file && mv temp_file ./data/album
sed -i 's/ [ \\t]*$//' ./data/album
sed -i 's/\.$//' ./data/album
album=`cat ./data/album`
FILE="$PWD"/Music/"$artist"/"$album"/"${title}.mp3"
# Check if the file already exists, and if not, download mp3 using the "spotdl" command
if [ -f "$FILE" ]; then
:
else
spotdl --client-id $clientID --client-secret $clientST $url
fi
# Create directories for the artist and album if they don't already exist,
# and move the downloaded mp3 file to the correct location
mkdir -p ./Music/"$artist"/"$album"
mv ./*.mp3 ./Music/"$artist"/"$album"/"${title}.mp3"
FILE2="$PWD"/Music/"$artist"/"$album"/"${title}.lrc"
windowspath=$(wslpath -w "$PWD")
fullpath="$windowspath\\Music\\$artist\\$album\\${title}.mp3"
# check if lrc exist
if [ -f "$FILE2" ]; then
clear
echo "lrc already exist"
echo ""
echo ""
echo "Show mp3 file? - $title"
options=("Yes" "No")
select opt in "${options[@]}"
do
case $opt in
"Yes")
# WSL
cmd.exe /C explorer.exe /select, "$fullpath"
# Linux
# gdbus call --session --dest org.freedesktop.FileManager1 --object-path /org/freedesktop/FileManager1 --method org.freedesktop.FileManager1.ShowItems "['file://$FILE']" ""
exit
;;
"No")
exit
;;
*) echo "Invalid option $REPLY";;
esac
done
exit;
else
clear
# downloading of synched lyrics
options=("Auto" "Spotify" "Musixmatch" "Netease" "Quit")
show_menu() {
echo "Please select an option:"
for i in "${!options[@]}"; do
printf "%3d%s) %s\n" $((i+1)) "${choices[i]:- }" "${options[i]}"
done
if [ "$msg" ]; then echo "$msg"; fi
}
default_choice=1
prompt="(Default to Auto if no input is given within 5 seconds): "
while true; do
show_menu
read -t 5 -p "$prompt" choice
if [ -z "$choice" ]; then
choice=$default_choice
echo -e "\nDefault selected: Option $choice"
fi
if [[ "$choice" =~ ^[1-${#options[@]}]$ ]]; then
case $choice in
# Auto will run from options 2-4 on consecutive order until lrc is found
1)
clear
./data/LyrSpotify
./data/translate
./data/cleanup
clear
if [ -f "$FILE2" ]; then
echo "downloaded lyrics is from Spotify"
echo ""
echo ""
break
else
clear
./data/LyrMusixmatch
./data/translate
./data/cleanup
clear
if [ -f "$FILE2" ]; then
echo "downloaded lyrics is from Musixmatch"
echo ""
echo ""
break
else
clear
./data/LyrNetease
./data/translate
./data/cleanup
clear
if [ -f "$FILE2" ]; then
echo "downloaded lyrics is from Netease"
echo ""
echo ""
break
else
echo "No Lyrics available"
echo ""
echo ""
break
fi
fi
fi
;;
2)
clear
./data/LyrSpotify
./data/translate
./data/cleanup
clear
if [ -f "$FILE2" ]; then
echo "downloaded lyrics is from Spotify"
echo ""
echo ""
else
echo "No Lyrics available"
echo ""
echo ""
fi
;;
3)
clear
./data/LyrMusixmatch
./data/translate
./data/cleanup
clear
if [ -f "$FILE2" ]; then
echo "downloaded lyrics is from Musixmatch"
echo ""
echo ""
else
echo "No Lyrics available"
echo ""
echo ""
fi
;;
4)
clear
./data/LyrNetease
./data/translate
./data/cleanup
clear
if [ -f "$FILE2" ]; then
echo "downloaded lyrics is from Netease"
echo ""
echo ""
else
echo "No Lyrics available"
echo ""
echo ""
fi
;;
5)
echo "Quitting..."
;;
*)
msg="Invalid option: $choice"
;;
esac
else
msg="Invalid option: $choice"
fi
done
fi
# after process
# Check if the mp3 file exists, and if so, cleanup and prompt the user to show the file in their default file manager
rm -f ./data/album
rm -f ./data/artist
rm -f ./data/query
rm -f ./data/stat
rm -f ./data/time
rm -f ./data/title
rm -f ./data/url
if [ -f "$FILE2" ]; then
# remove existing embedded lyrics
eyeD3 --remove-all-lyrics "$FILE"
# replace embedded lyrics
eyeD3 --add-lyrics="$FILE2" "$FILE"
clear
echo "Show mp3 file? - $title"
options=("Yes" "No")
select opt in "${options[@]}"
do
case $opt in
"Yes")
# WSL
cmd.exe /C explorer.exe /select, "$fullpath"
# (linux)
# gdbus call --session --dest org.freedesktop.FileManager1 --object-path /org/freedesktop/FileManager1 --method org.freedesktop.FileManager1.ShowItems "['file://$FILE']" ""
exit
;;
"No")
exit
;;
*) echo "Invalid option $REPLY";;
esac
done
fi