wordpress-rest-curl/post.sh

76 lines
2.1 KiB
Bash
Raw Normal View History

2020-10-06 20:28:37 +00:00
#!/bin/bash
# WordPress posting script
# config:
EDITOR=vim
2020-10-17 17:51:20 +00:00
TRANSFORM=() # empty or a subset of: 'title' 'markdown'
2020-10-06 20:28:37 +00:00
USER="" # WP user to create the post
PASSWORD="" # application password generated for your WP user
SERVER="" # server hostname, optionally with subdirectories
STATUS="draft" # one of publish,future,draft,pending,private
CATEGORIES="" # comma separated integer IDs of categories
TAGS="" # comma separated integer IDs of tags
TMPFILE=/tmp/wordpress-post.txt # location of a temporary file with the post text
source ~/.config/wordpress-rest-curl/config.sh
# let the user create the post
$EDITOR $TMPFILE || exit 1
[[ -e $TMPFILE ]] || exit 1
2020-10-06 21:17:14 +00:00
# transformations
2020-10-17 17:51:20 +00:00
cp $TMPFILE $TMPFILE.trans
for T in "${TRANSFORM[@]}"; do
if [[ "$T" == "title" ]]; then
python >$TMPFILE.trans2 <<EOF
import re
with open('$TMPFILE.trans', 'r') as file:
title = ''
content = file.read()
matches = re.match('^#+ (.+)', content, flags=0)
if matches:
title = matches.group(1)
content = content[len(matches.group(0)) + 1:]
with open('$TMPFILE.title', 'w') as titlefile:
titlefile.write(title)
print(content)
EOF
TITLE=`cat $TMPFILE.title`
rm $TMPFILE.title
mv $TMPFILE.trans2 $TMPFILE.trans
elif [[ "$T" == "markdown" ]]; then
python >$TMPFILE.trans2 <<EOF
2020-10-06 21:17:14 +00:00
import markdown2
2020-10-17 17:51:20 +00:00
print(markdown2.markdown_path('$TMPFILE.trans'))
2020-10-06 21:17:14 +00:00
EOF
2020-10-17 17:51:20 +00:00
mv $TMPFILE.trans2 $TMPFILE.trans
fi
done
CONTENT=`cat $TMPFILE.trans`
rm $TMPFILE.trans
2020-10-06 20:28:37 +00:00
echo "--- START POST ---"
echo $CONTENT
echo "--- END POST ---"
2020-10-17 17:51:20 +00:00
echo "Title: $TITLE"
2020-10-06 20:28:37 +00:00
echo "User: $USER"
echo "Server: $SERVER"
echo "Status: $STATUS"
echo "Categories: $CATEGORIES"
echo "Tags: $TAGS"
echo
read -p "Press enter to confirm..."
# push the post!
curl --user "$USER:$PASSWORD" -X POST \
2020-10-17 17:51:20 +00:00
--data-urlencode "title=$TITLE" \
2020-10-06 20:28:37 +00:00
--data-urlencode "content=$CONTENT" \
--data-urlencode "status=$STATUS" \
--data-urlencode "categories=$CATEGORIES" \
--data-urlencode "tags=$TAGS" \
"https://$SERVER/wp-json/wp/v2/posts/" || exit 1
# backup the posted data (temporarily until it is auto-removed)
mv $TMPFILE $TMPFILE.posted