Last active
May 23, 2025 06:36
-
-
Save pong106/2f9d99a628345c1dcacd81aa1def13cc to your computer and use it in GitHub Desktop.
ghost create post with upload feature image
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# Config | |
API_URL="http://192.168.1.188:2368" | |
IMAGE_DIR="/Users/USERNANE/images" | |
USERNAME="${GHOST_ADMIN_EMAIL}" | |
PASSWORD="${GHOST_ADMIN_PASSWORD}" | |
# Check if image directory exists | |
if [ ! -d "$IMAGE_DIR" ]; then | |
echo "π ERROR: Image directory '$IMAGE_DIR' does not exist." | |
exit 1 | |
fi | |
# Step 1: Authenticate and retrieve session cookie | |
echo "Authenticating..." | |
auth_response=$(curl --silent --include --location "$API_URL/ghost/api/admin/session" \ | |
--header 'Content-Type: application/json' \ | |
--data-raw "{\"username\":\"$USERNAME\", \"password\":\"$PASSWORD\"}") | |
# Extract session cookie from response headers | |
COOKIE=$(echo "$auth_response" | grep -i 'Set-Cookie:' | grep 'ghost-admin-api-session' | sed -E 's/Set-Cookie: (ghost-admin-api-session=[^;]+);.*/\1/') | |
if [ -z "$COOKIE" ]; then | |
echo "β Authentication failed. Could not retrieve session cookie." | |
exit 1 | |
fi | |
echo "β Authenticated successfully." | |
echo "" | |
# Loop through each image | |
for image in "$IMAGE_DIR"/*; do | |
echo "Uploading $image..." | |
# Upload image and capture response | |
upload_response=$(curl --silent --location "$API_URL/ghost/api/admin/images/upload/" \ | |
--header "Cookie: $COOKIE" \ | |
--form "file=@\"$image\"") | |
# Extract image URL | |
image_url=$(echo "$upload_response" | jq -r '.images[0].url') | |
if [[ "$image_url" == "null" || -z "$image_url" ]]; then | |
echo "β Failed to upload $image" | |
continue | |
fi | |
echo "β Uploaded: $image_url" | |
filename=$(basename "$image_url") | |
echo "$filename" | |
# Create post | |
post_data=$(jq -n \ | |
--arg image_url "$image_url" \ | |
--arg filename "$filename" \ | |
'{ | |
posts: [ | |
{ | |
feature_image: $image_url, | |
title: $filename, | |
lexical: "{\"root\":{\"children\":[{\"children\":[{\"detail\":0,\"format\":0,\"mode\":\"normal\",\"style\":\"\",\"text\":\"Hello, beautiful world! π\",\"type\":\"extended-text\",\"version\":1}],\"direction\":\"ltr\",\"format\":\"\",\"indent\":0,\"type\":\"paragraph\",\"version\":1}],\"direction\":\"ltr\",\"format\":\"\",\"indent\":0,\"type\":\"root\",\"version\":1}}", | |
status: "published" | |
} | |
] | |
}') | |
echo "Creating post for $image..." | |
curl --silent --location "$API_URL/ghost/api/admin/posts" \ | |
--header "Content-Type: application/json" \ | |
--header "Cookie: $COOKIE" \ | |
--data "$post_data" > /dev/null 2>&1 | |
echo "π¬ Post created for $image" | |
echo "" | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment