Fix torrent-processor to handle specific IDs and categories

- Updated --id option to only process the specified torrent
- Fixed category processing to filter torrents by category
- Added better filtering and pattern matching for category-based processing
This commit is contained in:
masterdraco 2025-03-04 17:21:42 +01:00
parent f572a241ef
commit fb56817e76

View File

@ -98,34 +98,266 @@ if [ -n "$TORRENT_ID" ]; then
name=$(echo "$info" | grep "Name:" | awk -F": " '{print $2}' | xargs) name=$(echo "$info" | grep "Name:" | awk -F": " '{print $2}' | xargs)
echo "Torrent name: $name" echo "Torrent name: $name"
# Run torrent-mover # Run torrent-mover with specific torrent ID
/usr/local/bin/torrent-mover --debug torrent_id="$TORRENT_ID"
# Check if output directory exists for this torrent
output_dir=$(grep "Location:" <<< "$info" | awk -F": " '{print $2}' | xargs)
if [[ -n "$output_dir" ]]; then
echo "Torrent location: $output_dir"
fi
# We need to modify torrent-mover to handle single IDs
# For now, we'll write a small temporary script to process just this ID
TMP_SCRIPT=$(mktemp)
cat > "$TMP_SCRIPT" << EOF
#!/bin/bash
source /etc/torrent/mover.conf
source /usr/local/lib/torrent-mover/common.sh
source /usr/local/lib/torrent-mover/file_operations.sh
source /usr/local/lib/torrent-mover/transmission_handler.sh
source /usr/local/lib/torrent-mover/archive_handler.sh
# Set debug mode
DEBUG=1
# Process just this one torrent
process_single_torrent() {
local id="\$1"
log_debug "Processing single torrent ID: \$id"
# Get torrent info
local info
info=\$(get_torrent_info "\${id}")
if [[ -z "\$info" ]]; then
log_error "Failed to get info for torrent \$id"
return 1
fi
# Process torrent info just like in the main script
local hash
hash=\$(grep "Hash:" <<< "\${info}" | awk '{print \$2}')
local ratio
ratio=\$(grep "Ratio:" <<< "\${info}" | awk '{print \$2 == "None" ? 0 : \$2}' | tr -cd '0-9.')
ratio=\${ratio:-0}
local time
time=\$(grep "Seeding Time:" <<< "\${info}" | awk '{print \$3 == "None" ? 0 : \$3}' | tr -cd '0-9.')
time=\${time:-0}
local percent_done
percent_done=\$(grep "Percent Done:" <<< "\${info}" | awk '{gsub(/%/, ""); print \$3 == "None" ? 0 : \$3}')
percent_done=\${percent_done:-0}
# Extract Transmission-reported directory and translate to local path.
local reported_dir
reported_dir=\$(grep -i "Location:" <<< "\${info}" | awk -F": " '{print \$2}' | xargs)
log_debug "Raw reported directory: '\${reported_dir}'"
# If the reported directory is empty, try to derive it from the name
if [[ -z "\${reported_dir}" ]]; then
local name
name=\$(grep -i "Name:" <<< "\${info}" | awk -F": " '{print \$2}' | xargs)
log_debug "Torrent name: '\${name}'"
# Check if there are labels we can use
local labels
labels=\$(grep -i "Labels:" <<< "\${info}" | awk -F": " '{print \$2}' | xargs)
log_debug "Torrent labels: '\${labels}'"
if [[ "\${labels}" == *"Books"* ]]; then
reported_dir="/downloads/Books"
elif [[ "\${labels}" == *"Movies"* ]]; then
reported_dir="/downloads/Movies"
elif [[ "\${labels}" == *"TV"* ]]; then
reported_dir="/downloads/TV"
elif [[ "\${labels}" == *"Games"* ]]; then
reported_dir="/downloads/Games"
elif [[ "\${labels}" == *"Apps"* ]]; then
reported_dir="/downloads/Apps"
elif [[ "\${labels}" == *"Music"* ]]; then
reported_dir="/downloads/Music"
else
# Default to Other if we can't determine
reported_dir="/downloads/Other"
fi
log_debug "Derived directory from labels: '\${reported_dir}'"
fi
local dir
dir=\$(translate_source "\${reported_dir}")
log_info "Torrent source directory: '\${reported_dir}' translated to '\${dir}'"
# Initialize empty directory mapping if needed
if [[ -z "\$dir" ]]; then
log_warn "Empty directory path detected, using default"
dir="\${LOCAL_PATH_PREFIX}/Other"
fi
local dst
dst=\$(get_destination "\${dir}")
# Process the torrent
if (( \$(bc <<< "\${percent_done} >= 100") )) && ! is_processed "\${hash}"; then
log_info "Processing completed torrent \${id} (\${percent_done}% done)"
process_copy "\${id}" "\${hash}" "\${dir}" "\${dst}"
else
log_info "Torrent \${id} already processed or not complete"
fi
# Check seed ratio/time criteria
if (( \$(bc <<< "\${ratio} >= \${SEED_RATIO}") )) || (( \$(bc <<< "\${time} >= \${SEED_TIME}") )); then
log_info "Removing torrent \${id} (Ratio: \${ratio}, Time: \${time})"
process_removal "\${id}"
fi
}
# Main function
process_single_torrent "$torrent_id"
EOF
chmod +x "$TMP_SCRIPT"
"$TMP_SCRIPT"
rm -f "$TMP_SCRIPT"
elif [ -n "$CATEGORY" ]; then elif [ -n "$CATEGORY" ]; then
echo "Processing category: $CATEGORY" echo "Processing category: $CATEGORY"
# Set category-specific filter # Set category-specific filter
CATEGORY_PATH=""
PATTERN=""
case $CATEGORY in case $CATEGORY in
books) books)
echo "Looking for book torrents..." echo "Looking for book torrents..."
CATEGORY_PATH="/downloads/Books"
PATTERN="*books*|*ebook*|*epub*|*pdf*"
;; ;;
movies) movies)
echo "Looking for movie torrents..." echo "Looking for movie torrents..."
CATEGORY_PATH="/downloads/Movies"
PATTERN="*movies*|*film*|*video*"
;; ;;
tv) tv)
echo "Looking for TV show torrents..." echo "Looking for TV show torrents..."
CATEGORY_PATH="/downloads/TV"
PATTERN="*tv*|*series*|*episode*"
;; ;;
apps) apps)
echo "Looking for application torrents..." echo "Looking for application torrents..."
CATEGORY_PATH="/downloads/Apps"
PATTERN="*apps*|*applications*|*programs*|*software*"
;; ;;
games) games)
echo "Looking for game torrents..." echo "Looking for game torrents..."
CATEGORY_PATH="/downloads/Games"
PATTERN="*games*"
;; ;;
esac esac
# Run torrent-mover # Create a script to process just this category
/usr/local/bin/torrent-mover --debug TMP_SCRIPT=$(mktemp)
cat > "$TMP_SCRIPT" << EOF
#!/bin/bash
source /etc/torrent/mover.conf
source /usr/local/lib/torrent-mover/common.sh
source /usr/local/lib/torrent-mover/file_operations.sh
source /usr/local/lib/torrent-mover/transmission_handler.sh
source /usr/local/lib/torrent-mover/archive_handler.sh
# Set debug mode
DEBUG=1
# Get all torrents
get_torrent_ids() {
local cmd="transmission-remote \${TRANSMISSION_IP}:\${TRANSMISSION_PORT} -n \${TRANSMISSION_USER}:\${TRANSMISSION_PASSWORD} -l"
local output
output=\$(retry_command "\$cmd" 3 20)
echo "\$output" | awk 'NR>1 && NF>1 {gsub(/^[ ]+/, "", \$1); if (\$1 ~ /^[0-9]+\$/) print \$1}'
}
# Process category torrents
process_category_torrents() {
local category_path="$CATEGORY_PATH"
local pattern="$PATTERN"
log_debug "Processing category: $CATEGORY with path \$category_path and pattern '\$pattern'"
# Get list of all torrents
local torrent_ids=\$(get_torrent_ids)
# Process each torrent
for id in \$torrent_ids; do
# Get torrent info
local info=\$(get_torrent_info "\$id")
if [[ -z "\$info" ]]; then
log_warn "Failed to get info for torrent \$id, skipping"
continue
fi
# Extract name and location
local name=\$(grep -i "Name:" <<< "\$info" | awk -F": " '{print \$2}' | xargs)
local reported_dir=\$(grep -i "Location:" <<< "\$info" | awk -F": " '{print \$2}' | xargs)
local labels=\$(grep -i "Labels:" <<< "\$info" | awk -F": " '{print \$2}' | xargs)
# Check if this torrent matches our category
if [[ "\$reported_dir" == "\$category_path" ]] ||
[[ "\$labels" == *"$CATEGORY"* ]] ||
[[ "\$name" =~ \$pattern ]]; then
log_info "Found matching torrent: \$id - \$name"
# Process torrent info
local hash=\$(grep "Hash:" <<< "\$info" | awk '{print \$2}')
local ratio=\$(grep "Ratio:" <<< "\$info" | awk '{print \$2 == "None" ? 0 : \$2}' | tr -cd '0-9.')
ratio=\${ratio:-0}
local time=\$(grep "Seeding Time:" <<< "\$info" | awk '{print \$3 == "None" ? 0 : \$3}' | tr -cd '0-9.')
time=\${time:-0}
local percent_done=\$(grep "Percent Done:" <<< "\$info" | awk '{gsub(/%/, ""); print \$3 == "None" ? 0 : \$3}')
percent_done=\${percent_done:-0}
# If the reported directory is empty, derive it
if [[ -z "\$reported_dir" ]]; then
reported_dir="\$category_path"
log_debug "Using derived directory: '\$reported_dir'"
fi
# Process the torrent
local dir=\$(translate_source "\$reported_dir")
log_info "Torrent source directory: '\$reported_dir' translated to '\$dir'"
# Initialize empty directory mapping if needed
if [[ -z "\$dir" ]]; then
log_warn "Empty directory path detected, using default"
dir="\${LOCAL_PATH_PREFIX}/$CATEGORY"
fi
local dst=\$(get_destination "\$dir")
# Process the torrent
if (( \$(bc <<< "\${percent_done} >= 100") )) && ! is_processed "\${hash}"; then
log_info "Processing completed torrent \${id} (\${percent_done}% done)"
process_copy "\${id}" "\${hash}" "\${dir}" "\${dst}"
else
log_info "Torrent \${id} already processed or not complete"
fi
# Check seed ratio/time criteria
if (( \$(bc <<< "\${ratio} >= \${SEED_RATIO}") )) || (( \$(bc <<< "\${time} >= \${SEED_TIME}") )); then
log_info "Removing torrent \${id} (Ratio: \${ratio}, Time: \${time})"
process_removal "\${id}"
fi
fi
done
}
# Main function
process_category_torrents
EOF
chmod +x "$TMP_SCRIPT"
"$TMP_SCRIPT"
rm -f "$TMP_SCRIPT"
else else
echo "Processing all torrents" echo "Processing all torrents"
# Run the main torrent-mover script directly
/usr/local/bin/torrent-mover --debug /usr/local/bin/torrent-mover --debug
fi fi