Skip to content

Commit 844fa9c

Browse files
committed
🦍 set post section
1 parent 45d17c5 commit 844fa9c

File tree

7 files changed

+67
-24
lines changed

7 files changed

+67
-24
lines changed

README.md

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,17 +58,28 @@ post = Post(
5858

5959
post.add({'type': 'paragraph', 'content': 'This is how you add a new paragraph to your post!'})
6060

61-
#bolden text
62-
post.add({'type': "paragraph",'content':[{'content': "This is how you "},{'content': "bolden ",'marks':[{'type': "strong"}]},{'content': "a word."}]})
61+
# bolden text
62+
post.add({'type': "paragraph",
63+
'content': [{'content': "This is how you "}, {'content': "bolden ", 'marks': [{'type': "strong"}]},
64+
{'content': "a word."}]})
6365

64-
#add hyperlink to text
65-
post.add({'type':'paragraph','content':[{'content': "View Link",'marks':[{'type': "link",'href':'https://whoraised.substack.com/'}]}]})
66+
# add hyperlink to text
67+
post.add({'type': 'paragraph', 'content': [
68+
{'content': "View Link", 'marks': [{'type': "link", 'href': 'https://whoraised.substack.com/'}]}]})
6669

67-
#add image
68-
post.add({'type':'captionedImage','src': "https://media.tenor.com/7B4jMa-a7bsAAAAC/i-am-batman.gif"})
70+
# add image
71+
post.add({'type': 'captionedImage', 'src': "https://media.tenor.com/7B4jMa-a7bsAAAAC/i-am-batman.gif"})
72+
73+
# add local image
74+
image = api.get_image('image.png')
75+
post.add({"type": "captionedImage", "src": image.get("url")})
6976

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

79+
# set section (THIS CAN BE DONE ONLY AFTER HAVING FIRST POSTED THE DRAFT)
80+
post.set_section("rick rolling", api.get_sections())
81+
api.put_draft(draft.get("id"), draft_section_id=post.draft_section_id)
82+
7283
api.prepublish_draft(draft.get("id"))
7384

7485
api.publish_draft(draft.get("id"))

examples/draft.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ audience:
66
"everyone" # everyone, only_paid, founding, only_free
77
write_comment_permissions:
88
"none" # none, only_paid, everyone
9+
section:
10+
"rick"
911
body:
1012
0:
1113
type: "heading"

examples/publish_post.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
type=str,
2121
)
2222
parser.add_argument(
23-
"--publish", help="Publish the draft.", action="store_true", default=False
23+
"--publish", help="Publish the draft.", action="store_true", default=True
2424
)
2525
args = parser.parse_args()
2626

@@ -53,6 +53,9 @@
5353

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

56+
post.set_section(post_data.get("section"), api.get_sections())
57+
api.put_draft(draft.get("id"), draft_section_id=post.draft_section_id)
58+
5659
if args.publish:
5760
api.prepublish_draft(draft.get("id"))
5861

pyproject.toml

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

substack/api.py

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -169,32 +169,20 @@ def post_draft(self, body) -> dict:
169169
def put_draft(
170170
self,
171171
draft,
172-
title=None,
173-
subtitle=None,
174-
body=None,
175-
cover_image=None,
172+
**kwargs
176173
) -> dict:
177174
"""
178175
179176
Args:
180-
draft: draft id
181-
title:
182-
subtitle:
183-
body:
184-
cover_image:
177+
draft:
178+
**kwargs:
185179
186180
Returns:
187181
188182
"""
189-
190183
response = self._session.put(
191184
f"{self.publication_url}/drafts/{draft}",
192-
json={
193-
"draft_title": title,
194-
"draft_subtitle": subtitle,
195-
"draft_body": body,
196-
"cover_image": cover_image,
197-
},
185+
json=kwargs,
198186
)
199187
return Api._handle_response(response=response)
200188

@@ -357,3 +345,18 @@ def delete_all_drafts(self):
357345
for draft in drafts:
358346
response = self.delete_draft(draft.get("id"))
359347
return response
348+
349+
def get_sections(self):
350+
"""
351+
Get a list of the sections of your publication.
352+
353+
TODO: this is hacky but I cannot find another place where to get the sections.
354+
Returns:
355+
356+
"""
357+
response = self._session.get(
358+
f"{self.publication_url}/subscriptions",
359+
)
360+
content = Api._handle_response(response=response)
361+
sections = [p.get("sections") for p in content.get("publications") if p.get("hostname") in self.publication_url]
362+
return sections[0]

substack/exceptions.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,7 @@ def __init__(self, message):
2626

2727
def __str__(self):
2828
return f"SubstackRequestException: {self.message}"
29+
30+
31+
class SectionNotExistsException(SubstackRequestException):
32+
pass

substack/post.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
__all__ = ["Post"]
55

6+
from substack.exceptions import SectionNotExistsException
7+
68

79
class Post:
810
def __init__(
@@ -27,13 +29,31 @@ def __init__(
2729
self.draft_body = {"type": "doc", "content": []}
2830
self.draft_bylines = [{"id": int(user_id), "is_guest": False}]
2931
self.audience = audience if audience is not None else "everyone"
32+
self.draft_section_id = None
33+
self.section_chosen = True
3034

3135
# TODO better understand the possible values and combinations with audience
3236
if write_comment_permissions is not None:
3337
self.write_comment_permissions = write_comment_permissions
3438
else:
3539
self.write_comment_permissions = self.audience
3640

41+
def set_section(self, name: str, sections: list):
42+
"""
43+
44+
Args:
45+
name:
46+
sections:
47+
48+
Returns:
49+
50+
"""
51+
section = [s for s in sections if s.get("name") == name]
52+
if len(section) != 1:
53+
raise SectionNotExistsException(name)
54+
section = section[0]
55+
self.draft_section_id = section.get("id")
56+
3757
def add(self, item: Dict):
3858
"""
3959

0 commit comments

Comments
 (0)