-
Notifications
You must be signed in to change notification settings - Fork 4
/
TiRIPPER.sh
311 lines (256 loc) · 7.17 KB
/
TiRIPPER.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
#!/bin/bash
#needs gsed, jq, metaflac & ffmpeg (for flac encodes)
#tidalSession="X-Tidal-SessionId: SESSIONKEYHERE"
#token=???
play=0
for var in "$@"
do
albumID="$var"
if [ -z "$albumID" ];then
echo "-->album ID?"
read albumID
fi
if [ ${#albumID} -ge 10 ]; then
play=1
fi
#superfluous crap probably
#-------------------------
nullCheck=0
while [ "$nullCheck" == 0 ];do
if [ "$play" = 1 ]; then
html=$(curl -s --http1.1 -H "$tidalSession" -H "X-Tidal-Token: $token" -H "User-Agent: TIDAL/362 CFNetwork/711.4.6 Darwin/14.0.0" "http://api.tidalhifi.com/v1/playlists/$albumID/items?countryCode=US&limit=100&orderDirection=ASC" | cat )
else
html=$(curl -s --http1.1 -H "$tidalSession" -H "X-Tidal-Token: $token" -H "User-Agent: TIDAL/362 CFNetwork/711.4.6 Darwin/14.0.0" "http://api.tidalhifi.com/v1/albums/$albumID/tracks?countryCode=US&limit=100&orderDirection=ASC" | cat )
fi
maxItems=$(jq -r '.totalNumberOfItems' <<< "$html")
if [ "$maxItems" == "null" ]; then
echo "$albumID Null error."
echo "-->album ID?"
read albumID
else
nullCheck=1
fi
done
#-------------------------
echo "----Selected Album has $maxItems tracks."
if [ -n "$1" ];then
index=1
avoid=1
avoidInput=1
if [ "$play" = 1 ];then
delm4a=0
else
delm4a=1
fi
else
echo "-->index? (default 1)"
read indexinput
if [ -z "$indexinput" ] || [ "$indexinput" -lt 1 ];then
index=1
avoid=1
avoidInput=1
elif [ "$indexinput" -gt "$maxItems" ];then
echo "Don't be dumb!"
exit
else
avoidInput="$indexinput"
avoid="$indexinput"
index=1
fi
echo "-->convert to flac? 1:yes 0:no (default 1)"
read delinput
if [ -z "$delinput" ] || [ "$delinput" -ne 0 ];then
delm4a=1
else
delm4a=0
fi
fi
for (( i=0; i<maxItems; i++ ))
do
if [ "$play" = 1 ];then
fastCheck=$(jq -r ".items[$i].item.audioQuality" <<< "$html")
else
fastCheck=$(jq -r ".items[$i].audioQuality" <<< "$html")
fi
if [ "$fastCheck" != "LOSSLESS" ] && [ "$fastCheck" != "HI_RES" ]; then
echo "Track $((i+1)) not LOSSLESS! Type anything once to stop ripping and continue output."
echo "$fastCheck"
if [ -z "$filler" ]; then
read filler
else
stop=1
fi
fi
done
if [ "$stop" = 1 ]; then
exit
fi
if [ "$play" = 1 ];then
while read -r name
do
trackName+=("$name");
done < <(jq -r '.items[].item.title' <<< "$html")
else
while read -r name
do
trackName+=("$name");
done < <(jq -r '.items[].title' <<< "$html")
fi
if [ "$play" = 1 ];then
while read -r name
do
trackNumber+=("$name");
done < <(jq -r '.items[].item.trackNumber' <<< "$html")
else
while read -r name
do
trackNumber+=("$name");
done < <(jq -r '.items[].trackNumber' <<< "$html")
fi
###split singles in different folder
if [ "$play" = 1 ];then
albumName="$albumID"
else
albumName=$(gsed -e 's@/@,@g' <<< $(jq -r '.items[0].album.title' <<< "$html"))
fi
#VA CHECK
#--------
if [ "$play" = 0 ];then
if [ "$maxItems" -ge 3 ];then
artistNameA=$(jq -r '.items[1].artist.name' <<< "$html")
artistNameB=$(jq -r '.items[2].artist.name' <<< "$html")
artistNameC=$(jq -r '.items[0].artist.name' <<< "$html")
if [ "$artistNameA" = "$artistNameB" ] && [ "$artistNameB" = "$artistNameC" ] && [ "$artistNameA" = "$artistNameC" ];then
artistName=$(jq -r '.items[1].artist.name' <<< "$html")
else
artistName="VA"
fi
else
artistName=$(jq -r '.items[0].artist.name' <<< "$html")
fi
fi
#--------
if [ "$play" = 0 ];then
yearF=$(jq -r ".items[0].streamStartDate" <<< "$html" | cut -c1-4)
fi
if [ "$play" = 0 ];then
if [ "$delm4a" = 1 ];then
mkdir "$artistName - $albumName ($yearF) [FLAC] web"
else
mkdir "$artistName - $albumName ($yearF) [ALAC] web"
fi
else
if [ "$delm4a" = 1 ];then
mkdir "$albumName [FLAC] web"
else
mkdir "$albumName [ALAC] web"
fi
fi
cd "$_"
#inefficient way of clearing probably but worth the hassle
if ls RIPPERTEMP.txt 1> /dev/null 2>&1; then
chmod 777 RIPPERTEMP.txt
rm RIPPERTEMP.txt
fi
if [ "$play" = 1 ];then
jq '.items[].item.id' <<< "$html" >> RIPPERTEMP.txt
else
jq '.items[].id' <<< "$html" >> RIPPERTEMP.txt
fi
filename="RIPPERTEMP.txt"
if ls cover.jpg 1> /dev/null 2>&1; then
echo "Already Cover Exists."
else
if [ "$play" = 0 ];then
coverLocation=$(jq -r '.items[0].album.cover' <<< "$html")
imageURL="https://resources.tidal.com/images/${coverLocation//-//}/1280x1280.jpg"
wget -O cover.jpg "$imageURL"
fi
fi
while read -r line
do
skip=0
if [ $avoid = $index ]; then
if [ $index -lt 10 ]; then
findex="0${index}"
else
findex="$index"
fi
dindex=$((index-1))
name="$line"
newfix=$(gsed -e 's@/@,@g' <<< "${trackName[dindex]}")
if ls "${findex} - $newfix.m4a" 1> /dev/null 2>&1;then
echo "Already Exists."
skip=1
index=$((index+1))
avoid=$((avoid+1))
elif ls "${findex} - $newfix.flac" 1> /dev/null 2>&1;then
echo "Already Exists."
skip=1
index=$((index+1))
avoid=$((avoid+1))
elif ls "$newfix.m4a" 1> /dev/null 2>&1;then
echo "Already Exists."
skip=1
index=$((index+1))
avoid=$((avoid+1))
elif ls "$newfix.flac" 1> /dev/null 2>&1;then
echo "Already Exists."
skip=1
index=$((index+1))
avoid=$((avoid+1))
else
if [ "$play" = 1 ];then
wget --tries=2 -O "$newfix.m4a" "$(curl -s -H "$tidalSession" -H "X-Tidal-Token: _DSTon1kC8pABnTw" -H "User-Agent: TIDAL/362 CFNetwork/711.4.6 Darwin/14.0.0" "http://api.tidalhifi.com/v1/tracks/$name/streamurl?countryCode=US&soundQuality=LOSSLESS" | jq -r ."url")"
else
wget --tries=2 -O "${findex} - $newfix.m4a" "$(curl -s -H "$tidalSession" -H "X-Tidal-Token: $token" -H "User-Agent: TIDAL/362 CFNetwork/711.4.6 Darwin/14.0.0" "http://api.tidalhifi.com/v1/tracks/$name/streamurl?countryCode=US&soundQuality=LOSSLESS" | jq -r ."url")"
fi
if [ "$delm4a" = 1 ] && [ "$skip" = 0 ];then
if [ "$play" = 1 ];then
year=$(jq -r ".items[$dindex].item.streamStartDate" <<< "$html" | cut -c1-4)
artistName=$(jq -r ".items[$dindex].item.artist.name" <<< "$html")
albumNameN=$(jq -r ".items[$dindex].item.album.title" <<< "$html")
ffmpeg -y -i "${findex} - ${trackName[dindex]}.m4a" -metadata title="${trackName[dindex]}" -metadata album="$albumNameN" -metadata track="${trackNumber[dindex]}" -metadata artist="$artistName" -metadata year="$year" -f flac "${findex} - ${trackName[dindex]}.flac" < /dev/null
rm "${findex} - ${trackName[dindex]}.m4a"
else
year=$(jq -r ".items[$dindex].streamStartDate" <<< "$html" | cut -c1-4)
artistName=$(jq -r ".items[$dindex].artist.name" <<< "$html")
ffmpeg -y -i "${findex} - ${trackName[dindex]}.m4a" -metadata title="${trackName[dindex]}" -metadata album="$albumName" -metadata track="${trackNumber[dindex]}" -metadata artist="$artistName" -metadata year="$year" -f flac "${findex} - ${trackName[dindex]}.flac" < /dev/null
rm "${findex} - ${trackName[dindex]}.m4a"
fi
fi
avoid=$((avoid+1))
index=$((index+1))
fi
else
index=$((index+1))
fi
done < "$filename"
if [ "$delm4a" = 1 ];then
echo 'Adding gain..'
#metaflac --add-replay-gain *.flac
fi
chmod 777 RIPPERTEMP.txt
rm RIPPERTEMP.txt
itemCount=$(($(\ls -afq | wc -l)-2))
if ls ".DS_Store" 1> /dev/null 2>&1;then
echo "----.DS_Store garbage found in folder."
maxItems=$((maxItems+1))
fi
if [ "$itemCount" = $((maxItems-avoidInput+2)) ] && [ "$play" = 0 ];then
right=1
elif [ "$itemCount" = $((maxItems-avoidInput+1)) ] && [ "$play" = 1 ];then
right=1
fi
if [ "$right" = 1 ];then
echo "Index start was at $avoidInput."
echo "$itemCount files found."
echo "100% SUCCESSFULLY RIPPED."
else
echo "early termination or error -- check files"
fi
trackName=()
trackNumber=()
cd ..
done
exit