download.sh 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. # Choose an inter-file delay
  9. DELAY="${2}"
  10. if [ -z "${DELAY}" ]; then
  11. DELAY=10
  12. fi
  13. # Move to the destination directory
  14. DEST="${1}"
  15. if [ -z "${DEST}" ]; then
  16. DEST=~/Desktop
  17. fi
  18. cd "${DEST}"
  19. # Use Transmission if the URL is a magenet link
  20. if echo "${URLS}" | head -n 1 | grep -Eqi '^magnet:'; then
  21. SESSION_ID=$(curl --silent --max-time "${TIMEOUT}" "${TRANS_URL}" | sed 's/.*<code>//g;s/<\/code>.*//g')
  22. if [ -n "${SESSION_ID}" ]; then
  23. SLEEP=0
  24. IFS=$'\n'
  25. for i in "${URLS}"; do
  26. if [ $SLEEP -gt 0 ]; then sleep $SLEEP; fi
  27. SLEEP="${DELAY}"
  28. RESULT="`curl --silent --max-time "${TIMEOUT}" \
  29. --header "${SESSION_ID}" "${TRANS_URL}" \
  30. -d '{"method":"torrent-add", "arguments":{"paused": "false", "filename": "'${i}'"}}'`"
  31. if ! echo "${RESULT}" | grep -q '"result":"success"'; then
  32. exit 1
  33. fi
  34. done
  35. exit 0
  36. else
  37. echo "Transmission not available for magnet link, falling back..." 1>&2
  38. fi
  39. fi
  40. # Use "open" if the URLs aren't HTTP/HTTPS
  41. if ! echo "${URLS}" | head -n 1 | grep -Eqi '^http'; then
  42. echo "${URLS}" | xargs -n 1 open
  43. exit "${?}"
  44. fi
  45. # Download
  46. wget --quiet \
  47. --connect-timeout "${TIMEOUT}" \
  48. --wait "${DELAY}" --random-wait \
  49. "--user-agent=${AGENT}" \
  50. --continue \
  51. --no-check-certificate \
  52. --input-file -