This repository has been archived by the owner on Jul 12, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 25
/
Audio.sh
1902 lines (1629 loc) · 83.8 KB
/
Audio.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
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
scriptVersion="1.0.10"
if [ -z "$lidarrUrl" ] || [ -z "$lidarrApiKey" ]; then
lidarrUrlBase="$(cat /config/config.xml | xq | jq -r .Config.UrlBase)"
if [ "$lidarrUrlBase" == "null" ]; then
lidarrUrlBase=""
else
lidarrUrlBase="/$(echo "$lidarrUrlBase" | sed "s/\///g")"
fi
lidarrApiKey="$(cat /config/config.xml | xq | jq -r .Config.ApiKey)"
lidarrAgentInstanceId="$(echo "$lidarrApiKey" | cut -c1-10)"
lidarrPort="$(cat /config/config.xml | xq | jq -r .Config.Port)"
lidarrUrl="http://localhost:${lidarrPort}${lidarrUrlBase}"
fi
# Debugging settings
#dlClientSource="tidal"
#topLimit="3"
#addDeezerTopArtists="true"
#addDeezerTopAlbumArtists="true"
#addDeezerTopTrackArtists="true"
#audioFormat="aac"
#audioBitrate="160"
#addRelatedArtists="true"
#numberOfRelatedArtistsToAddPerArtist="1"
#beetsMatchPercentage="90"
#requireQuality="false"
#searchSort="album"
#arlToken=""
#matchDistance=10
#enableBeetsTagging=true
sleepTimer=0.5
tidaldlFail=0
deemixFail=0
log () {
m_time=`date "+%F %T"`
echo $m_time" :: Audio :: $scriptVersion :: "$1
}
# auto-clean up log file to reduce space usage
if [ -f "/config/logs/Audio.txt" ]; then
find /config/logs -type f -name "Audio.txt" -size +5000k -delete
sleep 0.01
fi
touch "/config/logs/Audio.txt"
exec &> >(tee -a "/config/logs/Audio.txt")
chmod 666 "/config/logs/Audio.txt"
verifyApiAccess () {
until false
do
lidarrTest=$(wget --timeout=0 -q -O - "$lidarrUrl/api/v1/system/status?apikey=${lidarrApiKey}" | jq -r .appName)
if [ "$lidarrTest" == "Lidarr" ]; then
lidarrVersion=$(wget --timeout=0 -q -O - "$lidarrUrl/api/v1/system/status?apikey=${lidarrApiKey}" | jq -r .version)
log "Lidarr Version: $lidarrVersion"
break
else
log "Lidarr is not ready, sleeping until valid response..."
sleep 1
fi
done
}
log "-----------------------------------------------------------------------------"
log " |~) _ ._ _| _ ._ _ |\ |o._ o _ |~|_|_|"
log " |~\(_|| |(_|(_)| | || \||| |_|(_||~| | |<"
log " Presents: lidarr-extended ($scriptVersion)"
log " Docker Version: $dockerVersion"
log " May the beats be with you!"
log "-----------------------------------------------------------------------------"
log "Donate: https://github.com/sponsors/RandomNinjaAtk"
log "Project: https://github.com/RandomNinjaAtk/docker-lidarr-extended"
log "Support: https://github.com/RandomNinjaAtk/docker-lidarr-extended/discussions"
log "-----------------------------------------------------------------------------"
sleep 5
log ""
log "Lift off in..."; sleep 0.5
log "5"; sleep 1
log "4"; sleep 1
log "3"; sleep 1
log "2"; sleep 1
log "1"; sleep 1
if [ ! -d /config/xdg ]; then
mkdir -p /config/xdg
fi
Configuration () {
processdownloadid="$(ps -A -o pid,cmd|grep "Audio.sh" | grep -v grep | head -n 1 | awk '{print $1}')"
log "To kill script, use the following command:"
log "kill -9 $processdownloadid"
sleep 2
if [ -z $topLimit ]; then
topLimit=10
fi
verifyApiAccess
if [ "$addDeezerTopArtists" == "true" ]; then
log "Add Deezer Top $topLimit Artists is enabled"
else
log "Add Deezer Top Artists is disabled (enable by setting addDeezerTopArtists=true)"
fi
if [ "$addDeezerTopAlbumArtists" == "true" ]; then
log "Add Deezer Top $topLimit Album Artists is enabled"
else
log "Add Deezer Top Album Artists is disabled (enable by setting addDeezerTopAlbumArtists=true)"
fi
if [ "$addDeezerTopTrackArtists" == "true" ]; then
log "Add Deezer Top $topLimit Track Artists is enabled"
else
log "Add Deezer Top Track Artists is disabled (enable by setting addDeezerTopTrackArtists=true)"
fi
if [ "$addRelatedArtists" == "true" ]; then
log "Add Deezer Related Artists is enabled"
log "Add $numberOfRelatedArtistsToAddPerArtist Deezer related Artist for each Lidarr Artist"
else
log "Add Deezer Related Artists is disabled (enable by setting addRelatedArtists=true)"
fi
log "Download Location: $downloadPath"
log "Output format: $audioFormat"
if [ "$audioFormat" != "native" ]; then
if [ "$audioFormat" == "alac" ]; then
audioBitrateText="LOSSLESS"
else
audioBitrateText="${audioBitrate}k"
fi
fi
log "Output bitrate: $audioBitrateText"
if [ "$requireQuality" == "true" ]; then
log "Download Quality Check Enabled"
else
log "Download Quality Check Disabled (enable by setting: requireQuality=true"
fi
if [ "$audioLyricType" == "both" ] || [ "$audioLyricType" == "explicit" ] || [ "$audioLyricType" == "explicit" ]; then
log "Preferred audio lyric type: $audioLyricType"
fi
log "Tidal Country Code set to: $tidalCountryCode"
if [ "$enableReplaygainTags" == "true" ]; then
log "Replaygain Tagging Enabled"
else
log "Replaygain Tagging Disabled"
fi
log "Match Distance: $matchDistance"
if [ $enableBeetsTagging = true ]; then
log "Beets Tagging Enabled"
log "Beets Matching Threshold ${beetsMatchPercentage}%"
beetsMatchPercentage=$(expr 100 - $beetsMatchPercentage )
if cat /config/extended/scripts/beets-config.yaml | grep "strong_rec_thresh: 0.04" | read; then
log "Configuring Beets Matching Threshold"
sed -i "s/strong_rec_thresh: 0.04/strong_rec_thresh: 0.${beetsMatchPercentage}/g" /config/extended/scripts/beets-config.yaml
fi
else
log "Beets Tagging Disabled"
fi
}
DownloadClientFreyr () {
freyr -d /downloads-lidarr-extended/incomplete deezer:album:$1
}
DownloadFormat () {
if [ "$audioFormat" == "native" ]; then
if [ "$audioBitrate" == "master" ]; then
tidalQuality=Master
deemixQuality=flac
elif [ "$audioBitrate" == "lossless" ]; then
tidalQuality=HiFi
deemixQuality=flac
elif [ "$audioBitrate" == "high" ]; then
tidalQuality=High
deemixQuality=320
elif [ "$audioBitrate" == "low" ]; then
tidalQuality=128
deemixQuality=128
else
log "ERROR :: Invalid audioFormat and audioBitrate options set..."
log "ERROR :: Change audioBitrate to a low, high, or lossless..."
log "ERROR :: Exiting..."
NotifyWebhook "FatalError" "Invalid audioFormat and audioBitrate options set"
exit
fi
else
bitrateError="false"
audioFormatError="false"
tidalQuality=HiFi
deemixQuality=flac
case "$audioBitrate" in
lossless | high | low)
bitrateError="true"
;;
*)
bitrateError="false"
;;
esac
if [ "$bitrateError" == "true" ]; then
log "ERROR :: Invalid audioBitrate options set..."
log "ERROR :: Change audioBitrate to a desired bitrate number, example: 192..."
log "ERROR :: Exiting..."
NotifyWebhook "FatalError" "audioBitrate options set"
exit
fi
case "$audioFormat" in
mp3 | alac | opus | aac)
audioFormatError="false"
;;
*)
audioFormatError="true"
;;
esac
if [ "$audioFormatError" == "true" ]; then
log "ERROR :: Invalid audioFormat options set..."
log "ERROR :: Change audioFormat to a desired format (opus or mp3 or aac or alac)"
NotifyWebhook "FatalError" "audioFormat options set"
exit
fi
tidal-dl -q HiFi
deemixQuality=flac
bitrateError=""
audioFormatError=""
fi
}
DownloadFolderCleaner () {
# check for completed download folder
if [ -d "$downloadPath/complete" ]; then
log "Removing prevously completed downloads that failed to import..."
# check for completed downloads older than 1 day
if find "$downloadPath"/complete -mindepth 1 -type d -mtime +1 | read; then
# delete completed downloads older than 1 day, these most likely failed to import due to Lidarr failing to match
find "$downloadPath"/complete -mindepth 1 -type d -mtime +1 -exec rm -rf "{}" \; &>/dev/null
fi
fi
}
NotFoundFolderCleaner () {
# check for completed download folder
if [ -d /config/extended/logs/notfound ]; then
# check for notfound entries older than X days
if find /config/extended/logs/notfound -mindepth 1 -type f -mtime +$retryNotFound | read; then
log "Removing prevously notfound lidarr album ids older than $retryNotFound days to give them a retry..."
# delete ntofound entries older than X days
find /config/extended/logs/notfound -mindepth 1 -type f -mtime +$retryNotFound -delete
fi
fi
}
TidalClientSetup () {
log "TIDAL :: Verifying tidal-dl configuration"
touch /config/xdg/.tidal-dl.log
if [ -f /config/xdg/.tidal-dl.json ]; then
rm /config/xdg/.tidal-dl.json
fi
if [ ! -f /config/xdg/.tidal-dl.json ]; then
log "TIDAL :: No default config found, importing default config \"tidal.json\""
if [ -f /config/extended/scripts/tidal-dl.json ]; then
cp /config/extended/scripts/tidal-dl.json /config/xdg/.tidal-dl.json
chmod 777 -R /config/xdg/
fi
fi
TidaldlStatusCheck
tidal-dl -o "$downloadPath"/incomplete
DownloadFormat
if [ ! -f /config/xdg/.tidal-dl.token.json ]; then
TidaldlStatusCheck
#log "TIDAL :: ERROR :: Downgrade tidal-dl for workaround..."
#pip3 install tidal-dl==2022.3.4.2 --no-cache-dir &>/dev/null
log "TIDAL :: ERROR :: Loading client for required authentication, please authenticate, then exit the client..."
NotifyWebhook "FatalError" "TIDAL requires authentication, please authenticate now (check logs)"
TidaldlStatusCheck
tidal-dl
fi
if [ ! -d /config/extended/cache/tidal ]; then
mkdir -p /config/extended/cache/tidal
chmod 777 /config/extended/cache/tidal
fi
if [ -d /config/extended/cache/tidal ]; then
log "TIDAL :: Purging album list cache..."
rm /config/extended/cache/tidal/*-albums.json &>/dev/null
fi
if [ ! -d "$downloadPath/incomplete" ]; then
mkdir -p "$downloadPath"/incomplete
chmod 777 "$downloadPath"/incomplete
else
rm -rf "$downloadPath"/incomplete/*
fi
TidaldlStatusCheck
#log "TIDAL :: Upgrade tidal-dl to newer version..."
#pip3 install tidal-dl==2022.07.06.1 --no-cache-dir &>/dev/null
}
TidaldlStatusCheck () {
until false
do
running=no
if ps aux | grep "tidal-dl" | grep -v "grep" | read; then
running=yes
log "STATUS :: TIDAL-DL :: BUSY :: Pausing/waiting for all active tidal-dl tasks to end..."
sleep 2
continue
fi
break
done
}
DownloadProcess () {
# Required Input Data
# $1 = Album ID to download from online Service
# $2 = Download Client Type (DEEZER or TIDAL)
# $3 = Album Year that matches Album ID Metadata
# $4 = Album Title that matches Album ID Metadata
# $5 = Expected Track Count
# Create Required Directories
if [ ! -d "$downloadPath/incomplete" ]; then
mkdir -p "$downloadPath"/incomplete
chmod 777 "$downloadPath"/incomplete
else
rm -rf "$downloadPath"/incomplete/*
fi
if [ ! -d "$downloadPath/complete" ]; then
mkdir -p "$downloadPath"/complete
chmod 777 "$downloadPath"/complete
else
rm -rf "$downloadPath"/complete/*
fi
if [ ! -d "/config/extended/logs" ]; then
mkdir -p /config/extended/logs
chmod 777 /config/extended/logs
fi
if [ ! -d "/config/extended/logs/downloaded" ]; then
mkdir -p /config/extended/logs/downloaded
chmod 777 /config/extended/logs/downloaded
fi
if [ ! -d "/config/extended/logs/downloaded/deezer" ]; then
mkdir -p /config/extended/logs/downloaded/deezer
chmod 777 /config/extended/logs/downloaded/deezer
fi
if [ ! -d "/config/extended/logs/downloaded/tidal" ]; then
mkdir -p /config/extended/logs/downloaded/tidal
chmod 777 /config/extended/logs/downloaded/tidal
fi
if [ ! -d /config/extended/logs/downloaded/failed/deezer ]; then
mkdir -p /config/extended/logs/downloaded/failed/deezer
chmod 777 /config/extended/logs/downloaded/failed/deezer
fi
if [ ! -d /config/extended/logs/downloaded/failed/tidal ]; then
mkdir -p /config/extended/logs/downloaded/failed/tidal
chmod 777 /config/extended/logs/downloaded/failed/tidal
fi
downloadedAlbumTitleClean="$(echo "$4" | sed -e "s%[^[:alpha:][:digit:]._' ]% %g" -e "s/ */ /g" | sed 's/^[.]*//' | sed 's/[.]*$//g' | sed 's/^ *//g' | sed 's/ *$//g')"
if find "$downloadPath"/complete -type d -iname "$lidarrArtistNameSanitized-$downloadedAlbumTitleClean ($3)-*-$1-$2" | read; then
log "$page :: $wantedAlbumListSource :: $processNumber of $wantedListAlbumTotal :: $lidarrArtistName :: $lidarrAlbumTitle :: $lidarrAlbumType :: ERROR :: Previously Downloaded..."
return
fi
# check for log file
if [ "$2" == "DEEZER" ]; then
if [ -f /config/extended/logs/downloaded/deezer/$1 ]; then
log "$page :: $wantedAlbumListSource :: $processNumber of $wantedListAlbumTotal :: $lidarrArtistName :: $lidarrAlbumTitle :: $lidarrAlbumType :: ERROR :: Previously Downloaded ($1)..."
return
fi
if [ -f /config/extended/logs/downloaded/failed/deezer/$1 ]; then
log "$page :: $wantedAlbumListSource :: $processNumber of $wantedListAlbumTotal :: $lidarrArtistName :: $lidarrAlbumTitle :: $lidarrAlbumType :: ERROR :: Previously Attempted Download ($1)..."
return
fi
fi
# check for log file
if [ "$2" == "TIDAL" ]; then
if [ -f /config/extended/logs/downloaded/tidal/$1 ]; then
log "$page :: $wantedAlbumListSource :: $processNumber of $wantedListAlbumTotal :: $lidarrArtistName :: $lidarrAlbumTitle :: $lidarrAlbumType :: ERROR :: Previously Downloaded ($1)..."
return
fi
if [ -f /config/extended/logs/downloaded/failed/tidal/$1 ]; then
log "$page :: $wantedAlbumListSource :: $processNumber of $wantedListAlbumTotal :: $lidarrArtistName :: $lidarrAlbumTitle :: $lidarrAlbumType :: ERROR :: Previously Attempted Download ($1)..."
return
fi
fi
downloadTry=0
until false
do
downloadTry=$(( $downloadTry + 1 ))
if [ -f /temp-download ]; then
rm /temp-download
sleep 0.1
fi
touch /temp-download
sleep 0.1
log "$page :: $wantedAlbumListSource :: $processNumber of $wantedListAlbumTotal :: $lidarrArtistName :: $lidarrAlbumTitle :: $lidarrAlbumType :: Download Attempt number $downloadTry"
if [ "$2" == "DEEZER" ]; then
if [ -z $arlToken ]; then
DownloadClientFreyr $1
else
deemix -b $deemixQuality -p "$downloadPath"/incomplete "https://www.deezer.com/album/$1"
fi
if [ -d "/tmp/deemix-imgs" ]; then
rm -rf /tmp/deemix-imgs
fi
# Verify Client Works...
clientTestDlCount=$(find "$downloadPath"/incomplete/ -type f -regex ".*/.*\.\(flac\|opus\|m4a\|mp3\)" | wc -l)
if [ $clientTestDlCount -le 0 ]; then
# Add +1 to failed attempts
deemixFail=$(( $deemixFail + 1))
else
# Reset for successful download
deemixFail=0
fi
# If download failes 6 times, exit with error...
if [ $deemixFail -eq 6 ]; then
log "DEEZER :: ERROR :: Download failed"
log "DEEZER :: ERROR :: Please review log for errors in client"
log "DEEZER :: ERROR :: Try updating your ARL Token to possibly resolve the issue..."
log "DEEZER :: ERROR :: Exiting..."
rm -rf "$downloadPath"/incomplete/*
NotifyWebhook "FatalError" "DEEZER not authenticated but configured"
exit
fi
fi
if [ "$2" == "TIDAL" ]; then
TidaldlStatusCheck
tidal-dl -q $tidalQuality -o "$downloadPath/incomplete" -l "$1"
# Verify Client Works...
clientTestDlCount=$(find "$downloadPath"/incomplete/ -type f -regex ".*/.*\.\(flac\|opus\|m4a\|mp3\)" | wc -l)
if [ $clientTestDlCount -le 0 ]; then
# Add +1 to failed attempts
tidaldlFail=$(( $tidaldlFail + 1))
else
# Reset for successful download
tidaldlFail=0
fi
# If download failes 6 times, exit with error...
if [ $tidaldlFail -eq 6 ]; then
if [ -f /config/xdg/.tidal-dl.token.json ]; then
rm /config/xdg/.tidal-dl.token.json
fi
log "TIDAL :: ERROR :: Download failed"
log "TIDAL :: ERROR :: You will need to re-authenticate on next script run..."
log "TIDAL :: ERROR :: Exiting..."
rm -rf "$downloadPath"/incomplete/*
NotifyWebhook "FatalError" "TIDAL not authenticated but configured"
exit
fi
fi
find "$downloadPath/incomplete" -type f -iname "*.flac" -newer "/temp-download" -print0 | while IFS= read -r -d '' file; do
audioFlacVerification "$file"
if [ "$verifiedFlacFile" == "0" ]; then
log "$page :: $wantedAlbumListSource :: $processNumber of $wantedListAlbumTotal :: $lidarrArtistName :: $lidarrAlbumTitle :: $lidarrAlbumType :: Flac Verification :: $file :: Verified"
else
log "$page :: $wantedAlbumListSource :: $processNumber of $wantedListAlbumTotal :: $lidarrArtistName :: $lidarrAlbumTitle :: $lidarrAlbumType :: Flac Verification :: $file :: ERROR :: Failed Verification"
rm "$file"
fi
done
downloadCount=$(find "$downloadPath"/incomplete/ -type f -regex ".*/.*\.\(flac\|m4a\|mp3\)" | wc -l)
if [ "$downloadCount" -ne "$5" ]; then
log "$page :: $wantedAlbumListSource :: $processNumber of $wantedListAlbumTotal :: $lidarrArtistName :: $lidarrAlbumTitle :: $lidarrAlbumType :: ERROR :: download failed, missing tracks..."
completedVerification="false"
else
log "$page :: $wantedAlbumListSource :: $processNumber of $wantedListAlbumTotal :: $lidarrArtistName :: $lidarrAlbumTitle :: $lidarrAlbumType :: Success"
completedVerification="true"
fi
if [ "$completedVerification" == "true" ]; then
break
elif [ "$downloadTry" == "2" ]; then
if [ -d "$downloadPath"/incomplete ]; then
rm -rf "$downloadPath"/incomplete/*
fi
break
else
log "$page :: $wantedAlbumListSource :: $processNumber of $wantedListAlbumTotal :: $lidarrArtistName :: $lidarrAlbumTitle :: $lidarrAlbumType :: Retry Download in 1 second fix errors..."
sleep 1
fi
done
# Consolidate files to a single folder
log "$page :: $wantedAlbumListSource :: $processNumber of $wantedListAlbumTotal :: $lidarrArtistName :: $lidarrAlbumTitle :: $lidarrAlbumType :: Consolidating files to single folder"
find "$downloadPath/incomplete" -type f -exec mv "{}" "$downloadPath"/incomplete/ \;
find /downloads-lidarr-extended/incomplete/ -type d -mindepth 1 -maxdepth 1 -exec rm -rf {} \;
downloadCount=$(find "$downloadPath"/incomplete/ -type f -regex ".*/.*\.\(flac\|m4a\|mp3\)" | wc -l)
if [ "$downloadCount" -gt "0" ]; then
# Check download for required quality (checks based on file extension)
DownloadQualityCheck "$downloadPath/incomplete" "$2"
fi
downloadCount=$(find "$downloadPath"/incomplete/ -type f -regex ".*/.*\.\(flac\|m4a\|mp3\)" | wc -l)
if [ "$downloadCount" -ne "$5" ]; then
log "$page :: $wantedAlbumListSource :: $processNumber of $wantedListAlbumTotal :: $lidarrArtistName :: $lidarrAlbumTitle :: $lidarrAlbumType :: ERROR :: All download Attempts failed..."
log "$page :: $wantedAlbumListSource :: $processNumber of $wantedListAlbumTotal :: $lidarrArtistName :: $lidarrAlbumTitle :: $lidarrAlbumType :: Logging $1 as failed download..."
if [ "$2" == "DEEZER" ]; then
touch /config/extended/logs/downloaded/failed/deezer/$1
fi
if [ "$2" == "TIDAL" ]; then
touch /config/extended/logs/downloaded/failed/tidal/$1
fi
return
fi
# Log Completed Download
log "$page :: $wantedAlbumListSource :: $processNumber of $wantedListAlbumTotal :: $lidarrArtistName :: $lidarrAlbumTitle :: $lidarrAlbumType :: Logging $1 as successfully downloaded..."
if [ "$2" == "DEEZER" ]; then
touch /config/extended/logs/downloaded/deezer/$1
fi
if [ "$2" == "TIDAL" ]; then
touch /config/extended/logs/downloaded/tidal/$1
fi
# Correct Artist/albumartist Flac files
find "$downloadPath/incomplete" -type f -iname "*.flac" -print0 | while IFS= read -r -d '' file; do
log "$page :: $wantedAlbumListSource :: $processNumber of $wantedListAlbumTotal :: $lidarrArtistName :: $lidarrAlbumTitle :: $lidarrAlbumType :: Setting ARTIST/ALBUMARTIST tag to \"$lidarrArtistName\" :: $file"
metaflac --remove-tag=ALBUMARTIST "$file"
metaflac --remove-tag=ARTIST "$file"
metaflac --remove-tag=MUSICBRAINZ_ARTISTID "$file"
metaflac --remove-tag=MUSICBRAINZ_ALBUMARTISTID "$file"
metaflac --set-tag=ALBUMARTIST="$lidarrArtistName" "$file"
metaflac --set-tag=ARTIST="$lidarrArtistName" "$file"
metaflac --set-tag=MUSICBRAINZ_ARTISTID="$lidarrArtistForeignArtistId" "$file"
metaflac --set-tag=MUSICBRAINZ_ALBUMARTISTID="$lidarrArtistForeignArtistId" "$file"
done
# Tag with beets
if [ "$enableBeetsTagging" == "true" ]; then
if [ -f /config/extended/beets-error ]; then
rm /config/extended/beets-error
fi
log "$page :: $wantedAlbumListSource :: $processNumber of $wantedListAlbumTotal :: $lidarrArtistName :: $lidarrAlbumTitle :: $lidarrAlbumType :: Processing files with beets..."
ProcessWithBeets "$downloadPath/incomplete"
if [ -f /config/extended/beets-error ]; then
return
fi
fi
# Embed Lyrics into Flac files
find "$downloadPath/incomplete" -type f -iname "*.flac" -print0 | while IFS= read -r -d '' file; do
lrcFile="${file%.*}.lrc"
if [ -f "$lrcFile" ]; then
log "$page :: $wantedAlbumListSource :: $processNumber of $wantedListAlbumTotal :: $lidarrArtistName :: $lidarrAlbumTitle :: $lidarrAlbumType :: Embedding lyrics (lrc) into $file"
metaflac --remove-tag=Lyrics "$file"
metaflac --set-tag-from-file="Lyrics=$lrcFile" "$file"
rm "$lrcFile"
fi
done
if [ "$audioFormat" != "native" ]; then
log "$page :: $wantedAlbumListSource :: $processNumber of $wantedListAlbumTotal :: $lidarrArtistName :: $lidarrAlbumTitle :: $lidarrAlbumType :: Converting Flac Audio to ${audioFormat^^} ($audioBitrateText)"
if [ "$audioFormat" == "opus" ]; then
options="-c:a libopus -b:a ${audioBitrate}k -application audio -vbr off"
extension="opus"
fi
if [ "$audioFormat" == "mp3" ]; then
options="-c:a libmp3lame -b:a ${audioBitrate}k"
extension="mp3"
fi
if [ "$audioFormat" == "aac" ]; then
options="-c:a aac -b:a ${audioBitrate}k -movflags faststart"
extension="m4a"
fi
if [ "$audioFormat" == "alac" ]; then
options="-c:a alac -movflags faststart"
extension="m4a"
fi
find "$downloadPath/incomplete" -type f -iname "*.flac" -print0 | while IFS= read -r -d '' audio; do
file="${audio}"
filename="$(basename "$audio")"
foldername="$(dirname "$audio")"
filenamenoext="${filename%.*}"
if [ "$audioFormat" == "opus" ]; then
if opusenc --bitrate ${audioBitrate} --vbr --music "$file" "$foldername/${filenamenoext}.$extension"; then
log "$page :: $wantedAlbumListSource :: $processNumber of $wantedListAlbumTotal :: $lidarrArtistName :: $lidarrAlbumTitle :: $lidarrAlbumType :: $filename :: Conversion to $audioFormat ($audioBitrateText) successful"
rm "$file"
else
log "$page :: $wantedAlbumListSource :: $processNumber of $wantedListAlbumTotal :: $lidarrArtistName :: $lidarrAlbumTitle :: $lidarrAlbumType :: $filename :: ERROR :: Conversion Failed"
rm "$foldername/${filenamenoext}.$extension"
fi
continue
fi
if ffmpeg -loglevel warning -hide_banner -nostats -i "$file" -n -vn $options "$foldername/${filenamenoext}.$extension" < /dev/null; then
log "$page :: $wantedAlbumListSource :: $processNumber of $wantedListAlbumTotal :: $lidarrArtistName :: $lidarrAlbumTitle :: $lidarrAlbumType :: $filename :: Conversion to $audioFormat ($audioBitrateText) successful"
rm "$file"
else
log "$page :: $wantedAlbumListSource :: $processNumber of $wantedListAlbumTotal :: $lidarrArtistName :: $lidarrAlbumTitle :: $lidarrAlbumType :: $filename :: ERROR :: Conversion Failed"
rm "$foldername/${filenamenoext}.$extension"
fi
done
fi
if [ "$enableReplaygainTags" == "true" ]; then
AddReplaygainTags "$downloadPath/incomplete"
else
log "$page :: $wantedAlbumListSource :: $processNumber of $wantedListAlbumTotal :: $lidarrArtistName :: $lidarrAlbumTitle :: $lidarrAlbumType :: Replaygain Tagging Disabled (set enableReplaygainTags=true to enable...)"
fi
albumquality="$(find "$downloadPath"/incomplete/ -type f -regex ".*/.*\.\(flac\|opus\|m4a\|mp3\)" | head -n 1 | egrep -i -E -o "\.{1}\w*$" | sed 's/\.//g')"
downloadedAlbumFolder="$lidarrArtistNameSanitized-$downloadedAlbumTitleClean ($3)-${albumquality^^}-$1-$2"
find "$downloadPath/incomplete" -type f -regex ".*/.*\.\(flac\|opus\|m4a\|mp3\)" -print0 | while IFS= read -r -d '' audio; do
file="${audio}"
filenoext="${file%.*}"
filename="$(basename "$audio")"
extension="${filename##*.}"
filenamenoext="${filename%.*}"
if [ ! -d "$downloadPath/complete" ]; then
mkdir -p "$downloadPath"/complete
chmod 777 "$downloadPath"/complete
fi
mkdir -p "$downloadPath/complete/$downloadedAlbumFolder"
mv "$file" "$downloadPath/complete/$downloadedAlbumFolder"/
done
chmod -R 777 "$downloadPath"/complete
if [ -d "$downloadPath/complete/$downloadedAlbumFolder" ]; then
NotifyLidarrForImport "$downloadPath/complete/$downloadedAlbumFolder"
LidarrTaskStatusCheck
CheckLidarrBeforeImport "$checkLidarrAlbumId"
if [ "$alreadyImported" == "true" ]; then
log "$page :: $wantedAlbumListSource :: $processNumber of $wantedListAlbumTotal :: $lidarrArtistName :: $lidarrAlbumTitle :: $lidarrAlbumType :: Already Imported, skipping..."
rm -rf "$downloadPath"/incomplete/*
fi
fi
if [ -d "$downloadPath/complete/$downloadedAlbumFolder" ]; then
NotifyLidarrForImport "$downloadPath/complete/$downloadedAlbumFolder"
LidarrTaskStatusCheck
CheckLidarrBeforeImport "$checkLidarrAlbumId"
if [ "$alreadyImported" == "true" ]; then
log "$page :: $wantedAlbumListSource :: $processNumber of $wantedListAlbumTotal :: $lidarrArtistName :: $lidarrAlbumTitle :: $lidarrAlbumType :: Already Imported, skipping..."
rm -rf "$downloadPath"/incomplete/*
fi
fi
if [ -d "$downloadPath/complete/$downloadedAlbumFolder" ]; then
NotifyLidarrForImport "$downloadPath/complete/$downloadedAlbumFolder"
LidarrTaskStatusCheck
CheckLidarrBeforeImport "$checkLidarrAlbumId"
if [ "$alreadyImported" == "true" ]; then
log "$page :: $wantedAlbumListSource :: $processNumber of $wantedListAlbumTotal :: $lidarrArtistName :: $lidarrAlbumTitle :: $lidarrAlbumType :: Already Imported, skipping..."
rm -rf "$downloadPath"/incomplete/*
fi
fi
if [ -d "$downloadPath/complete/$downloadedAlbumFolder" ]; then
rm -rf "$downloadPath"/incomplete/*
fi
# NotifyPlexToScan
}
ProcessWithBeets () {
# Input
# $1 Download Folder to process
if [ -f /config/extended/beets-library.blb ]; then
rm /config/extended/beets-library.blb
sleep 0.5
fi
if [ -f /config/extended/logs/beets.log ]; then
rm /config/extended/logs/beets.log
sleep 0.5
fi
if [ -f "/config/beets-match" ]; then
rm "/config/beets-match"
sleep 0.5
fi
touch "/config/beets-match"
sleep 0.5
beet -c /config/extended/scripts/beets-config.yaml -l /config/extended/beets-library.blb -d "$1" import -qC "$1"
if [ $(find "$1" -type f -regex ".*/.*\.\(flac\|opus\|m4a\|mp3\)" -newer "/config/beets-match" | wc -l) -gt 0 ]; then
log "$page :: $wantedAlbumListSource :: $processNumber of $wantedListAlbumTotal :: $lidarrArtistName :: $lidarrAlbumTitle :: $lidarrAlbumType :: SUCCESS: Matched with beets!"
find "$downloadPath/incomplete" -type f -iname "*.flac" -print0 | while IFS= read -r -d '' file; do
getArtistCredit="$(ffprobe -loglevel 0 -print_format json -show_format -show_streams "$file" | jq -r ".format.tags.ARTIST_CREDIT" | sed "s/null//g" | sed "/^$/d")"
metaflac --remove-tag=ALBUMARTIST "$file"
metaflac --remove-tag=ALBUMARTIST_CREDIT "$file"
metaflac --remove-tag=ALBUMARTISTSORT "$file"
metaflac --remove-tag=ALBUM_ARTIST "$file"
metaflac --remove-tag="ALBUM ARTIST" "$file"
metaflac --remove-tag=ARTISTSORT "$file"
metaflac --remove-tag=ARTIST "$file"
metaflac --remove-tag=MUSICBRAINZ_ARTISTID "$file"
metaflac --remove-tag=MUSICBRAINZ_ALBUMARTISTID "$file"
metaflac --set-tag=ARTIST="$lidarrArtistName" "$file"
metaflac --set-tag=ALBUMARTIST="$lidarrArtistName" "$file"
metaflac --set-tag=MUSICBRAINZ_ARTISTID="$lidarrArtistForeignArtistId" "$file"
metaflac --set-tag=MUSICBRAINZ_ALBUMARTISTID="$lidarrArtistForeignArtistId" "$file"
done
else
log "$page :: $wantedAlbumListSource :: $processNumber of $wantedListAlbumTotal :: $lidarrArtistName :: $lidarrAlbumTitle :: $lidarrAlbumType :: ERROR :: Unable to match using beets to a musicbrainz release..."
return
fi
if [ -f "/config/beets-match" ]; then
rm "/config/beets-match"
sleep 0.1
fi
# Get file metadata
GetFile=$(find "$downloadPath/incomplete" -type f -regex ".*/.*\.\(flac\|opus\|m4a\|mp3\)" | head -n1)
extension="${GetFile##*.}"
if [ "$extension" == "opus" ]; then
matchedTags=$(ffprobe -hide_banner -loglevel fatal -show_error -show_format -show_streams -show_programs -show_chapters -show_private_data -print_format json "$GetFile" | jq -r ".streams[].tags")
else
matchedTags=$(ffprobe -hide_banner -loglevel fatal -show_error -show_format -show_streams -show_programs -show_chapters -show_private_data -print_format json "$GetFile" | jq -r ".format.tags")
fi
# Get Musicbrainz Release Group ID and Album Artist ID from tagged file
if [ "$extension" == "flac" ] || [ "$extension" == "opus" ]; then
matchedTagsAlbumReleaseGroupId="$(echo $matchedTags | jq -r ".MUSICBRAINZ_RELEASEGROUPID")"
matchedTagsAlbumArtistId="$(echo $matchedTags | jq -r ".MUSICBRAINZ_ALBUMARTISTID")"
elif [ "$extension" == "mp3" ] || [ "$extension" == "m4a" ]; then
matchedTagsAlbumReleaseGroupId="$(echo $matchedTags | jq -r '."MusicBrainz Release Group Id"')"
matchedLidarrAlbumArtistId="$(echo $matchedTags | jq -r '."MusicBrainz Ablum Artist Id"')"
fi
if [ ! -d "/config/extended/logs/downloaded/musicbrainz_matched" ]; then
mkdir -p "/config/extended/logs/downloaded/musicbrainz_matched"
chmod 777 "/config/extended/logs/downloaded/musicbrainz_matched"
fi
if [ ! -f "/config/extended/logs/downloaded/musicbrainz_matched/$matchedTagsAlbumReleaseGroupId" ]; then
log "$page :: $wantedAlbumListSource :: $processNumber of $wantedListAlbumTotal :: $lidarrArtistName :: $lidarrAlbumTitle :: $lidarrAlbumType :: Marking MusicBrainz Release Group ($matchedTagsAlbumReleaseGroupId) as succesfully downloaded..."
touch "/config/extended/logs/downloaded/musicbrainz_matched/$matchedTagsAlbumReleaseGroupId"
fi
getLidarrAlbumId=$(curl -s "$lidarrUrl/api/v1/search?term=lidarr%3A${matchedTagsAlbumReleaseGroupId}&apikey=$lidarrApiKey" | jq -r .[].album.releases[].albumId | sort -u)
checkLidarrAlbumData="$(curl -s "$lidarrUrl/api/v1/album/$getLidarrAlbumId?apikey=${lidarrApiKey}")"
checkLidarrAlbumPercentOfTracks=$(echo "$checkLidarrAlbumData" | jq -r ".statistics.percentOfTracks")
if [ "$checkLidarrAlbumPercentOfTracks" = "null" ]; then
checkLidarrAlbumPercentOfTracks=0
return
fi
if [ ${checkLidarrAlbumPercentOfTracks%%.*} -ge 100 ]; then
if [ "$wantedAlbumListSource" == "missing" ]; then
log "$page :: $wantedAlbumListSource :: $processNumber of $wantedListAlbumTotal :: $lidarrArtistName :: $lidarrAlbumTitle :: $lidarrAlbumType :: ERROR :: Already Imported Album (Missing)"
rm -rf "$downloadPath/incomplete"/*
touch /config/extended/beets-error
return
else
log "$page :: $wantedAlbumListSource :: $processNumber of $wantedListAlbumTotal :: $lidarrArtistName :: $lidarrAlbumTitle :: $lidarrAlbumType :: Importing Album (Cutoff)"
return
fi
fi
}
DownloadQualityCheck () {
if [ "$requireQuality" == "true" ]; then
log "$page :: $wantedAlbumListSource :: $processNumber of $wantedListAlbumTotal :: $lidarrArtistName :: $lidarrAlbumTitle :: $lidarrAlbumType :: Checking for unwanted files"
if [ "$audioFormat" != "native" ]; then
if find "$1" -type f -regex ".*/.*\.\(opus\|m4a\|mp3\)"| read; then
log "$page :: $wantedAlbumListSource :: $processNumber of $wantedListAlbumTotal :: $lidarrArtistName :: $lidarrAlbumTitle :: $lidarrAlbumType :: Unwanted files found!"
log "$page :: $wantedAlbumListSource :: $processNumber of $wantedListAlbumTotal :: $lidarrArtistName :: $lidarrAlbumTitle :: $lidarrAlbumType :: Performing cleanup..."
rm "$1"/*
else
log "$page :: $wantedAlbumListSource :: $processNumber of $wantedListAlbumTotal :: $lidarrArtistName :: $lidarrAlbumTitle :: $lidarrAlbumType :: No unwanted files found!"
fi
fi
if [ "$audioFormat" == "native" ]; then
if [ "$audioBitrate" == "master" ]; then
if find "$1" -type f -regex ".*/.*\.\(opus\|m4a\|mp3\)"| read; then
log "$page :: $wantedAlbumListSource :: $processNumber of $wantedListAlbumTotal :: $lidarrArtistName :: $lidarrAlbumTitle :: $lidarrAlbumType :: Unwanted files found!"
log "$page :: $wantedAlbumListSource :: $processNumber of $wantedListAlbumTotal :: $lidarrArtistName :: $lidarrAlbumTitle :: $lidarrAlbumType :: Performing cleanup..."
rm "$1"/*
else
log "$page :: $wantedAlbumListSource :: $processNumber of $wantedListAlbumTotal :: $lidarrArtistName :: $lidarrAlbumTitle :: $lidarrAlbumType :: No unwanted files found!"
fi
elif [ "$audioBitrate" == "lossless" ]; then
if find "$1" -type f -regex ".*/.*\.\(opus\|m4a\|mp3\)"| read; then
log "$page :: $wantedAlbumListSource :: $processNumber of $wantedListAlbumTotal :: $lidarrArtistName :: $lidarrAlbumTitle :: $lidarrAlbumType :: Unwanted files found!"
log "$page :: $wantedAlbumListSource :: $processNumber of $wantedListAlbumTotal :: $lidarrArtistName :: $lidarrAlbumTitle :: $lidarrAlbumType :: Performing cleanup..."
rm "$1"/*
else
log "$page :: $wantedAlbumListSource :: $processNumber of $wantedListAlbumTotal :: $lidarrArtistName :: $lidarrAlbumTitle :: $lidarrAlbumType :: No unwanted files found!"
fi
elif [ "$2" == "DEEZER" ]; then
if find "$1" -type f -regex ".*/.*\.\(opus\|m4a\|flac\)"| read; then
log "$page :: $wantedAlbumListSource :: $processNumber of $wantedListAlbumTotal :: $lidarrArtistName :: $lidarrAlbumTitle :: $lidarrAlbumType :: Unwanted files found!"
log "$page :: $wantedAlbumListSource :: $processNumber of $wantedListAlbumTotal :: $lidarrArtistName :: $lidarrAlbumTitle :: $lidarrAlbumType :: Performing cleanup..."
rm "$1"/*
else
log "$page :: $wantedAlbumListSource :: $processNumber of $wantedListAlbumTotal :: $lidarrArtistName :: $lidarrAlbumTitle :: $lidarrAlbumType :: No unwanted files found!"
fi
elif [ "$2" == "TIDAL" ]; then
if find "$1" -type f -regex ".*/.*\.\(opus\|flac\|mp3\)"| read; then
log "$page :: $wantedAlbumListSource :: $processNumber of $wantedListAlbumTotal :: $lidarrArtistName :: $lidarrAlbumTitle :: $lidarrAlbumType :: Unwanted files found!"
log "$page :: $wantedAlbumListSource :: $processNumber of $wantedListAlbumTotal :: $lidarrArtistName :: $lidarrAlbumTitle :: $lidarrAlbumType :: Performing cleanup..."
rm "$1"/*
else
log "$page :: $wantedAlbumListSource :: $processNumber of $wantedListAlbumTotal :: $lidarrArtistName :: $lidarrAlbumTitle :: $lidarrAlbumType :: No unwanted files found!"
fi
fi
fi
else
log "$page :: $wantedAlbumListSource :: $processNumber of $wantedListAlbumTotal :: $lidarrArtistName :: $lidarrAlbumTitle :: $lidarrAlbumType :: Skipping download quality check... (enable by setting: requireQuality=true)"
fi
}
AddReplaygainTags () {
# Input Data
# $1 Folder path to scan and add tags
log "$page :: $wantedAlbumListSource :: $processNumber of $wantedListAlbumTotal :: $lidarrArtistName :: $lidarrAlbumTitle :: $lidarrAlbumType :: Adding Replaygain Tags using r128gain"
r128gain -r -c 1 -a "$1" &>/dev/null
}
NotifyLidarrForImport () {
LidarrProcessIt=$(curl -s "$lidarrUrl/api/v1/command" --header "X-Api-Key:"${lidarrApiKey} -H "Content-Type: application/json" --data "{\"name\":\"DownloadedAlbumsScan\", \"path\":\"$1\"}")
log "$page :: $wantedAlbumListSource :: $processNumber of $wantedListAlbumTotal :: $lidarrArtistName :: $lidarrAlbumTitle :: $lidarrAlbumType :: LIDARR IMPORT NOTIFICATION SENT! :: $1"
}
NotifyPlexToScan () {
LidarrTaskStatusCheck
CheckLidarrBeforeImport "$checkLidarrAlbumId"
if [ "$alreadyImported" == "true" ]; then
log "$page :: $wantedAlbumListSource :: $processNumber of $wantedListAlbumTotal :: $lidarrArtistName :: $lidarrAlbumTitle :: $lidarrAlbumType :: Notifying Plex to Scan :: $lidarrArtistPath"
bash /config/extended/scripts/PlexNotify.bash "$lidarrArtistPath"
fi
}
DeemixClientSetup () {
log "DEEZER :: Verifying deemix configuration"
if [ ! -z "$arlToken" ]; then
arlToken="$(echo $arlToken | sed -e "s%[^[:alpha:][:digit:]]%%g" -e "s/ */ /g" | sed 's/^[.]*//' | sed 's/[.]*$//g' | sed 's/^ *//g' | sed 's/ *$//g')"
# Create directories
mkdir -p /config/xdg/deemix
if [ -f "/config/xdg/deemix/.arl" ]; then
rm "/config/xdg/deemix/.arl"
fi
if [ ! -f "/config/xdg/deemix/.arl" ]; then
echo -n "$arlToken" > "/config/xdg/deemix/.arl"
fi
log "DEEZER :: ARL Token: Configured"
else
log "DEEZER :: ERROR :: arlToken setting invalid, currently set to: $arlToken"
fi
if [ -f "/config/xdg/deemix/config.json" ]; then
rm /config/xdg/deemix/config.json
fi
if [ -f "/config/extended/scripts/deemix_config.json" ]; then
log "DEEZER :: Configuring deemix client"
cp /config/extended/scripts/deemix_config.json /config/xdg/deemix/config.json
chmod 777 /config/xdg/deemix/config.json
fi
if [ -d /config/extended/cache/deezer ]; then
log "DEEZER :: Purging album list cache..."
rm /config/extended/cache/deezer/*-albums.json &>/dev/null
fi
if [ ! -d "$downloadPath/incomplete" ]; then
mkdir -p "$downloadPath"/incomplete
chmod 777 "$downloadPath"/incomplete
else
rm -rf "$downloadPath"/incomplete/*
fi
#log "DEEZER :: Upgrade deemix to the latest..."
#pip install deemix --upgrade &>/dev/null
}
LidarrRootFolderCheck () {
if curl -s "$lidarrUrl/api/v1/rootFolder" -H "X-Api-Key: ${lidarrApiKey}" | sed '1q' | grep "\[\]" | read; then
log "ERROR :: No root folder found"
log "ERROR :: Configure root folder in Lidarr to continue..."
log "ERROR :: Exiting..."
NotifyWebhook "FatalError" "No root folder found"
exit
fi
}
GetMissingCutOffList () {
# Remove previous search missing/cutoff list
if [ -d /config/extended/cache/lidarr/list ]; then
rm -rf /config/extended/cache/lidarr/list
sleep 0.1
fi
# Create list folder if does not exist
mkdir -p /config/extended/cache/lidarr/list
# Create notfound log folder if does not exist
if [ ! -d /config/extended/logs/notfound ]; then
mkdir -p /config/extended/logs/notfound
chmod 777 /config/extended/logs/notfound
fi
# Configure searchSort preferences based on settings
if [ "$searchSort" == "date" ]; then
searchOrder="releaseDate"
searchDirection="descending"
fi
if [ "$searchSort" == "album" ]; then
searchOrder="albumType"
searchDirection="ascending"
fi
lidarrMissingTotalRecords=$(wget --timeout=0 -q -O - "$lidarrUrl/api/v1/wanted/missing?page=1&pagesize=1&sortKey=$searchOrder&sortDirection=$searchDirection&apikey=${lidarrApiKey}" | jq -r .totalRecords)
log "FINDING MISSING ALBUMS :: sorted by $searchSort"
amountPerPull=1000
page=0
log "$lidarrMissingTotalRecords Missing Albums Found!"
log "Getting Missing Album IDs"
if [ $lidarrMissingTotalRecords -ge 1 ]; then
offsetcount=$(( $lidarrMissingTotalRecords / $amountPerPull ))
for ((i=0;i<=$offsetcount;i++)); do
page=$(( $i + 1 ))
offset=$(( $i * $amountPerPull ))
dlnumber=$(( $offset + $amountPerPull ))
if [ "$dlnumber" -gt "$lidarrMissingTotalRecords" ]; then
dlnumber="$lidarrMissingTotalRecords"
fi
log "$page :: missing :: Downloading page $page... ($offset - $dlnumber of $lidarrMissingTotalRecords Results)"
lidarrRecords=$(wget --timeout=0 -q -O - "$lidarrUrl/api/v1/wanted/missing?page=$page&pagesize=$amountPerPull&sortKey=$searchOrder&sortDirection=$searchDirection&apikey=${lidarrApiKey}" | jq -r '.records[].id')
log "$page :: missing :: Filtering Album IDs by removing previously searched Album IDs (/config/extended/logs/notfound/<files>)"
for lidarrRecordId in $(echo $lidarrRecords); do
if [ ! -f /config/extended/logs/notfound/$lidarrRecordId--* ]; then
touch "/config/extended/cache/lidarr/list/${lidarrRecordId}-missing"