diff --git a/README.md b/README.md index 9c6a461..7409de5 100644 --- a/README.md +++ b/README.md @@ -92,6 +92,10 @@ post.add({'type': 'captionedImage', 'src': "https://media.tenor.com/7B4jMa-a7bsA image = api.get_image('image.png') post.add({"type": "captionedImage", "src": image.get("url")}) +# embed publication +embedded = api.publication_embed("https://jackio.substack.com/") +post.add({"type": "embeddedPublication", "url": embedded}) + draft = api.post_draft(post.get_draft()) # set section (THIS CAN BE DONE ONLY AFTER HAVING FIRST POSTED THE DRAFT) diff --git a/pyproject.toml b/pyproject.toml index fa48afc..9af917c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "python-substack" -version = "0.1.14" +version = "0.1.15" description = "A Python wrapper around the Substack API." authors = ["Paolo Mazza "] license = "MIT" diff --git a/substack/api.py b/substack/api.py index faf3939..6e1dd53 100644 --- a/substack/api.py +++ b/substack/api.py @@ -128,7 +128,11 @@ def signin_for_pub(self, publication): response = self._session.get( f"https://substack.com/sign-in?redirect=%2F&for_pub={publication['subdomain']}", ) - return Api._handle_response(response=response) + try: + output = Api._handle_response(response=response) + except SubstackRequestException as ex: + output = {} + return output def change_publication(self, publication): """ @@ -538,3 +542,32 @@ def get_sections(self): if p.get("hostname") in self.publication_url ] return sections[0] + + def publication_embed(self, url): + """ + + Args: + url: + + Returns: + + """ + return self.call("/publication/embed", "GET", url=url) + + def call(self, endpoint, method, **params): + """ + + Args: + endpoint: + method: + **params: + + Returns: + + """ + response = self._session.request( + method=method, + url=f"{self.publication_url}/{endpoint}", + params=params, + ) + return Api._handle_response(response=response) diff --git a/substack/post.py b/substack/post.py index bdae760..825baf5 100644 --- a/substack/post.py +++ b/substack/post.py @@ -84,6 +84,8 @@ def add(self, item: Dict): content = item.get("content") if item.get("type") == "captionedImage": self.captioned_image(**item) + elif item.get("type") == "embeddedPublication": + self.draft_body["content"][-1]["attrs"] = item.get("url") elif item.get("type") == "youtube2": self.youtube(item.get("src")) elif item.get("type") == "subscribeWidget":