···88TRACKING_FILE="/tank/media/tv/.downloaded_shows"
99LOG_FILE="/tank/media/tv/download-log"
1010LOCK_FILE="/tmp/get-tv-sync.lock"
1111-SONARR_API_KEY="PLACEHOLDER"
1111+SONARR_API_KEY="9dba6d5f31eb42cf8c65f4f6f21cc8d2"
1212SONARR_URL="http://localhost:8989"
13131414umask 002
···1818touch "$TRACKING_FILE"
19192020log() { echo "$(date): $1" >>"$LOG_FILE"; }
2121+2222+# Extract a match key from a release name for fuzzy matching.
2323+# Sonarr's sourceTitle often differs from the actual filename on the seedbox:
2424+# - sourceTitle omits file extensions (.mkv, .ts, etc.)
2525+# - sourceTitle omits episode titles that appear in the actual filename
2626+# We match on episode identifier + release group, which are consistent on both sides.
2727+# Returns "episode_id|||group" e.g. "Top.Chef.S23E01|||RAWR" or "Jeopardy.2026.03.19|||BTN"
2828+match_key() {
2929+ local name="$1"
3030+ # Strip known video file extensions
3131+ name="${name%.mkv}"
3232+ name="${name%.ts}"
3333+ name="${name%.mp4}"
3434+ name="${name%.avi}"
3535+ name="${name%.m4v}"
3636+3737+ local ep_id="" group=""
3838+3939+ # Extract release group: everything after the last hyphen
4040+ if [[ "$name" == *-* ]]; then
4141+ group="${name##*-}"
4242+ fi
4343+4444+ # Extract episode identifier: show name + S##E##, YYYY.MM.DD, or S## pattern
4545+ if [[ "$name" =~ ^(.*\.[Ss][0-9]+[Ee][0-9]+) ]]; then
4646+ ep_id="${BASH_REMATCH[1]}"
4747+ elif [[ "$name" =~ ^(.*\.[0-9]{4}\.[0-9]{2}\.[0-9]{2}) ]]; then
4848+ ep_id="${BASH_REMATCH[1]}"
4949+ elif [[ "$name" =~ ^(.*\.[Ss][0-9]+)\. ]]; then
5050+ ep_id="${BASH_REMATCH[1]}"
5151+ fi
5252+5353+ if [ -n "$ep_id" ] && [ -n "$group" ]; then
5454+ echo "${ep_id}|||${group}"
5555+ fi
5656+}
21572258# Acquire exclusive lock to prevent concurrent runs
2359exec 9>"$LOCK_FILE"
···136172 continue
137173 fi
138174139139- # Check if it exists on the remote seedbox (exact match on entry name)
140140- # Use ENVIRON to pass source_title to awk, avoiding backslash interpretation from -v
141141- REMOTE_MATCH=$(SOURCE_TITLE="$source_title" awk -F'\t' 'BEGIN { name = ENVIRON["SOURCE_TITLE"] } $2 == name { print; exit }' "$REMOTE_LISTING_FILE") || true
175175+ # Match sourceTitle against remote seedbox entries using episode ID + release group.
176176+ # Sonarr's sourceTitle often differs from the actual filename (missing extension,
177177+ # missing episode title in the name), so exact matching doesn't work.
178178+ SOURCE_KEY=$(match_key "$source_title")
179179+ if [ -z "$SOURCE_KEY" ]; then
180180+ log "WARNING: Could not extract match key from sourceTitle: $source_title"
181181+ continue
182182+ fi
183183+184184+ REMOTE_MATCH=""
185185+ while IFS=$'\t' read -r rtype rname; do
186186+ [ -z "$rname" ] && continue
187187+ REMOTE_KEY=$(match_key "$rname")
188188+ if [ "$SOURCE_KEY" = "$REMOTE_KEY" ]; then
189189+ REMOTE_MATCH="$rtype"$'\t'"$rname"
190190+ break
191191+ fi
192192+ done <"$REMOTE_LISTING_FILE"
193193+142194 if [ -z "$REMOTE_MATCH" ]; then
143195 log "Not yet on seedbox (still downloading?): $source_title"
144196 continue
···146198147199 # Determine if it's a directory or file from the listing
148200 ENTRY_TYPE=$(echo "$REMOTE_MATCH" | cut -f1)
201201+ REMOTE_NAME=$(echo "$REMOTE_MATCH" | cut -f2)
149202150150- log "Downloading '$source_title' -> '$series_folder/'"
203203+ log "Downloading '$source_title' (remote: '$REMOTE_NAME') -> '$series_folder/'"
151204 mkdir -p "$LOCAL_PATH/$series_folder"
152205153206 if [ "$ENTRY_TYPE" = "d" ]; then
154207 # Directory: rsync recursively, trailing slash to put contents into series folder
155208 if rsync -rs --partial --timeout=600 --log-file="$LOG_FILE" \
156156- "$REMOTE_HOST:$REMOTE_PATH$source_title/" \
209209+ "$REMOTE_HOST:$REMOTE_PATH$REMOTE_NAME/" \
157210 "$LOCAL_PATH/$series_folder/"; then
158211 echo "$source_title" >>"$TRACKING_FILE"
159159- log "Successfully downloaded directory: $source_title"
212212+ log "Successfully downloaded directory: $REMOTE_NAME"
160213 DOWNLOADED=$((DOWNLOADED + 1))
161214 else
162162- log "Failed to download directory: $source_title"
215215+ log "Failed to download directory: $REMOTE_NAME"
163216 fi
164217 else
165218 # Single file: rsync into series folder
166219 if rsync -s --partial --timeout=600 --log-file="$LOG_FILE" \
167167- "$REMOTE_HOST:$REMOTE_PATH$source_title" \
220220+ "$REMOTE_HOST:$REMOTE_PATH$REMOTE_NAME" \
168221 "$LOCAL_PATH/$series_folder/"; then
169222 echo "$source_title" >>"$TRACKING_FILE"
170170- log "Successfully downloaded file: $source_title"
223223+ log "Successfully downloaded file: $REMOTE_NAME"
171224 DOWNLOADED=$((DOWNLOADED + 1))
172225 else
173173- log "Failed to download file: $source_title"
226226+ log "Failed to download file: $REMOTE_NAME"
174227 fi
175228 fi
176229done <<<"$RECORDS"