download.sh 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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://baldwin-ipv4.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. open >/dev/null 2>&1
  47. if [ $? -eq 1 ]; then
  48. echo "${URLS}" | xargs -n 1 open
  49. exit "${?}"
  50. else
  51. echo "Non-HTTP URL, open() not available. Aborting..." 1>&2
  52. exit 1
  53. fi
  54. fi
  55. # Download
  56. cd "${DEST}"
  57. wget --quiet \
  58. --connect-timeout "${TIMEOUT}" \
  59. --wait "${DELAY}" --random-wait \
  60. "--user-agent=${AGENT}" \
  61. --continue \
  62. --no-check-certificate \
  63. --input-file -