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
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "python-substack"
version = "0.1.1"
version = "0.1.2"
description = "A Python wrapper around the Substack API."
authors = ["Paolo Mazza <mazzapaolo2019@gmail.com>"]
license = "MIT"
Expand Down
25 changes: 25 additions & 0 deletions substack/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,12 @@ def get_drafts(self, filter: str = None, offset: int = None, limit: int = None):
)
return Api._handle_response(response=response)

def delete_draft(self, draft_id):
response = self._session.delete(
f"{self.publication_url}/drafts/{draft_id}"
)
return Api._handle_response(response=response)

def post_draft(self, body) -> dict:
"""

Expand Down Expand Up @@ -189,6 +195,15 @@ def publish_draft(
)
return Api._handle_response(response=response)

def get_image(self, image):
response = self._session.post(
f"{self.publication_url}/image",
json={
"image": image
},
)
return Api._handle_response(response=response)

def get_categories(self):
"""

Expand Down Expand Up @@ -235,3 +250,13 @@ def get_single_category(self, category_id: int, category_type: str, page: int |
"more": page_output.get("more", False)
}
return output

def delete_all_drafts(self):
response = None
while True:
drafts = self.get_drafts(filter="draft", limit=10, offset=0)
if len(drafts) == 0:
break
for draft in drafts:
response = self.delete_draft(draft.get("id"))
return response
66 changes: 63 additions & 3 deletions substack/post.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,21 @@ def __init__(self, title: str, subtitle: str, user_id: str):
def add(self, item):
self.draft_body["content"] = self.draft_body.get("content", []) + [{"type": item.get("type")}]
content = item.get("content")
if content is not None:
self.text(content)
if item.get("type") == "captionedImage":
self.captioned_image(item)
elif item.get("type") == "youtube2":
self.youtube(item.get("src"))
else:
if content is not None:
self.add_complex_text(content)

if item.get("type") == "heading":
self.attrs(item.get("level", 1))

marks = item.get("marks")
if marks is not None:
self.marks(marks)

return self

def paragraph(self, content=None):
Expand All @@ -41,6 +51,22 @@ def attrs(self, level):
self.draft_body["content"][-1]["attrs"] = content_attrs
return self

def captioned_image(self, value):
content = self.draft_body["content"][-1].get("content", [])
content += [{"type": "image2", "attrs": {
"src": value.get("src"),
"fullscreen": False,
"imageSize": value.get("size", "normal"),
"height": 819,
"width": 1456,
"resizeWidth": 728,

"bytes": None, "alt": None, "title": None, "type": None, "href": None, "belowTheFold": False,
"internalRedirect": None

}}]
self.draft_body["content"][-1]["content"] = content

def text(self, value: str):
"""

Expand All @@ -57,11 +83,25 @@ def text(self, value: str):
self.draft_body["content"][-1]["content"] = content
return self

def add_complex_text(self, text):
if isinstance(text, str):
self.text(text)
else:
for chunk in text:
if chunk:
self.text(chunk.get("content")).marks(chunk.get("marks"))

def marks(self, marks):
content = self.draft_body["content"][-1].get("content", [])[-1]
content_marks = content.get("marks", [])
for mark in marks:
content_marks.append({"type": mark})
new_mark = {"type": mark.get("type")}
if mark.get("type") == "link":
href = mark.get("href")
new_mark.update({"attrs": {
"href": href
}})
content_marks.append(new_mark)
content["marks"] = content_marks
return self

Expand All @@ -72,3 +112,23 @@ def get_draft(self):
out = vars(self)
out["draft_body"] = json.dumps(out["draft_body"])
return out

def subscribe_with_caption(self, value):
content = self.draft_body["content"][-1].get("content", [])
content += [{"type": "subscribeWidget",
"attrs": {"url": "%%checkout_url%%", "text": "Subscribe"},
"content": [
{
"type": "ctaCaption",
"content": [{"type": "text",
"text": f"Thanks for reading {value}! Subscribe for free to receive new posts and support my work."}]
}
]}]
self.draft_body["content"][-1]["content"] = content
return self

def youtube(self, value):
content_attrs = self.draft_body["content"][-1].get("attrs", {})
content_attrs.update({"videoId": value})
self.draft_body["content"][-1]["attrs"] = content_attrs
return self