Advertisement
Guest User

wget-uploaded.sh

a guest
Apr 15th, 2018
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.15 KB | None | 0 0
  1. #!/bin/sh
  2. # Copyright 2015 Jan-Philip Gehrcke, http://gehrcke.de
  3. # See http://gehrcke.de/2015/03/uploaded-to-download-with-wget/
  4.  
  5.  
  6. USERNAME="user"
  7. PASSWORD="password"
  8.  
  9.  
  10. if [ "$#" -ne 1 ]; then
  11. echo "Missing argument: URLs file (containing one URL per line)." >&2
  12. exit 1
  13. fi
  14.  
  15.  
  16. URLSFILE="${1}"
  17. if [ ! -r "${URLSFILE}" ]; then
  18. echo "Cannot read URLs file ${URLSFILE}. Exit." >&2
  19. exit 1
  20. fi
  21. if [ ! -s "${URLSFILE}" ]; then
  22. echo "URLs file is empty. Exit." >&2
  23. exit 1
  24. fi
  25.  
  26.  
  27. TMPDIR="$(mktemp -d)"
  28. # Install trap that removes the temporary directory recursively
  29. # upon exit (except for when this program retrieves SIGKILL).
  30. trap 'rm -rf "$TMPDIR"' EXIT
  31.  
  32.  
  33. LOGINRESPFILE="${TMPDIR}/login.response"
  34. LOGINOUTPUTFILE="${TMPDIR}/login.outerr"
  35. COOKIESFILE="${TMPDIR}/login.cookies"
  36. LOGINURL="http://uploaded.net/io/login"
  37.  
  38.  
  39. echo "Temporary directory: ${TMPDIR}"
  40. echo "Log in via POST request to ${LOGINURL}, save cookies."
  41. wget --save-cookies=${COOKIESFILE} --server-response \
  42. --output-document ${LOGINRESPFILE} \
  43. --post-data="id=${USERNAME}&pw=${PASSWORD}" \
  44. ${LOGINURL} > ${LOGINOUTPUTFILE} 2>&1
  45.  
  46. # Status code is 200 even if login failed.
  47. # Uploaded sends a '{"err":"User and password do not match!"}'-like response
  48. # body in case of error.
  49.  
  50. echo "Verify that login response is empty."
  51. # Response is more than 0 bytes in case of login error.
  52. if [ -s "${LOGINRESPFILE}" ]; then
  53. echo "Login response larger than 0 bytes. Print response and exit." >&2
  54. cat "${LOGINRESPFILE}"
  55. exit 1
  56. fi
  57.  
  58. # Zero response size does not necessarily imply successful login.
  59. # Wget adds three commented lines to the cookies file by default, so
  60. # set cookies should result in more than three lines in this file.
  61. COOKIESFILELINES="$(cat ${COOKIESFILE} | wc -l)"
  62. echo "${COOKIESFILELINES} lines in cookies file found."
  63. if [ "${COOKIESFILELINES}" -lt "4" ]; then
  64. echo "Expected >3 lines in cookies file. Exit.". >&2
  65. exit 1
  66. fi
  67.  
  68. echo "Process URLs."
  69. # Assume that login worked. Iterate through URLs.
  70. while read CURRENTURL; do
  71. if [ "x$CURRENTURL" = "x" ]; then
  72. # Skip empty lines.
  73. continue
  74. fi
  75. echo -e "\n\n"
  76. TMPFILE="$(mktemp --tmpdir=${TMPDIR} response.html.XXXX)"
  77. echo "GET ${CURRENTURL} (use auth cookie), store response."
  78. wget --no-verbose --load-cookies=${COOKIESFILE} \
  79. --output-document ${TMPFILE} ${CURRENTURL}
  80.  
  81. if [ ! -s "${TMPFILE}" ]; then
  82. echo "No HTML response: ${TMPFILE} is zero size. Skip processing."
  83. continue
  84. fi
  85.  
  86. # Extract (temporarily valid) download URL from HTML.
  87. LINEOFINTEREST="$(grep post ${TMPFILE} | grep action | grep uploaded)"
  88. # Match entire line, include space after action="bla" , replace
  89. # entire line with first group, which is bla.
  90. DLURL=$(echo $LINEOFINTEREST | sed 's/.*action="\(.\+\)" .*/\1/')
  91. echo "Extracted download URL: ${DLURL}"
  92. # This file contains account details, so delete as soon as not required
  93. # anymore.
  94. rm -f "${TMPFILE}"
  95. echo "POST to URL w/o data. Response is file. Get filename from header."
  96. # --content-disposition should extract the proper filename.
  97. wget --content-disposition --post-data='' "${DLURL}"
  98. done < "${URLSFILE}"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement