Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| fa8538339c | |||
| 4cdd1ef457 |
@@ -0,0 +1,144 @@
|
||||
#!/bin/bash
|
||||
# Plex TV Shows Cleanup Script with Sonarr Integration for Unraid
|
||||
|
||||
# Configuration
|
||||
PLEX_TOKEN="uZn42JMVkQpyb_duFsvT" # Replace with your Plex API token
|
||||
PLEX_SERVER_URL="https://192.168.50.111:32400" # Replace with your Plex server URL
|
||||
TV_SHOWS_PATH="/mnt/user/stuff/videos/tvshows" # Replace with your TV shows directory path
|
||||
SONARR_API_KEY="2537de37fded4874ae83da9cf3c14f34" # Replace with your Sonarr API key
|
||||
SONARR_URL="http://192.168.50.111:8989" # Replace with your Sonarr server URL
|
||||
SHOWS_LIST=("Agatha All Along" "Arcane") # Replace with your TV show names
|
||||
|
||||
# Log File Configuration
|
||||
LOG_DIR="/mnt/user/appdata/customlog" # Directory for log files
|
||||
LOG_FILE="$LOG_DIR/plex_cleanup_$(date '+%Y-%m-%d').log" # Daily log file
|
||||
|
||||
# Ensure dependencies
|
||||
# command -v curl >/dev/null 2>&1 || { echo "curl is not installed. Exiting."; exit 1; }
|
||||
# command -v jq >/dev/null 2>&1 || { echo "jq is not installed. Exiting."; exit 1; }
|
||||
|
||||
# Ensure log directory exists
|
||||
mkdir -p "$LOG_DIR"
|
||||
|
||||
# Function to log messages
|
||||
log() {
|
||||
local message="$1"
|
||||
echo "$(date '+%Y-%m-%d %H:%M:%S') - $message" | tee -a "$LOG_FILE"
|
||||
}
|
||||
|
||||
# Function to fetch the Plex library section ID for TV Shows
|
||||
get_tv_shows_section_id() {
|
||||
log "Fetching TV Shows section ID"
|
||||
local plex_section_id
|
||||
plex_section_id=$(curl -s -X GET "$PLEX_SERVER_URL/library/sections" -H "X-Plex-Token: $PLEX_TOKEN" | \
|
||||
jq -r '.MediaContainer.Directory[] | select(.type == "show") | .key')
|
||||
|
||||
|
||||
if [[ -z "$plex_section_id" ]]; then
|
||||
log "Could not find TV Shows section ID. Exiting."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo "$plex_section_id"
|
||||
}
|
||||
|
||||
# Function to fetch watched status for a show
|
||||
fetch_watched_episodes() {
|
||||
local show_name="$1"
|
||||
local plex_section_id="$2"
|
||||
local show_id
|
||||
local watched_episodes
|
||||
|
||||
log "Fetching watched episodes for: $show_name"
|
||||
|
||||
# Get the show ID for the specific show
|
||||
show_id=$(curl -s -X GET "$PLEX_SERVER_URL/library/sections/$plex_section_id/all" -H "X-Plex-Token: $PLEX_TOKEN" | \
|
||||
jq -r ".MediaContainer.Metadata[] | select(.title == \"$show_name\") | .ratingKey")
|
||||
|
||||
if [[ -z "$show_id" ]]; then
|
||||
log "Could not find show ID for $show_name. Skipping."
|
||||
return
|
||||
fi
|
||||
|
||||
# Fetch watched episodes for the specific show
|
||||
watched_episodes=$(curl -s -X GET "$PLEX_SERVER_URL/library/metadata/$show_id/allLeaves" -H "X-Plex-Token: $PLEX_TOKEN" | \
|
||||
jq '.MediaContainer.Metadata[] | select(.viewCount != null) | .title')
|
||||
|
||||
echo "$watched_episodes"
|
||||
}
|
||||
|
||||
# Function to delete old seasons and mark unmonitored in Sonarr
|
||||
delete_and_unmonitor_seasons() {
|
||||
local show_name="$1"
|
||||
local show_path="$2"
|
||||
|
||||
log "Processing directory: $show_path"
|
||||
|
||||
for season_dir in "$show_path"/*; do
|
||||
if [[ -d "$season_dir" ]]; then
|
||||
season_name=$(basename "$season_dir")
|
||||
|
||||
if [[ "$season_name" =~ ^Season[[:space:]][[:digit:]]+$ ]]; then
|
||||
log "Deleting watched season: $season_name"
|
||||
rm -rf "$season_dir"
|
||||
|
||||
# Mark as unmonitored in Sonarr
|
||||
season_number=$(echo "$season_name" | grep -oE '[0-9]+')
|
||||
mark_unmonitored_in_sonarr "$show_name" "$season_number"
|
||||
fi
|
||||
fi
|
||||
done
|
||||
}
|
||||
|
||||
# Function to mark a season as unmonitored in Sonarr
|
||||
mark_unmonitored_in_sonarr() {
|
||||
local show_name="$1"
|
||||
local season_number="$2"
|
||||
local series_id
|
||||
local seasons_data
|
||||
|
||||
log "Marking $show_name - Season $season_number as unmonitored in Sonarr."
|
||||
|
||||
# Get the series ID
|
||||
series_id=$(curl -s "$SONARR_URL/api/v3/series" -H "X-Api-Key: $SONARR_API_KEY" | \
|
||||
jq -r ".[] | select(.title==\"$show_name\") | .id")
|
||||
|
||||
if [[ -z "$series_id" ]]; then
|
||||
log "Could not find series ID for $show_name in Sonarr. Skipping."
|
||||
return
|
||||
fi
|
||||
|
||||
# Fetch seasons data
|
||||
seasons_data=$(curl -s "$SONARR_URL/api/v3/series/$series_id" -H "X-Api-Key: $SONARR_API_KEY")
|
||||
|
||||
# Update the monitored status for the specific season
|
||||
updated_seasons=$(echo "$seasons_data" | jq ".seasons | map(if .seasonNumber == $season_number then .monitored = false else . end)")
|
||||
|
||||
# Send the updated data back to Sonarr
|
||||
curl -s -X PUT "$SONARR_URL/api/v3/series/$series_id" \
|
||||
-H "X-Api-Key: $SONARR_API_KEY" \
|
||||
-H "Content-Type: application/json" \
|
||||
--data "$(echo "$seasons_data" | jq ".seasons = $updated_seasons")" > /dev/null
|
||||
|
||||
log "Marked $show_name - Season $season_number as unmonitored in Sonarr."
|
||||
}
|
||||
|
||||
# Main Script Execution
|
||||
log "Starting Plex TV Shows Cleanup with Sonarr Integration"
|
||||
|
||||
plex_section_id=$(get_tv_shows_section_id)
|
||||
|
||||
for show in "${SHOWS_LIST[@]}"; do
|
||||
log "Checking show: $show"
|
||||
watched=$(fetch_watched_episodes "$show" "$plex_section_id")
|
||||
|
||||
if [[ "$watched" -gt 0 ]]; then
|
||||
log "Show '$show' has watched episodes. Cleaning up..."
|
||||
show_path="$TV_SHOWS_PATH/$show"
|
||||
delete_and_unmonitor_seasons "$show" "$show_path"
|
||||
else
|
||||
log "No watched episodes for '$show'. Skipping cleanup."
|
||||
fi
|
||||
done
|
||||
|
||||
log "Plex TV Shows Cleanup with Sonarr Integration completed"
|
||||
Reference in New Issue
Block a user