upload_yuque.sh 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #!/bin/bash
  2. # bash upload_yuque.sh <FileName|FolderName> <ThreadNum> |tee -a "log.txt"
  3. # by MoeClub.org
  4. # User Cookies
  5. ctoken=""
  6. session=""
  7. # Config
  8. DebugMode=0
  9. ShowTask=0
  10. ShowFileName=1
  11. # Main
  12. FileName=${1:-}
  13. ThreadNum=${2:-10}
  14. command -v curl >>/dev/null 2>&1
  15. [ $? -eq 0 ] || exit 1
  16. [ -n "$FileName" ] && [ -e "$FileName" ] || exit 1
  17. PIPE=$(mktemp -u)
  18. mkfifo $PIPE
  19. exec 77<>$PIPE
  20. trap "exec 77>&-;exec 77<&-;rm $PIPE;exit 0" 2
  21. for((i=0; i<$ThreadNum; i=i+1)); do echo >&77; done
  22. function Upload() {
  23. Name=`echo "$1" |sed 's/[[:space:]]//g'`;
  24. [ -n "${Name}" ] && [ -f "${Name}" ] || { echo >&77; return; }
  25. [ $ShowTask == 1 ] && echo "Upload Task: ${Name}";
  26. OUTPUT=`curl -sSL --connect-timeout 5 --retry-delay 3 --retry 3 --http1.1 \
  27. -H "User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:68.0) Gecko/20100101 Firefox/68.0" \
  28. -H "Referer: https://www.yuque.com/yuque/topics/new" \
  29. -H "Cookie: ctoken=${ctoken}; _yuque_session=${session}" \
  30. -F "file=@${Name};filename=_.png;type=image/png" \
  31. -X POST "https://www.yuque.com/api/upload/attach?ctoken=${ctoken}"`
  32. [ $DebugMode == 1 ] && echo "$OUTPUT";
  33. URL=`echo "$OUTPUT" |grep -o '"url":"[^"]*"' |grep -o 'https://[^"]*'`;
  34. if [ -n "${URL}" ]; then
  35. if [ $ShowFileName == 1 ]; then
  36. echo "${Name}; ${URL}";
  37. else
  38. echo "${URL}";
  39. fi
  40. else
  41. StatusCode=`echo "$OUTPUT" |cut -d'"' -f4 |sed 's/[[:space:]]/-/g'`
  42. echo "${Name}; NULL_${StatusCode}";
  43. fi
  44. echo >&77;
  45. }
  46. if [ -d "${FileName}" ]; then
  47. for item in `find "${FileName}" -type f ! -name ".*"`; do
  48. read -u77
  49. Upload "${item}" &
  50. done
  51. elif [ -f "${FileName}" ]; then
  52. # ShowFileName=0
  53. Upload "${FileName}" &
  54. else
  55. exit 1
  56. fi
  57. wait
  58. exit 0