forked from tronyx/sonarr-episode-name-checker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SonarrEpisodeNameChecker.sh
137 lines (122 loc) · 5.37 KB
/
SonarrEpisodeNameChecker.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
#!/usr/bin/env bash
#
# Find unnamed TV Show episodes that have "TBA" or "Episode ##" as the title
# Tronyx
# Define some variables
# Path to parent dir for all media
mediaPath='/mnt/data/media/Videos'
# List of shows you want to be excluded
nameExclusionList='excludes/name_excludes.txt'
# List of paths you want to be excluded
pathExclusionList='excludes/path_excludes.txt'
# Column number to use with the awk command in the find_shows function
# You may need to adjust this value depending on the number of columns in the full path to the show name
# For example, my path to an episode is "/mnt/data/media/Videos/TV Shows/Show Name/Season 1/Episode Name" which ends up being 7
column=7
# Whether or not you want to display output on the CLI
cliDisplay='true'
# Whether or not you want to send a notification to Discord
notify='true'
# Send a notification even when no shows are found.
notifyAll='false'
# Discord webhook URL
webhookUrl=''
# CLI font colors
readonly red='\e[31m'
readonly endColor='\e[0m'
## Notes
## Path exclusion file should look like this, with the FULL path (No # at the beginning of the line):
#/mnt/data/media/Videos/HD Movies/
#/mnt/data/media/Videos/Movies/
#/mnt/data/media/Videos/Anime/Naruto Shippuuden/Season 13/
## Name exclusion file should look like this with the FULL directory name for the show or movie (No # at the beginning of the line):
#Alice in Borderland (2020)
#Drunk History (UK)
#Ripley's Believe It or Not! (2000)
## You will need to narrow down which shows to include/exclude based on whether or not the Series actually uses "Episode ##" for the episode titles.
# Function to gather script information.
get_scriptname() {
local source
local dir
source="${BASH_SOURCE[0]}"
while [[ -L ${source} ]]; do
dir="$(cd -P "$(dirname "${source}")" > /dev/null && pwd)"
source="$(readlink "${source}")"
[[ ${source} != /* ]] && source="${dir}/${source}"
done
echo "${source}"
}
readonly scriptname="$(get_scriptname)"
# Function to grab line numbers of the user-defined and status variables.
get_line_numbers() {
webhookUrlLineNum=$(head -25 "${scriptname}" | grep -En -A1 'Discord webhook' | tail -1 | awk -F- '{print $1}')
}
# Function to check that the webhook URL is defined if alert is set to true.
# If alert is set to true and the URL is not defined, prompt the user to provide it.
check_webhook_url() {
if [[ ${webhookUrl} == '' ]] && [[ ${notify} == 'true' ]]; then
echo -e "${red}You didn't define your Discord webhook URL!${endColor}"
echo ''
echo 'Enter your webhook URL:'
read -r url
echo ''
echo ''
sed -i "${webhookUrlLineNum} s|webhookUrl='[^']*'|webhookUrl='${url}'|" "${scriptname}"
webhookUrl="${url}"
fi
}
# Function to find list of shows with incorrect names.
find_shows() {
showsFile=$(mktemp)
find "${mediaPath}" -type f \( -name "* - Episode*" -o -name "*TBA*" \) | grep -v partial | grep -v -f "${pathExclusionList}" | grep -v -f "${nameExclusionList}" | awk -F/ -v col="${column}" '{print $col}' | uniq > "${showsFile}"
}
# Function to determine the number of results to then determine whether or not what output to display.
show_count() {
showCount=$(wc -l "${showsFile}" | awk '{print $1}')
}
# Function to display the output
display_output() {
if [[ ${showCount} -gt '0' ]]; then
echo 'The following shows have episodes named "Episode ##" or "TBA":'
echo ''
cat "${showsFile}"
echo ''
echo "You should perform a Refresh & Scan on these shows in Sonarr and then rename them if they've updated with the correct name."
echo ''
elif [[ ${showCount} -eq '0' ]]; then
echo 'No shows found with episodes named "Episode ##" or "TBA".'
echo ''
fi
}
# Function to send Discord notifications.
send_notification() {
if [[ -f ${showsFile} ]]; then
badShows=$(awk '{print}' ORS='\\n' "${showsFile}")
if [[ ${notifyAll} == 'true' ]]; then
if [[ ${showCount} -gt '0' ]]; then
curl -s -H "Content-Type: application/json" -X POST -d '{"embeds": [{"title": "The following shows have episodes named Episode ## or TBA:", "description": "'"${badShows}"'\n**You should perform a Refresh & Scan on these shows in Sonarr and then rename them if they have updated with the correct name.**", "color": 16711680}]}' "${webhookUrl}"
elif [[ ${showCount} -eq '0' ]]; then
curl -s -H "Content-Type: application/json" -X POST -d '{"embeds": [{"description": "**No shows found with episodes named Episode ## or TBA.**","color": 39219}]}' "${webhookUrl}"
fi
elif [[ ${notifyAll} == 'false' ]]; then
if [[ ${showCount} -gt '0' ]]; then
curl -s -H "Content-Type: application/json" -X POST -d '{"embeds": [{"title": "The following shows have episodes named Episode ## or TBA:", "description": "'"${badShows}"'\n**You should perform a Refresh & Scan on these shows in Sonarr and then rename them if they have updated with the correct name.**", "color": 16711680}]}' "${webhookUrl}"
fi
fi
fi
}
# Main function to run all other functions.
main() {
get_scriptname
get_line_numbers
check_webhook_url
find_shows
show_count
if [[ ${cliDisplay} == 'true' ]]; then
display_output
fi
if [[ ${notify} == 'true' ]]; then
send_notification
fi
}
main