findAllTorrents.sh 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #!/bin/bash
  2. # Parameters
  3. if [ -z "${TIMEOUT}" ]; then
  4. TIMEOUT=600
  5. fi
  6. VIDEO_DIR="${HOME}/bin/video"
  7. # Bail if the media share isn't available
  8. if ! "${VIDEO_DIR}/isMediaMounted"; then
  9. exit 0
  10. fi
  11. # Allow easy cancelation
  12. control_c() {
  13. echo "Canceled" 1>&2
  14. exit 1
  15. }
  16. trap control_c SIGINT
  17. trap control_c SIGTERM
  18. # Provide a method to skip ahead in the alphabetical list of series
  19. REGEX=""
  20. if [ -n "${1}" ]; then
  21. if ! echo "${1}" | grep -qi '^[a-z]$'; then
  22. echo "Usage: `basename "${0}"` [skip_to_char]" 1>&2
  23. exit 1
  24. fi
  25. REGEX="`echo "${1}" | tr 'A-Z' 'a-z'`"
  26. REGEX='^[0-9a-'"${REGEX}"']'
  27. fi
  28. # Run a search for each monitored series
  29. while read -d $'\0' SERIES ; do
  30. # Skip matching series
  31. if [ -n "${REGEX}" ]; then
  32. TITLE="`dirname "${SERIES}"`"
  33. TITLE="`basename "${TITLE}"`"
  34. if echo "${TITLE}" | grep -qi "${REGEX}"; then
  35. continue
  36. fi
  37. fi
  38. # Actual search, with timeout to ensure we don't get stuck
  39. URLS="`"${VIDEO_DIR}/timeout" -t "${TIMEOUT}" "${VIDEO_DIR}/findTorrent.pl" "${SERIES}"`"
  40. RET=$?
  41. if [ $RET -ne 0 ]; then
  42. if [ $RET -eq 143 ]; then
  43. echo "Timeout (${TIMEOUT}) searching: ${SERIES}" 1>&2
  44. else
  45. echo "Error (${RET}) searching: ${SERIES}" 1>&2
  46. fi
  47. continue
  48. fi
  49. # Download, if we found anything
  50. if [ -n "${URLS}" ]; then
  51. if ! echo "${URLS}" | bash -x "${VIDEO_DIR}/download.sh"; then
  52. echo "Error downloading: ${SERIES}" 1>&2
  53. continue
  54. fi
  55. fi
  56. # Loop on the null-delimited list of monitored series/seasons
  57. done < <("${VIDEO_DIR}/torrentMonitored.pl" null)
  58. # Cleanup
  59. exit 0