Skip to content

Commit d02bce1

Browse files
authored
Post draft (#2)
* here i go again * fix draft body * publish_post example working correctly
1 parent a2c3c02 commit d02bce1

6 files changed

Lines changed: 414 additions & 266 deletions

File tree

examples/draft.yaml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
title:
2+
"How to publish a Substack post using the Python API"
3+
subtitle:
4+
"This post was published using the Python API"
5+
body:
6+
0:
7+
type: "heading"
8+
level: 1
9+
content: "Steps"
10+
1:
11+
type: "paragraph"
12+
content: "1)"
13+
2:
14+
type: "paragraph"
15+
content: "Discover your USER ID by inspecting the request body of any publish request."
16+
3:
17+
type: "horizontal_rule"
18+
4:
19+
type: "paragraph"
20+
content: "2)"
21+
5:
22+
type: "paragraph"
23+
content: "Set the EMAIL, PASSWORD, PUBLICATION_URL and USER_ID environment variables."

examples/publish_post.py

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,42 @@
1+
import argparse
12
import os
23

4+
import yaml
35
from dotenv import load_dotenv
46

57
from substack import Api
8+
from substack.post import Post
69

710
load_dotenv()
811

9-
content = ""
10-
title = ""
11-
subtitle = ""
12+
if __name__ == "__main__":
1213

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

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

21-
draft = api.post_draft(
22-
[{"id": os.getenv("USER_ID"), "is_guest": False}],
23-
title=title,
24-
subtitle=subtitle,
25-
body=body,
26-
)
23+
title = post_data.get("title", "")
24+
subtitle = post_data.get("subtitle", "")
25+
body = post_data.get("body", {})
2726

28-
api.prepublish_draft(draft.get("id"))
27+
api = Api(
28+
email=os.getenv("EMAIL"),
29+
password=os.getenv("PASSWORD"),
30+
publication_url=os.getenv("PUBLICATION_URL"),
31+
)
2932

30-
api.publish_draft(draft.get("id"))
33+
post = Post(title, subtitle, os.getenv("USER_ID"))
34+
for _, item in body.items():
35+
post.add(item)
36+
37+
draft = api.post_draft(post.get_draft())
38+
39+
if args.publish:
40+
api.prepublish_draft(draft.get("id"))
41+
42+
api.publish_draft(draft.get("id"))

poetry.lock

Lines changed: 57 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,17 @@ packages = [
1010

1111
readme = "README.md"
1212

13-
repository = "https://github.com/hogier/python-substack"
14-
homepage = "https://github.com/hogier/python-substack"
13+
repository = "https://github.com/mazza8/python-substack"
14+
homepage = "https://github.com/mazza8/python-substack"
1515

1616
keywords = ["substack"]
1717

1818
[tool.poetry.dependencies]
1919
python = "^3.8"
2020

2121
requests = "^2.28.1"
22-
python-dotenv = "^0.20.0"
22+
python-dotenv = "^0.21.0"
23+
PyYAML = "^6.0"
2324

2425

2526
[tool.poetry.dev-dependencies]

0 commit comments

Comments
 (0)