tvdup.sh 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #!/bin/bash
  2. set failglob
  3. BASE="/mnt/media/TV"
  4. NOW="`date '+%s'`"
  5. THEN=$(( $NOW - 3600 ))
  6. function updateCount() {
  7. for i in "${dir}/${num} - "*; do
  8. if echo "${i}" | grep -q '.nfo$'; then
  9. continue
  10. fi
  11. if echo "${i}" | grep -q '.srt$'; then
  12. continue
  13. fi
  14. count=$(( $count + 1 ))
  15. done
  16. }
  17. declare -A seen
  18. while IFS= read -r -d '' lpath; do
  19. # Reset the loop
  20. count=0
  21. file="`basename "${lpath}"`"
  22. dir="`dirname "${lpath}"`"
  23. num="`echo "${file}" | sed 's%\([0-9]*\).*%\1%'`"
  24. # Only process things in a format we understand
  25. if [ -z "${num}" ]; then
  26. echo "Error on: ${lpath}" 1>&2
  27. continue
  28. fi
  29. # Skip if we're down to one
  30. updateCount
  31. if [ $count -lt 2 ]; then
  32. continue
  33. fi
  34. # Build a list of checked files and skip dups
  35. id="${dir}/${num} - "
  36. if [ -n "${seen["${id}"]}" ]; then
  37. continue
  38. fi
  39. seen["${id}"]=1
  40. # Skip things modified since $THEN
  41. if [ "`uname`" == "Darwin" ]; then
  42. MTIME="`stat -f '%m' "${id}"*`"
  43. else
  44. MTIME="`stat -c '%Y' "${id}"*`"
  45. fi
  46. MTIME="`echo "${MTIME}" | sort | head -n 1`"
  47. if [ $MTIME -gt $THEN ]; then
  48. echo "Skipping recent file: ${lpath}" 1>&2
  49. continue
  50. fi
  51. # Use mkvRename (which protects against small files internally)
  52. sudo chattr -i "${id}"*
  53. ~/bin/video/mkvRename "${lpath}"
  54. # Skip if we're down to one
  55. updateCount
  56. if [ $count -lt 2 ]; then
  57. continue
  58. fi
  59. # Human intervention
  60. sudo chattr -i "${id}"*
  61. touch "${id}"*
  62. echo "Multiple matches for: ${id}" 1>&2
  63. short="`echo "${id}" | sed "s%^${BASE}/%%"`"
  64. (cd "${BASE}" && ls -lhtQ "${short}"*)
  65. done< <(find "${BASE}" -type f -path '*Season*' -name '[0-9]*.*' -print0)