Skip to content

Commit d263c53

Browse files
committed
πŸ‰ spring cleaning
1 parent f03324f commit d263c53

7 files changed

Lines changed: 629 additions & 476 deletions

File tree

β€ŽREADME.mdβ€Ž

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,25 @@
44

55
This is an unofficial library providing a Python interface for [Substack](https://substack.com/).
66
I am in no way affiliated with Substack. It works with
7-
Python versions from 3.8+.
7+
Python versions from 3.7+.
88

99
# Installation
1010

1111
You can install python-substack using:
1212

13-
$ pip install python-substack
13+
$ pip install python-substack
14+
15+
# Usage
16+
17+
Set the following environment variables by creating a **.env** file:
18+
19+
PUBLICATION_URL=https://ma2za.substack.com
20+
EMAIL=
21+
PASSWORD=
22+
USER_ID=
23+
24+
The only way I found to discover the USER_ID is to inspect
25+
the payload to a **/drafts** request. Under the fields **draftBylines**
26+
or **postBylines** there is a subfield **user_id** or **id**
27+
28+
The .env file will be ignored by git but always be careful.

β€Žexamples/draft.yamlβ€Ž

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,7 @@ body:
2020
content: "2)"
2121
5:
2222
type: "paragraph"
23-
content: "Set the EMAIL, PASSWORD, PUBLICATION_URL and USER_ID environment variables."
23+
content: "Set the EMAIL, PASSWORD, PUBLICATION_URL and USER_ID environment variables."
24+
6:
25+
type: "captionedImage"
26+
src: "https://i.insider.com/602ee9d81a89f20019a377c6?width=1136&format=jpeg"

β€Žexamples/publish_post.pyβ€Ž

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -11,32 +11,35 @@
1111

1212
if __name__ == "__main__":
1313

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()
19-
20-
with open(args.post, "r") as fp:
21-
post_data = yaml.safe_load(fp)
22-
23-
title = post_data.get("title", "")
24-
subtitle = post_data.get("subtitle", "")
25-
body = post_data.get("body", {})
26-
27-
api = Api(
28-
email=os.getenv("EMAIL"),
29-
password=os.getenv("PASSWORD"),
30-
publication_url=os.getenv("PUBLICATION_URL"),
31-
)
32-
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"))
14+
parser = argparse.ArgumentParser()
15+
parser.add_argument("-p", "--post", default="draft.yaml", required=False,
16+
help="YAML file containing the post to publish.", type=str)
17+
parser.add_argument("--publish", help="Publish the draft.", action="store_true", default=False)
18+
args = parser.parse_args()
19+
20+
with open(args.post, "r") as fp:
21+
post_data = yaml.safe_load(fp)
22+
23+
title = post_data.get("title", "")
24+
subtitle = post_data.get("subtitle", "")
25+
body = post_data.get("body", {})
26+
27+
api = Api(
28+
email=os.getenv("EMAIL"),
29+
password=os.getenv("PASSWORD"),
30+
publication_url=os.getenv("PUBLICATION_URL"),
31+
)
32+
33+
post = Post(title, subtitle, os.getenv("USER_ID"))
34+
for _, item in body.items():
35+
if item.get("type") == "captionedImage":
36+
image = api.get_image(item.get("src"))
37+
item.update({"src": image.get("url")})
38+
post.add(item)
39+
40+
draft = api.post_draft(post.get_draft())
41+
42+
if args.publish:
43+
api.prepublish_draft(draft.get("id"))
44+
45+
api.publish_draft(draft.get("id"))

β€Žpoetry.lockβ€Ž

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

β€Žpyproject.tomlβ€Ž

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "python-substack"
3-
version = "0.1.5"
3+
version = "0.1.6"
44
description = "A Python wrapper around the Substack API."
55
authors = ["Paolo Mazza <mazzapaolo2019@gmail.com>"]
66
license = "MIT"
@@ -10,8 +10,8 @@ packages = [
1010

1111
readme = "README.md"
1212

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

1616
keywords = ["substack"]
1717

0 commit comments

Comments
Β (0)