Skip to content

Instantly share code, notes, and snippets.

@tecking
Last active December 28, 2015 05:49
Show Gist options
  • Save tecking/7453052 to your computer and use it in GitHub Desktop.
Save tecking/7453052 to your computer and use it in GitHub Desktop.
genpost.sh
#!/bin/sh
#
# Configuration files.
# "post_content.txt" is based on "Ryoko No Konjaku"(KODA Rohan).
# http://www.aozora.gr.jp/cards/000051/files/1444_47834.html
# "post_thumbnail.txt" is based on "PAKUTASO".
# http://www.pakutaso.com/
#
CONTENT_CONFIG="http://dl.dropboxusercontent.com/u/3554441/genpost/post_content.txt"
IMAGE_CONFIG="http://dl.dropboxusercontent.com/u/3554441/genpost/post_thumbnail.txt"
#
# Enter arguments.
#
echo "Enter some arguments to create posts."
while true; do
echo -n "Target directory? (e.g., /vagrant/www/foo) : "
read TARGET_DIR
if [ ! -d $TARGET_DIR -o ]; then
echo "Target directory doesn't exist! Try again."
else
break
fi
done
echo -n "Number of posts?(default = 10) : "
read NUM_POSTS
echo -n "Max number of paragraphs?(default = 5) : "
read NUM_PRGRPHS
echo -n "Post type?(default = post) : "
read POST_TYPE
echo -n "Post status?(default = publish) : "
read POST_STATUS
echo -n "Post title prefix?(default = Post) : "
read POST_TITLE_PRFX
echo -n "Attach featured image?(yes/no) : "
read POST_FTRD_IMG
#
# Default values.
#
NUM_POSTS=${NUM_POSTS:-10}
NUM_PRGRPHS=${NUM_PRGRPHS:-5}
POST_TYPE=${POST_TYPE:-post}
POST_STATUS=${POST_STATUS:-publish}
POST_TITLE_PRFX=${POST_TITLE_PRFX:-Post}
#
# Replace string and assign variable.
#
TARGET_DIR=${TARGET_DIR%/}
#
# Define function.
#
gen_rand() {
local SEED=`date +%N`
return `awk "BEGIN{ srand($SEED); print int( $1 * rand() ) + 1 }"`
}
#
# Download configuration file(s).
#
cd $TARGET_DIR
echo ""
echo "Downloading post content configuration file..."
wget -q -nc $CONTENT_CONFIG
echo "\033[32mSuccess:\033[0m Downloaded post_content.txt"
if [ "$POST_FTRD_IMG" = "y" -o "$POST_FTRD_IMG" = "yes" ]; then
echo "Downloading featured image configuration file..."
wget -q -nc $IMAGE_CONFIG
echo "\033[32mSuccess:\033[0m Downloaded post_thumbnail.txt"
else
break;
fi
echo ""
#
# Create post(s).
#
POST_NUMBER=1
for I in `seq 1 "$NUM_POSTS"`; do
unset CONTENT
gen_rand $NUM_PRGRPHS
for J in `seq 1 $?`; do
gen_rand 20
CONTENT=${CONTENT}'<p>'`cat post_content.txt | head -"$?" | tail -1`'</p>'
done
if [ "$POST_FTRD_IMG" = "y" -o "$POST_FTRD_IMG" = "yes" ]; then
POST_ID=`wp post create --post_type=$POST_TYPE --post_title="$POST_TITLE_PRFX $POST_NUMBER" --post_content=$CONTENT --post_status=$POST_STATUS --porcelain`
echo "\033[32mSuccess:\033[0m Created post "$POST_ID
gen_rand 20
IMAGE_FILE=`cat post_thumbnail.txt | head -$? | tail -1 | tr -d '\r' | tr -d '\n'`
wp media import $IMAGE_FILE --post_id=$POST_ID --title="$POST_TITLE_PRFX $POST_NUMBER" --featured_image
else
wp post create --post_type=$POST_TYPE --post_title="$POST_TITLE_PRFX $POST_NUMBER" --post_content=$CONTENT --post_status=$POST_STATUS
fi
POST_NUMBER=`expr $POST_NUMBER + 1`
done
#
# Remove configuration file(s).
#
echo ""
if [ -f "post_content.txt" ]; then
rm post_content.txt
echo "\033[32mSuccess:\033[0m Removed post content configuration file."
fi
if [ -f "post_thumbnail.txt" ]; then
rm post_thumbnail.txt
echo "\033[32mSuccess:\033[0m Removed featured image configuration file."
fi
exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment