#!/bin/bash

# Define directories
base_directory="/root/sub/latest/"
output_directory="/root/sub/latest/link/"

# Ensure the output directory exists
if [ ! -d "$output_directory" ]; then
    mkdir -p "$output_directory"
fi

# Change to base directory
cd "$base_directory"

# Find .html files that do not contain the specified text
find . -type f -name '*.html' -exec grep -L '<td class="empty" colspan="5">No subtitles match your filter. Try changing it.' {} + | while read file; do
    # Generate a unique filename for the log
    filename="${output_directory}$(date +%s)$((RANDOM % 200 + 1)).log"
    echo "Processing $file -> $filename"
    # Dump the links from the file to the output log
    lynx -dump -listonly "$file" > "$filename"
    if [ $? -ne 0 ]; then
        echo "lynx failed to process $file"
    elif [ ! -s "$filename" ]; then
        echo "$filename is empty, no links found in $file."
        rm "$filename"  # Optionally remove empty log files
    else
        echo "Saved links to $filename"
    fi
done
