push 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #!/bin/bash
  2. #this script just uploads updated/new files to neocities (or deletes entries removed by the user (you))
  3. if [ ! -d "entries" ] || [ ! -d "public_html" ]; then
  4. echo "You haven't initialized nci system yet... Please use \"./init\" before posting a new entry."
  5. exit 1
  6. fi
  7. if [ ! -f "nci.conf" ]; then
  8. echo "Congrats! You deleted your configuration file. Now nci can't work. You are really smart :)"
  9. echo "To fix this problem, create a file named \"nci.conf\" in this directory, and fill it with the following data:"
  10. echo "1st line - Your neocities username (without \".neocities.org\" at the end of the string)."
  11. echo "2nd line - The filename of your blog html file (without \".html\" at the end of the string)."
  12. echo
  13. echo "Now try not to delete your configuration file, silly."
  14. exit 1
  15. fi
  16. use_key=0
  17. if [ ! -z "$1" ]; then
  18. if [ "$1" == "-k" ]; then
  19. use_key=1
  20. else
  21. echo "Invalid option $1. Available options: -k"
  22. exit 1
  23. fi
  24. fi
  25. username=""
  26. password=""
  27. apikey=""
  28. #get username from configuration file
  29. read -r username < "nci.conf"
  30. #prompt user for its neocities password
  31. echo "You are about to upload your website (public_html/*) to Neocities!"
  32. if [ "$use_key" = 0 ]; then
  33. echo "Password for $username.neocities.org:"
  34. read -r -s password
  35. else
  36. echo "API key for $username.neocities.org:"
  37. read -r -s apikey
  38. fi
  39. #delete some files for bug prevention
  40. rm -f files.json files.delete.list files.upload.list
  41. echo "Checking credentials..."
  42. if [ "$use_key" = 0 ]; then
  43. curl -s "https://$username:$password@neocities.org/api/list" >> files.json
  44. else
  45. curl -s -H "Authorization: Bearer $apikey" "https://neocities.org/api/list" >> files.json
  46. fi
  47. #checkfiles.py will parse the response json
  48. python3 neocities_files.py
  49. if [ -f "fail" ]; then
  50. rm -f "fail"
  51. exit 1
  52. fi
  53. #prompt user for confirmation before uploading
  54. yn=""
  55. while true; do
  56. echo "---------------------------------------------------------"
  57. echo "WARNING: Existing files on Neocities will be overwritten."
  58. echo "WARNING: Existing files on Neocities that don't exist in your public_html/ directory will be DELETED! Download your whole website and copy your files into the public_html/ directory!"
  59. echo "Please confirm: Are you sure you want to continue? (y/n)"
  60. read -r yn
  61. case $yn in
  62. [Yy]* ) rm -f "upresult.*" "delresult.*"
  63. echo "Uploading files..."
  64. file=""
  65. u=0
  66. f=0
  67. d=0
  68. e=0
  69. while read -r file; do
  70. echo "Uploading $file..."
  71. if [ "$use_key" = 0 ]; then
  72. curl -sF "$file=@public_html/$file" "https://$username:$password@neocities.org/api/upload" >> result.json
  73. else
  74. curl -sF "$file=@public_html/$file" -H "Authorization: Bearer $apikey" "https://neocities.org/api/upload" >> result.json
  75. fi
  76. python3 check_upload.py "$file"
  77. if [ -f "upresult.ok" ]; then
  78. ((u+=1))
  79. fi
  80. if [ -f "upresult.err" ]; then
  81. ((f+=1))
  82. fi
  83. rm -f "upresult.ok" "delresult.err"
  84. done < "files.upload.list"
  85. echo "Removing files..."
  86. while read -r file; do
  87. echo "Removing $file..."
  88. if [ "$use_key" = 0 ]; then
  89. curl -sd "filenames[]=$file" "https://$username:$password@neocities.org/api/delete" >> deletion.json
  90. else
  91. curl -sd "filenames[]=$file" -H "Authorization: Bearer $apikey" "https://neocities.org/api/delete" >> deletion.json
  92. fi
  93. python3 check_deletion.py "$file"
  94. if [ -f "delresult.ok" ]; then
  95. ((d+=1))
  96. fi
  97. if [ -f "delresult.err" ]; then
  98. ((e+=1))
  99. fi
  100. rm -f "delresult.ok" "delresult.err"
  101. done < "files.delete.list"
  102. rm -f files.upload.list files.delete.list
  103. echo "--------------------------------------------"
  104. echo "$u file(s) successfully uploaded!"
  105. echo "$f file(s) not uploaded."
  106. echo "$d file(s) deleted."
  107. echo "$e file(s) not deleted."
  108. echo "OK."
  109. exit 1
  110. ;;
  111. [Nn]* ) rm -f "files.upload.list" "files.delete.list"
  112. echo "Operation cancelled."
  113. exit 1
  114. ;;
  115. * ) echo "Error: Please answer just y/n.";;
  116. esac
  117. done