Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions examples/draft.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
title:
"How to publish a Substack post using the Python API"
subtitle:
"This post was published using the Python API"
body:
0:
type: "heading"
level: 1
content: "Steps"
1:
type: "paragraph"
content: "1)"
2:
type: "paragraph"
content: "Discover your USER ID by inspecting the request body of any publish request."
3:
type: "horizontal_rule"
4:
type: "paragraph"
content: "2)"
5:
type: "paragraph"
content: "Set the EMAIL, PASSWORD, PUBLICATION_URL and USER_ID environment variables."
46 changes: 29 additions & 17 deletions examples/publish_post.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,42 @@
import argparse
import os

import yaml
from dotenv import load_dotenv

from substack import Api
from substack.post import Post

load_dotenv()

content = ""
title = ""
subtitle = ""
if __name__ == "__main__":

api = Api(
email=os.getenv("EMAIL"),
password=os.getenv("PASSWORD"),
publication_url=os.getenv("PUBLICATION_URL"),
)
parser = argparse.ArgumentParser()
parser.add_argument("-p", "--post", default="draft.yaml", required=True,
help="YAML file containing the post to publish.", type=str)
parser.add_argument("--publish", help="Publish the draft.", action="store_true")
args = parser.parse_args()

body = f'{{"type":"doc","content": {content}}}'
with open(args.post, "r") as fp:
post_data = yaml.safe_load(fp)

draft = api.post_draft(
[{"id": os.getenv("USER_ID"), "is_guest": False}],
title=title,
subtitle=subtitle,
body=body,
)
title = post_data.get("title", "")
subtitle = post_data.get("subtitle", "")
body = post_data.get("body", {})

api.prepublish_draft(draft.get("id"))
api = Api(
email=os.getenv("EMAIL"),
password=os.getenv("PASSWORD"),
publication_url=os.getenv("PUBLICATION_URL"),
)

api.publish_draft(draft.get("id"))
post = Post(title, subtitle, os.getenv("USER_ID"))
for _, item in body.items():
post.add(item)

draft = api.post_draft(post.get_draft())

if args.publish:
api.prepublish_draft(draft.get("id"))

api.publish_draft(draft.get("id"))
61 changes: 57 additions & 4 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,17 @@ packages = [

readme = "README.md"

repository = "https://github.com/hogier/python-substack"
homepage = "https://github.com/hogier/python-substack"
repository = "https://github.com/mazza8/python-substack"
homepage = "https://github.com/mazza8/python-substack"

keywords = ["substack"]

[tool.poetry.dependencies]
python = "^3.8"

requests = "^2.28.1"
python-dotenv = "^0.20.0"
python-dotenv = "^0.21.0"
PyYAML = "^6.0"


[tool.poetry.dev-dependencies]
Expand Down
Loading