download.sh 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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 "open", if available, for non-HTTP URLs
  45. if ! echo "${URLS}" | head -n 1 | grep -Eqi '^http'; then
  46. if open >/dev/null 2>&1; then
  47. echo "${URLS}" | xargs -n 1 open
  48. exit "${?}"
  49. else
  50. echo "Non-HTTP URL, open() not available. Aborting..." 1>&2
  51. exit 1
  52. fi
  53. fi
  54. # Download
  55. cd "${DEST}"
  56. wget --quiet \
  57. --connect-timeout "${TIMEOUT}" \
  58. --wait "${DELAY}" --random-wait \
  59. "--user-agent=${AGENT}" \
  60. --continue \
  61. --no-check-certificate \
  62. --input-file -