download.sh 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #!/bin/bash
  2. # Config
  3. source ~/.download.config
  4. TIMEOUT=10
  5. AGENT="Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8"
  6. # Grab the input URLs
  7. URLS="`cat -`"
  8. # Bail if there's nothing to process
  9. if [ -z "${URLS}" ]; then
  10. exit 0
  11. fi
  12. # Choose an inter-file delay
  13. DELAY="${2}"
  14. if [ -z "${DELAY}" ]; then
  15. DELAY=10
  16. fi
  17. # Find the destination directory
  18. DEST="${1}"
  19. if [ -z "${DEST}" ]; then
  20. DEST="${HOME}/Desktop"
  21. if [ -e "${DEST}" ]; then DEST="${HOME}"; fi
  22. fi
  23. # Use Transmission if the URL is a magenet link
  24. if [ -n "${TRANS_URL}" ] && echo "${URLS}" | head -n 1 | grep -Eqi '^magnet:'; then
  25. SESSION_ID=$(curl --silent --max-time "${TIMEOUT}" "${TRANS_URL}" | sed 's/.*<code>//g;s/<\/code>.*//g')
  26. if [ -n "${SESSION_ID}" ]; then
  27. IFS=$'\n'
  28. SLEEP=0
  29. for i in ${URLS}; do
  30. if [ $SLEEP -gt 0 ]; then sleep $SLEEP; fi
  31. SLEEP="${DELAY}"
  32. RESULT="`curl --silent --max-time "${TIMEOUT}" \
  33. --header "${SESSION_ID}" "${TRANS_URL}" \
  34. -d '{"method":"torrent-add", "arguments":{"paused": "false", "filename": "'${i}'"}}'`"
  35. if ! echo "${RESULT}" | grep -q '"result":"success"'; then
  36. exit 1
  37. fi
  38. done
  39. exit 0
  40. else
  41. echo "Transmission not available for magnet link, falling back..." 1>&2
  42. fi
  43. # Use NZBGet if the URL is an NZB file
  44. elif [ -n "${NZB_URL}" ] && echo "${URLS}" | head -n 1 | grep -Eqi 'nzb.cat/getnzb/'; then
  45. if curl --silent --max-time "${TIMEOUT}" "${NZB_URL}" | grep -q 'version'; then
  46. IFS=$'\n'
  47. SLEEP=0
  48. for i in ${URLS}; do
  49. if [ $SLEEP -gt 0 ]; then sleep $SLEEP; fi
  50. SLEEP="${DELAY}"
  51. RESULT="`curl --silent --max-time "${TIMEOUT}" "${NZB_URL}" \
  52. -d '{"method":"append","params":["download.sh.nzb","'"${i}"'","",0,false,false,"",0,"force"]}'`"
  53. if ! echo "${RESULT}" | grep 'result' | grep -q '[1-9]'; then
  54. exit 1
  55. fi
  56. done
  57. exit 0
  58. else
  59. echo "NZBGet not available for NZB link, falling back..." 1>&2
  60. fi
  61. fi
  62. # Use "open", if available, for non-HTTP URLs
  63. if ! echo "${URLS}" | head -n 1 | grep -Eqi '^http'; then
  64. if open >/dev/null 2>&1; then
  65. echo "${URLS}" | xargs -n 1 open
  66. exit "${?}"
  67. else
  68. echo "Non-HTTP URL, open() not available. Aborting..." 1>&2
  69. exit 1
  70. fi
  71. fi
  72. # Download
  73. cd "${DEST}"
  74. wget --quiet \
  75. --connect-timeout "${TIMEOUT}" \
  76. --wait "${DELAY}" --random-wait \
  77. "--user-agent=${AGENT}" \
  78. --continue \
  79. --no-check-certificate \
  80. --input-file -