CLI tool for submitting posts to WordPress through its REST API using curl.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

76 lines
2.1 KiB

  1. #!/bin/bash
  2. # WordPress posting script
  3. # config:
  4. EDITOR=vim
  5. TRANSFORM=() # empty or a subset of: 'title' 'markdown'
  6. USER="" # WP user to create the post
  7. PASSWORD="" # application password generated for your WP user
  8. SERVER="" # server hostname, optionally with subdirectories
  9. STATUS="draft" # one of publish,future,draft,pending,private
  10. CATEGORIES="" # comma separated integer IDs of categories
  11. TAGS="" # comma separated integer IDs of tags
  12. TMPFILE=/tmp/wordpress-post.txt # location of a temporary file with the post text
  13. source ~/.config/wordpress-rest-curl/config.sh
  14. # let the user create the post
  15. $EDITOR $TMPFILE || exit 1
  16. [[ -e $TMPFILE ]] || exit 1
  17. # transformations
  18. cp $TMPFILE $TMPFILE.trans
  19. for T in "${TRANSFORM[@]}"; do
  20. if [[ "$T" == "title" ]]; then
  21. python >$TMPFILE.trans2 <<EOF
  22. import re
  23. with open('$TMPFILE.trans', 'r') as file:
  24. title = ''
  25. content = file.read()
  26. matches = re.match('^#+ (.+)', content, flags=0)
  27. if matches:
  28. title = matches.group(1)
  29. content = content[len(matches.group(0)) + 1:]
  30. with open('$TMPFILE.title', 'w') as titlefile:
  31. titlefile.write(title)
  32. print(content)
  33. EOF
  34. TITLE=`cat $TMPFILE.title`
  35. rm $TMPFILE.title
  36. mv $TMPFILE.trans2 $TMPFILE.trans
  37. elif [[ "$T" == "markdown" ]]; then
  38. python >$TMPFILE.trans2 <<EOF
  39. import markdown2
  40. print(markdown2.markdown_path('$TMPFILE.trans'))
  41. EOF
  42. mv $TMPFILE.trans2 $TMPFILE.trans
  43. fi
  44. done
  45. CONTENT=`cat $TMPFILE.trans`
  46. rm $TMPFILE.trans
  47. echo "--- START POST ---"
  48. echo $CONTENT
  49. echo "--- END POST ---"
  50. echo "Title: $TITLE"
  51. echo "User: $USER"
  52. echo "Server: $SERVER"
  53. echo "Status: $STATUS"
  54. echo "Categories: $CATEGORIES"
  55. echo "Tags: $TAGS"
  56. echo
  57. read -p "Press enter to confirm..."
  58. # push the post!
  59. curl --user "$USER:$PASSWORD" -X POST \
  60. --data-urlencode "title=$TITLE" \
  61. --data-urlencode "content=$CONTENT" \
  62. --data-urlencode "status=$STATUS" \
  63. --data-urlencode "categories=$CATEGORIES" \
  64. --data-urlencode "tags=$TAGS" \
  65. "https://$SERVER/wp-json/wp/v2/posts/" || exit 1
  66. # backup the posted data (temporarily until it is auto-removed)
  67. mv $TMPFILE $TMPFILE.posted