forked from RandomNinjaAtk/arr-scripts-v2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLidarr-MusicAutomator.bash
More file actions
409 lines (346 loc) · 17.4 KB
/
Lidarr-MusicAutomator.bash
File metadata and controls
409 lines (346 loc) · 17.4 KB
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
#!/usr/bin/with-contenv bash
scriptVersion="2.3"
scriptName="Lidarr-MusicAutomator"
dockerPath="/config/logs"
arrApp="Lidarr"
searchOrder="releaseDate"
searchDirection="descending"
deemixFolder="/root/.config/deemix"
settings () {
log "Import Script $1 Settings..."
source "$1"
arrUrl="$lidarrUrl"
arrApiKey="$lidarrApiKey"
}
verifyConfig () {
if [ "$enableLidarrMusicAutomator" != "true" ]; then
log "Script is not enabled, enable by setting enableLidarrMusicAutomator to \"true\" by modifying the \"/config/<filename>.conf\" config file..."
log "Sleeping (infinity)"
sleep infinity
fi
}
InstallDependencies () {
# Fix: Check for py3-pip specifically. Checking only for python3 can lead to a state where
# python is installed but pip is missing, causing the subsequent 'python3 -m pip' commands to fail.
if apk --no-cache list | grep installed | grep py3-pip | read; then
log "Dependencies already installed, skipping..."
else
log "Installing script dependencies...."
apk add -U --update --no-cache \
jq \
xq \
git \
opus-tools \
python3 \
py3-pip
log "done"
python3 -m pip install deemix streamrip pyxDamerauLevenshtein --upgrade --break-system-packages
fi
}
ArlSetup () {
if [ -z "$arl" ]; then
log "ERROR :: ARL Key is missing!"
log "ERROR :: Please correct for script to continue running..."
log "ERROR :: Exiting..."
exit
fi
if [ ! -d "$deemixFolder" ]; then
log "Creating Deemix Config folder"
mkdir -p "$deemixFolder"
chown ${PUID:-1000}:${PGID:-1000} "$deemixFolder"
fi
if [ -f "$deemixFolder/.arl" ]; then
log "Deleting ARL"
rm "$deemixFolder/.arl"
fi
if [ ! -f "$deemixFolder/.arl" ]; then
log "Creating ARL file"
echo -n "$arl" > "$deemixFolder/.arl"
chmod 777 "$deemixFolder/.arl"
fi
if [ -f "/config/config/config.json" ]; then
if [ -f "$deemixFolder/config.json" ]; then
log "Importing custom deemix config"
rm "$deemixFolder/config.json"
cp "/config/config/config.json" "$deemixFolder/config.json"
chmod 777 "$deemixFolder/config.json"
fi
fi
}
logfileSetup () {
logFileName="$scriptName-$(date +"%Y_%m_%d_%I_%M_%p").txt"
if find "$dockerPath" -type f -iname "$scriptName-*.txt" | read; then
# Keep only the last 2 log files for 3 active log files at any given time...
rm -f $(ls -1t $dockerPath/$scriptName-* | tail -n +5)
# delete log files older than 5 days
find "$dockerPath" -type f -iname "$scriptName-*.txt" -mtime +5 -delete
fi
if [ ! -f "$dockerPath/$logFileName" ]; then
echo "" > "$dockerPath/$logFileName"
chown ${PUID:-1000}:${PGID:-1000} "$dockerPath/$logFileName"
chmod 666 "$dockerPath/$logFileName"
fi
}
log () {
m_time=`date "+%F %T"`
echo $m_time" :: $scriptName (v$scriptVersion) :: "$1
}
ArrWaitForTaskCompletion () {
log "$processNumber of $lidarrTotalRecords :: $lidarrAlbumArtistName :: $lidarrAlbumTitle :: Checking $arrApp App Status"
alerted="no"
until false
do
taskCount=$(curl -s "$arrUrl/api/v1/command?apikey=${arrApiKey}" | jq -r '.[] | select(.status=="started") | .name' | wc -l)
arrRefreshMonitoredDownloadTaskCount=$(curl -s "$arrUrl/api/v3/command?apikey=${arrApiKey}" | jq -r '.[] | select(.status=="started") | .name' | grep "RefreshMonitoredDownloads" | wc -l)
if [ "$taskCount" -ge 3 ]; then
if [ "$alerted" == "no" ]; then
alerted="yes"
log "$processNumber of $lidarrTotalRecords :: $lidarrAlbumArtistName :: $lidarrAlbumTitle :: STATUS :: $arrApp APP BUSY :: Pausing/waiting for all active Arr app tasks to end..."
log "$processNumber of $lidarrTotalRecords :: $lidarrAlbumArtistName :: $lidarrAlbumTitle :: STATUS :: $arrApp APP BUSY :: Waiting..."
fi
else
break
fi
done
log "$processNumber of $lidarrTotalRecords :: $lidarrAlbumArtistName :: $lidarrAlbumTitle :: STATUS :: Done"
}
VerifyApiAccess () {
log "Step - Verifying $arrApp API is accessible"
alerted="no"
until false
do
arrApiTest=""
arrApiVersion=""
if [ -z "$arrApiTest" ]; then
arrApiVersion="v3"
arrApiTest="$(curl -s "$arrUrl/api/$arrApiVersion/system/status?apikey=$arrApiKey" | jq -r .instanceName)"
fi
if [ -z "$arrApiTest" ]; then
arrApiVersion="v1"
arrApiTest="$(curl -s "$arrUrl/api/$arrApiVersion/system/status?apikey=$arrApiKey" | jq -r .instanceName)"
fi
if [ ! -z "$arrApiTest" ]; then
break
else
if [ "$alerted" == "no" ]; then
alerted="yes"
log "STATUS :: $arrApp is not ready, sleeping until valid response..."
fi
sleep 1
fi
done
log "STATUS :: Done"
}
SearchDeezerAlbums () {
log "$processNumber of $lidarrTotalRecords :: $lidarrAlbumArtistName :: $lidarrAlbumTitle :: Searching $3 Deezer $2 Albums for a potential match...."
for deezerAlbumId in $(echo "$1"); do
deezerAlbumData=$(echo "$getDeezerArtistAlbums" | jq -r ".data[] | select(.id==$deezerAlbumId)")
deezerAlbumTitle="$(echo "$deezerAlbumData" | jq -r .title)"
deezerExplicitLyrics="$(echo "$deezerAlbumData" | jq -r .explicit_lyrics)"
deezerAlbumTitleClean="$(echo "$deezerAlbumTitle" | sed 's/[^0-9A-Za-z]*//g')"
deezerAlbumReleaseDate="$(echo "$deezerAlbumData" | jq -r .release_date)"
deezerAlbumYear="${deezerAlbumReleaseDate:0:4}"
downloadAlbumFolderName="$deezerArtistName - $deezerAlbumTitle ($deezerAlbumYear)"
match=""
match="$(echo "${lidarrAlbumReleaseTitlesClean,,}" | grep "^${deezerAlbumTitleClean,,}$")"
diff=1
if [ ! -z "$match" ]; then
diff=0
deezerAlbumTrackCount=$(curl -s "https://api.deezer.com/album/$deezerAlbumId" | jq -r .nb_tracks)
trackCountMatch="$(echo "$lidarrAlbumReleasesTrackCounts" | grep "^$deezerAlbumTrackCount$")"
if [ -z "$trackCountMatch" ]; then
log "$processNumber of $lidarrTotalRecords :: $lidarrAlbumArtistName :: $lidarrAlbumTitle :: ERROR :: Matched album title, but trackcount miss-match, skipping..."
continue
fi
#if echo "${lidarrAlbumTitleClean,,}" | grep "${deezerAlbumTitleClean,,}" | read; then
#diff=$(python -c "from pyxdameraulevenshtein import damerau_levenshtein_distance; print(damerau_levenshtein_distance(\"${lidarrAlbumTitleClean,,}\", \"${deezerAlbumTitleClean,,}\"))" 2>/dev/null)
else
continue
fi
if [ $diff = 0 ]; then
if [ -f "$completedSearchIdLocation/deezer-$deezerAlbumId" ]; then
log "$processNumber of $lidarrTotalRecords :: $lidarrAlbumArtistName :: $lidarrAlbumTitle :: Previously Downloaded from Deezer (deezer-$deezerAlbumId), skipping..."
continue
fi
log "$processNumber of $lidarrTotalRecords :: $lidarrAlbumArtistName :: $lidarrAlbumTitle :: $deezerAlbumTitle :: Explicit Lyrics ($deezerExplicitLyrics) :: Match Found!"
if [ -d "$incompleteDownloadPath" ]; then
rm -rf "$incompleteDownloadPath"
fi
if [ ! -d "$completeDownloadPath/$downloadAlbumFolderName" ]; then
# delete temporary download location if needed
if [ ! -d "$incompleteDownloadPath" ]; then
mkdir -p "$incompleteDownloadPath"
chown ${PUID:-1000}:${PGID:-1000} "$incompleteDownloadPath"
fi
# download tracks
deemix -p "$incompleteDownloadPath" -b flac "https://www.deezer.com/en/album/$deezerAlbumId"
# Create import location
if [ ! -d "$completeDownloadPath" ]; then
mkdir -p "$completeDownloadPath"
chown ${PUID:-1000}:${PGID:-1000} "$completeDownloadPath"
chmod 777 -R "$completeDownloadPath"
fi
# Create import location album folder
if [ ! -d "$completeDownloadPath/$downloadAlbumFolderName" ]; then
mkdir -p "$completeDownloadPath/$downloadAlbumFolderName"
chown ${PUID:-1000}:${PGID:-1000} "$completeDownloadPath/$downloadAlbumFolderName"
fi
# Move downloaded files to import location album folder
mv "$incompleteDownloadPath"/* "$completeDownloadPath/$downloadAlbumFolderName"/
# set permissions
if [ -d "$completeDownloadPath/$downloadAlbumFolderName" ]; then
chmod 777 -R "$completeDownloadPath/$downloadAlbumFolderName"
fi
fi
log "$processNumber of $lidarrTotalRecords :: $lidarrAlbumArtistName :: $lidarrAlbumTitle :: Notifying Lidarr to Import \"$downloadAlbumFolderName\""
LidarrProcessIt=$(curl -s "$arrUrl/api/v1/command" --header "X-Api-Key:"${arrApiKey} -H "Content-Type: application/json" --data "{\"name\":\"DownloadedAlbumsScan\", \"path\":\"$completeDownloadPath/$downloadAlbumFolderName\"}")
touch /config/found
if [ ! -d "$completedSearchIdLocation" ]; then
mkdir -p "$completedSearchIdLocation"
chown ${PUID:-1000}:${PGID:-1000} "$completedSearchIdLocation"
chmod 777 -R "$completedSearchIdLocation"
fi
if [ -d "$completedSearchIdLocation" ]; then
touch "$completedSearchIdLocation/deezer-$deezerAlbumId"
chmod 777 "$completedSearchIdLocation/deezer-$deezerAlbumId"
fi
break
else
# For debugging only...
# log "$processNumber of $lidarrTotalRecords :: $lidarrAlbumArtistName :: $lidarrAlbumTitle :: ERROR :: $lidarrAlbumTitle vs $deezerAlbumTitle :: Failed to match, different by: $diff"
sleep 0.01
fi
done
}
LidarrWantedSearch () {
lidarrTotalRecords=$(echo "$1" | jq -r .totalRecords)
lidarrWantedIds=$(echo "$1" | jq -r '.records[].id')
processNumber=0
for lidarrAlbumId in $(echo "$lidarrWantedIds"); do
processNumber=$(( $processNumber + 1 ))
lidarrAlbumData="$(curl -s "$arrUrl/api/v1/album/$lidarrAlbumId?apikey=${arrApiKey}")"
lidarrAlbumArtistData=$(echo "${lidarrAlbumData}" | jq -r ".artist")
lidarrAlbumArtistName=$(echo "${lidarrAlbumArtistData}" | jq -r ".artistName")
lidarrAlbumArtistForeignArtistId=$(echo "${lidarrAlbumArtistData}" | jq -r ".foreignArtistId")
lidarrAlbumType=$(echo "$lidarrAlbumData" | jq -r ".albumType")
lidarrAlbumTitle=$(echo "$lidarrAlbumData" | jq -r ".title")
lidarrAlbumTitleClean="$(echo "$lidarrAlbumTitle" | sed 's/[^0-9A-Za-z]*//g')"
lidarrAlbumForeignAlbumId=$(echo "$lidarrAlbumData" | jq -r ".foreignAlbumId")
tidalArtistUrl=$(echo "${lidarrAlbumArtistData}" | jq -r ".links | .[] | select(.name==\"tidal\") | .url")
tidalArtistIds="$(echo "$tidalArtistUrl" | grep -o '[[:digit:]]*' | sort -u)"
deezerArtistUrl=$(echo "${lidarrAlbumArtistData}" | jq -r ".links | .[] | select(.name==\"deezer\") | .url")
deezerArtistIds="$(echo "$deezerArtistUrl" | grep -o '[[:digit:]]*' | sort -u)"
log "$processNumber of $lidarrTotalRecords :: $lidarrAlbumArtistName :: $lidarrAlbumTitle"
lidarrAlbumReleaseTitles=$(echo "$lidarrAlbumData" | jq -r ".releases[] | .title")
lidarrAlbumReleaseTitlesClean="$(echo "$lidarrAlbumReleaseTitles" | sed 's/[^0-9A-Za-z]*//g')"
lidarrAlbumReleaseDisambiguation=$(echo "$lidarrAlbumData" | jq -r ".releases[] | .disambiguation")
lidarrAlbumReleasesTrackCounts=$(echo "$lidarrAlbumData" | jq -r ".releases[].trackCount" | sort -u)
lidarrAlbumReleasesMinTrackCount=$(echo "$lidarrAlbumData" | jq -r ".releases[].trackCount" | sort -n | head -n1)
lidarrAlbumReleasesMaxTrackCount=$(echo "$lidarrAlbumData" | jq -r ".releases[].trackCount" | sort -n -r | head -n1)
if [ -f "$completedSearchIdLocation/lidarr-$lidarrAlbumId" ]; then
log "$processNumber of $lidarrTotalRecords :: $lidarrAlbumArtistName :: $lidarrAlbumTitle :: Previously Searched, skipping..."
continue
fi
for deezerArtistId in $(echo "$deezerArtistIds"); do
# Uncomment for debugging purposes
# log "$processNumber of $lidarrTotalRecords :: $lidarrAlbumArtistName :: $lidarrAlbumTitle :: $lidarrAlbumForeignAlbumId :: $deezerArtistId"
getDeezerArtistData=$(curl -s "https://api.deezer.com/artist/$deezerArtistId")
deezerArtistName="$(echo "$getDeezerArtistData" | jq -r '.name')"
getDeezerArtistAlbums=$(curl -s "https://api.deezer.com/artist/$deezerArtistId/albums?limit=1000")
if [ "$lidarrAlbumType" = "Single" ]; then
getDeezerAlbumTitles="$(echo "$getDeezerArtistAlbums" | jq -r '.data[] | select(.record_type=="single") | .title')"
getDeezerArtistAlbumsExplicitIds="$(echo "$getDeezerArtistAlbums" | jq -r '.data[] | select(.explicit_lyrics==true) | select(.record_type=="single") | .id')"
getDeezerArtistAlbumsCleanIds="$(echo "$getDeezerArtistAlbums" | jq -r '.data[] | select(.explicit_lyrics==false) | select(.record_type=="single")| .id')"
else
getDeezerAlbumTitles="$(echo "$getDeezerArtistAlbums" | jq -r '.data[] | .title')"
getDeezerArtistAlbumsExplicitIds="$(echo "$getDeezerArtistAlbums" | jq -r '.data[] | select(.explicit_lyrics==true) | .id')"
getDeezerArtistAlbumsCleanIds="$(echo "$getDeezerArtistAlbums" | jq -r '.data[] | select(.explicit_lyrics==false) | .id')"
fi
getDeezerArtistAlbumsCount="$(echo "$getDeezerArtistAlbums" | jq -r .total)"
getDeezerArtistAlbumsExplicitIdsCount=$(echo "$getDeezerArtistAlbumsExplicitIds" | wc -l)
getDeezerArtistAlbumsCleanIdsCount=$(echo "$getDeezerArtistAlbumsCleanIds" | wc -l)
getDeezerAlbumTitlesClean="$(echo "$getDeezerAlbumTitles" | sed 's/[^0-9A-Za-z]*//g')"
# Quick matching to speed process up...
match=""
for title in $(echo "${lidarrAlbumReleaseTitlesClean,,}" | sort -u); do
match="$(echo "${getDeezerAlbumTitlesClean,,}" | grep "^${title,,}$")"
done
if [ -z "$match" ]; then
continue
else
log "$processNumber of $lidarrTotalRecords :: $lidarrAlbumArtistName :: $lidarrAlbumTitle :: Quick Match found, performing deeper search/matching..."
fi
if [ -f /config/found ]; then
rm /config/found
fi
# begin explicit search
SearchDeezerAlbums "$getDeezerArtistAlbumsExplicitIds" "Explicit" "$getDeezerArtistAlbumsExplicitIdsCount"
if [ ! -f /config/found ]; then
# begin clean search
SearchDeezerAlbums "$getDeezerArtistAlbumsCleanIds" "Clean" "$getDeezerArtistAlbumsCleanIdsCount"
fi
done
if [ ! -d "$completedSearchIdLocation" ]; then
mkdir -p "$completedSearchIdLocation"
chown ${PUID:-1000}:${PGID:-1000} "$completedSearchIdLocation"
chmod 777 -R "$completedSearchIdLocation"
fi
if [ -f /config/found ]; then
rm /config/found
ArrWaitForTaskCompletion
else
log "$processNumber of $lidarrTotalRecords :: $lidarrAlbumArtistName :: $lidarrAlbumTitle :: No match found :("
if [ -d "$completedSearchIdLocation" ]; then
touch "$completedSearchIdLocation/lidarr-$lidarrAlbumId"
chmod 777 "$completedSearchIdLocation/lidarr-$lidarrAlbumId"
fi
fi
done
}
for (( ; ; )); do
let i++
logfileSetup
touch "$dockerPath/$logFileName"
exec &> >(tee -a "$dockerPath/$logFileName")
log "Starting..."
InstallDependencies
confFiles=$(find /config -mindepth 1 -type f -name "*.conf")
confFileCount=$(echo "$confFiles" | wc -l)
if [ -z "$confFiles" ]; then
log "ERROR :: No config files found, exiting..."
exit
fi
for f in $confFiles; do
count=$(($count+1))
log "Processing \"$f\" config file"
settings "$f"
verifyConfig
if [ ! -z "$arrUrl" ]; then
if [ ! -z "$arrApiKey" ]; then
SECONDS=0
VerifyApiAccess
ArlSetup
log "Step - Removing previously downloaded items that failed to import..."
if [ -d "$completeDownloadPath" ]; then
rm -rf "$completeDownloadPath"/*
fi
log "Step - Begining Missing search!"
lidarrMissingRecords=$(wget --timeout=0 -q -O - "$arrUrl/api/v1/wanted/missing?page=1&pagesize=999999&sortKey=$searchOrder&sortDirection=$searchDirection&apikey=${arrApiKey}")
LidarrWantedSearch "$lidarrMissingRecords"
log "Step - Begining Cutoff search!"
lidarrCutoffRecords=$(wget --timeout=0 -q -O - "$arrUrl/api/v1/wanted/cutoff?page=1&pagesize=999999&sortKey=$searchOrder&sortDirection=$searchDirection&apikey=${arrApiKey}")
LidarrWantedSearch "$lidarrCutoffRecords"
else
log "ERROR :: Skipping $arrApp, missing API Key..."
fi
else
log "ERROR :: Skipping $arrApp, missing URL..."
fi
duration=$SECONDS
durationOutput="$(printf '%dd:%dh:%dm:%ds\n' $((duration/86400)) $((duration%86400/3600)) $((duration%3600/60)) $((duration%60)))"
log "Script Completed in $durationOutput!"
done
log "Sleeping $lidarrMusicAutomatorScriptInterval..."
sleep $lidarrMusicAutomatorScriptInterval
done
exit