forked from ultimate-pms/ultimate-media-server-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lidarr_search-missing-tracks.sh
executable file
·106 lines (82 loc) · 3.18 KB
/
lidarr_search-missing-tracks.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
#!/bin/bash
#
# Quick script to hit the Lidarr API and search for any tracks that are missing (and on your wanted list) in Lidarr...
# Suggest running this script on a cron each night.
#
# Author: DN
# https://github.com/ultimate-pms/ultimate-plex-setup
#
################################################################################################
LIDARR_API_KEY="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
LIDARR_HOST="127.0.0.1"
LIDARR_PORT="8686"
BATCH_SIZE=10 # How many albums do we want to search for at once (suggest 10 - 20) - Don't make this too high or you'll probably get banned on your indexer...
SLEEP_TIME=30 # How long to pause between each "batched" group of searches (don't make this too low or you'll fire off too many searches to your indexers at once and get banned)
################################################################################################
jqInstalled() {
if ! [ -x "$(command -v jq)" ]; then
echo 'Please install the JQ tool to: [/usr/bin/jq], before running this script. Download JQ at: https://stedolan.github.io/jq/' >&2
exit 1
else
JQ_BIN=`which jq`
fi
}
# Progress bar function
prog() {
local w=50 p=$1; shift
printf -v dots "%*s" "$(( $p*$w/100 ))" ""; dots=${dots// /#};
printf "\r\e[K|%-*s| %3d %% %s" "$w" "$dots" "$p" "$*";
}
# Check JQ Installed first..
jqInstalled
echo -e "++ Lidarr Music Search Trigger(er) ++\n------------------------------\n\n"
echo "Querying API for missing tracks - please be patient..."
getTracks() {
if [ -z "$1" ]; then
PAGE=1
else
PAGE=$1
fi
PAGE_MISSING=`curl -s -H "Accept: application/json" \
-H "Content-Type: application/json" \
-H "X-Api-Key: $LIDARR_API_KEY" \
-X GET http://$LIDARR_HOST:$LIDARR_PORT/api/v1/wanted/missing?page=$PAGE&pageSize=$BATCH_SIZE&sortDirection=descending&sortKey=releaseDate&monitored=true`
echo $PAGE_MISSING
}
i=0
current_page=1
finished=false
PAGE_RESULTS=`getTracks $current_page`
total=`echo $PAGE_RESULTS | jq '.totalRecords'`
echo -e "\nProcessing results:"
itiratePage() {
SEARCH_IDS=()
for row in $(echo "${PAGE_RESULTS}" | jq -r '.records[] | @base64'); do
_jq() {
echo ${row} | /usr/bin/base64 --decode | eval $JQ_BIN -r ${1}
}
i=$((i + 1))
ALBUMID=`echo $(_jq '.id')`
TITLE=`echo $(_jq '.title')`
# Simple progress bar so we know how far through the script we are (great for large collections)...
taskpercent=$((i*100/total))
prog "$taskpercent" $TITLE...
SEARCH_IDS+=($ALBUMID)
done
ALBUM_IDS=`printf "%s," "${SEARCH_IDS[@]}" | cut -d "," -f 1-${#SEARCH_IDS[@]}`
# Fire off request to search the first page of results...
curl -s "http://$LIDARR_HOST:$LIDARR_PORT/api/v1/command" \
-H "X-Api-Key: $LIDARR_API_KEY" \
--data "{\"name\":\"AlbumSearch\",\"albumIds\":[$ALBUM_IDS]}" >> /dev/null
if [ "$i" == "$total" ]; then
finished=true
fi
sleep $SLEEP_TIME
}
while [ "$finished" != "true" ]; do
current_page=$((current_page + 1))
PAGE_RESULTS=`getTracks $current_page`
itiratePage
done
prog "$taskpercent" ""
echo -e "\n Finished."