fant0men

Tracker list fix script

Sep 7th, 2019
157
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2. # This script parses a tracker list text file, sorts, removes duplicates,
  3. # checks online status of each URL, and echoes the list to STDOUT
  4. # in the correct format.
  5.  
  6. usage () {
  7.     bname=$(basename "$0")
  8.     echo -e "Usage: ${bname} [tracker txt]\n"
  9.     exit
  10. }
  11.  
  12. if [[ -z $1 || ! -f $1 ]]; then
  13.     usage
  14. fi
  15.  
  16. declare -a trackers
  17.  
  18. txt="$1"
  19. txt_tmp="/dev/shm/trackers-${RANDOM}.txt"
  20. line_n=0
  21. n=0
  22. switch=0
  23. elements=0
  24.  
  25. sort --unique <"$txt" > "$txt_tmp"
  26. txt_n=$(wc --lines <"$txt_tmp")
  27.  
  28. cat "$txt_tmp" | while read line; do
  29.     switch=0
  30.     let line_n++
  31.  
  32.     if [[ $line ]]; then
  33.         elements=${#trackers[@]}
  34.  
  35.         for (( i = 0; i < $elements; i++ )); do
  36.             line_tmp=$(sed -e 's_/$__' -e 's_/announce__' <<<"$line")
  37.             grep --quiet "$line_tmp" <<<"${trackers[${i}]}"
  38.  
  39.             if [[ $? -eq 0 ]]; then
  40.                 switch=1
  41.  
  42.                 array_l=$(wc --chars <<<"${trackers[${i}]}")
  43.                 line_l=$(wc --chars <<<"$line")
  44.  
  45.                 if [[ $line_l > $array_l && $line =~ /announce$ ]]; then
  46.                     trackers[${i}]="$line"
  47.                 fi
  48.             fi
  49.         done
  50.  
  51.         if [[ $switch -eq 0 ]]; then
  52.             trackers[${n}]="$line"
  53.             let n++
  54.         fi
  55.     fi
  56.  
  57.     #echo "$line_n : $txt_n"
  58.     if [[ $line_n -eq $txt_n ]]; then
  59.         elements=${#trackers[@]}
  60.  
  61.         declare -A md5h
  62.  
  63.         for (( i = 0; i < $elements; i++ )); do
  64.             md5=$(tr -d '[:space:]' <<<"${trackers[${i}]}" | md5sum -)
  65.  
  66.             if [[ ${md5h[${md5}]} -eq 1 ]]; then
  67.                 continue
  68.             else
  69.                 md5h[${md5}]=1
  70.             fi
  71.  
  72.             curl --retry 8 --silent --output /dev/null "${trackers[${i}]}"
  73.  
  74.             if [[ $? -ne 0 ]]; then
  75.                 address=$(sed -e 's_^.*//__' -e 's_:[0-9]*__' -e 's_/.*$__' <<<"${trackers[${i}]}")
  76.                 ping -c 10 "$address" &> /dev/null
  77.  
  78.                 if [[ $? -eq 0 ]]; then
  79.                     echo -e "${trackers[${i}]}\n"
  80.                 fi
  81.  
  82.             elif [[ $? -eq 0 ]]; then
  83.                 echo -e "${trackers[${i}]}\n"
  84.             fi
  85.         done
  86.     fi
  87. done
  88.  
  89. rm "$txt_tmp"
RAW Paste Data