-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtagarr_rename.sh
More file actions
executable file
·316 lines (264 loc) · 10.8 KB
/
tagarr_rename.sh
File metadata and controls
executable file
·316 lines (264 loc) · 10.8 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
#!/usr/bin/env bash
# -----------------------------------------------------------------------------
# Tagarr Rename — Bulk Tag Rename Tool
# Version: 1.0.0
#
# Renames tags in one or two Radarr instances by creating a new tag,
# migrating all movies from old to new via the bulk /movie/editor API,
# and optionally deleting the old tag definition.
#
# Features:
# RENAME — Create new tag, migrate movies, remove old tag
# DUAL — Process both primary and secondary instances
# DRY-RUN — Preview what would be renamed (default mode)
# CLEANUP — Optionally delete old tag definitions after migration
#
# Usage:
# ./tagarr_rename.sh # Dry-run (default, shows what would happen)
# ./tagarr_rename.sh --live # Execute renames
#
# Configuration: tagarr_rename.conf
#
# Author: prophetSe7en
#
# WARNING: Always run with --dry-run first (default) to preview changes.
# Old tag definitions are deleted after rename when DELETE_OLD_TAGS=true.
# -----------------------------------------------------------------------------
set -euo pipefail
SCRIPT_VERSION="1.0.0"
########################################
# CONFIG LOADING
########################################
SCRIPT_DIR=$(dirname "$(readlink -f "$0")")
SCRIPT_NAME="$(basename "$0" .sh)"
CONFIG_FILE="${SCRIPT_DIR}/${SCRIPT_NAME}.conf"
if [ -f "$CONFIG_FILE" ]; then
source "$CONFIG_FILE"
else
echo "ERROR: Config not found: $CONFIG_FILE"
exit 1
fi
########################################
# ARGUMENT HANDLING
########################################
DRY_RUN="${ENABLE_DRY_RUN:-true}"
while [[ $# -gt 0 ]]; do
case "$1" in
--live)
DRY_RUN=false
shift
;;
--dry-run)
DRY_RUN=true
shift
;;
*)
echo "Unknown argument: $1"
echo "Usage: $0 [--live|--dry-run]"
exit 1
;;
esac
done
########################################
# LOGGING
########################################
log() {
local msg="$(date '+%Y-%m-%d %H:%M:%S') [$1] $2"
echo "$msg"
if [ "${ENABLE_LOGGING:-true}" = "true" ] && [ -n "${LOG_FILE:-}" ]; then
echo "$msg" >> "$LOG_FILE"
fi
}
# Ensure log directory exists
if [ "${ENABLE_LOGGING:-true}" = "true" ] && [ -n "${LOG_FILE:-}" ]; then
mkdir -p "$(dirname "$LOG_FILE")"
fi
# Log rotation — 2 MiB
if [ "${ENABLE_LOGGING:-true}" = "true" ] && [ -n "${LOG_FILE:-}" ] && [ -f "$LOG_FILE" ]; then
LOG_SIZE=$(stat -c%s "$LOG_FILE" 2>/dev/null || stat -f%z "$LOG_FILE" 2>/dev/null || echo 0)
if [ "$LOG_SIZE" -gt 2097152 ]; then
[ -f "${LOG_FILE}.old" ] && rm "${LOG_FILE}.old"
mv "$LOG_FILE" "${LOG_FILE}.old"
log "INFO" "Log rotated"
fi
fi
########################################
# UPDATE CHECK
########################################
_check_for_update() {
local versions_url="https://raw.githubusercontent.com/ProphetSe7en/tagarr/main/versions.json"
local script_name
script_name="$(basename "$0")"
local remote_json
remote_json=$(curl -fsSL --max-time 5 "$versions_url" 2>/dev/null) || return 0
local latest
latest=$(echo "$remote_json" | jq -r --arg s "$script_name" '.[$s] // ""' 2>/dev/null) || return 0
# Only alert when remote is strictly newer than local (sort -V = version sort).
# Prevents "update available: older-version" if local runs ahead of versions.json.
if [ -n "$latest" ] && [ "$latest" != "$SCRIPT_VERSION" ] && \
[ "$(printf '%s\n%s\n' "$latest" "$SCRIPT_VERSION" | sort -V 2>/dev/null | tail -1)" = "$latest" ]; then
log "INFO" "Update available: v${latest} (current: v${SCRIPT_VERSION})"
fi
}
_check_for_update
########################################
# INSTANCE PROCESSOR
########################################
process_instance() {
local instance_name="$1"
local radarr_url="$2"
local radarr_api_key="$3"
log "INFO" ""
log "INFO" "========================================"
log "INFO" "Processing: $instance_name"
log "INFO" "========================================"
if [ ${#TAG_RENAMES[@]} -eq 0 ]; then
log "INFO" "No renames configured"
return 0
fi
log "INFO" "Renames: ${#TAG_RENAMES[@]} operations"
log "INFO" "Delete old tags: $DELETE_OLD_TAGS"
log "INFO" ""
# Test connection
log "INFO" "Testing connection..."
if ! curl -s -f "${radarr_url}/api/v3/system/status?apikey=${radarr_api_key}" > /dev/null; then
log "ERROR" "Cannot connect to $instance_name at ${radarr_url}"
return 1
fi
log "INFO" "Connected"
# Fetch tags and movies
log "INFO" "Fetching tags and movies..."
local all_tags all_movies total
all_tags=$(curl -s "${radarr_url}/api/v3/tag?apikey=${radarr_api_key}")
all_movies=$(curl -s "${radarr_url}/api/v3/movie?apikey=${radarr_api_key}")
total=$(echo "$all_movies" | jq 'length')
log "INFO" "Found $total movies"
log "INFO" ""
local total_renamed=0
local operations_success=0
local operations_failed=0
for rename_pair in "${TAG_RENAMES[@]}"; do
local old_tag="${rename_pair%%:*}"
local new_tag="${rename_pair#*:}"
log "INFO" "Rename: '$old_tag' -> '$new_tag'"
# Get old tag ID
local old_tag_id
old_tag_id=$(echo "$all_tags" | jq -r --arg tag "$old_tag" '.[] | select(.label == $tag) | .id')
if [ -z "$old_tag_id" ]; then
log "WARN" " Old tag '$old_tag' not found — skipping"
operations_failed=$((operations_failed + 1))
continue
fi
log "INFO" " Old tag ID: $old_tag_id"
# Check if new tag already exists
local new_tag_id
new_tag_id=$(echo "$all_tags" | jq -r --arg tag "$new_tag" '.[] | select(.label == $tag) | .id')
if [ -n "$new_tag_id" ]; then
log "INFO" " New tag '$new_tag' already exists (ID: $new_tag_id)"
else
if [ "$DRY_RUN" = "true" ]; then
log "INFO" " [DRY-RUN] Would create tag '$new_tag'"
new_tag_id="999999"
else
log "INFO" " Creating tag '$new_tag'..."
local new_tag_response
new_tag_response=$(curl -s -X POST "${radarr_url}/api/v3/tag?apikey=${radarr_api_key}" \
-H "Content-Type: application/json" \
-d "{\"label\": \"${new_tag}\"}")
new_tag_id=$(echo "$new_tag_response" | jq -r 'if type == "array" then .[0].id else .id end // empty')
if [ -z "$new_tag_id" ] || [ "$new_tag_id" = "null" ]; then
log "ERROR" " Failed to create tag '$new_tag'"
log "ERROR" " Response: $new_tag_response"
operations_failed=$((operations_failed + 1))
continue
fi
log "INFO" " Created tag (ID: $new_tag_id)"
# Refresh tags after creation
all_tags=$(curl -s "${radarr_url}/api/v3/tag?apikey=${radarr_api_key}")
fi
fi
# Find movies with old tag
local movie_ids
movie_ids=$(echo "$all_movies" | jq -r --argjson tid "$old_tag_id" '.[] | select(.tags | index($tid)) | .id')
if [ -z "$movie_ids" ]; then
log "INFO" " No movies have old tag"
if [ "$DELETE_OLD_TAGS" = "true" ]; then
if [ "$DRY_RUN" = "true" ]; then
log "INFO" " [DRY-RUN] Would delete old tag definition"
else
curl -s -X DELETE "${radarr_url}/api/v3/tag/${old_tag_id}?apikey=${radarr_api_key}" >/dev/null
log "INFO" " Deleted old tag definition"
fi
fi
operations_success=$((operations_success + 1))
continue
fi
local movie_array movies_count
movie_array=$(echo "$movie_ids" | jq -s '.')
movies_count=$(echo "$movie_ids" | wc -l)
log "INFO" " Found $movies_count movies with old tag"
if [ "$DRY_RUN" = "true" ]; then
log "INFO" " [DRY-RUN] Would add '$new_tag' to $movies_count movies"
log "INFO" " [DRY-RUN] Would remove '$old_tag' from $movies_count movies"
if [ "$DELETE_OLD_TAGS" = "true" ]; then
log "INFO" " [DRY-RUN] Would delete old tag definition"
fi
else
# Step 1: Add new tag
log "INFO" " Step 1/3: Adding '$new_tag' to $movies_count movies..."
curl -s -X PUT -H "Content-Type: application/json" \
-d "{\"movieIds\": $movie_array, \"tags\": [$new_tag_id], \"applyTags\": \"add\"}" \
"${radarr_url}/api/v3/movie/editor?apikey=${radarr_api_key}" >/dev/null
log "INFO" " Done"
# Step 2: Remove old tag
log "INFO" " Step 2/3: Removing '$old_tag' from $movies_count movies..."
curl -s -X PUT -H "Content-Type: application/json" \
-d "{\"movieIds\": $movie_array, \"tags\": [$old_tag_id], \"applyTags\": \"remove\"}" \
"${radarr_url}/api/v3/movie/editor?apikey=${radarr_api_key}" >/dev/null
log "INFO" " Done"
# Step 3: Delete old tag definition
if [ "$DELETE_OLD_TAGS" = "true" ]; then
log "INFO" " Step 3/3: Deleting old tag definition..."
curl -s -X DELETE "${radarr_url}/api/v3/tag/${old_tag_id}?apikey=${radarr_api_key}" >/dev/null
log "INFO" " Done"
else
log "INFO" " Step 3/3: Keeping old tag (DELETE_OLD_TAGS=false)"
fi
total_renamed=$((total_renamed + movies_count))
fi
operations_success=$((operations_success + 1))
done
log "INFO" ""
log "INFO" "Summary for $instance_name:"
log "INFO" " Successful: $operations_success"
log "INFO" " Failed: $operations_failed"
if [ "$DRY_RUN" = "true" ]; then
log "INFO" " Mode: DRY-RUN (no changes made)"
else
log "INFO" " Movies updated: $total_renamed"
fi
}
########################################
# MAIN
########################################
log "INFO" "========================================"
log "INFO" "Tagarr Rename v${SCRIPT_VERSION}"
log "INFO" "========================================"
if [ "$DRY_RUN" = "true" ]; then
log "INFO" "Mode: DRY-RUN (use --live to execute)"
else
log "INFO" "Mode: LIVE"
fi
START_TIME=$(date +%s)
# Process primary
process_instance "$PRIMARY_RADARR_NAME" "$PRIMARY_RADARR_URL" "$PRIMARY_RADARR_API_KEY"
# Process secondary
if [ "${ENABLE_SECONDARY:-false}" = "true" ]; then
process_instance "$SECONDARY_RADARR_NAME" "$SECONDARY_RADARR_URL" "$SECONDARY_RADARR_API_KEY"
fi
END_TIME=$(date +%s)
DURATION=$((END_TIME - START_TIME))
log "INFO" ""
log "INFO" "========================================"
log "INFO" "Completed in ${DURATION}s"
log "INFO" "========================================"