Skip to content

milnomada/ghost-cli

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

drawing  Ghost Cli

A Python client for Ghost

Cov GitHub tag

About

Ghost-cli is a mininal client library for Ghost, thought to ease the management of Ghost resources for programmatic tasks.
It makes use of client credentials from a Custom Integration that must be created to configure the client.

Ghost-cli implements CRUD operations on the following models:

Name Doc
Post https://ghost.org/docs/admin-api/#posts
Tag https://ghost.org/docs/content-api/#tags
Author https://ghost.org/docs/content-api/#authors

Integration with Newsletters, Members or other resources of Ghost are not available.

Configuration

Create a Custom Integration as explained here.
The Integration will provide the Admin API Key, use it as value to create the GHOST_KEY environment variable:

export GHOST_KEY="633391e30a8cba0bc1e96f20:a12bc792cd9ed482d4a7d1da2c045e5261feae2117fab4d8dad0d93f7e34bc82"

The Tokener class loads the GHOST_KEY by default, unless a key is provided. To provide the key otherwise, it is possible instantiate the Tokener class providing the key parameter.

Install

pip install git+https://github.com/milnomada/ghost-cli.git#egg=ghost-cli

Usage

Configure the Client

import os
from ghost_cli import GhostCli, Tokener

tokener = Tokener()
token = tokener.generate(expire=30)

# or
my_key = os.environ.get("MY_ENV_KEY", None)
tokener = Tokener(key=my_key)
token = tokener.generate()

cli = GhostCli("http://my.ghost.io", token)

Create a post

cli.create_post(title="hello world")

# or
tag = cli.get_tag("name", "Philosophy")
my_post = {
    "title": "hello world"
    "created_at": "2022-11-26T02:31:12.000Z",
    "published_at": "2022-11-26T02:43:15.000Z",
    "tags": [tag.__dict__]
}
cli.create_post(**my_post)

Find a post

post = cli.get_post_by_title("hello world")
print(f"{post.title}, {post.slug}, {post.published_at}, {post.created_at}")
post = cli.get_post("slug", "hello-world")

Update a post

post = cli.get_post("slug", "hello-world")
updated = cli.update_post(post.id, title="Hello World Auth", updated_at=post.updated_at)

# or
my_update = {
    "title": "Hello World Auth",
    "slug": "hello-world-auth",
    "updated_at": post.updated_at,
}
updated = cli.update_post(post.id, **my_update)

print(f"updated: {updated}")

Delete a post

post = cli.get_post("slug", "hello-world")
deleted = cli.delete_post(post.id)
print(f"deleted: {deleted}")

Ghost Versions

Ghost-cli is compatible with the latest Ghost versions: