Driving an API-less blog from a Python CLI
This blog is a git repo of markdown files, pushed live by a single-file Python CLI. Bear Blog has no public API, so the CLI logs in with account credentials, keeps the session cookie, and drives the same dashboard forms the web UI uses. This page describes it in enough detail that a coding agent can rebuild it without the source.
python bearblog.py list # uid + title of every post
python bearblog.py get <uid> -o post.md # download a post
python bearblog.py new post.md # create and publish from a file
python bearblog.py edit <uid> post.md # overwrite an existing post
Shape
One Python file. requests is the only dependency, and HTML is parsed with regular expressions. Credentials come from environment variables or a .env file in the working directory:
| Variable | Meaning |
|---|---|
BEARBLOG_EMAIL |
account email |
BEARBLOG_PASSWORD |
account password |
BEARBLOG_BLOG |
blog id, the <id> in <id>.bearblog.dev |
BEARBLOG_BASE_URL |
optional, defaults to https://bearblog.dev |
Real environment variables win over .env. A missing setting stops the script with a message naming it. .env is gitignored.
Post files
A post is one file: a header of key: value lines, a line containing only ___, then the Markdown body. This is the same format Bear Blog's editor uses, so a file maps straight onto the form.
title: My First Post
link: my-first-post
published_date: 2026-06-20 09:00
tags: python, blogging
make_discoverable: true
___
# Hello
Body markdown goes here.
Header keys Bear Blog understands: title, link, alias, published_date, tags, make_discoverable, is_page, class_name, canonical_url, lang, meta_description, meta_image. Only title is required. The script splits on the first ___ line and does not validate keys.
Commands
| Command | What it does |
|---|---|
list |
print every post as uid and title |
get <uid> [-o FILE] |
fetch a post in the file format above, to stdout or a file |
new <file> [--draft] |
create a post from a file, refusing a file with no title |
edit <uid> <file> [--draft] |
overwrite an existing post from a file |
delete <uid> [--yes] |
delete a post, asking for confirmation unless --yes |
publish <uid> |
publish a draft |
unpublish <uid> |
move a post back to draft |
Every command logs in first. new and edit publish unless --draft is passed. publish and unpublish fetch the post and save it back unchanged with the publish flag flipped.
Endpoints
Bear Blog is a Django app with django-allauth for login. Everything goes through these:
| Action | Request |
|---|---|
| Login | POST /accounts/login/ with login, password |
| List | GET /<blog>/dashboard/posts/ |
| Read | GET /<blog>/dashboard/posts/<uid>/ |
| Create | POST /<blog>/dashboard/posts/new/ |
| Update | POST /<blog>/dashboard/posts/<uid>/ |
| Delete | POST /<blog>/dashboard/posts/<uid>/delete/ |
Create and update send three form fields: header_content, body_content, and publish (true or false).
Keep all of this in one class. When the dashboard HTML shifts, there is one place to patch.
What the dashboard expects
These are the parts a rebuild gets wrong without being told.
CSRF wants three things. GET the login page first to receive the csrftoken cookie. Every POST then needs that token as the csrfmiddlewaretoken form field, plus a Referer and an Origin header for the same site. Miss one and Django returns 403.
The login response lies. django-allauth answers a failed login with 200. After posting credentials, request the blog's posts dashboard instead. Redirected back to /accounts/login: bad credentials. 404: wrong blog id.
Listing is link scraping. The posts dashboard links each post as /<blog>/dashboard/posts/<uid>/ with the title as link text. Collect those, skip new, and keep one entry per uid.
Reading is form scraping. On a post's edit page the body is a <textarea> named body_content. The header is a contenteditable <div id="header_content"> holding each field as <b>key:</b> value<br>. Turn <br> into newlines, strip the remaining tags, and unescape HTML entities to get key: value lines back. It used to be a textarea, so check for that too.
The header splits on CRLF only. Send the header with bare \n and Bear Blog parses it as one line: the whole block lands in title and the slug is regenerated, which breaks the post's URL. Rewrite header line endings to \r\n before posting.
HTTP 200 can mean rejected. A rejected save still returns 200. The only signal is a banner in the returned page:
<p style="color: lightsalmon">... your post has not been saved. Error: ...</p>
Look for that paragraph, not for the phrase. "has not been saved" also appears in the edit page's autosave help text, so searching the whole page flags every successful save as a failure. The first version of this script did that: new reported a rejection while creating the post, and each retry made another copy. Seven of them, once.
Known limits
- Posts are addressed by uid, never by title, slug, or filename. Run
listto find one. make_discoverabledoes not round-trip. The edit page does not render it back, soget,publish, andunpublishdrop it. Treat the local file as the source of truth and useedit.- Bear Blog rewrites what you send. Tags come back alphabetised and header fields reordered.
editreplaces the whole post. There is no merge.- No 2FA. Password login fails on accounts that have it enabled.
- Cloudflare. Since November 2026 the login page answers scripts with a JavaScript challenge, so the CLI cannot log in. It does not work around this. For now I publish through the dashboard in a browser.