· 6 years ago · Feb 21, 2020, 11:20 PM
1#!/bin/bash
2#####################################################################################################
3# Lidarr Automated Downloader #
4# (Powered by: Deezloader Remix) #
5# Credit: RandomNinjaAtk #
6#####################################################################################################
7# Script Start #
8#####################################################################################################
9
10############ Lidarr Settings
11LidarrUrl="$LidarrUrl" # Set domain or IP to your Lidarr instance including port. If using reverse proxy, do not use a trailing slash. Ensure you specify http/s.
12LidarrApiKey="$LidarrApiKey" # Lidarr API key.
13LidarrImportLocation="/mnt/user/media/downloads/lidarr-import"
14downloaddir="/mnt/user/media/downloads/deezloaderremix" # Deezloader download directory location
15deezloaderurl="http://127.0.0.1:1730" # Url to the download client
16downloadmethod="album" # album or track :: album method will fallback to track method if it runs into an issue
17enablefallback="true" # enables fallback to lower quality if required...
18VerifyTrackCount="true" # true = enabled :: This will verify album track count vs dl track count, if tracks are found missing, it will skip import...
19dlcheck=2 # Set the number to desired wait time before checking for completed downloads (if your connection is unstable, longer may be better)
20albumtimeoutpercentage=8 # Set the number between 1 and 100 :: This number is used to caculate album download timeout length by multiplying Album Length by ##%
21tracktimeoutpercentage=25 # Set the number between 1 and 100 :: This number is used to caculate track download timeout length by multiplying Track Length by ##%
22quality="OPUS" # SET TO: OPUS or AAC or MP3 or ALAC or FLAC - converts lossless FLAC files to set format
23ConversionBitrate="192" # Set to desired bitrate when converting to OPUS/AAC/MP3 format types
24ReplaygainTagging="TRUE" # TRUE = ENABLED, adds replaygain tags for compatible players (FLAC ONLY)
25ammount="1000000000" # Maximum: 1000000000 :: Number of wanted albums to look for....
26
27temptrackfile="${downloaddir}/temp-track"
28
29configuration () {
30
31 if [ "$VerifyTrackCount" = "true" ]; then
32 vtc="Enabled"
33 else
34 vtc="Disabled"
35 fi
36
37 if [ "$ReplaygainTagging" = "TRUE" ]; then
38 gain="Enabled"
39 else
40 gain="Disabled"
41 fi
42
43 echo "START"
44 echo ""
45 echo "Global Settings"
46 echo "Download Client: $deezloaderurl"
47 echo "Download Directory: $downloaddir"
48 echo "Lidarr Temp Import Location: $LidarrImportLocation"
49 echo "Download Quality: $quality"
50 if [ "$quality" = "OPUS" ]; then
51 echo "Download Bitrate: ${ConversionBitrate}k"
52 elif [ "$quality" = "AAC" ]; then
53 echo "Download Bitrate: ${ConversionBitrate}k"
54 elif [ "$quality" = "MP3" ]; then
55 echo "Download Bitrate: ${ConversionBitrate}k"
56 else
57 echo "Download Bitrate: lossless"
58 fi
59 echo "Download Track Count Verification: $vtc"
60 if [ "$quality" = "FLAC" ]; then
61 echo "Replaygain Tagging: $gain"
62 fi
63 echo "Total Number of Albums To Process: $wantittotal"
64 echo ""
65 echo "Begin archive process..."
66 sleep 1s
67
68 if [ "$quality" != "MP3" ]; then
69 dlquality="flac"
70 else
71 dlquality="mp3"
72 fi
73}
74
75paths () {
76
77 if [ ! -d "$downloaddir" ]; then
78 mkdir -p "$downloaddir"
79 fi
80
81 if [ ! -d "$LidarrImportLocation" ]; then
82 mkdir -p "$LidarrImportLocation"
83 fi
84
85}
86
87CleanDLPath () {
88 rm -rf "$downloaddir"/*
89}
90
91DurationCalc () {
92 local T=$1
93 local D=$((T/60/60/24))
94 local H=$((T/60/60%24))
95 local M=$((T/60%60))
96 local S=$((T%60))
97 (( $D > 0 )) && printf '%d days and ' $D
98 (( $H > 0 )) && printf '%d:' $H
99 (( $M > 0 )) && printf '%02d:' $M
100 (( $D > 0 || $H > 0 || $M > 0 )) && printf ''
101 printf '%02ds\n' $S
102}
103
104LidarrAlbums () {
105
106 wantit=$(curl -s --header "X-Api-Key:"${LidarrApiKey} --request GET "$LidarrUrl/api/v1/wanted/missing/?page=1&pagesize=${ammount}&includeArtist=true&monitored=true&sortDir=desc&sortKey=releaseDate")
107 if [ -z "$wantit" ]; then
108 echo "ERROR: Cannot communicate with Lidarr"
109 exit 1
110 fi
111 #curl -s --header "X-Api-Key:"${LidarrApiKey} --request GET "$LidarrUrl/api/v1/wanted/missing/?page=1&pagesize=${ammount}&includeArtist=true&monitored=true&sortDir=desc&sortKey=releaseDate" -o wantit.json
112 wantitid=($(echo "${wantit}"| jq -r '.records | .[] | .id'))
113 wantittotal=$(echo "${wantit}"| jq -r '.records | .[] | .id' | wc -l)
114}
115
116ProcessLidarrAlbums () {
117
118 LidarrAlbums
119
120 configuration
121
122 for id in ${!wantitid[@]}; do
123 currentprocess=$(( $id + 1 ))
124 albumid="${wantitid[$id]}"
125 wantitalbum=$(curl -s --header "X-Api-Key:"${LidarrApiKey} --request GET "$LidarrUrl/api/v1/album?albumIds=${albumid}")
126 if [ -z "$wantitalbum" ]; then
127 echo "ERROR: Cannot communicate with Lidarr"
128 exit 1
129 fi
130 wantitalbumtitle=$(echo "${wantitalbum}"| jq -r '.[] | .title')
131 wantitalbumalbumType=$(echo "${wantitalbum}"| jq -r '.[] | .albumType')
132 wantitalbumartistname=$(echo "${wantitalbum}"| jq -r '.[] | .artist.artistName')
133 wantitalbumartistdeezerid=$(echo "${wantitalbum}"| jq -r '.[] | .artist.links | .[] | select(.name=="deezer") | .url')
134 normalizetype="${wantitalbumalbumType,,}"
135 sanatizedwantitalbumtitle="$(echo "$wantitalbumtitle" | sed -e 's/[\\/:\*\?"<>\|\x01-\x1F\x7F]//g' -e 's/^\(nul\|prn\|con\|lpt[0-9]\|com[0-9]\|aux\)\(\.\|$\)//i' -e 's/^\.*$//' -e 's/^$/NONAME/' -e 's/./\L&/g')"
136 echo "Lidarr Artist Name: $wantitalbumartistname"
137 echo "Lidarr Album Title: $wantitalbumtitle ($currentprocess of $wantittotal)"
138 echo "Lidarr Album Type: $normalizetype"
139 if [ -z "$wantitalbumartistdeezerid" ]; then
140 echo "ERROR: Update artist with url"
141 continue
142 fi
143 GetDeezerArtistAlbumList
144 done
145}
146
147DownloadList () {
148
149 if [ ! -f "cache/$DeezerArtistID-albumlist.json" ]; then
150
151
152 if [ -d "temp" ]; then
153 sleep 0.1
154 rm -rf "temp"
155 fi
156 DeezerArtistAlbumListID=($(echo "${DeezerArtistAlbumList}" | jq ".data | .[] | .id"))
157 for id in ${!DeezerArtistAlbumListID[@]}; do
158 albumid="${DeezerArtistAlbumListID[$id]}"
159 if [ ! -d "temp" ]; then
160 mkdir -p "temp"
161 fi
162 if curl -sL --fail "https://api.deezer.com/album/${albumid}" -o "temp/${albumid}-temp-album.json"; then
163 sleep 0.1
164 albumtitle="$(cat "temp/${albumid}-temp-album.json" | jq ".title")"
165 sanatizedalbumtitle="$(echo "$albumtitle" | sed -e 's/[\\/:\*\?"<>\|\x01-\x1F\x7F]//g' -e 's/^\(nul\|prn\|con\|lpt[0-9]\|com[0-9]\|aux\)\(\.\|$\)//i' -e 's/^\.*$//' -e 's/^$/NONAME/' -e 's/./\L&/g')"
166 jq ". + {\"sanatized_album_name\": \"$sanatizedalbumtitle\"}" "temp/${albumid}-temp-album.json" > "temp/${albumid}-album.json"
167 rm "temp/${albumid}-temp-album.json"
168 sleep 0.1
169 else
170 echo "Error getting album information"
171 fi
172 done
173
174 if [ -f "downloadlist.json" ]; then
175 rm "downloadlist.json"
176 sleep 0.1
177 fi
178
179 if [ ! -d "cache" ]; then
180 sleep 0.1
181 mkdir -p "cache"
182 fi
183
184 jq -s '.' temp/*-album.json > "cache/$DeezerArtistID-albumlist.json"
185
186 if [ -d "temp" ]; then
187 sleep 0.1
188 rm -rf "temp"
189 fi
190
191 fi
192}
193
194GetDeezerArtistAlbumList () {
195
196 DeezerArtistID=$(printf -- "%s" "${wantitalbumartistdeezerid##*/}")
197 DeezerArtistAlbumList=$(curl -s "https://api.deezer.com/artist/${DeezerArtistID}/albums&limit=1000")
198 echo "Deezer Artist ID: $DeezerArtistID"
199 if [ -z "$DeezerArtistAlbumList" ]; then
200 echo "ERROR: Cannot communicate with Deezer"
201 fi
202
203 DownloadList
204
205 sanatizedwantitalbumtitle="$(echo "$wantitalbumtitle" | sed -e 's/[\\/:\*\?"<>\|\x01-\x1F\x7F]//g' -e 's/^\(nul\|prn\|con\|lpt[0-9]\|com[0-9]\|aux\)\(\.\|$\)//i' -e 's/^\.*$//' -e 's/^$/NONAME/' -e 's/./\L&/g')"
206
207 if [ "$normalizetype" = "single" ]; then
208 DeezerArtistMatchID=$(cat "cache/${DeezerArtistID}-albumlist.json" | jq "sort_by(.explicit_lyrics, .nb_tracks) | reverse | .[] | select(.record_type==\"single\") | select(.sanatized_album_name==\"${sanatizedwantitalbumtitle}\") | .id" | head -n1)
209 DeezerArtistAlbumListSortTotal=$(cat "cache/${DeezerArtistID}-albumlist.json" | jq "sort_by(.explicit_lyrics, .nb_tracks) | reverse | .[] | select(.record_type==\"single\") | .id" | wc -l)
210 else
211 DeezerArtistMatchID=($(cat "cache/${DeezerArtistID}-albumlist.json" | jq "sort_by(.explicit_lyrics, .nb_tracks) | reverse | .[] | select(.record_type!=\"single\") | select(.sanatized_album_name==\"${sanatizedwantitalbumtitle}\") | .id" | head -n1))
212 DeezerArtistAlbumListSortTotal=$(cat "cache/${DeezerArtistID}-albumlist.json" | jq "sort_by(.explicit_lyrics, .nb_tracks) | reverse | .[] | select(.record_type!=\"single\") | .id" | wc -l)
213 fi
214
215 echo "Checking.... $DeezerArtistAlbumListSortTotal Albums for match"
216 if [ -z "$DeezerArtistMatchID" ]; then
217 echo "ERROR: NOT FOUND"
218 echo "Fallback to fuzzy search"
219 if [ "$normalizetype" = "single" ]; then
220 DeezerArtistMatchID=$(cat "cache/${DeezerArtistID}-albumlist.json" | jq "sort_by(.explicit_lyrics, .nb_tracks) | reverse | .[] | select(.record_type==\"single\") | select(.sanatized_album_name | contains(\"${sanatizedwantitalbumtitle}\")) | .id" | head -n1)
221 DeezerArtistAlbumListSortTotal=$(cat "cache/${DeezerArtistID}-albumlist.json" | jq "sort_by(.explicit_lyrics, .nb_tracks) | reverse | .[] | select(.record_type==\"single\") | .id" | wc -l)
222 else
223 DeezerArtistMatchID=($(cat "cache/${DeezerArtistID}-albumlist.json" | jq "sort_by(.explicit_lyrics, .nb_tracks) | reverse | .[] | select(.record_type!=\"single\") | select(.sanatized_album_name | contains(\"${sanatizedwantitalbumtitle}\")) | .id" | head -n1))
224 DeezerArtistAlbumListSortTotal=$(cat "cache/${DeezerArtistID}-albumlist.json" | jq "sort_by(.explicit_lyrics, .nb_tracks) | reverse | .[] | select(.record_type!=\"single\") | .id" | wc -l)
225 fi
226 if [ -z "$DeezerArtistMatchID" ]; then
227 echo "ERROR: NOT FOUND, skipping..."
228 fi
229 fi
230
231
232
233 if [ ! -z "$DeezerArtistMatchID" ]; then
234 # echo "SUCCESS: Match Found: $DeezerArtistMatchID"
235 albumid="${DeezerArtistMatchID}"
236 albumurl="https://www.deezer.com/album/${albumid}"
237 albuminfo=$(curl -sL --fail "https://api.deezer.com/album/$albumid")
238 if [ -z "$albuminfo" ]; then
239 echo "ERROR: Cannot communicate with Deezer"
240 fi
241
242 albumname=$(echo "${albuminfo}" | jq -r ".title")
243 sanatizedalbumname="$(echo "${albumname}" | sed -e 's/[\\/:\*\?"<>\|\x01-\x1F\x7F]//g' -e 's/^\(nul\|prn\|con\|lpt[0-9]\|com[0-9]\|aux\)\(\.\|$\)//i' -e 's/^\.*$//' -e 's/^$/NONAME/' -e 's/./\L&/g')"
244 tracktotal=$(echo "${albuminfo}" | jq -r ".nb_tracks")
245 actualtracktotal=$(echo "$albuminfo" | jq -r ".tracks.data | .[] | .id" | wc -l)
246 albumdartistid=$(echo "${albuminfo}" | jq -r ".artist | .id")
247 albumlyrictype="$(echo "${albuminfo}" | jq -r ".explicit_lyrics")"
248 albumartworkurl="$(echo "${albuminfo}" | jq -r ".cover_xl")"
249 albumdate="$(echo "${albuminfo}" | jq -r ".release_date")"
250 albumyear=$(echo ${albumdate::4})
251 albumtype="$(echo "${albuminfo}" | jq -r ".record_type")"
252 albumtypecap="${albumtype^^}"
253 albumduration=$(echo "${albuminfo}" | jq -r ".duration")
254 albumdurationdisplay=$(DurationCalc $albumduration)
255 albumtimeout=$(($albumduration*$albumtimeoutpercentage/100))
256 albumtimeoutdisplay=$(DurationCalc $albumtimeout)
257 albumfallbacktimout=$(($albumduration*2))
258 importalbumfolder="${wantitalbumartistname} - ${sanatizedalbumname} (WEB)-DREMIX"
259
260 TrackCountVerification
261
262 if [ "$albumlyrictype" = true ]; then
263 albumlyrictype="Explicit"
264 elif [ "$albumlyrictype" = false ]; then
265 albumlyrictype="Clean"
266 fi
267
268 echo "Deezer Matched Album Title: $albumname (ID: $albumid)"
269 echo "Album Link: $albumurl"
270 echo "Album Release Year: $albumyear"
271 echo "Album Release Type: $albumtype"
272 echo "Album Lyric Type: $albumlyrictype"
273 echo "Album Duration: $albumdurationdisplay"
274 echo "Album Track Count: $tracktotal"
275
276 AlbumDL
277
278 if [ $trackdlfallback = 1 ]; then
279 TrackMethod
280 fi
281
282 DLAlbumArtwork
283
284 downloadedtrackcount=$(find "$downloaddir" -type f -iregex ".*/.*\.\(flac\|opus\|m4a\|mp3\)" | wc -l)
285 downloadedlyriccount=$(find "$downloaddir" -type f -iname "*.lrc" | wc -l)
286 downloadedalbumartcount=$(find "$downloaddir" -type f -iname "folder.*" | wc -l)
287 replaygaintrackcount=$(find "$downloaddir" -type f -iname "*.flac" | wc -l)
288 converttrackcount=$(find "$downloaddir" -type f -iname "*.flac" | wc -l)
289 echo "Downloaded: $downloadedtrackcount Tracks"
290 echo "Downloaded: $downloadedlyriccount Synced Lyrics"
291 echo "Downloaded: $downloadedalbumartcount Album Cover"
292
293 TrackCountDownloadVerification
294
295 conversion "$downloaddir"
296
297 if [ "${ReplaygainTagging}" = TRUE ]; then
298 replaygain "$downloaddir"
299 else
300 echo "REPLAYGAIN TAGGING DISABLED"
301 fi
302
303 ImportProcess
304
305 NotifyLidarr
306
307 CleanDLPath
308 fi
309 echo ""
310 sleep 2
311}
312
313AlbumDL () {
314 check=1
315 error=0
316 trackdlfallback=0
317 if [ "$downloadmethod" = "album" ]; then
318 if curl -s --request GET "$deezloaderurl/api/download/?url=$albumurl&quality=$dlquality" >/dev/null; then
319 echo "Download Timeout: $albumtimeoutdisplay"
320 echo "Downloading $tracktotal Tracks..."
321 sleep $dlcheck
322 let j=0
323 while [[ "$check" -le 1 ]]; do
324 let j++
325 if curl -s --request GET "$deezloaderurl/api/queue/" | grep "length\":0,\"items\":\[\]" >/dev/null; then
326 check=2
327 else
328 sleep 1s
329 if [ "$j" = "$albumtimeout" ]; then
330 dlid=$(curl -s --request GET "$deezloaderurl/api/queue/" | jq -r ".items | .[] | .queueId")
331 if curl -s --request GET "$deezloaderurl/api/canceldownload/?queueId=$dlid" >/dev/null; then
332 echo "Error downloading $albumname ($dlquality), retrying...via track method "
333 trackdlfallback=1
334 error=1
335 fi
336 fi
337 fi
338 done
339 if find "$downloaddir" -iname "*.flac" | read; then
340 fallbackqualitytext="FLAC"
341 elif find "$downloaddir" -iname "*.mp3" | read; then
342 fallbackqualitytext="MP3"
343 fi
344 if [ $error = 1 ]; then
345 rm -rf "$downloaddir"/*
346 echo "$artistname :: $albumname :: $fallbackqualitytext :: Fallback to track download method" >> "download-album-error.log"
347 else
348 echo "Downloaded Album: $albumname (Format: $fallbackqualitytext; Length: $albumdurationdisplay)"
349 Verify
350 fi
351 else
352 echo "Error sending download to Deezloader-Remix (Attempt 1)"
353 trackdlfallback=1
354 fi
355 else
356 trackdlfallback=1
357 fi
358}
359
360TrackDL () {
361 check=1
362 error=0
363 retry=0
364 fallback=0
365 fallbackbackup=0
366 fallbackquality="$dlquality"
367 if curl -s --request GET "$deezloaderurl/api/download/?url=$trackurl&quality=$dlquality" >/dev/null; then
368 sleep $dlcheck
369 let j=0
370 while [[ "$check" -le 1 ]]; do
371 let j++
372 if curl -s --request GET "$deezloaderurl/api/queue/" | grep "length\":0,\"items\":\[\]" >/dev/null; then
373 check=2
374 else
375 sleep 1s
376 retry=0
377 if [ "$j" = "$tracktimeout" ]; then
378 dlid=$(curl -s --request GET "$deezloaderurl/api/queue/" | jq -r ".items | .[] | .queueId")
379 if curl -s --request GET "$deezloaderurl/api/canceldownload/?queueId=$dlid" >/dev/null; then
380 echo "Error downloading track $tracknumber: $trackname ($dlquality), retrying...download"
381 retry=1
382 find "$downloaddir" -type f -iname "*.flac" -newer "$temptrackfile" -delete
383 find "$downloaddir" -type f -iname "*.mp3" -newer "$temptrackfile" -delete
384 fi
385 fi
386 fi
387 done
388 else
389 echo "Error sending download to Deezloader-Remix (Attempt 2)"
390 fi
391 if [ $retry = 1 ]; then
392 if curl -s --request GET "$deezloaderurl/api/download/?url=$trackurl&quality=$dlquality" >/dev/null; then
393 sleep $dlcheck
394 let k=0
395 while [[ "$retry" -le 1 ]]; do
396 let k++
397 if curl -s --request GET "$deezloaderurl/api/queue/" | grep "length\":0,\"items\":\[\]" >/dev/null; then
398 retry=2
399 else
400 sleep 1s
401 fallback=0
402 if [ "$k" = "$trackfallbacktimout" ]; then
403 dlid=$(curl -s --request GET "$deezloaderurl/api/queue/" | jq -r ".items | .[] | .queueId")
404 if curl -s --request GET "$deezloaderurl/api/canceldownload/?queueId=$dlid" >/dev/null; then
405 echo "Error downloading track $tracknumber: $trackname ($dlquality), retrying...as mp3 320"
406 fallback=1
407 find "$downloaddir" -type f -iname "*.flac" -newer "$temptrackfile" -delete
408 find "$downloaddir" -type f -iname "*.mp3" -newer "$temptrackfile" -delete
409 fi
410 fi
411 fi
412 done
413 else
414 echo "Error sending download to Deezloader-Remix (Attempt 3)"
415 fi
416 fi
417 if [ $fallback = 1 ]; then
418 if [ "$enablefallback" = true ]; then
419 if [ "$dlquality" = flac ]; then
420 fallbackquality="320"
421 bitrate="320"
422 elif [ "$dlquality" = 320 ]; then
423 fallbackquality="128"
424 bitrate="128"
425 fi
426 if curl -s --request GET "$deezloaderurl/api/download/?url=$trackurl&quality=$fallbackquality" >/dev/null; then
427 sleep $dlcheck
428 let l=0
429 while [[ "$fallback" -le 1 ]]; do
430 let l++
431 if curl -s --request GET "$deezloaderurl/api/queue/" | grep "length\":0,\"items\":\[\]" >/dev/null; then
432 fallback=2
433 else
434 sleep 1s
435 if [ "$l" = $tracktimeout ]; then
436 dlid=$(curl -s --request GET "$deezloaderurl/api/queue/" | jq -r ".items | .[] | .queueId")
437 if curl -s --request GET "$deezloaderurl/api/canceldownload/?queueId=$dlid" >/dev/null; then
438 if [ "$fallbackquality" = 128 ]; then
439 echo "Error downloading track $tracknumber: $trackname (mp3 128), skipping..."
440 error=1
441 else
442 echo "Error downloading track $tracknumber: $trackname (mp3 320), retrying...as mp3 128"
443 fallbackbackup=1
444 fi
445 find "$downloaddir" -type f -iname "*.mp3" -newer "$temptrackfile" -delete
446 fi
447 fi
448 fi
449 done
450 else
451 echo "Error sending download to Deezloader-Remix (Attempt 4)"
452 fi
453 else
454 echo "Error downloading track $tracknumber: $trackname ($dlquality), skipping..."
455 error=1
456 fi
457 fi
458 if [ $fallbackbackup = 1 ]; then
459 if [ "$enablefallback" = true ]; then
460 fallbackquality="128"
461 bitrate="128"
462 if curl -s --request GET "$deezloaderurl/api/download/?url=$trackurl&quality=$fallbackquality" >/dev/null; then
463 sleep $dlcheck
464 let l=0
465 while [[ "$fallbackbackup" -le 1 ]]; do
466 let l++
467 if curl -s --request GET "$deezloaderurl/api/queue/" | grep "length\":0,\"items\":\[\]" >/dev/null; then
468 fallbackbackup=2
469 else
470 sleep 1s
471 if [ "$l" = $trackfallbacktimout ]; then
472 dlid=$(curl -s --request GET "$deezloaderurl/api/queue/" | jq -r ".items | .[] | .queueId")
473 if curl -s --request GET "$deezloaderurl/api/canceldownload/?queueId=$dlid" >/dev/null; then
474 echo "Error downloading track $tracknumber: $trackname (mp3 128), skipping..."
475 error=1
476 find "$downloaddir" -type f -iname "*.mp3" -newer "$temptrackfile" -delete
477 fi
478 fi
479 fi
480 done
481 else
482 echo "Error sending download to Deezloader-Remix (Attempt 5)"
483 fi
484 else
485 echo "Error downloading track $tracknumber: $trackname ($dlquality), skipping..."
486 error=1
487 fi
488 fi
489
490 if find "$downloaddir" -iname "*.flac" -newer "$temptrackfile" | read; then
491 fallbackqualitytext="FLAC"
492 elif find "$downloaddir" -iname "*.mp3" -newer "$temptrackfile" | read; then
493 fallbackqualitytext="MP3"
494 fi
495 if [ $error = 1 ]; then
496 echo "$artistname :: $albumname :: $fallbackqualitytext :: $trackname (${trackid[$track]})" >> "download-track-error.log"
497 elif find "$downloaddir" -type f -iregex ".*/.*\.\(flac\|mp3\)" -newer "$temptrackfile" | read; then
498 echo "Download Track $tracknumber of $tracktotal: $trackname (Format: $fallbackqualitytext; Length: $trackdurationdisplay)"
499 Verify
500 else
501 error=1
502 echo "$artistname :: $albumname :: $fallbackqualitytext :: $trackname (${trackid[$track]})" >> "download-track-error.log"
503 fi
504}
505
506TrackMethod () {
507 CleanDLPath
508 sleep 0.1
509 echo "Downloading $tracktotal Tracks..."
510 trackid=($(echo "${albuminfo}" | jq -r ".tracks | .data | .[] | .id"))
511 for track in ${!trackid[@]}; do
512 tracknumber=$(( $track + 1 ))
513 trackname=$(echo "${albuminfo}" | jq -r ".tracks | .data | .[] | select(.id=="${trackid[$track]}") | .title")
514 trackduration=$(echo "${albuminfo}" | jq -r ".tracks | .data | .[] | select(.id=="${trackid[$track]}") | .duration")
515 trackdurationdisplay=$(DurationCalc $trackduration)
516 trackurl="https://www.deezer.com/track/${trackid[$track]}"
517 tracktimeout=$(($trackduration*$tracktimeoutpercentage/100))
518 trackfallbacktimout=$(($tracktimeout*2))
519 if [[ "$tracktimeout" -le 60 ]]; then
520 tracktimeout="60"
521 trackfallbacktimout=$(($tracktimeout*2))
522 fi
523 if [ -f "$temptrackfile" ]; then
524 rm "$temptrackfile"
525 sleep 0.1
526 fi
527 touch "$temptrackfile"
528 TrackDL
529 if [ -f "$temptrackfile" ]; then
530 rm "$temptrackfile"
531 sleep 0.1
532 fi
533 done
534}
535
536ImportProcess () {
537 if [ -d "$LidarrImportLocation/$importalbumfolder" ]; then
538 rm -rf "$LidarrImportLocation/$importalbumfolder"
539 fi
540 if [ ! -d "$LidarrImportLocation/$importalbumfolder" ]; then
541 mkdir -p "$LidarrImportLocation/$importalbumfolder"
542 fi
543 for file in "$downloaddir"/*; do
544 mv "$file" "$LidarrImportLocation/$importalbumfolder"/
545 done
546}
547
548NotifyLidarr () {
549 echo "Notified Lidarr to scan \"$LidarrImportLocation/$importalbumfolder\" for import"
550 path="$LidarrImportLocation/$importalbumfolder"
551 LidarrProcessIt=$(curl -s "$LidarrUrl/api/v1/command" --header "X-Api-Key:"${LidarrApiKey} --data "{\"name\":\"DownloadedAlbumsScan\", \"path\":\"${path}\"}" );
552}
553
554Verify () {
555 if [ $trackdlfallback = 0 ]; then
556 if find "$downloaddir" -iname "*.flac" | read; then
557 if ! [ -x "$(command -v flac)" ]; then
558 echo "ERROR: FLAC verification utility not installed (ubuntu: apt-get install -y flac)"
559 else
560 for fname in "${downloaddir}"/*.flac; do
561 filename="$(basename "$fname")"
562 if flac -t --totally-silent "$fname"; then
563 echo "Verified Track: $filename"
564 else
565 echo "Track Verification Error: \"$filename\" deleted...retrying download via track method"
566 rm -rf "$downloaddir"/*
567 sleep 0.5
568 trackdlfallback=1
569 fi
570 done
571 fi
572 fi
573 if find "$downloaddir" -iname "*.mp3" | read; then
574 if ! [ -x "$(command -v mp3val)" ]; then
575 echo "MP3VAL verification utility not installed (ubuntu: apt-get install -y mp3val)"
576 else
577 for fname in "${downloaddir}"/*.mp3; do
578 filename="$(basename "$fname")"
579 if mp3val -f -nb "$fname" > /dev/null; then
580 echo "Verified Track: $filename"
581 fi
582 done
583 fi
584 fi
585 elif [ $trackdlfallback = 1 ]; then
586 if ! [ -x "$(command -v flac)" ]; then
587 echo "ERROR: FLAC verification utility not installed (ubuntu: apt-get install -y flac)"
588 else
589 if find "$downloaddir" -iname "*.flac" -newer "$temptrackfile" | read; then
590 find "$downloaddir" -iname "*.flac" -newer "$temptrackfile" -print0 | while IFS= read -r -d '' file; do
591 filename="$(basename "$file")"
592 if flac -t --totally-silent "$file"; then
593 echo "Verified Track $tracknumber of $tracktotal: $trackname (Format: $fallbackqualitytext; Length: $trackdurationdisplay)"
594 else
595 rm "$file"
596 if [ "$enablefallback" = true ]; then
597 echo "Track Verification Error: \"$trackname\" deleted...retrying as MP3"
598 origdlquality="$dlquality"
599 dlquality="320"
600 DownloadURL
601 dlquality="$origdlquality"
602 else
603 echo "Verification Error: \"$trackname\" deleted..."
604 echo "Fallback quality disabled, skipping..."
605 echo "$artistname :: $albumname :: $fallbackqualitytext :: $trackname (${trackid[$track]})" >> "download-track-error.log"
606 fi
607 fi
608 done
609 fi
610 fi
611 if ! [ -x "$(command -v mp3val)" ]; then
612 echo "MP3VAL verification utility not installed (ubuntu: apt-get install -y mp3val)"
613 else
614 if find "$downloaddir" -iname "*.mp3" -newer "$temptrackfile" | read; then
615 find "$downloaddir" -iname "*.mp3" -newer "$temptrackfile" -print0 | while IFS= read -r -d '' file; do
616 filename="$(basename "$file")"
617 if mp3val -f -nb "$file" > /dev/null; then
618 echo "Verified Track $tracknumber of $tracktotal: $trackname (Format: $fallbackqualitytext; Length: $trackdurationdisplay)"
619 fi
620 done
621 fi
622 fi
623 fi
624}
625
626conversion () {
627 converttrackcount=$(find "$1"/ -name "*.flac" | wc -l)
628 targetformat="$quality"
629 bitrate="$ConversionBitrate"
630 if [ "${quality}" = OPUS ]; then
631 options="-acodec libopus -ab ${bitrate}k -application audio"
632 extension="opus"
633 targetbitrate="${bitrate}k"
634 fi
635 if [ "${quality}" = AAC ]; then
636 options="-acodec aac -ab ${bitrate}k -movflags faststart"
637 extension="m4a"
638 targetbitrate="${bitrate}k"
639 fi
640 if [ "${quality}" = MP3 ]; then
641 options="-acodec libmp3lame -ab ${bitrate}k"
642 extension="mp3"
643 targetbitrate="${bitrate}k"
644 fi
645 if [ "${quality}" = ALAC ]; then
646 options="-acodec alac -movflags faststart"
647 extension="m4a"
648 targetbitrate="lossless"
649 fi
650 if [ "${quality}" = FLAC ]; then
651 options="-acodec flac"
652 extension="flac"
653 targetbitrate="lossless"
654 fi
655 if [ -x "$(command -v ffmpeg)" ]; then
656 if find "$1"/ -name "*.flac" | read; then
657 echo ""
658 echo "Converting: $converttrackcount Tracks (Target Format: $targetformat (${targetbitrate}))"
659 for fname in "$1"/*.flac; do
660 filename="$(basename "${fname%.flac}")"
661 if ffmpeg -loglevel warning -hide_banner -nostats -i "$fname" -n -vn $options "${fname%.flac}.temp.$extension"; then
662 echo "Converted: $filename"
663 if [ -f "${fname%.flac}.temp.$extension" ]; then
664 rm "$fname"
665 sleep 0.1
666 mv "${fname%.flac}.temp.$extension" "${fname%.flac}.$extension"
667 fi
668 else
669 echo "Conversion failed: $filename, performing cleanup..."
670 rm -rf "$1"/*
671 sleep 0.1
672 exit 1
673 fi
674 done
675 fi
676 else
677 echo "ERROR: ffmpeg not installed, please install ffmpeg to use this conversion feature"
678 sleep 5
679 fi
680}
681
682replaygain () {
683 if ! [ -x "$(command -v flac)" ]; then
684 echo "ERROR: METAFLAC replaygain utility not installed (ubuntu: apt-get install -y flac)"
685 elif find "$1" -iname "*.flac" | read; then
686 replaygaintrackcount=$(find "$1"/ -iname "*.flac" | wc -l)
687 echo ""
688 find "$1" -iname "*.flac" -exec metaflac --add-replay-gain "{}" + && echo "Replaygain: $replaygaintrackcount Tracks Tagged"
689 fi
690}
691
692DLAlbumArtwork () {
693 if curl -sL --fail "${albumartworkurl}" -o "$downloaddir/folder.jpg"; then
694 sleep 0.1
695 else
696 echo "Failed downloading album cover picture..."
697 fi
698}
699
700TrackCountVerification () {
701 if [ "$VerifyTrackCount" = true ]; then
702 if [ "$tracktotal" -ne "$actualtracktotal" ]; then
703 echo "ERROR: Listed Track Count does not match actual track count"
704 continue
705 fi
706 fi
707}
708
709TrackCountDownloadVerification () {
710 if [ "$VerifyTrackCount" = true ]; then
711 if [ "$tracktotal" -ne "$downloadedtrackcount" ]; then
712 echo "ERROR: Downloaded Track Count ($downloadedtrackcount) and Album Track Count ($tracktotal) do not match, skipping and performing cleanup..."
713 CleanDLPath
714 continue
715 fi
716 fi
717}
718
719
720CleanDLPath
721
722paths
723
724ProcessLidarrAlbums
725
726CleanDLPath
727
728# echo "$LidarrUrl"
729# echo "$LidarrApiKey"
730
731#####################################################################################################
732# Script End #
733#####################################################################################################
734exit 0