download.sh 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #!/bin/bash
  2. # Config
  3. 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"
  4. TRANS_URL="http://vera.uberzach.com:9091/transmission/rpc"
  5. TIMEOUT=10
  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 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. SLEEP=0
  28. IFS=$'\n'
  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. fi
  44. # Use NZBget if the URL is an NZB file
  45. if echo "${URLS}" | head -n 1 | grep -Eqi 'nzb.cat/getnzb/'; then
  46. echo "NZBget not available" 1>&2
  47. exit 1
  48. fi
  49. # Use "open", if available, for non-HTTP URLs
  50. if ! echo "${URLS}" | head -n 1 | grep -Eqi '^http'; then
  51. if open >/dev/null 2>&1; then
  52. echo "${URLS}" | xargs -n 1 open
  53. exit "${?}"
  54. else
  55. echo "Non-HTTP URL, open() not available. Aborting..." 1>&2
  56. exit 1
  57. fi
  58. fi
  59. # Download
  60. cd "${DEST}"
  61. wget --quiet \
  62. --connect-timeout "${TIMEOUT}" \
  63. --wait "${DELAY}" --random-wait \
  64. "--user-agent=${AGENT}" \
  65. --continue \
  66. --no-check-certificate \
  67. --input-file -