findAllTorrents.sh 1.4 KB

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